libdballe  7.19
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 
15 namespace dballe {
16 
20 struct Station
21 {
23  std::string report;
24 
30  int ana_id = MISSING_INT;
31 
34 
37 
38  Station() = default;
39  Station(const dballe::Record& rec) { set_from_record(rec); }
40 
42  void clear_ids() { ana_id = MISSING_INT; }
43 
45  void set_from_record(const Record& rec);
46 
47  bool operator==(const Station& o) const
48  {
49  return report == o.report && ana_id == o.ana_id && coords == o.coords && ident == o.ident;
50  }
51 
58  void print(FILE* out, const char* end="\n") const;
59 };
60 
64 struct Sampling : public Station
65 {
68 
71 
74 
75  Sampling() = default;
76  Sampling(const dballe::Record& rec) { set_from_record(rec); }
77  void set_from_record(const Record& rec);
78  Sampling& operator=(const Sampling&) = default;
79  Sampling& operator=(const Station& st) {
80  Station::operator=(st);
81  return *this;
82  }
83 
84  bool operator==(const Sampling& o) const
85  {
86  return Station::operator==(o) && datetime == o.datetime && level == o.level && trange == o.trange;
87  }
88 
95  void print(FILE* out, const char* end="\n") const;
96 };
97 
98 namespace values {
99 
103 struct Value
104 {
106  int data_id = MISSING_INT;
107 
109  wreport::Var* var = nullptr;
110 
111  Value(const Value& o) : data_id(o.data_id), var(o.var ? new wreport::Var(*o.var) : nullptr) {}
112  Value(Value&& o) : data_id(o.data_id), var(o.var) { o.var = nullptr; }
113 
115  Value(const wreport::Var& var) : var(new wreport::Var(var)) {}
116 
118  Value(std::unique_ptr<wreport::Var>&& var) : var(var.release()) {}
119 
120  ~Value() { delete var; }
121 
122  Value& operator=(const Value& o)
123  {
124  if (this == &o) return *this;
125  data_id = o.data_id;
126  delete var;
127  var = o.var ? new wreport::Var(*o.var) : nullptr;
128  return *this;
129  }
130  Value& operator=(Value&& o)
131  {
132  if (this == &o) return *this;
133  data_id = o.data_id;
134  delete var;
135  var = o.var;
136  o.var = nullptr;
137  return *this;
138  }
139  bool operator==(const Value& o) const
140  {
141  if (data_id != o.data_id) return false;
142  if (var == o.var) return true;
143  if (!var || !o.var) return false;
144  return *var == *o.var;
145  }
146 
148  void clear_ids() { data_id = MISSING_INT; }
149 
151  void set(const wreport::Var& v)
152  {
153  delete var;
154  var = new wreport::Var(v);
155  }
156 
158  void set(std::unique_ptr<wreport::Var>&& v)
159  {
160  delete var;
161  var = v.release();
162  }
163 
165  void print(FILE* out) const;
166 };
167 
168 struct Encoder
169 {
170  std::vector<uint8_t> buf;
171 
172  Encoder();
173  void append_uint16(uint16_t val);
174  void append_uint32(uint32_t val);
175  void append_cstring(const char* val);
176  void append(const wreport::Var& var);
177  void append_attributes(const wreport::Var& var);
178 };
179 
180 struct Decoder
181 {
182  const uint8_t* buf;
183  unsigned size;
184 
185  Decoder(const std::vector<uint8_t>& buf);
186  uint16_t decode_uint16();
187  uint32_t decode_uint32();
188  const char* decode_cstring();
189  std::unique_ptr<wreport::Var> decode_var();
190 
194  static void decode_attrs(const std::vector<uint8_t>& buf, wreport::Var& var);
195 };
196 
197 }
198 
202 struct Values : protected std::map<wreport::Varcode, values::Value>
203 {
204  Values() = default;
205  Values(const dballe::Record& rec) { set_from_record(rec); }
206 
207  typedef std::map<wreport::Varcode, values::Value>::const_iterator const_iterator;
208  typedef std::map<wreport::Varcode, values::Value>::iterator iterator;
209  const_iterator begin() const { return std::map<wreport::Varcode, values::Value>::begin(); }
210  const_iterator end() const { return std::map<wreport::Varcode, values::Value>::end(); }
211  iterator begin() { return std::map<wreport::Varcode, values::Value>::begin(); }
212  iterator end() { return std::map<wreport::Varcode, values::Value>::end(); }
213  size_t size() const { return std::map<wreport::Varcode, values::Value>::size(); }
214  bool empty() const { return std::map<wreport::Varcode, values::Value>::empty(); }
215  void clear() { return std::map<wreport::Varcode, values::Value>::clear(); }
216  void erase(wreport::Varcode code) { std::map<wreport::Varcode, values::Value>::erase(code); }
217  bool operator==(const Values& o) const;
218 
219  const values::Value& operator[](wreport::Varcode code) const;
220  const values::Value& operator[](const char* code) const { return operator[](resolve_varcode(code)); }
221  const values::Value& operator[](const std::string& code) const { return operator[](resolve_varcode(code)); }
222  const values::Value* get(wreport::Varcode code) const;
223  const values::Value* get(const char* code) const { return get(resolve_varcode(code)); }
224  const values::Value* get(const std::string& code) const { return get(resolve_varcode(code)); }
225 
227  void set(const wreport::Var&);
228 
230  void set(std::unique_ptr<wreport::Var>&&);
231 
233  void set(const Values& vals);
234 
236  template<typename C, typename T> void set(C code, const T& val) { this->set(newvar(code, val)); }
237 
239  void add_data_id(wreport::Varcode code, int data_id);
240 
242  void set_from_record(const Record& rec);
243 
245  void clear_ids()
246  {
247  for (auto& i : *this)
248  i.second.clear_ids();
249  }
250 
254  std::vector<uint8_t> encode() const;
255 
259  static std::vector<uint8_t> encode_attrs(const wreport::Var& var);
260 
264  static void decode(const std::vector<uint8_t>& buf, std::function<void(std::unique_ptr<wreport::Var>)> dest);
265 
267  void print(FILE* out) const;
268 };
269 
274 {
275  Station info;
276  Values values;
277 
278  StationValues() = default;
279  StationValues(const dballe::Record& rec) : info(rec), values(rec) {}
280 
282  void set_from_record(const Record& rec);
283 
284  bool operator==(const StationValues& o) const
285  {
286  return info == o.info && values == o.values;
287  }
288 
290  void clear_ids()
291  {
292  info.clear_ids();
293  values.clear_ids();
294  }
295 
297  void print(FILE* out) const;
298 };
299 
304 {
305  Sampling info;
306  Values values;
307 
308  DataValues() = default;
309  DataValues(const dballe::Record& rec) : info(rec), values(rec) {}
310 
312  void set_from_record(const Record& rec);
313 
314  bool operator==(const DataValues& o) const
315  {
316  return info == o.info && values == o.values;
317  }
318 
320  void clear_ids()
321  {
322  info.clear_ids();
323  values.clear_ids();
324  }
325 
327  void print(FILE* out) const;
328 };
329 
330 }
331 
332 #endif
Information about a physical variable.
Definition: values.h:64
Ident ident
Mobile station identifier.
Definition: values.h:36
void clear_ids()
Reset all the database IDs.
Definition: values.h:320
Information about a station.
Definition: values.h:20
Definition: values.h:168
A station or measured value.
Definition: values.h:103
Coords coords
Station coordinates.
Definition: values.h:33
Coordinates.
Definition: types.h:337
A set of measured values.
Definition: values.h:303
int ana_id
Database ID of the station.
Definition: values.h:30
int data_id
Database ID of the value.
Definition: values.h:106
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:148
Key/value store where keys are strings and values are wreport variables.
Definition: record.h:16
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
Definition: values.h:180
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
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:109
Trange trange
Time range.
Definition: values.h:73
uint16_t Varcode
void clear_ids()
Reset all the database IDs.
Definition: values.h:245
Level level
Vertical level or layer.
Definition: values.h:70
Value(std::unique_ptr< wreport::Var > &&var)
Construct from a wreport::Var, taking ownership of it.
Definition: values.h:118
void clear_ids()
Reset the database ID.
Definition: values.h:42
std::string report
rep_memo for this station
Definition: values.h:23
void clear_ids()
Reset all the database IDs.
Definition: values.h:290
Date and time.
Definition: types.h:158
A set of station values.
Definition: values.h:273
Datetime datetime
Date and time at which the value was measured or forecast.
Definition: values.h:67
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:115
Common definitions.
Shortcut functions to work with wreport::Var in DB-All.e.
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
Collection of Value objects, indexed by wreport::Varcode.
Definition: values.h:202
wreport::Varcode resolve_varcode(const char *name)
Resolve a variable name to a varcode proper, dealing with aliases and validation. ...