CHROMA
named_obj.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 /*! @file
4  * @brief Named object support
5  */
6 
7 /*! \defgroup support Support routines
8  * \ingroup lib
9  *
10  * Support routines
11  */
12 
13 #ifndef __named_obj_h__
14 #define __named_obj_h__
15 
16 #include "chromabase.h"
17 #include "handle.h"
18 #include <map>
19 #include <string>
20 
21 namespace Chroma
22 {
23  //--------------------------------------------------------------------------------------
24  //! Typeinfo Hiding Base Clase
25  /*! @ingroup support
26  */
28  {
29  public:
31 
32  //! Setter
33  virtual void setFileXML(XMLReader& xml) = 0;
34 
35  //! Setter
36  virtual void setFileXML(XMLBufferWriter& xml) = 0;
37 
38  //! Setter
39  virtual void setRecordXML(XMLReader& xml) = 0;
40 
41  //! Setter
42  virtual void setRecordXML(XMLBufferWriter& xml) = 0;
43 
44  //! Getter
45  virtual void getFileXML(XMLReader& xml) const = 0;
46 
47  //! Getter
48  virtual void getFileXML(XMLBufferWriter& xml) const = 0;
49 
50  //! Getter
51  virtual void getRecordXML(XMLReader& xml) const = 0;
52 
53  //! Getter
54  virtual void getRecordXML(XMLBufferWriter& xml) const = 0;
55 
56  // This is key for cleanup
57  virtual ~NamedObjectBase() {}
58  };
59 
60 
61  //--------------------------------------------------------------------------------------
62  //! Type specific named object
63  /*! @ingroup support
64  */
65  template<typename T>
66  class NamedObject : public NamedObjectBase
67  {
68  public:
69  //! Constructor
70  NamedObject() : data(new T) {}
71 
72  template<typename P1>
73  NamedObject(const P1& p1) : data(new T(p1)) {}
74 
75  //! Destructor
77 
78  //! Setter
79  void setFileXML(XMLReader& xml)
80  {
81  std::ostringstream os;
82  xml.printCurrentContext(os);
83  file_xml = os.str();
84  }
85 
86  //! Setter
87  void setFileXML(XMLBufferWriter& xml)
88  {
89  file_xml = xml.printCurrentContext();
90  }
91 
92  //! Setter
93  void setRecordXML(XMLReader& xml)
94  {
95  std::ostringstream os;
96  xml.printCurrentContext(os);
97  record_xml = os.str();
98  }
99 
100  //! Setter
101  void setRecordXML(XMLBufferWriter& xml)
102  {
103  record_xml = xml.printCurrentContext();
104  }
105 
106  //! Getter
107  void getFileXML(XMLReader& xml) const
108  {
109  std::istringstream os(file_xml);
110  xml.open(os);
111  }
112 
113  //! Getter
114  void getFileXML(XMLBufferWriter& xml) const
115  {
116  xml.writeXML(file_xml);
117  }
118 
119  //! Getter
120  void getRecordXML(XMLReader& xml) const
121  {
122  std::istringstream os(record_xml);
123  xml.open(os);
124  }
125 
126  //! Getter
127  void getRecordXML(XMLBufferWriter& xml) const
128  {
129  xml.writeXML(record_xml);
130  }
131 
132  //! Mutable data ref
133  virtual T& getData() {
134  return *data;
135  }
136 
137  //! Const data ref
138  virtual const T& getData() const {
139  return *data;
140  }
141 
142  private:
146  };
147 
148 
149  //--------------------------------------------------------------------------------------
150  //! The Map Itself
151  /*! @ingroup support
152  */
154  {
155  public:
156  // Creation: clear the std::map
158  the_map.clear();
159  };
160 
161  // Destruction: erase all elements of the std::map
163  {
164  typedef std::map<std::string, NamedObjectBase*>::iterator I;
165  while( ! the_map.empty() )
166  {
167  I iter = the_map.begin();
168 
169  delete iter->second;
170 
171  the_map.erase(iter);
172  }
173  }
174 
175 
176  //! Create an entry of arbitrary type.
177  template<typename T>
178  void create(const std::string& id)
179  {
180  // Lookup and throw exception if duplicate found
181  typedef std::map<std::string, NamedObjectBase*>::iterator I;
182  I iter = the_map.find(id);
183  if( iter != the_map.end())
184  {
185  std::ostringstream error_stream;
186  error_stream << "NamedObjectMap::create : duplicate id = " << id << std::endl;
187  throw error_stream.str();
188  }
189 
190  // Create a new object of specified type (empty)
191  // Dynamic cast to Typeless base clasee.
192  // Note multi1d's need to be loked up and resized appropriately
193  // and no XML files are added at this point
194  the_map[id] = dynamic_cast<NamedObjectBase*>(new NamedObject<T>());
195  if (NULL == the_map[id])
196  {
197  std::ostringstream error_stream;
198  error_stream << "NamedObjectMap::create : error creating NamedObject for id= " << id << std::endl;
199  throw error_stream.str();
200  }
201  }
202 
203  //! Create an entry of arbitrary type, with 1 parameter
204  template<typename T, typename P1>
205  void create(const std::string& id, const P1& p1)
206  {
207  // Lookup and throw exception if duplicate found
208  MapType_t::iterator iter = the_map.find(id);
209  if(iter != the_map.end())
210  {
211  std::ostringstream error_stream;
212  error_stream << "NamedObjectMap::create : duplicate id = " << id << std::endl;
213  throw error_stream.str();
214  }
215 
216  // Create a new object of specified type (empty)
217  // Dynamic cast to Typeless base clasee.
218  // Note multi1d's need to be loked up and resized appropriately
219  // and no XML files are added at this point
220  the_map[id] = dynamic_cast<NamedObjectBase*>(new NamedObject<T>(p1));
221  if (NULL == the_map[id])
222  {
223  std::ostringstream error_stream;
224  error_stream << "NamedObjectMap::create : error creating NamedObject for id= " << id << std::endl;
225  throw error_stream.str();
226  }
227  }
228 
229 
230  //! Check if an id exists
231  bool check(const std::string& id) const
232  {
233  // Do a lookup
234  MapType_t::const_iterator iter = the_map.find(id);
235 
236  // If found then return true
237  return (iter != the_map.end()) ? true : false;
238  }
239 
240 
241  //! Delete an item that we no longer neeed
242  void erase(const std::string& id)
243  {
244  // Do a lookup
245  MapType_t::iterator iter = the_map.find(id);
246 
247  // If found then delete it.
248  if( iter != the_map.end() )
249  {
250  // Delete the data.of the record
251  delete iter->second;
252 
253  // Delete the record
254  the_map.erase(iter);
255  }
256  else
257  {
258  // We attempt to erase something non existent
259  std::ostringstream error_stream;
260  error_stream << "NamedObjectMap::erase : erasing unknown id = " << id << std::endl;
261  throw error_stream.str();
262  }
263  }
264 
265 
266  //! Dump out all objects
267  void dump() const
268  {
269  QDPIO::cout << "Available Keys are : " << std::endl;
270  for(MapType_t::const_iterator j = the_map.begin(); j != the_map.end(); j++)
271  QDPIO::cout << j->first << std::endl;
272  }
273 
274 
275  //! Look something up and return a NamedObjectBase reference
276  NamedObjectBase& get(const std::string& id) const
277  {
278  // Find it
279  MapType_t::const_iterator iter = the_map.find(id);
280  if (iter == the_map.end())
281  {
282  // Not found -- lookup exception
283  std::ostringstream error_stream;
284  error_stream << "NamedObjectMap::get : unknown id = " << id << std::endl;
285  throw error_stream.str();
286  }
287  else
288  {
289  // Found, return the reference
290  return *(iter->second);
291  }
292  }
293 
294  //! Look something up and return a ref to the derived named object
295  template<typename T>
296  T& getObj(const std::string& id)
297  {
298  return dynamic_cast<NamedObject<T>&>(get(id));
299  }
300 
301  //! Look something up and return a ref to the derived named object
302  template<typename T>
303  const T& getObj(const std::string& id) const
304  {
305  return dynamic_cast<NamedObject<T>&>(get(id));
306  }
307 
308  //! Look something up and return a ref to actual data
309  template<typename T>
310  T& getData(const std::string& id)
311  {
312  return dynamic_cast<NamedObject<T>&>(get(id)).getData();
313  }
314 
315  //! Look something up and return a ref to actual data
316  template<typename T>
317  const T& getData(const std::string& id) const
318  {
319  return dynamic_cast<NamedObject<T>&>(get(id)).getData();
320  }
321 
322  private:
323  typedef std::map<std::string, NamedObjectBase*> MapType_t;
325  };
326 
327 }
328 
329 #endif
Primary include file for CHROMA library code.
Class for counted reference semantics.
Definition: handle.h:33
Typeinfo Hiding Base Clase.
Definition: named_obj.h:28
virtual void getRecordXML(XMLBufferWriter &xml) const =0
Getter.
virtual void setFileXML(XMLBufferWriter &xml)=0
Setter.
virtual ~NamedObjectBase()
Definition: named_obj.h:57
virtual void setRecordXML(XMLReader &xml)=0
Setter.
virtual void setRecordXML(XMLBufferWriter &xml)=0
Setter.
virtual void getRecordXML(XMLReader &xml) const =0
Getter.
virtual void setFileXML(XMLReader &xml)=0
Setter.
virtual void getFileXML(XMLReader &xml) const =0
Getter.
virtual void getFileXML(XMLBufferWriter &xml) const =0
Getter.
The Map Itself.
Definition: named_obj.h:154
std::map< std::string, NamedObjectBase * > MapType_t
Definition: named_obj.h:323
void create(const std::string &id, const P1 &p1)
Create an entry of arbitrary type, with 1 parameter.
Definition: named_obj.h:205
bool check(const std::string &id) const
Check if an id exists.
Definition: named_obj.h:231
void dump() const
Dump out all objects.
Definition: named_obj.h:267
NamedObjectBase & get(const std::string &id) const
Look something up and return a NamedObjectBase reference.
Definition: named_obj.h:276
void create(const std::string &id)
Create an entry of arbitrary type.
Definition: named_obj.h:178
const T & getObj(const std::string &id) const
Look something up and return a ref to the derived named object.
Definition: named_obj.h:303
T & getObj(const std::string &id)
Look something up and return a ref to the derived named object.
Definition: named_obj.h:296
const T & getData(const std::string &id) const
Look something up and return a ref to actual data.
Definition: named_obj.h:317
void erase(const std::string &id)
Delete an item that we no longer neeed.
Definition: named_obj.h:242
T & getData(const std::string &id)
Look something up and return a ref to actual data.
Definition: named_obj.h:310
Type specific named object.
Definition: named_obj.h:67
void getRecordXML(XMLReader &xml) const
Getter.
Definition: named_obj.h:120
~NamedObject()
Destructor.
Definition: named_obj.h:76
std::string file_xml
Definition: named_obj.h:144
void setFileXML(XMLReader &xml)
Setter.
Definition: named_obj.h:79
std::string record_xml
Definition: named_obj.h:145
void getFileXML(XMLBufferWriter &xml) const
Getter.
Definition: named_obj.h:114
void getFileXML(XMLReader &xml) const
Getter.
Definition: named_obj.h:107
void setFileXML(XMLBufferWriter &xml)
Setter.
Definition: named_obj.h:87
virtual T & getData()
Mutable data ref.
Definition: named_obj.h:133
NamedObject(const P1 &p1)
Definition: named_obj.h:73
NamedObject()
Constructor.
Definition: named_obj.h:70
void getRecordXML(XMLBufferWriter &xml) const
Getter.
Definition: named_obj.h:127
void setRecordXML(XMLBufferWriter &xml)
Setter.
Definition: named_obj.h:101
Handle< T > data
Definition: named_obj.h:143
virtual const T & getData() const
Const data ref.
Definition: named_obj.h:138
void setRecordXML(XMLReader &xml)
Setter.
Definition: named_obj.h:93
Class for counted reference semantics.
unsigned j
Definition: ldumul_w.cc:35
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
LinOpSysSolverMGProtoClover::T T
::std::string string
Definition: gtest.h:1979