astrotypes  0.0
SigProcVariable.cpp
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 
25 
26 namespace pss {
27 namespace astrotypes {
28 namespace sigproc {
29 
30 // --- generic implementation ----------
31 template<typename T>
32 unsigned SigProcVariable<T>::write(std::ostream& stream, T const& var)
33 {
34  stream.write(reinterpret_cast<const char*>(&var), sizeof(T));
35  return sizeof(T);
36 }
37 
38 template<typename T>
39 unsigned SigProcVariable<T>::read(std::istream& stream, T& var)
40 {
41  stream.read(reinterpret_cast<char*>(&var), sizeof(T));
42  return stream.gcount();
43 }
44 
45 // ------- boost::units::quantity ------------------------
46 template<typename Unit, typename T>
47 unsigned SigProcVariable<boost::units::quantity<Unit, T>>::write(std::ostream& stream, boost::units::quantity<Unit, T> const& var)
48 {
49  return SigProcVariable<T>::write(stream, var.value());
50 }
51 
52 template<typename Unit, typename NumericalRep>
53 unsigned SigProcVariable<boost::units::quantity<Unit, NumericalRep>>::read(std::istream& stream, boost::units::quantity<Unit, NumericalRep>& var)
54 {
55  stream.read(reinterpret_cast<char*>(const_cast<NumericalRep*>(&var.value())), sizeof(NumericalRep));
56  return stream.gcount();
57 }
58 
59 // ------- std::string ------------------------
60 inline unsigned SigProcVariable<std::string>::write(std::ostream& stream, std::string const& var)
61 {
62  int32_t size=static_cast<int32_t>(var.size());
63  if( size > 80 ) {
64  throw std::runtime_error("SigProc::write: illegal size of string (max 80): " + std::to_string(size));
65  }
66  // write out the size of the string
67  unsigned s = SigProcVariable<decltype(size)>::write(stream, size);
68  stream.write(var.data(), size);
69  return size + s;
70 }
71 
72 inline unsigned SigProcVariable<std::string>::read(std::istream& stream, std::string& var)
73 {
74  // read in the size of the string
75  int32_t size=0;
76  stream.read(reinterpret_cast<char*>(&size), sizeof(size));
77  auto s = sizeof(int32_t);
78 
79  if( size < 0 || size > 80 ) {
80  std::runtime_error e("SigProcVariable<std::string>::read: illegal size of string: " + std::to_string(size));
81  throw e;
82  }
83 
84  if(!stream.fail()) {
85  var.resize(size);
86  stream.read(&var[0], size);
87  s += stream.gcount();
88  }
89  return s;
90 }
91 
92 inline void SigProcLabel::write(std::ostream& stream) const
93 {
94  SigProcVariable<std::string>::write(stream, _string);
95 }
96 
97 inline void SigProcLabel::read(std::istream& stream)
98 {
99  SigProcVariable<std::string>::read(stream, _string);
100 }
101 
102 inline std::istream& operator>>(std::istream& stream, SigProcLabel& var)
103 {
104  var.read(stream);
105  return stream;
106 }
107 
108 inline std::ostream& operator<<(std::ostream& stream, SigProcLabel const& var)
109 {
110  var.write(stream);
111  return stream;
112 }
113 
114 } // namespace sigproc
115 } // namespace astrotypes
116 } // namespace pss
117 
118 namespace std {
119  template<>
120  struct hash<pss::astrotypes::sigproc::SigProcLabel>
121  {
122  std::size_t operator()(const pss::astrotypes::sigproc::SigProcLabel& quantity) const
123  {
124  return std::hash<std::string>()(quantity.string());
125  }
126  };
127 } // namespace std
128 
static unsigned write(std::ostream &, T const &var)
write the provided variable to the stream
std::string const & string() const
STL namespace.
static unsigned read(std::istream &, T &var)
read form the stream into the variable provided
std::istream & operator>>(std::istream &stream, HeaderBase< Derived > &headers)
Definition: HeaderBase.cpp:180
Read and write variables to sigproc format.
Header::InfoSentry< Stream > operator<<(Stream &os, Header::Info const &adapter)
Definition: Header.cpp:397
std::size_t operator()(const pss::astrotypes::sigproc::SigProcLabel &quantity) const
void write(std::ostream &os) const