Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DataMap.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file
11 /// @brief
12 /// @author Sarel Fleishman
13 
14 #ifndef INCLUDED_basic_datacache_DataMap_hh
15 #define INCLUDED_basic_datacache_DataMap_hh
16 
17 // Project headers
18 #include <basic/Tracer.hh> // for Tracer
19 #include <map> // for map, __map_cons...
20 #include <memory> // for shared_ptr, dyn...
21 #include <platform/types.hh> // for Size
22 #include <sstream> // for string, operator<<
23 #include <string> // for char_traits
24 #include <utility/excn/Exceptions.hh> // for EXCN_Msg_Exception
25 #include <utility> // for pair
26 #include <utility/pointer/owning_ptr.hh> // for dynamic_pointer_cast
27 #include <utility/pointer/ReferenceCount.hh> // for ReferenceCount
28 
29 namespace basic {
30 namespace datacache {
31 
32 static THREAD_LOCAL basic::Tracer TR_hh( "basic.datacache.DataMap_hh" );
33 
34 /// @brief general-purpose store for any reference-count derived object
35 
37 public:
38  typedef std::map< std::string, std::map< std::string, utility::pointer::ReferenceCountOP > >::iterator iterator;
39  typedef std::map< std::string, std::map< std::string, utility::pointer::ReferenceCountOP > >::const_iterator const_iterator;
40 
41 public:
42  DataMap();
43  virtual ~DataMap();
44 
45  iterator begin();
46  iterator end();
47 
48  const_iterator begin() const;
49  const_iterator end() const;
50 
51  // @brief add an object to the map, returning false if an object of that
52  // name already exists
53  bool add(
54  std::string const & type,
55  std::string const & name,
57  );
58 
59  bool has( std::string const & type, std::string const & name="" ) const;
60  template< class Ty > Ty get( std::string const & type, std::string const & name ) const;
61  template< class Ty > utility::pointer::shared_ptr< Ty > get_ptr( std::string const & type, std::string const & name ) const;
62 
63  std::map< std::string, utility::pointer::ReferenceCountOP > & operator [](
64  std::string const & type
65  );
66 
67  /// @brief returns the size of the map (how many different types are in data_map_
68  platform::Size size() const;
69 
70 private:
71  std::map< std::string, std::map< std::string, utility::pointer::ReferenceCountOP > > data_map_;
72 };
73 
74 /// @details a template utility function to grab any type of object from the
75 /// Data_map. Downcasts the ReferenceCount object in map to the template data
76 /// type using dynamic_cast to ensure type-correctness
77 /// @throws Throws a utility::excn::EXCN_Msg_Exception in the event that
78 /// the requested object cannot be found in the DataMap.
79 template< class Ty >
80 Ty
81 DataMap::get( std::string const & type, std::string const & name ) const {
82  using namespace utility::pointer;
83  Ty ret( 0 );
84 
85  if ( !has( type, name ) ) {
86  std::stringstream error_message;
87  error_message << "ERROR: Could not find "<<type<<" and name "<<name<<" in Datamap\n";
88  throw utility::excn::EXCN_Msg_Exception( error_message.str() );
89  }
90 
91  std::map< std::string, utility::pointer::ReferenceCountOP > const dm( data_map_.find( type )->second );
92  for ( std::map< std::string, ReferenceCountOP >::const_iterator it=dm.begin(); it!=dm.end(); ++it ) {
93  if ( it->first == name ) {
94  ret = dynamic_cast< Ty >( it->second.get() );
95  break;
96  }
97  }
98  if ( ret==0 ) {
99  std::stringstream error_message;
100  error_message << "ERROR: Dynamic_cast failed for type "<<type<<" and name "<<name<<'\n';
101  throw utility::excn::EXCN_Msg_Exception( error_message.str() );
102  }
103  return( ret );
104 }
105 
106 /// @details a template utility function to grab any type of object from the
107 /// Data_map. Downcasts the owning pointer in map to the template data
108 /// type using dynamic_pointer_cast to ensure type-correctness
109 /// @throws Throws a utility::excn::EXCN_Msg_Exception in the event that
110 /// the requested object cannot be found in the DataMap.
111 template< class Ty >
112 utility::pointer::shared_ptr< Ty >
113 DataMap::get_ptr( std::string const & type, std::string const & name ) const {
114  using namespace utility::pointer;
115  utility::pointer::shared_ptr< Ty > ret( 0 );
116 
117  if ( !has( type, name ) ) {
118  std::stringstream error_message;
119  error_message << "ERROR: Could not find "<<type<<" and name "<<name<<" in Datamap\n";
120  throw utility::excn::EXCN_Msg_Exception( error_message.str() );
121  }
122 
123  std::map< std::string, utility::pointer::ReferenceCountOP > const dm( data_map_.find( type )->second );
124  for ( std::map< std::string, ReferenceCountOP >::const_iterator it=dm.begin(); it!=dm.end(); ++it ) {
125  if ( it->first == name ) {
126  ret = utility::pointer::dynamic_pointer_cast< Ty >( it->second );
127  break;
128  }
129  }
130  if ( ret==0 ) {
131  std::stringstream error_message;
132  error_message << "ERROR: Dynamic_cast failed for type "<<type<<" and name "<<name<<'\n';
133  throw utility::excn::EXCN_Msg_Exception( error_message.str() );
134  }
135  return( ret );
136 }
137 
138 
139 /// @brief templated function for adding or getting an item from the datamap. Automatically checks whether an item
140 /// of the requested type and name exists on the datamap. If so, returns the OP for that item, if not, instantiates
141 /// that item on the datamap and returns the OP for it.
142 template < class Ty >
143 utility::pointer::shared_ptr< Ty >
144 get_set_from_datamap( std::string const & type, std::string const & name, basic::datacache::DataMap & data ){
145  utility::pointer::shared_ptr< Ty > obj;
146  if ( data.has( type, name ) ) {
147  obj = data.get_ptr< Ty >( type, name );
148  TR_hh<<"Getting object-type, name "<<type<<' '<<name<<" from datamap"<<std::endl;
149  } else {
150  obj = utility::pointer::shared_ptr< Ty >( new Ty );
151  data.add( type, name, obj );
152  TR_hh<<"Adding object-type, name "<<type<<' '<<name<<" to datamap"<<std::endl;
153  }
154  return obj;
155 }
156 
157 } // datacache
158 } // basic
159 
160 #endif
std::map< std::string, std::map< std::string, utility::pointer::ReferenceCountOP > >::iterator iterator
Definition: DataMap.hh:38
#define THREAD_LOCAL
utility::keys::KeyLookup< KeyType >::const_iterator const_iterator
Key collection iterators.
ReferenceCount base class – dispatch class.
static THREAD_LOCAL basic::Tracer TR_hh("basic.datacache.DataMap_hh")
Non-owning access smart pointer – dispatch class.
std::map< std::string, utility::pointer::ReferenceCountOP > & operator[](std::string const &type)
Definition: DataMap.cc:53
utility::pointer::shared_ptr< Ty > get_ptr(std::string const &type, std::string const &name) const
Definition: DataMap.hh:113
common derived classes for thrown exceptions
Base class for reference-counted polymorphic classes.
std::map< std::string, std::map< std::string, utility::pointer::ReferenceCountOP > >::const_iterator const_iterator
Definition: DataMap.hh:39
Tracer IO system.
bool add(std::string const &type, std::string const &name, utility::pointer::ReferenceCountOP const op)
Definition: DataMap.cc:29
general-purpose store for any reference-count derived object
Definition: DataMap.hh:36
Ty get(std::string const &type, std::string const &name) const
Definition: DataMap.hh:81
boost::shared_ptr< ReferenceCount > ReferenceCountOP
utility::pointer::shared_ptr< Ty > get_set_from_datamap(std::string const &type, std::string const &name, basic::datacache::DataMap &data)
templated function for adding or getting an item from the datamap. Automatically checks whether an it...
Definition: DataMap.hh:144
platform::Size size() const
returns the size of the map (how many different types are in data_map_
Definition: DataMap.cc:70
Class for handling user debug/warnings/errors. Use instance of this class instead of 'std::cout' for ...
Definition: Tracer.hh:134
rule< Scanner, string_closure::context_t > name
Definition: Tag.cc:376
bool has(std::string const &type, std::string const &name="") const
Definition: DataMap.cc:40
std::size_t Size
Definition: types.hh:37
std::map< std::string, std::map< std::string, utility::pointer::ReferenceCountOP > > data_map_
Definition: DataMap.hh:71