ObservationIO.h
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
7 #ifndef NUKLEI_OBSERVATIONSERIAL_H
8 #define NUKLEI_OBSERVATIONSERIAL_H
9 
10 #include <typeinfo>
11 #include <boost/utility.hpp>
12 
13 #include <nuklei/Definitions.h>
14 #include <nuklei/Observation.h>
16 #include <nuklei/nullable.h>
17 
18 namespace nuklei {
19 
20  class KernelCollection;
21 
22  class ObservationIOError : public Error
23  {
24  public: ObservationIOError(const std::string &s) : Error(s) {}
25  };
26 
27 
28  /**
29  * @brief Base class for kernel reader and point reader classes.
30  *
31  * This class also provides methods for reading a file with automatic type
32  * detection.
33  */
34  class ObservationReader : boost::noncopyable
35  {
36  public:
37  virtual ~ObservationReader();
38 
39  NUKLEI_UNIQUE_PTR<Observation> readObservation();
40  NUKLEI_UNIQUE_PTR<KernelCollection> readObservations();
42 
43  virtual Observation::Type type() const = 0;
44 
45  void registerType(Observation::Type t)
46  {
47  oc.type_ = nameFromType<Observation>(t);
48  }
49 
50  void registerType(const ObservationReader& reader)
51  {
52  oc.type_ = typeid(reader).name();
53  }
54 
55  virtual nullable<unsigned> nObservations() const
56  { return undefined(); }
57 
58  virtual void addRegionOfInterest(boost::shared_ptr<RegionOfInterest> roi);
59 
60 
61  virtual void init() { registerType(*this); init_(); }
62  virtual void reset() = 0;
63 
64  class Counter
65  {
66  typedef std::map<std::string, unsigned> map_t;
67  typedef std::list<std::string> list_t;
68  public:
69  Counter() {}
70 
71  void incLabel(const std::string &label);
72  bool empty() const;
73 
74  //private:
75  friend std::ostream& operator<<(std::ostream &out, const Counter &c);
76  std::string type_;
77  map_t counts_;
78  list_t labels_;
79  };
80 
81  static NUKLEI_UNIQUE_PTR<ObservationReader>
82  createReader(const std::string& arg);
83  static NUKLEI_UNIQUE_PTR<ObservationReader>
84  createReader(const std::string& arg, const Observation::Type t);
85  protected:
86  virtual NUKLEI_UNIQUE_PTR<Observation> readObservation_() = 0;
87  virtual void init_() = 0;
88  Counter oc;
89  private:
90  boost::shared_ptr<RegionOfInterest> roi_;
91  };
92 
93  std::ostream& operator<<(std::ostream &out, const ObservationReader::Counter &c);
94 
95  /**
96  * @brief Reads the data available from the reader @p r and stores it into @p
97  * kc.
98  */
100  /**
101  * @brief Reads the file @p s (with automatic type detection) and stores the
102  * read data into @p kc.
103  */
104  void readObservations(const std::string &s, KernelCollection &kc);
105  /**
106  * @brief Reads the file @p s (with automatic type detection) and stores the
107  * read data into @p kc.
108  *
109  * @p t is set to the format of the file @p s.
110  */
111  void readObservations(const std::string &s, KernelCollection &kc,
112  Observation::Type& t);
113  /**
114  * @brief Reads the file @p s (@em no automatic type detection) and stores the
115  * read data into @p kc.
116  *
117  * @p t is the format in which the data is stored.
118  */
119  void readObservationsWithSpecificFormat(const std::string &s,
120  KernelCollection &kc,
121  const Observation::Type& t);
122 
123  /**
124  * @brief Reads a single observation from file @p s (with automatic type
125  * detection), and returns it.
126  *
127  * This method check that the file @p s contains a single observation. If it
128  * is not the case, an exception is thrown.
129  */
130  kernel::base::ptr readSingleObservation(const std::string &s);
131  /**
132  * @brief Reads a single observation from file @p s (with automatic type
133  * detection), and returns it.
134  *
135  * @p t is set to the format of the file @p s.
136  *
137  * This method check that the file @p s contains a single observation. If it
138  * is not the case, an exception is thrown.
139  */
140  kernel::base::ptr readSingleObservation(const std::string &s,
141  Observation::Type& t);
142  /**
143  * @brief Reads a single observation from file @p s (@em no automatic type
144  * detection), and returns it.
145  *
146  * @p t is the format in which the data is stored.
147  *
148  * This method check that the file @p s contains a single observation. If it
149  * is not the case, an exception is thrown.
150  */
152  readSingleObservationWithSpecificFormat(const std::string &s,
153  const Observation::Type& t);
154 
155  /** @brief Base class for kernel writer and point writer classes. */
156  class ObservationWriter : boost::noncopyable
157  {
158  public:
159  virtual ~ObservationWriter();
160 
161  virtual void writeObservation(const Observation &o) = 0;
162  void writeObservations(const KernelCollection &kc);
163 
164  virtual void writeBuffer() = 0;
165  virtual void init() = 0;
166  virtual void reset() = 0;
167  virtual NUKLEI_UNIQUE_PTR<Observation> templateObservation() const = 0;
168 
169  virtual Observation::Type type() const = 0;
170 
171  static NUKLEI_UNIQUE_PTR<ObservationWriter>
172  createWriter(const std::string& arg, const Observation::Type t);
173 
174  };
175 
176  /** @brief Writes the content of @p kc using the provided writer @p w. */
178  /** @brief Writes the content of @p kc to file @p s, using file format @p t. */
179  void writeObservations(const std::string &s, const KernelCollection &kc,
180  const Observation::Type &t = Observation::NUKLEI);
181  /**
182  * @brief Writes @p k to file @p s, using file format @p t.
183  */
184  void writeSingleObservation(const std::string &s, const kernel::base &k,
185  const Observation::Type &t = Observation::NUKLEI);
186 
187 }
188 
189 #endif
190 
Definition: Observation.h:17
Definition: nullable.h:19
Public namespace.
Definition: Color.cpp:9
void writeObservations(ObservationWriter &w, const KernelCollection &kc)
Writes the content of kc using the provided writer w.
Definition: ObservationIO.h:64
This class acts as a vector-like container for kernels. It also provides methods related to kernel de...
Nuklei-related errors.
Definition: Common.h:205
kernel::base::ptr readSingleObservationWithSpecificFormat(const std::string &s, const Observation::Type &t)
Reads a single observation from file s (no automatic type detection), and returns it.
Polymorphic kernel class.
Definition: Kernel.h:45
NUKLEI_UNIQUE_PTR< kernel::base > ptr
NUKLEI_UNIQUE_PTR for kernel::base.
Definition: Kernel.h:50
Base class for kernel reader and point reader classes.
Definition: ObservationIO.h:34
Definition: ObservationIO.h:22
void readObservations(ObservationReader &r, KernelCollection &kc)
Reads the data available from the reader r and stores it into kc.
kernel::base::ptr readSingleObservation(const std::string &s)
Reads a single observation from file s (with automatic type detection), and returns it.
void readObservationsWithSpecificFormat(const std::string &s, KernelCollection &kc, const Observation::Type &t)
Reads the file s (no automatic type detection) and stores the read data into kc.
Base class for kernel writer and point writer classes.
void writeSingleObservation(const std::string &s, const kernel::base &k, const Observation::Type &t=Observation::NUKLEI)
Writes k to file s, using file format t.
Obsolete – use boost::optional and boost::none instead.
Definition: nullable.h:25
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.