Bayeux  3.4.1
Core Foundation library for SuperNEMO
things-inl.h
Go to the documentation of this file.
1 
3 #ifndef DATATOOLS_THINGS_INL_H
4 #define DATATOOLS_THINGS_INL_H
5 
6 // This project:
7 #include <datatools/exception.h>
8 
9 namespace datatools {
10 
11  // Template methods of the things class:
12 
13  // Add a new bank of given type, name, description and constness
14  template<class T>
15  T& things::add(const std::string& a_name,
16  const std::string& a_desc,
17  bool a_const)
18  {
19  T* new_obj = 0;
20  new_obj = new T;
21  DT_THROW_IF (new_obj == 0,
22  std::logic_error,
23  "Cannot allocate object '" << a_name << "' !");
25  = dynamic_cast<datatools::i_serializable*>(new_obj);
26  if (new_cast == 0) {
27  if (new_obj != 0) {
28  delete new_obj;
29  }
30  const std::type_info& ti = typeid(T);
31  std::ostringstream message;
32  message << "datatools::things::add<T>: Request type '"
33  << ti.name() << "' does not inherit from the '" << "datatools::i_serializable" << "' base class !";
34  DT_THROW_IF(true,
36  "Request type '" << ti.name() << "' does not inherit from the '" << "datatools::i_serializable" << "' base class !");
37  }
38  this->add_impl(a_name, new_cast, a_desc, a_const);
39  return *new_obj;
40  }
41 
42 
43  template<class T>
44  T* things::pop(const std::string& a_name)
45  {
46  dict_type::iterator found = _things_.find(a_name);
47  DT_THROW_IF (found == _things_.end(),
48  std::logic_error,
49  "No stored object has name '" << a_name << "' !");
50  const std::type_info& ti = typeid(T);
51  datatools::i_serializable& cur = *found->second.handle;
52  const std::type_info& tf = typeid(cur);
53  T tmp;
54  DT_THROW_IF (ti != tf,
56  "Request type '" << ti.name() << "' ('" << tmp.get_serial_tag()
57  << "') does not match the type '" << tf.name() << "' of the stored object named '"
58  << a_name << "' ('" << found->second.handle->get_serial_tag() << "') !");
59  T* ptr = dynamic_cast<T*>(found->second.handle);
60  found->second.handle = 0;
61  _things_.erase(found);
62  return ptr;
63  }
64 
65 
66  template<class T>
67  T& things::grab(const std::string& a_name)
68  {
69  dict_type::iterator found = _things_.find(a_name);
70  DT_THROW_IF (found == _things_.end(),
71  std::logic_error,
72  "No stored object has name '" << a_name << "' !");
73  const std::type_info& ti = typeid(T);
74  datatools::i_serializable& cur = *found->second.handle;
75  const std::type_info& tf = typeid(cur);
76  T tmp;
77  DT_THROW_IF (ti != tf,
79  "Request type '" << ti.name() << tmp.get_serial_tag() << "') does not match the type '"
80  << tf.name() << "' of the stored object named '" << a_name << "' ('"
81  << found->second.handle->get_serial_tag() << "') !");
82  DT_THROW_IF (found->second.is_const(),
83  std::logic_error,
84  "Object named '" << a_name << "' is constant !");
85  return *(dynamic_cast<T*>(found->second.handle));
86  }
87 
88 
89  template<class T>
90  bool things::is_a(const std::string& a_name) const {
91  dict_type::const_iterator found = _things_.find(a_name);
92  DT_THROW_IF (found == _things_.end(),
93  std::logic_error,
94  "No object named '" << a_name << "' !");
95  const std::type_info& ti = typeid(T);
96  datatools::i_serializable& cur = *found->second.handle;
97  const std::type_info& tf = typeid(cur);
98  return (ti == tf);
99  }
100 
101 
102  template<class T>
103  const T& things::get(const std::string& a_name) const
104  {
105  dict_type::const_iterator found = _things_.find(a_name);
106  DT_THROW_IF (found == _things_.end(),
107  std::logic_error,
108  "No object named '" << a_name << "' !");
109  const std::type_info& ti = typeid(T);
110  datatools::i_serializable& cur = *found->second.handle;
111  const std::type_info& tf = typeid(cur);
112  T tmp;
113  DT_THROW_IF (ti != tf,
115  "Request type '" << ti.name() << "' ('" << tmp.get_serial_tag()
116  << "') does not match the type '" << tf.name() << "' of the stored object named '" << a_name
117  << "' ('" << found->second.handle->get_serial_tag() << "') !");
118  return *(dynamic_cast<const T*>(found->second.handle));
119  }
120 
121 } // end of namespace datatools
122 
123 #endif // DATATOOLS_THINGS_INL_H
124 
125 // Local Variables: --
126 // mode: c++ --
127 // c-file-style: "gnu" --
128 // tab-width: 2 --
129 // End: --
T & grab(const std::string &name_)
Return a reference to a mutable object of given type and name.
Definition: things-inl.h:67
Base abstract class of all serializable (and possibly introspectable) classes.
Definition: i_serializable.h:51
bool is_a(const std::string &name_) const
Check if a stored object with given name is of given type.
Definition: things-inl.h:90
const T & get(const std::string &name_) const
Return a reference to a non mutable object of given type and name.
Definition: things-inl.h:103
Utility macros for exception handling.
An exception for invalid cast operation within the things class.
Definition: things.h:67
#define DT_THROW_IF(Condition, ExceptionType, Message)
Definition: exception.h:76
T * pop(const std::string &name_)
Definition: things-inl.h:44
T & add(const std::string &name_, const std::string &desc_="", bool const_=false)
Instantiate and add an object of given type and name.
Definition: things-inl.h:15
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13