Bayeux  3.4.1
Core Foundation library for SuperNEMO
smart_ref.h
Go to the documentation of this file.
1 /* Author(s) : Francois Mauger <mauger@lpccaen.in2p3.fr>
3  * Creation date: 2010-03-16
4  * Last modified: 2010-03-16
5  *
6  * License:
7  *
8  * Description:
9  * Smart reference to an existing instance
10  * The referenced type must have a default constructor.
11  *
12  */
13 
14 #ifndef DATATOOLS_SMART_REF_H
15 #define DATATOOLS_SMART_REF_H
16 
17 // Standard Library:
18 #include <cstdlib>
19 #include <iostream>
20 #include <list>
21 #include <string>
22 #include <stdexcept>
23 #include <sstream>
24 
25 // Third Party:
26 // - Boost:
27 #include <boost/serialization/access.hpp>
28 #include <boost/serialization/nvp.hpp>
29 
30 // This Project:
31 #include <datatools/properties.h>
32 
33 namespace datatools {
34 
36  template <class T>
37  class smart_ref
38  {
39  public:
40  typedef T instance_type;
45  typedef std::list<instance_type> col_type;
47 
50  : _ref_(nullptr)
51  , _properties_()
52  {
53  return;
54  }
55 
58  {
59  this->set(obj);
60  return;
61  }
62 
64  virtual ~smart_ref()
65  {
66  return;
67  }
68 
71  {
72  _ref_ = const_cast<pointer_type>(&obj);
73  return;
74  }
75 
78  {
79  return *_ref_;
80  }
81 
84  {
85  return _properties_;
86  }
87 
90  {
91  return _properties_;
92  }
93 
96  {
97  return _properties_;
98  }
99 
102  {
103  _properties_ = props;
104  return;
105  }
106 
108  void reset()
109  {
110  _ref_ = 0;
111  return;
112  }
113 
115  bool is_valid() const
116  {
117  return _ref_ != 0;
118  }
119 
121  class has_flag
122  : public std::unary_function<smart_ref_type, bool>
123  {
124  public:
125 
126  explicit has_flag(const std::string & flag_)
127  : _flag_(flag_)
128  {
129  return;
130  }
131 
132  bool operator()(const smart_ref_type & s_) const
133  {
134  if (!s_.is_valid()) return false;
135  return (s_.get_properties().has_flag(_flag_));
136  }
137 
138  private:
139 
140  std::string _flag_;
141 
142  };
143 
144  private:
145 
146  pointer_type _ref_;
147  datatools::properties _properties_;
148 
150 
152  template<class Archive>
153  void serialize(Archive& ar_, const unsigned int /*version*/)
154  {
155  ar_ & boost::serialization::make_nvp("properties", _properties_);
156  ar_ & boost::serialization::make_nvp("ref", _ref_);
157  return;
158  }
159 
160  };
161 
162 } // end of namespace datatools
163 
164 #endif // DATATOOLS_SMART_REF_H
165 
166 // Local Variables: --
167 // mode: c++ --
168 // c-file-style: "gnu" --
169 // tab-width: 2 --
170 // End: --
bool is_valid() const
Check the validity of the smart reference.
Definition: smart_ref.h:115
const instance_type & const_reference_type
Definition: smart_ref.h:42
const instance_type * const_pointer_type
Definition: smart_ref.h:44
void serialize(Archive &a_ar, geomtools::vector_3d &v_, const unsigned int a_version)
instance_type * pointer_type
Definition: smart_ref.h:43
void set_properties(const datatools::properties &props)
Set the container of properties.
Definition: smart_ref.h:101
const_reference_type get() const
Return the reference to the target object.
Definition: smart_ref.h:77
T instance_type
Definition: smart_ref.h:40
bool has_flag(const std::string &key_) const
Check if a boolean value with a given key/name exists with value 'true'.
smart_ref()
Default constructor.
Definition: smart_ref.h:49
virtual ~smart_ref()
Destructor.
Definition: smart_ref.h:64
void reset()
Reset the internal reference.
Definition: smart_ref.h:108
datatools::properties & grab_properties()
Return a mutable reference to the container of properties.
Definition: smart_ref.h:89
const datatools::properties & get_properties() const
Return a non mutable reference to the container of properties.
Definition: smart_ref.h:83
instance_type & reference_type
Definition: smart_ref.h:41
friend class boost::serialization::access
Definition: smart_ref.h:149
std::list< instance_type > col_type
Definition: smart_ref.h:45
A template class that stores a reference to an object and additional associated meta-data.
Definition: smart_ref.h:37
smart_ref(const_reference_type obj)
Constructor.
Definition: smart_ref.h:57
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
Predicate used by the smart_ref template class.
Definition: smart_ref.h:121
has_flag(const std::string &flag_)
Definition: smart_ref.h:126
void set(const_reference_type obj)
Set the internal reference to the target object.
Definition: smart_ref.h:70
datatools::properties & get_properties()
Definition: smart_ref.h:95
smart_ref< instance_type > smart_ref_type
Definition: smart_ref.h:46
bool operator()(const smart_ref_type &s_) const
Definition: smart_ref.h:132
A dictionary of arbitrary properties.
Definition: properties.h:125