libdballe  7.29
values.h
Go to the documentation of this file.
1 #ifndef DBALLE_CORE_VALUES_H
2 #define DBALLE_CORE_VALUES_H
3 
8 #include <dballe/core/defs.h>
9 #include <dballe/core/var.h>
10 #include <dballe/record.h>
11 #include <wreport/varinfo.h>
12 #include <vector>
13 #include <map>
14 #include <functional>
15 
16 namespace dballe {
17 
21 struct Station
22 {
24  std::string report;
25 
31  int ana_id = MISSING_INT;
32 
35 
38 
39  Station() = default;
40  Station(const dballe::Record& rec) { set_from_record(rec); }
41 
43  void clear_ids() { ana_id = MISSING_INT; }
44 
46  void set_from_record(const Record& rec);
47 
48  bool operator==(const Station& o) const
49  {
50  return report == o.report && ana_id == o.ana_id && coords == o.coords && ident == o.ident;
51  }
52 
59  void print(FILE* out, const char* end="\n") const;
60 };
61 
65 struct Sampling : public Station
66 {
69 
72 
75 
76  Sampling() = default;
77  Sampling(const dballe::Record& rec) { set_from_record(rec); }
78  void set_from_record(const Record& rec);
79  Sampling& operator=(const Sampling&) = default;
80  Sampling& operator=(const Station& st) {
81  Station::operator=(st);
82  return *this;
83  }
84 
85  bool operator==(const Sampling& o) const
86  {
87  return Station::operator==(o) && datetime == o.datetime && level == o.level && trange == o.trange;
88  }
89 
96  void print(FILE* out, const char* end="\n") const;
97 };
98 
99 namespace values {
100 
104 struct Value
105 {
107  int data_id = MISSING_INT;
108 
110  wreport::Var* var = nullptr;
111 
112  Value(const Value& o) : data_id(o.data_id), var(o.var ? new wreport::Var(*o.var) : nullptr) {}
113  Value(Value&& o) : data_id(o.data_id), var(o.var) { o.var = nullptr; }
114 
116  Value(const wreport::Var& var) : var(new wreport::Var(var)) {}
117 
119  Value(std::unique_ptr<wreport::Var>&& var) : var(var.release()) {}
120 
121  ~Value() { delete var; }
122 
123  Value& operator=(const Value& o)
124  {
125  if (this == &o) return *this;
126  data_id = o.data_id;
127  delete var;
128  var = o.var ? new wreport::Var(*o.var) : nullptr;
129  return *this;
130  }
131  Value& operator=(Value&& o)
132  {
133  if (this == &o) return *this;
134  data_id = o.data_id;
135  delete var;
136  var = o.var;
137  o.var = nullptr;
138  return *this;
139  }
140  bool operator==(const Value& o) const
141  {
142  if (data_id != o.data_id) return false;
143  if (var == o.var) return true;
144  if (!var || !o.var) return false;
145  return *var == *o.var;
146  }
147 
149  void clear_ids() { data_id = MISSING_INT; }
150 
152  void set(const wreport::Var& v)
153  {
154  delete var;
155  var = new wreport::Var(v);
156  }
157 
159  void set(std::unique_ptr<wreport::Var>&& v)
160  {
161  delete var;
162  var = v.release();
163  }
164 
166  void print(FILE* out) const;
167 };
168 
169 struct Encoder
170 {
171  std::vector<uint8_t> buf;
172 
173  Encoder();
174  void append_uint16(uint16_t val);
175  void append_uint32(uint32_t val);
176  void append_cstring(const char* val);
177  void append(const wreport::Var& var);
178  void append_attributes(const wreport::Var& var);
179 };
180 
181 struct Decoder
182 {
183  const uint8_t* buf;
184  unsigned size;
185 
186  Decoder(const std::vector<uint8_t>& buf);
187  uint16_t decode_uint16();
188  uint32_t decode_uint32();
189  const char* decode_cstring();
190  std::unique_ptr<wreport::Var> decode_var();
191 
195  static void decode_attrs(const std::vector<uint8_t>& buf, wreport::Var& var);
196 };
197 
198 }
199 
203 struct Values : protected std::map<wreport::Varcode, values::Value>
204 {
205  Values() = default;
206  Values(const dballe::Record& rec) { set_from_record(rec); }
207 
208  typedef std::map<wreport::Varcode, values::Value>::const_iterator const_iterator;
209  typedef std::map<wreport::Varcode, values::Value>::iterator iterator;
210  const_iterator begin() const { return std::map<wreport::Varcode, values::Value>::begin(); }
211  const_iterator end() const { return std::map<wreport::Varcode, values::Value>::end(); }
212  iterator begin() { return std::map<wreport::Varcode, values::Value>::begin(); }
213  iterator end() { return std::map<wreport::Varcode, values::Value>::end(); }
214  size_t size() const { return std::map<wreport::Varcode, values::Value>::size(); }
215  bool empty() const { return std::map<wreport::Varcode, values::Value>::empty(); }
216  void clear() { return std::map<wreport::Varcode, values::Value>::clear(); }
217  void erase(wreport::Varcode code) { std::map<wreport::Varcode, values::Value>::erase(code); }
218  bool operator==(const Values& o) const;
219 
220  const values::Value& operator[](wreport::Varcode code) const;
221  const values::Value& operator[](const char* code) const { return operator[](resolve_varcode(code)); }
222  const values::Value& operator[](const std::string& code) const { return operator[](resolve_varcode(code)); }
223  const values::Value* get(wreport::Varcode code) const;
224  const values::Value* get(const char* code) const { return get(resolve_varcode(code)); }
225  const values::Value* get(const std::string& code) const { return get(resolve_varcode(code)); }
226 
228  void set(const wreport::Var&);
229 
231  void set(std::unique_ptr<wreport::Var>&&);
232 
234  void set(const Values& vals);
235 
237  template<typename C, typename T> void set(C code, const T& val) { this->set(newvar(code, val)); }
238 
240  void add_data_id(wreport::Varcode code, int data_id);
241 
243  void set_from_record(const Record& rec);
244 
246  void clear_ids()
247  {
248  for (auto& i : *this)
249  i.second.clear_ids();
250  }
251 
255  std::vector<uint8_t> encode() const;
256 
260  static std::vector<uint8_t> encode_attrs(const wreport::Var& var);
261 
265  static void decode(const std::vector<uint8_t>& buf, std::function<void(std::unique_ptr<wreport::Var>)> dest);
266 
268  void print(FILE* out) const;
269 };
270 
275 {
276  Station info;
277  Values values;
278 
279  StationValues() = default;
280  StationValues(const dballe::Record& rec) : info(rec), values(rec) {}
281 
283  void set_from_record(const Record& rec);
284 
285  bool operator==(const StationValues& o) const
286  {
287  return info == o.info && values == o.values;
288  }
289 
291  void clear_ids()
292  {
293  info.clear_ids();
294  values.clear_ids();
295  }
296 
298  void print(FILE* out) const;
299 };
300 
305 {
306  Sampling info;
307  Values values;
308 
309  DataValues() = default;
310  DataValues(const dballe::Record& rec) : info(rec), values(rec) {}
311 
313  void set_from_record(const Record& rec);
314 
315  bool operator==(const DataValues& o) const
316  {
317  return info == o.info && values == o.values;
318  }
319 
321  void clear_ids()
322  {
323  info.clear_ids();
324  values.clear_ids();
325  }
326 
328  void print(FILE* out) const;
329 };
330 
331 }
332 
333 #endif
Information about a physical variable.
Definition: values.h:65
std::vector< uint8_t > encode() const
Encode these values in a DB-All.e specific binary representation.
void print(FILE *out) const
Print the contents of this StationValues.
Ident ident
Mobile station identifier.
Definition: values.h:37
void set(C code, const T &val)
Set from a variable created by dballe::newvar()
Definition: values.h:237
void clear_ids()
Reset all the database IDs.
Definition: values.h:321
void set(const wreport::Var &v)
Fill from a wreport::Var.
Definition: values.h:152
Information about a station.
Definition: values.h:21
Definition: values.h:169
A station or measured value.
Definition: values.h:104
void set(const wreport::Var &)
Set from a wreport::Var.
Coords coords
Station coordinates.
Definition: values.h:34
Coordinates.
Definition: types.h:337
A set of measured values.
Definition: values.h:304
int ana_id
Database ID of the station.
Definition: values.h:31
int data_id
Database ID of the value.
Definition: values.h:107
Information on how a value has been sampled or computed with regards to time.
Definition: types.h:587
void clear_ids()
Reset the database ID.
Definition: values.h:149
Key/value store where keys are strings and values are wreport variables.
Definition: record.h:17
void set(std::unique_ptr< wreport::Var > &&v)
Fill from a wreport::Var, taking ownership of it.
Definition: values.h:159
Definition: values.h:181
static std::vector< uint8_t > encode_attrs(const wreport::Var &var)
Encode the attributes of var in a DB-All.e specific binary representation.
Vertical level or layer.
Definition: types.h:532
A station identifier, that can be any string (including the empty string) or a missing value...
Definition: core/defs.h:19
static void decode(const std::vector< uint8_t > &buf, std::function< void(std::unique_ptr< wreport::Var >)> dest)
Decode variables from a DB-All.e specific binary representation.
void set_from_record(const Record &rec)
Fill this Station with values from a dballe::Record.
wreport::Var * var
wreport::Var representing the value
Definition: values.h:110
Trange trange
Time range.
Definition: values.h:74
uint16_t Varcode
void clear_ids()
Reset all the database IDs.
Definition: values.h:246
Level level
Vertical level or layer.
Definition: values.h:71
Value(std::unique_ptr< wreport::Var > &&var)
Construct from a wreport::Var, taking ownership of it.
Definition: values.h:119
void print(FILE *out) const
Print the contents of this Value.
void clear_ids()
Reset the database ID.
Definition: values.h:43
std::string report
rep_memo for this station
Definition: values.h:24
void set_from_record(const Record &rec)
Set from the contents of a dballe::Record.
void set_from_record(const Record &rec)
Set from the contents of a dballe::Record.
void clear_ids()
Reset all the database IDs.
Definition: values.h:291
Date and time.
Definition: types.h:158
A set of station values.
Definition: values.h:274
void print(FILE *out, const char *end="\n") const
Print the Sampling contents to a FILE*.
Datetime datetime
Date and time at which the value was measured or forecast.
Definition: values.h:68
void print(FILE *out, const char *end="\n") const
Print the Station to a FILE*.
Value(const wreport::Var &var)
Construct from a wreport::Var.
Definition: values.h:116
Common definitions.
void print(FILE *out) const
Print the contents of this Values.
Shortcut functions to work with wreport::Var in DB-All.e.
void print(FILE *out) const
Print the contents of this StationValues.
std::unique_ptr< wreport::Var > newvar(C code, const T &val)
Create a new Var, from the DB-All.e B table, with value.
Definition: var.h:62
static void decode_attrs(const std::vector< uint8_t > &buf, wreport::Var &var)
Decode the attributes of var from a buffer.
void add_data_id(wreport::Varcode code, int data_id)
Set the database ID for the Value with this wreport::Varcode.
Collection of Value objects, indexed by wreport::Varcode.
Definition: values.h:203
wreport::Varcode resolve_varcode(const char *name)
Resolve a variable name to a varcode proper, dealing with aliases and validation. ...
void set_from_record(const Record &rec)
Set from the contents of a dballe::Record.