Teuchos_RCPNode.hpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
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   // Above is made a pointer to reduce overhead for the general case
00135   // where this is not used
00136   void impl_pre_delete_extra_data();
00137   // Not defined and not to be called
00138   RCPNode();
00139   RCPNode(const RCPNode&);
00140   RCPNode& operator=(const RCPNode&);
00141 }; // end class RCPNode;
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   // not defined and not to be called
00169   RCPNodeTmpl();
00170   RCPNodeTmpl(const RCPNodeTmpl&);
00171   RCPNodeTmpl& operator=(const RCPNodeTmpl&);
00172 
00173 }; // end class RCPNodeTmpl<T>
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 } // end namespace Teuchos
00265 
00266 
00267 namespace {
00268 // This static variable should be delcared before all other static variables
00269 // that depend on RCP or other classes. Therefore, this static varaible should
00270 // be deleted *after* all of these other static variables that depend on RCP
00271 // or created classes go away!
00272 Teuchos::PrintActiveRCPNodes local_printActiveRCPNodes;
00273 } // namespace
00274 
00275 
00276 #endif // TEUCHOS_RCP_NODE_HPP

Generated on Wed May 12 21:40:32 2010 for Teuchos - Trilinos Tools Package by  doxygen 1.4.7