Bayeux  3.4.1
Core Foundation library for SuperNEMO
tools.h
Go to the documentation of this file.
1 /* Author(s) : François Mauger <mauger@lpccaen.in2p3.fr>
3  * Creation date: 2010-09-22
4  * Last modified: 2010-09-22
5  *
6  * Description : Some typedef definitions and template class for handling pointers
7  *
8  */
9 
10 #ifndef MATERIALS_DETAIL_TOOLS_H
11 #define MATERIALS_DETAIL_TOOLS_H 1
12 
13 // Standard library:
14 #include <string>
15 #include <map>
16 
18 namespace materials {
19 
21  template <class T>
22  class smart_ref
23  {
24  public:
25 
27  bool is_owned() const
28  {
29  return _owned_;
30  }
31 
33  void set_owned(bool owned_)
34  {
35  _owned_ = owned_;
36  return;
37  }
38 
40  void set_alias_of(const std::string & name_)
41  {
42  _alias_of_ = name_;
43  return;
44  }
45 
47  const std::string & get_alias_of() const
48  {
49  return _alias_of_;
50  }
51 
53  bool is_alias() const
54  {
55  return ! is_owned() && ! _alias_of_.empty();
56  }
57 
60  {
61  _alias_of_.clear();
62  return;
63  }
64 
66  bool has_ref() const
67  {
68  return _ref_ != 0;
69  }
70 
72  T * grab_ptr()
73  {
74  return _ref_;
75  }
76 
78  const T * get_ptr() const
79  {
80  return _ref_;
81  }
82 
84  T & grab_ref()
85  {
86  return *_ref_;
87  }
88 
90  const T & get_ref() const
91  {
92  return *_ref_;
93  }
94 
96  smart_ref() : _owned_(false), _ref_(0)
97  {
98  return;
99  }
100 
102  virtual ~smart_ref()
103  {
104  reset_ref();
105  return;
106  }
107 
109  void reset_ref()
110  {
111  reset_alias_of();
112  if (_ref_ != 0) {
113  if (_owned_) delete _ref_;
114  _ref_ = 0;
115  }
116  return;
117  }
118 
120  void set_ref(T * ref_)
121  {
122  reset_ref();
123  _ref_ = ref_;
124  _owned_ = true;
125  }
126 
128  void set_ref(T & ref_)
129  {
130  reset_ref();
131  _ref_ = &ref_;
132  _owned_ = false;
133  return;
134  }
135 
136  private:
137 
138  bool _owned_;
139  T * _ref_;
140  std::string _alias_of_;
141 
142  };
143 
144  class isotope;
145  class element;
146  class material;
147 
148  typedef std::map<std::string, smart_ref<isotope> > isotope_dict_type;
149  typedef std::map<std::string, smart_ref<element> > element_dict_type;
150  typedef std::map<std::string, smart_ref<material> > material_dict_type;
151 
152 } // end of namespace materials
153 
154 #endif // MATERIALS_DETAIL_TOOLS_H
155 
156 /*
157 ** Local Variables: --
158 ** mode: c++ --
159 ** c-file-style: "gnu" --
160 ** tab-width: 2 --
161 ** End: --
162 */
bool is_alias() const
Check for existing alias information.
Definition: tools.h:53
std::map< std::string, smart_ref< element > > element_dict_type
Definition: tools.h:149
void set_alias_of(const std::string &name_)
Set the alias information.
Definition: tools.h:40
const T & get_ref() const
Return a reference to the non mutable object.
Definition: tools.h:90
T * grab_ptr()
Return a pointer to the mutable object.
Definition: tools.h:72
A handler object that is used as a smart reference for an object of which it can have the responsibil...
Definition: tools.h:22
void set_ref(T &ref_)
Set the internal reference without the responsibility of the deletion.
Definition: tools.h:128
std::map< std::string, smart_ref< isotope > > isotope_dict_type
Definition: tools.h:146
void reset_alias_of()
Reset the alias information.
Definition: tools.h:59
bool is_owned() const
Check the object deletion responsibility.
Definition: tools.h:27
const std::string & get_alias_of() const
Return the alias information.
Definition: tools.h:47
void set_ref(T *ref_)
Set the internal reference with the responsibility of the deletion (ala boost::scoped_ptr)
Definition: tools.h:120
T & grab_ref()
Return a reference to the mutable object.
Definition: tools.h:84
std::map< std::string, smart_ref< material > > material_dict_type
Definition: tools.h:150
const T * get_ptr() const
Return a pointer to the non mutable object.
Definition: tools.h:78
void set_owned(bool owned_)
Set the object deletion responsibility flag.
Definition: tools.h:33
smart_ref()
Default constructor.
Definition: tools.h:96
void reset_ref()
Reset the internal reference and delete the object if we have the responsibility of its deletion.
Definition: tools.h:109
The description of a material.
Definition: material.h:116
virtual ~smart_ref()
Destructor.
Definition: tools.h:102
Top-level namespace of the Bayeux/materials module library.
Definition: geom_manager_utils.h:14
bool has_ref() const
Check for a existing reference.
Definition: tools.h:66