IODA Bundle
Codec.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 #ifndef odc_core_Codec_H
12 #define odc_core_Codec_H
13 
14 
15 #include <cstring>
16 #include <limits>
17 
18 #include "odc/api/ColumnType.h"
19 #include "odc/core/CodecFactory.h"
20 #include "odc/core/DataStream.h"
21 #include "odc/MDI.h"
22 
23 namespace eckit { class DataHandle; }
24 
25 namespace odc {
26 namespace core {
27 
28 //----------------------------------------------------------------------------------------------------------------------
29 
30 
31 class Codec {
32 public:
33  Codec(const std::string& name, api::ColumnType type);
34  virtual ~Codec();
35 
36  /// Creates a clone of this codec. NOTE: the clone is not really usefull for coding/decoding, but has the same stats/missing
37  /// values as the original codec, which can be useful sometimes.
38  virtual std::unique_ptr<Codec> clone();
39 
40  const std::string& name() const { return name_; }
41 
42  char* encode(char* p, const double& d) { return reinterpret_cast<char*>(encode(reinterpret_cast<uint8_t*>(p), d)); }
43  virtual unsigned char* encode(unsigned char* p, const double& d) = 0;
44  virtual void decode(double* out) = 0;
45  virtual void skip() = 0;
46 
48  virtual void setDataStream(DataStream<SameByteOrder>& ds);
49  virtual void setDataStream(DataStream<OtherByteOrder>& ds);
50  virtual void clearDataStream() = 0;
51 
52  void load(GeneralDataStream& ds);
53  virtual void load(DataStream<SameByteOrder>& ds);
54  virtual void load(DataStream<OtherByteOrder>& ds);
55  void save(GeneralDataStream& ds);
56  virtual void save(DataStream<SameByteOrder>& ds);
57  virtual void save(DataStream<OtherByteOrder>& ds);
58 
59  void resetStats() { min_ = max_ = missingValue_; hasMissing_ = false; }
60 
61  virtual void gatherStats(const double& v);
62 
63  void hasMissing(bool h) { hasMissing_ = h; }
64  int32_t hasMissing() const { return hasMissing_; }
65 
66  void min(double m) { min_ = m; }
67  double min() const { return min_; }
68 
69  void max(double m) { max_ = m; }
70  double max() const { return max_; }
71 
72  void missingValue(double v);
73  double missingValue() const { return missingValue_; }
74 
75  // Some special functions for string handling inside the CodecOptimizer
76  virtual size_t numStrings() const { NOTIMP; }
77  virtual void copyStrings(Codec& rhs) { NOTIMP; }
78 
79  virtual size_t dataSizeDoubles() const { return 1; }
80  virtual void dataSizeDoubles(size_t count) {
81  if (count != 1)
82  throw eckit::SeriousBug("Data size cannot be changed from 1x8 bytes", Here());
83  }
84 
85 private: // methods
86 
87  virtual void print(std::ostream& s) const;
88 
89  friend std::ostream& operator<<(std::ostream& s, const Codec& p) {
90  p.print(s);
91  return s;
92  }
93 
94 protected:
95 
96  std::string name_;
97 
98  int32_t hasMissing_;
99  double missingValue_;
100  double min_;
101  double max_;
102 
104 
105 private:
106  Codec(const Codec&);
108 };
109 
110 
111 //template <typename DATASTREAM>
112 //Codec* Codec::findCodec(const std::string& name, bool differentByteOrder)
113 //{
114 // return AbstractCodecFactory<typename DATASTREAM::DataHandleType>::getCodec(name, differentByteOrder);
115 //}
116 
117 /// We need somewhere to distinguish the behaviour for SameByteOrder vs OtherByteOrder. That
118 /// somewhere is here.
119 
120 template <typename ByteOrder>
121 class DataStreamCodec : public Codec {
122 
123 public: // methods
124 
125  DataStreamCodec(const std::string& name, api::ColumnType type) : Codec(name, type), ds_(0) {}
126 
127  using Codec::setDataStream;
129  ds_ = &ds;
130  }
131  void clearDataStream() override { ds_ = 0; }
132 
133 protected: // methods
134 
135  using Codec::load;
136  void load(DataStream<ByteOrder>& ds) override {
137  // n.b. name read by the CodecFactory.
138  ds.read(hasMissing_);
139  ds.read(min_);
140  ds.read(max_);
141  ds.read(missingValue_);
142  }
143 
144  using Codec::save;
145  void save(DataStream<ByteOrder>& ds) override {
146  // n.b. Name is written by the _column_ not the codec.
147  // ds.write(name_);
148  ds.write(hasMissing_);
149  ds.write(min_);
150  ds.write(max_);
151  ds.write(missingValue_);
152  }
153 
154 protected:
155 
156  // n.b. ds_ MUST be initialised before it is used.
157  DataStream<ByteOrder>& ds() { ASSERT(ds_); return *ds_; }
159 };
160 
161 
162 //----------------------------------------------------------------------------------------------------------------------
163 
164 } // namespace core
165 } // namespace odc
166 
167 #endif
168 
static void count(void *counter, const double *data, size_t n)
Definition: UnitTests.cc:531
Codec(const std::string &name, api::ColumnType type)
Definition: Codec.cc:24
void max(double m)
Definition: Codec.h:69
virtual void decode(double *out)=0
std::string name_
Definition: Codec.h:96
void setDataStream(GeneralDataStream &ds)
Definition: Codec.cc:44
virtual size_t numStrings() const
Definition: Codec.h:76
void resetStats()
Definition: Codec.h:59
virtual size_t dataSizeDoubles() const
Definition: Codec.h:79
virtual void copyStrings(Codec &rhs)
Definition: Codec.h:77
Codec(const Codec &)
void hasMissing(bool h)
Definition: Codec.h:63
const std::string & name() const
Definition: Codec.h:40
virtual void print(std::ostream &s) const
Definition: Codec.cc:111
double missingValue() const
Definition: Codec.h:73
double min() const
Definition: Codec.h:67
double missingValue_
Definition: Codec.h:99
void save(GeneralDataStream &ds)
Definition: Codec.cc:76
char * encode(char *p, const double &d)
Definition: Codec.h:42
virtual void clearDataStream()=0
void min(double m)
Definition: Codec.h:66
int32_t hasMissing() const
Definition: Codec.h:64
double max_
Definition: Codec.h:101
api::ColumnType type_
Definition: Codec.h:103
virtual void gatherStats(const double &v)
Definition: Codec.cc:98
friend std::ostream & operator<<(std::ostream &s, const Codec &p)
Definition: Codec.h:89
double min_
Definition: Codec.h:100
void load(GeneralDataStream &ds)
Definition: Codec.cc:60
virtual std::unique_ptr< Codec > clone()
Definition: Codec.cc:32
double max() const
Definition: Codec.h:70
int32_t hasMissing_
Definition: Codec.h:98
virtual void skip()=0
virtual void dataSizeDoubles(size_t count)
Definition: Codec.h:80
virtual unsigned char * encode(unsigned char *p, const double &d)=0
virtual ~Codec()
Definition: Codec.cc:42
Codec & operator=(const Codec &)
DataStream< ByteOrder > * ds_
Definition: Codec.h:158
void clearDataStream() override
Definition: Codec.h:131
DataStreamCodec(const std::string &name, api::ColumnType type)
Definition: Codec.h:125
void save(DataStream< ByteOrder > &ds) override
Definition: Codec.h:145
void load(DataStream< ByteOrder > &ds) override
Definition: Codec.h:136
void setDataStream(DataStream< ByteOrder > &ds) override
Definition: Codec.h:128
DataStream< ByteOrder > & ds()
Definition: Codec.h:157
Definition: ColumnInfo.h:23