Bayeux  3.4.1
Core Foundation library for SuperNEMO
safe_serial.h
Go to the documentation of this file.
1 
19 #ifndef DATATOOLS_SAFE_SERIAL_H
20 #define DATATOOLS_SAFE_SERIAL_H
21 
22 // Standard Library:
23 #include <list>
24 #include <stdexcept>
25 
26 // This project:
27 #include <datatools/exception.h>
28 
29 namespace datatools {
30 
32  template<class T>
34  {
35  public:
36  typedef T data_type;
37 
39  safe_serial() : data_()
40  {
41  last_ = data_.rend();
42  }
43 
45  virtual ~safe_serial()
46  {
47  this->clear();
48  }
49 
51  void clear()
52  {
53  data_.clear();
54  last_ = data_.rend();
55  }
56 
58  bool empty() const
59  {
60  return data_.size() == 0;
61  }
62 
64  const data_type & get() const
65  {
66  DT_THROW_IF (this->empty(), std::logic_error, "No data!");
67  return *(last_);
68  }
69 
70  // /// \deprecated Return a mutable reference to the current object
71  // data_type& get() {
72  // DT_THROW_IF (this->empty(), std::logic_error, "No data!");
73  // return *(last_);
74  // }
75 
78  {
79  DT_THROW_IF (this->empty(), std::logic_error, "No data!");
80  return *(last_);
81  }
82 
84  void make()
85  {
86  try {
87  data_.push_back(data_type());
88  }
89  catch (...) {
90  DT_THROW_IF(true, std::logic_error, "Cannot insert new data element!");
91  }
92  last_ = data_.rbegin();
93  }
94 
96  void set(const data_type & data)
97  {
98  this->make();
99  this->get() = data;
100  }
101 
102  private:
103 
104  typename std::list<T> data_;
105  typename std::list<T>::reverse_iterator last_;
106 
107  };
108 
109 } // end of namespace datatools
110 
111 #endif // DATATOOLS_SAFE_SERIAL_H
112 
113 // Local Variables: --
114 // mode: c++ --
115 // c-file-style: "gnu" --
116 // tab-width: 2 --
117 // End: --
void set(const data_type &data)
Set the current object.
Definition: safe_serial.h:96
bool empty() const
Check if the container is empty.
Definition: safe_serial.h:58
data_type & grab()
Return a mutable reference to the current object.
Definition: safe_serial.h:77
void make()
Insert a new object at the end.
Definition: safe_serial.h:84
const data_type & get() const
Return a const reference to the current object.
Definition: safe_serial.h:64
Utility macros for exception handling.
T data_type
Definition: safe_serial.h:36
virtual ~safe_serial()
Destructor.
Definition: safe_serial.h:45
#define DT_THROW_IF(Condition, ExceptionType, Message)
Definition: exception.h:76
safe_serial()
Default constructor.
Definition: safe_serial.h:39
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
void clear()
Clear the container.
Definition: safe_serial.h:51
Template container wrapper class for safe storage of object with regards to Boost/Serilization memory...
Definition: safe_serial.h:33