astrotypes  0.0
HeaderField.h
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2018 PulsarSearchSoft
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef PSS_ASTROTYPES_SIGPROC_HEADERFIELD_H
25 #define PSS_ASTROTYPES_SIGPROC_HEADERFIELD_H
28 #include <string>
29 #include <vector>
30 
31 
32 namespace pss {
33 namespace astrotypes {
34 namespace sigproc {
35 
36 class Header;
37 
45 {
46  protected:
47  // allows us to hide HeaderFields from the writer
49 
51  void add_read(SigProcLabel const& header_label, HeaderFieldBase& field, Header& header);
52 
53  public:
54  // registers the Field to be read/written by the Header parser
55  HeaderFieldBase(SigProcLabel const& header_name, Header& h);
56  ~HeaderFieldBase() {}; // virtual not needed, we never expect to pass these around
57 
59  // @return the number of bytes read
60  virtual unsigned read(std::istream &) { return 0; };
61 
63  // @return the number of bytes written
64  virtual unsigned write(std::ostream &) const { return 0; };
65 
67  virtual void write_info(std::ostream &) const { };
68 
70  virtual std::string const& header_info(std::string const& h) const { return h; };
71 
73  virtual void reset() = 0;
74 
76  virtual bool is_set() const = 0;
77 
78  virtual bool operator==(const HeaderFieldBase&) const { return true; };
79 
80  virtual void operator=(const HeaderFieldBase&) = 0;
81 };
82 
83 template<typename T>
85 {
86  typedef HeaderFieldBase BaseT;
87 
88  protected:
89  // for expansion only, not to be used as a public interface
91 
92  public:
94  HeaderField(SigProcLabel const& header_label, Header& header);
95  HeaderField(SigProcLabel const& header_label, Header& header, T const& to_copy);
96 
97  // Warning HeaderFieldBase must be convertible to HeaderField
98  HeaderField(SigProcLabel const& header_label, Header& header, HeaderField const&);
99 
100  operator T const&() const { return *_var; }
101  operator T&() { return *_var; }
102  operator utils::Optional<T> const&() const { return _var; }
103  HeaderField& operator=(T const& var);
104  void operator=(const HeaderFieldBase&) override;
105 
106  unsigned read(std::istream &) override;
107  unsigned write(std::ostream &) const override;
108  void write_info(std::ostream &) const override;
109  void reset() override;
110  bool is_set() const override;
111  bool operator==(const HeaderFieldBase&) const override;
112  bool operator==(const HeaderField&) const;
113 
114  protected:
116 };
117 
121 template<typename T, typename ToleranceType>
123 {
124  typedef HeaderField<T> BaseT;
125 
126  public:
127  HeaderFieldWithTolerance(SigProcLabel const& header_label, Header& header, ToleranceType const&);
128  HeaderFieldWithTolerance(SigProcLabel const& header_label, Header& header, ToleranceType const&, HeaderFieldWithTolerance const&);
129  HeaderFieldWithTolerance& operator=(T const& var);
130 
131  bool operator==(const HeaderFieldBase&) const override;
132  bool operator==(const HeaderFieldWithTolerance&) const;
133 
134  private:
135  ToleranceType _tolerance;
136 };
137 
138 
146 template<typename T>
147 class HeaderField<std::vector<T>> : public HeaderFieldBase
148 {
149  typedef HeaderFieldBase BaseT;
150  // vectors are marked with start and end tags
151  class NullField : public HeaderFieldBase {
152  public:
153  NullField() {}
154  // do content other than the label
155  bool is_set() const override { return false; }
156  void reset() override {};
157  void operator=(const HeaderFieldBase&) override {};
158  };
159 
160  class ItemField : public HeaderFieldBase {
161  // only needs to read
162  public:
163  ItemField(std::vector<T>& vec) : _vec(vec) {}
164  bool is_set() const override { return false; }
165  unsigned read(std::istream &) override;
166  void reset() override {};
167  void operator=(const HeaderFieldBase&) override {}
168 
169  private:
170  std::vector<T>& _vec;
171  };
172 
173  public:
174  HeaderField(SigProcLabel const& start_label, SigProcLabel const& item_label, SigProcLabel const& end_label, Header& header);
175  HeaderField(SigProcLabel const& start_label, SigProcLabel const& item_label, SigProcLabel const& end_label, Header& header, HeaderField const& copy);
176 
177  operator std::vector<T> const&() const { return _var; }
178  operator std::vector<T>&() { return _var; }
179  operator utils::Optional<std::vector<T>> const&() const { return _var; }
180  HeaderField& operator=(std::vector<T> const& var);
181 
182  unsigned read(std::istream &) override;
183  unsigned write(std::ostream &) const override;
184  std::string const& header_info(std::string const& h) const override;
185  void write_info(std::ostream &) const override;
186  bool is_set() const override;
187  void reset() override;
188 
189  std::vector<T> const& operator*() const { return _var; };
190  std::vector<T>& operator*() { return _var; };
191 
192  bool operator==(const HeaderFieldBase&) const override;
193  bool operator==(const HeaderField&) const;
194  void operator=(const HeaderFieldBase&) override;
195 
196  private:
197  std::vector<T> _var;
198  ItemField _item_label_handler;
199  NullField _end_label_handler;
200  SigProcLabel _end_label;
201  SigProcLabel _item_label;
202 };
203 
204 } // namespace sigproc
205 } // namespace astrotypes
206 } // namespace pss
207 
208 #endif // PSS_ASTROTYPES_SIGPROC_HEADERFIELD_H
class to provide a virtual lookup table for read/write the varoious types of SigProcVariables ...
Definition: HeaderField.h:44
virtual bool operator==(const HeaderFieldBase &) const
Definition: HeaderField.h:78
HeaderFieldWithTolerance(SigProcLabel const &header_label, Header &header, ToleranceType const &)
bool operator==(const HeaderFieldBase &) const override
virtual void operator=(const HeaderFieldBase &)=0
void reset() override
reset the variable to undefined state
Definition: HeaderField.cpp:90
specialisation to allow the == operator to return true if the values are within specified tolerance ...
Definition: HeaderField.h:122
Store SigProc header information.
Definition: Header.h:73
virtual std::string const & header_info(std::string const &h) const
return the required header string to be used in info output
Definition: HeaderField.h:70
STL namespace.
HeaderField & operator=(T const &var)
Definition: HeaderField.cpp:56
virtual bool is_set() const =0
return true if the variable has been set, false otherwise
bool operator==(const HeaderFieldBase &) const override
void write_info(std::ostream &) const override
an information string about the value in the field (for debugging etc)
Definition: HeaderField.cpp:84
unsigned read(std::istream &) override
read into variable
Definition: HeaderField.cpp:69
A simplified, faster, but less secure and less versatile version of boost::optional.
Definition: Optional.h:38
virtual unsigned write(std::ostream &) const
serialise a variable to sigproc header format
Definition: HeaderField.h:64
HeaderFieldWithTolerance & operator=(T const &var)
virtual void write_info(std::ostream &) const
an information string about the value in the field (for debugging etc)
Definition: HeaderField.h:67
bool is_set() const override
return true if the variable has been set, false otherwise
Definition: HeaderField.cpp:96
unsigned write(std::ostream &) const override
serialise a variable to sigproc header format
Definition: HeaderField.cpp:78
void add_read(SigProcLabel const &header_label, HeaderFieldBase &field, Header &header)
add the provided field as a read_only parameter
virtual unsigned read(std::istream &)
read into variable
Definition: HeaderField.h:60
virtual void reset()=0
reset the variable to undefined state