Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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_protocols_moves_DataMap_hh
15 #define INCLUDED_protocols_moves_DataMap_hh
16 
17 // Project headers
18 #include <core/types.hh>
19 #include <sstream>
20 // ObjexxFCL Headers
21 
22 // C++ Headers
23 // AUTO-REMOVED #include <string>
24 #include <map>
25 
26 // Utility Headers
27 #include <utility/pointer/ReferenceCount.hh>
28 
29 #include <utility/exit.hh>
30 #include <string>
31 #include <basic/Tracer.hh>
32 
33 namespace protocols {
34 namespace moves {
35 
36 static basic::Tracer TR_hh( "protocols.moves.DataMap_hh" );
37 
38 /// @brief general-purpose store for any reference-count derived object
39 
41 public:
42  typedef std::map< std::string, std::map< std::string, utility::pointer::ReferenceCountOP > >::iterator iterator;
43  typedef std::map< std::string, std::map< std::string, utility::pointer::ReferenceCountOP > >::const_iterator const_iterator;
44 
45 public:
46  DataMap();
47  virtual ~DataMap();
48  iterator begin();
49  iterator end();
50  const_iterator begin() const;
51  const_iterator end() const;
52  // @brief add an object to the map, returning false if an object of that
53  // name already exists
54  bool add(
55  std::string const type,
56  std::string const name,
57  utility::pointer::ReferenceCountOP const op
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  std::map< std::string, utility::pointer::ReferenceCountOP > & operator [](
62  std::string const & type
63  );
64  /// @brief returns the size of the map (how many different types are in data_map_
65  core::Size size() const;
66 
67 private:
68  std::map< std::string, std::map< std::string, utility::pointer::ReferenceCountOP > > data_map_;
69 };
70 
71 /// @details a template utility function to grab any type of object from the
72 /// Data_map. Downcasts the ReferenceCount object in map to the template data
73 /// type using dynamic_cast to ensure type-correctness
74 template< class Ty >
75 Ty
76 DataMap::get( std::string const type, std::string const name ) const {
77  using namespace utility::pointer;
78  Ty ret( 0 );
79 
80  if( !has( type, name ) ){
81  std::stringstream error_message;
82  error_message << "ERROR: Could not find "<<type<<" and name "<<name<<" in Datamap\n";
83 
84  utility_exit_with_message( error_message.str() );
85  }
86 
87  std::map< std::string, utility::pointer::ReferenceCountOP > const dm( data_map_.find( type )->second );
88  for( std::map< std::string, ReferenceCountOP >::const_iterator it=dm.begin(); it!=dm.end(); ++it ) {
89  if( it->first == name ) {
90  ret = dynamic_cast< Ty >( it->second.get() );
91  break;
92  }
93  }
94  if( ret==0 ) {
95  std::stringstream error_message;
96  error_message << "ERROR: Dynamic_cast failed for type "<<type<<" and name "<<name<<'\n';
97 
98  utility_exit_with_message( error_message.str() );
99  }
100  return( ret );
101 }
102 
103 
104 /// @brief templated function for adding or getting an item from the datamap. Automatically checks whether an item
105 /// of the requested type and name exists on the datamap. If so, returns the OP for that item, if not, instantiates
106 /// that item on the datamap and returns the OP for it.
107 template < class Ty >
108 Ty *
110  Ty *obj;
111  if( data.has( type, name ) ){
112  obj = data.get< Ty * >( type, name );
113  TR_hh<<"Getting object-type, name "<<type<<' '<<name<<" from datamap"<<std::endl;
114  }
115  else{
116  obj = new Ty;
117  data.add( type, name, obj );
118  TR_hh<<"Adding object-type, name "<<type<<' '<<name<<" to datamap"<<std::endl;
119  }
120  return obj;
121 }
122 
123 } // moves
124 } // protocols
125 
126 #endif