00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef TEUCHOS_RCP_NODE_HPP
00030 #define TEUCHOS_RCP_NODE_HPP
00031
00032
00039 #include "Teuchos_ConfigDefs.hpp"
00040 #include "Teuchos_any.hpp"
00041 #include "Teuchos_map.hpp"
00042
00043
00044 namespace Teuchos {
00045
00046
00051 enum EPrePostDestruction { PRE_DESTROY, POST_DESTROY };
00052
00053
00062 class RCPNode {
00063 public:
00065 RCPNode(bool has_ownership_in)
00066 : count_(1), has_ownership_(has_ownership_in), extra_data_map_(NULL)
00067 {}
00069 virtual ~RCPNode()
00070 {
00071 if(extra_data_map_)
00072 delete extra_data_map_;
00073 }
00075 int count() const {
00076 return count_;
00077 }
00079 int incr_count() {
00080 return ++count_;
00081 }
00083 int deincr_count() {
00084 return --count_;
00085 }
00087 void has_ownership(bool has_ownership_in) {
00088 has_ownership_ = has_ownership_in;
00089 }
00091 bool has_ownership() const {
00092 return has_ownership_;
00093 }
00095 void set_extra_data(
00096 const any &extra_data, const std::string& name,
00097 EPrePostDestruction destroy_when, bool force_unique );
00099 any& get_extra_data( const std::string& type_name,
00100 const std::string& name );
00102 const any& get_extra_data( const std::string& type_name,
00103 const std::string& name ) const
00104 {
00105 return const_cast<RCPNode*>(this)->get_extra_data(type_name,name);
00106 }
00108 any* get_optional_extra_data(
00109 const std::string& type_name, const std::string& name );
00111 const any* get_optional_extra_data(
00112 const std::string& type_name, const std::string& name ) const
00113 {
00114 return const_cast<RCPNode*>(this)->get_optional_extra_data(type_name,name);
00115 }
00116 protected:
00118 void pre_delete_extra_data() {
00119 if(extra_data_map_)
00120 impl_pre_delete_extra_data();
00121 }
00122 private:
00123 struct extra_data_entry_t {
00124 extra_data_entry_t() : destroy_when(POST_DESTROY) {}
00125 extra_data_entry_t( const any &_extra_data, EPrePostDestruction _destroy_when )
00126 : extra_data(_extra_data), destroy_when(_destroy_when) {}
00127 any extra_data;
00128 EPrePostDestruction destroy_when;
00129 };
00130 typedef Teuchos::map<std::string,extra_data_entry_t> extra_data_map_t;
00131 int count_;
00132 bool has_ownership_;
00133 extra_data_map_t *extra_data_map_;
00134
00135
00136 void impl_pre_delete_extra_data();
00137
00138 RCPNode();
00139 RCPNode(const RCPNode&);
00140 RCPNode& operator=(const RCPNode&);
00141 };
00142
00143
00148 template<class T, class Dealloc_T>
00149 class RCPNodeTmpl : public RCPNode {
00150 public:
00152 RCPNodeTmpl(T* p, Dealloc_T dealloc, bool has_ownership_in)
00153 : RCPNode(has_ownership_in), ptr_(p), dealloc_(dealloc)
00154 {}
00156 Dealloc_T& get_nonconst_dealloc() { return dealloc_; }
00158 const Dealloc_T& get_dealloc() const { return dealloc_; }
00160 ~RCPNodeTmpl() {
00161 this->pre_delete_extra_data();
00162 if( has_ownership() )
00163 dealloc_.free(ptr_);
00164 }
00165 private:
00166 T *ptr_;
00167 Dealloc_T dealloc_;
00168
00169 RCPNodeTmpl();
00170 RCPNodeTmpl(const RCPNodeTmpl&);
00171 RCPNodeTmpl& operator=(const RCPNodeTmpl&);
00172
00173 };
00174
00175
00180 void add_new_RCPNode( RCPNode* rcp_node, const std::string &info );
00181
00182
00187 void remove_RCPNode( RCPNode* rcp_node );
00188
00189
00194 class PrintActiveRCPNodes {
00195 public:
00197 PrintActiveRCPNodes();
00199 ~PrintActiveRCPNodes();
00201 void foo();
00202 private:
00203 static int count_;
00204 };
00205
00206
00214 bool isTracingActiveRCPNodes();
00215
00216
00217 #ifdef TEUCHOS_DEBUG
00218
00232 void setTracingActiveRCPNodes(bool tracingActiveNodes);
00233
00234 #endif // TEUCHOS_DEBUG
00235
00236
00238 int numActiveRCPNodes();
00239
00240
00257 void printActiveRCPNodes(std::ostream &out);
00258
00259
00261 void throw_null_ptr_error( const std::string &type_name );
00262
00263
00264 }
00265
00266
00267 namespace {
00268
00269
00270
00271
00272 Teuchos::PrintActiveRCPNodes local_printActiveRCPNodes;
00273 }
00274
00275
00276 #endif // TEUCHOS_RCP_NODE_HPP