astrotypes  0.0
HeaderBase.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_HEADERBASE_H
25 #define PSS_ASTROTYPES_SIGPROC_HEADERBASE_H
26 
27 #include "../HeaderField.h"
28 #include <iostream>
29 #include <exception>
30 #include <map>
31 
32 namespace pss {
33 namespace astrotypes {
34 namespace sigproc {
35 
68 template<typename Derived>
70 {
71  private:
72  friend class HeaderFieldBase;
73 
74  public:
75  template<typename Stream>
76  class InfoSentry {
77  public:
78  InfoSentry(Stream& os);
79  Stream& operator<<(Derived const& os) const;
80 
81  private:
82  Stream& _os;
83  };
84 
85  public:
86  // Adapter for ouputing debug info about the header
87  class Info {
88  public:
89  Info();
90  template<typename Stream>
91  InfoSentry<Stream> sentry(Stream& os) const;
92  };
93 
94  public:
95  HeaderBase();
96  // maps will not be copied over
97  HeaderBase(HeaderBase const&);
98 
104  void read(std::istream & stream);
105 
111  void write(std::ostream & stream) const;
112 
122  bool operator==(HeaderBase const&) const;
123 
128  bool operator!=(HeaderBase const&) const;
129 
133  std::size_t size() const;
134 
138  void reset();
139 
140  protected:
144  Derived& copy_header_values(Derived const& dst);
145 
146  // default implemention overridable in Derived type
147  void do_read(std::istream & stream);
148  void do_write(std::ostream & stream) const;
149  bool do_equal(HeaderBase const&) const;
150  void do_reset();
151 
152  protected:
153 
154  template<typename T>
155  std::runtime_error parse_error(std::string const& msg, T const& msg2) const;
156 
160  void add(SigProcLabel const& label, HeaderFieldBase& field);
161 
165  void add_read(SigProcLabel const& label, HeaderFieldBase& field);
166 
171  void add_compare_field(SigProcLabel const&);
172 
176  void remove_compare_field(SigProcLabel const&);
177 
178  private:
179  mutable unsigned _size; // byte size of the header
180 
181  std::map<SigProcLabel, HeaderFieldBase*> _headers;
182  std::map<SigProcLabel, HeaderFieldBase*> _compare_headers;
183  std::map<SigProcLabel, HeaderFieldBase*> _read_only_headers;
184 };
185 
186 template<typename Derived>
187 std::ostream& operator<<(std::ostream& os, HeaderBase<Derived> const&);
188 
189 template<typename Derived>
190 std::istream& operator>>(std::istream& os, HeaderBase<Derived>&);
191 
192 } // namespace sigproc
193 } // namespace astrotypes
194 } // namespace pss
195 #include "HeaderBase.cpp"
196 
197 #endif // PSS_ASTROTYPES_SIGPROC_HEADERBASE_H
class to provide a virtual lookup table for read/write the varoious types of SigProcVariables ...
Definition: HeaderField.h:44
std::runtime_error parse_error(std::string const &msg, T const &msg2) const
Definition: HeaderBase.cpp:33
bool do_equal(HeaderBase const &) const
Definition: HeaderBase.cpp:193
void reset()
reset all header variables to an undefined state
Definition: HeaderBase.cpp:58
void remove_compare_field(SigProcLabel const &)
remvoe a field to be checked in operator==
Definition: HeaderBase.cpp:72
std::size_t size() const
returns the number of bytes in the header
Definition: HeaderBase.cpp:230
Store SigProc header information.
Definition: HeaderBase.h:69
void add_compare_field(SigProcLabel const &)
add a field to be checked in operator==
Definition: HeaderBase.cpp:80
bool operator==(HeaderBase const &) const
returns true if the data formats match
Definition: HeaderBase.cpp:187
void add(SigProcLabel const &label, HeaderFieldBase &field)
add a field to be parsed for read and write
Definition: HeaderBase.cpp:245
std::istream & operator>>(std::istream &stream, HeaderBase< Derived > &headers)
Definition: HeaderBase.cpp:180
InfoSentry< Stream > sentry(Stream &os) const
void do_read(std::istream &stream)
Definition: HeaderBase.cpp:106
void add_read(SigProcLabel const &label, HeaderFieldBase &field)
add a field to be parsed for read only
Definition: HeaderBase.cpp:255
bool operator!=(HeaderBase const &) const
returns true if the data format meta data does not match
Definition: HeaderBase.cpp:224
void write(std::ostream &stream) const
write header data to the provided stream
Definition: HeaderBase.cpp:145
void do_write(std::ostream &stream) const
Definition: HeaderBase.cpp:150
void read(std::istream &stream)
read in header data from the provided stream
Definition: HeaderBase.cpp:100
Derived & copy_header_values(Derived const &dst)
copy values from one header to another
Definition: HeaderBase.cpp:88
Stream & operator<<(Derived const &os) const
Definition: HeaderBase.cpp:281