IODA Bundle
Constant.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 1996-2012 ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation nor
8  * does it submit to any jurisdiction.
9  */
10 
11 /// @author Piotr Kuchta
12 /// @author Simon Smart
13 /// @date January 2019
14 
15 #ifndef odc_core_codec_Constant_H
16 #define odc_core_codec_Constant_H
17 
18 #include "odc/core/Codec.h"
19 
20 namespace odc {
21 namespace codec {
22 
23 //----------------------------------------------------------------------------------------------------------------------
24 
25 // Now the actual codecs!
26 
27 
28 template<typename ByteOrder, typename ValueType>
29 class CodecConstant : public core::DataStreamCodec<ByteOrder> {
30 
31 public: // definitions
32 
33  constexpr static const char* codec_name() { return "constant"; }
34 
35 public: // methods
36 
37  CodecConstant(api::ColumnType type, const std::string& name=codec_name()) : core::DataStreamCodec<ByteOrder>(name, type) {}
39 
40 private: // methods
41 
42  void gatherStats(const double& v) override;
43  unsigned char* encode(unsigned char* p, const double& d) override;
44  void decode(double* out) override;
45  void skip() override;
46 
47  void print(std::ostream& s) const override;
48 };
49 
50 
51 template<typename ByteOrder>
52 class CodecConstantString : public CodecConstant<ByteOrder, double> {
53 
54 public: // name
55 
56  constexpr static const char* codec_name() { return "constant_string"; }
57 
58 public: // methods
59 
61 
62 private: // methods
63 
64  unsigned char* encode(unsigned char* p, const double& d) override;
65  void decode(double* out) override;
66  void skip() override;
67 
68  void print(std::ostream& s) const override;
69  size_t numStrings() const override { return 1; }
70 
73  void load(core::DataStream<ByteOrder>& ds) override;
74  void save(core::DataStream<ByteOrder>& ds) override;
75 };
76 
77 //----------------------------------------------------------------------------------------------------------------------
78 
79 // Implementation of Constant
80 
81 template <typename ByteOrder, typename ValueType>
83  static_assert(sizeof(ValueType) == sizeof(v), "unsafe casting check");
84  const ValueType& val(reinterpret_cast<const ValueType&>(v));
86 }
87 
88 template <typename ByteOrder, typename ValueType>
89 unsigned char* CodecConstant<ByteOrder, ValueType>::encode(unsigned char* p, const double&) {
90  return p;
91 }
92 
93 template <typename ByteOrder, typename ValueType>
95  static_assert(sizeof(ValueType) == sizeof(double), "unsafe casting check");
96  *reinterpret_cast<ValueType*>(out) = static_cast<ValueType>(this->min_);
97 }
98 
99 template <typename ByteOrder, typename ValueType>
101 
102 template <typename ByteOrder, typename ValueType>
103 void CodecConstant<ByteOrder, ValueType>::print(std::ostream& s) const {
104  s << this->name_ << ", value=" << std::fixed << static_cast<ValueType>(this->min_);
105 }
106 
107 //----------------------------------------------------------------------------------------------------------------------
108 
109 // Implementation of ConstantString
110 
111 template <typename ByteOrder>
112 unsigned char* CodecConstantString<ByteOrder>::encode(unsigned char* p, const double&) {
113  return p;
114 }
115 
116 template <typename ByteOrder>
118  (*out) = this->min_;
119 }
120 
121 template <typename ByteOrder>
123 
124 template <typename ByteOrder>
127  ByteOrder::swap(this->min_);
128  ByteOrder::swap(this->max_);
129 }
130 
131 template <typename ByteOrder>
133  ByteOrder::swap(this->min_);
134  ByteOrder::swap(this->max_);
136  ByteOrder::swap(this->min_);
137  ByteOrder::swap(this->max_);
138 }
139 
140 template <typename ByteOrder>
141 void CodecConstantString<ByteOrder>::print(std::ostream& s) const {
142  s << this->name_ << ", value='"
143  << std::string(reinterpret_cast<const char*>(&this->min_), sizeof(double))
144  << "'";
145 }
146 
147 //----------------------------------------------------------------------------------------------------------------------
148 
149 } // namespace codec
150 } // namespace odc
151 
152 #endif
153 
void decode(double *out) override
Definition: Constant.h:94
void skip() override
Definition: Constant.h:100
constexpr static const char * codec_name()
Definition: Constant.h:33
unsigned char * encode(unsigned char *p, const double &d) override
Definition: Constant.h:89
void print(std::ostream &s) const override
Definition: Constant.h:103
void gatherStats(const double &v) override
Definition: Constant.h:82
CodecConstant(api::ColumnType type, const std::string &name=codec_name())
Definition: Constant.h:37
void decode(double *out) override
Definition: Constant.h:117
unsigned char * encode(unsigned char *p, const double &d) override
Definition: Constant.h:112
size_t numStrings() const override
Definition: Constant.h:69
CodecConstantString(api::ColumnType type)
Definition: Constant.h:60
constexpr static const char * codec_name()
Definition: Constant.h:56
void print(std::ostream &s) const override
Definition: Constant.h:141
const std::string & name() const
Definition: Codec.h:40
virtual void gatherStats(const double &v)
Definition: Codec.cc:98
DataStreamCodec(const std::string &name, api::ColumnType type)
Definition: Codec.h:125
void save(GeneralDataStream &ds)
Definition: Codec.cc:76
void load(GeneralDataStream &ds)
Definition: Codec.cc:60
DataStream< ByteOrder > & ds()
Definition: Codec.h:157
Definition: ColumnInfo.h:23