Teuchos_RefCountPtr.cpp

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 #include "Teuchos_RefCountPtr.hpp"
00030 #include "Teuchos_TestForException.hpp"
00031 
00032 namespace Teuchos {
00033 
00034 void PrivateUtilityPack::throw_null( const std::string &type_name )
00035 {
00036   TEST_FOR_EXCEPTION(
00037     true, std::logic_error
00038     ,"RefCountPtr<"<<type_name<<">::assert_not_null() : You can not "
00039     " call operator->() or operator*() if get()==NULL!" );
00040 }
00041 
00042 namespace PrivateUtilityPack {
00043 
00044 void RefCountPtr_node::set_extra_data( const any &extra_data, const std::string& name, bool force_unique, EPrePostDestruction destroy_when )
00045 {
00046   if(extra_data_map_==NULL) {
00047     extra_data_map_ = new extra_data_map_t;
00048   }
00049   const std::string type_and_name( extra_data.type().name() + std::string(":") + name );
00050   if( !extra_data_map_->empty() && force_unique ) {
00051     extra_data_map_t::iterator itr = extra_data_map_->find(type_and_name);
00052     TEST_FOR_EXCEPTION(
00053       itr != extra_data_map_->end(), std::invalid_argument
00054       ,"Error, the type:name pair \'" << type_and_name << "\' already exists and force_unique==true!" );
00055   }
00056   (*extra_data_map_)[type_and_name] = extra_data_entry_t(extra_data,destroy_when); // This may add or replace!
00057 }
00058 
00059 any& RefCountPtr_node::get_extra_data( const std::string& type_name, const std::string& name )
00060 {
00061   TEST_FOR_EXCEPTION(
00062     extra_data_map_==NULL, std::invalid_argument
00063     ,"Error, no extra data has been set yet!" );
00064   any *extra_data = get_optional_extra_data(type_name,name);
00065   if(extra_data) return *extra_data;
00066   const std::string type_and_name( type_name + std::string(":") + name );
00067   TEST_FOR_EXCEPTION(
00068     extra_data == NULL, std::invalid_argument
00069     ,"Error, the type:name pair \'" << type_and_name << "\' is not found!" );
00070   return *extra_data; // Will never be executed!
00071 }
00072 
00073 any* RefCountPtr_node::get_optional_extra_data( const std::string& type_name, const std::string& name )
00074 {
00075   if( extra_data_map_ == NULL ) return NULL;
00076   const std::string type_and_name( type_name + std::string(":") + name );
00077   extra_data_map_t::iterator itr = extra_data_map_->find(type_and_name);
00078   if(itr != extra_data_map_->end())
00079     return &(*itr).second.extra_data;
00080   return NULL;
00081 }
00082 
00083 void RefCountPtr_node::impl_pre_delete_extra_data()
00084 {
00085   for( extra_data_map_t::iterator itr = extra_data_map_->begin(); itr != extra_data_map_->end(); ++itr ) {
00086     extra_data_map_t::value_type &entry = *itr;
00087     if(entry.second.destroy_when == PRE_DESTROY)
00088       entry.second.extra_data = any();
00089   }
00090 }
00091 
00092 } // namespace PrivateUtilityPack
00093 } // namespace Teuchos
00094 

Generated on Thu Sep 18 12:39:10 2008 for Teuchos - Trilinos Tools Package by doxygen 1.3.9.1