libdballe  7.6
values.h
1 #ifndef DBALLE_CORE_VALUES_H
2 #define DBALLE_CORE_VALUES_H
3 
4 #include <dballe/core/defs.h>
5 #include <dballe/core/var.h>
6 #include <dballe/record.h>
7 #include <wreport/varinfo.h>
8 #include <map>
9 
10 namespace dballe {
11 
12 struct Station
13 {
14  std::string report;
15  int ana_id = MISSING_INT;
16  Coords coords;
17  Ident ident;
18 
19  Station() = default;
20  Station(const dballe::Record& rec) { set_from_record(rec); }
21  void clear_ids() { ana_id = MISSING_INT; }
22  void set_from_record(const Record& rec);
23  bool operator==(const Station& o) const
24  {
25  return report == o.report && ana_id == o.ana_id && coords == o.coords && ident == o.ident;
26  }
27 
28  void print(FILE* out, const char* end="\n") const;
29 };
30 
31 struct Sampling : public Station
32 {
33  Datetime datetime;
34  Level level;
35  Trange trange;
36 
37  Sampling() = default;
38  Sampling(const dballe::Record& rec) { set_from_record(rec); }
39  void set_from_record(const Record& rec);
40  Sampling& operator=(const Sampling&) = default;
41  Sampling& operator=(const Station& st) {
42  Station::operator=(st);
43  return *this;
44  }
45 
46  bool operator==(const Sampling& o) const
47  {
48  return Station::operator==(o) && datetime == o.datetime && level == o.level && trange == o.trange;
49  }
50 
51  void print(FILE* out, const char* end="\n") const;
52 };
53 
54 namespace values {
55 struct Value
56 {
57  int data_id = MISSING_INT;
58  wreport::Var* var = nullptr;
59 
60  Value(const Value& o) : data_id(o.data_id), var(o.var ? new wreport::Var(*o.var) : nullptr) {}
61  Value(Value&& o) : data_id(o.data_id), var(o.var) { o.var = nullptr; }
62  Value(const wreport::Var& var) : var(new wreport::Var(var)) {}
63  Value(std::unique_ptr<wreport::Var>&& var) : var(var.release()) {}
64  ~Value() { delete var; }
65  Value& operator=(const Value& o)
66  {
67  if (this == &o) return *this;
68  data_id = o.data_id;
69  delete var;
70  var = o.var ? new wreport::Var(*o.var) : nullptr;
71  return *this;
72  }
73  Value& operator=(Value&& o)
74  {
75  if (this == &o) return *this;
76  data_id = o.data_id;
77  delete var;
78  var = o.var;
79  o.var = nullptr;
80  return *this;
81  }
82  bool operator==(const Value& o) const
83  {
84  if (data_id != o.data_id) return false;
85  if (var == o.var) return true;
86  if (!var || !o.var) return false;
87  return *var == *o.var;
88  }
89  void clear_ids() { data_id = MISSING_INT; }
90  void set(const wreport::Var& v)
91  {
92  delete var;
93  var = new wreport::Var(v);
94  }
95  void set(std::unique_ptr<wreport::Var>&& v)
96  {
97  delete var;
98  var = v.release();
99  }
100 
101  void print(FILE* out) const;
102 };
103 }
104 
105 // FIXME: map, or hashmap, or vector enforced to be unique
106 struct Values : protected std::map<wreport::Varcode, values::Value>
107 {
108  Values() = default;
109  Values(const dballe::Record& rec) { set_from_record(rec); }
110 
111  typedef std::map<wreport::Varcode, values::Value>::const_iterator const_iterator;
112  typedef std::map<wreport::Varcode, values::Value>::iterator iterator;
113  const_iterator begin() const { return std::map<wreport::Varcode, values::Value>::begin(); }
114  const_iterator end() const { return std::map<wreport::Varcode, values::Value>::end(); }
115  iterator begin() { return std::map<wreport::Varcode, values::Value>::begin(); }
116  iterator end() { return std::map<wreport::Varcode, values::Value>::end(); }
117  size_t size() const { return std::map<wreport::Varcode, values::Value>::size(); }
118  bool empty() const { return std::map<wreport::Varcode, values::Value>::empty(); }
119  void clear() { return std::map<wreport::Varcode, values::Value>::clear(); }
120  bool operator==(const Values& o) const;
121 
122  const values::Value& operator[](wreport::Varcode code) const;
123  const values::Value& operator[](const char* code) const { return operator[](resolve_varcode(code)); }
124  const values::Value& operator[](const std::string& code) const { return operator[](resolve_varcode(code)); }
125  const values::Value* get(wreport::Varcode code) const;
126  const values::Value* get(const char* code) const { return get(resolve_varcode(code)); }
127  const values::Value* get(const std::string& code) const { return get(resolve_varcode(code)); }
128  void set(const wreport::Var&);
129  void set(std::unique_ptr<wreport::Var>&&);
130  template<typename C, typename T> void set(C code, const T& val) { this->set(newvar(code, val)); }
131  void add_data_id(wreport::Varcode code, int data_id);
132  void set_from_record(const Record& rec);
133  void clear_ids()
134  {
135  for (auto& i : *this)
136  i.second.clear_ids();
137  }
138 
139  void print(FILE* out) const;
140 };
141 
143 {
144  Station info;
145  Values values;
146 
147  StationValues() = default;
148  StationValues(const dballe::Record& rec) : info(rec), values(rec) {}
149  void set_from_record(const Record& rec);
150  bool operator==(const StationValues& o) const
151  {
152  return info == o.info && values == o.values;
153  }
154  void clear_ids()
155  {
156  info.clear_ids();
157  values.clear_ids();
158  }
159 
160  void print(FILE* out) const;
161 };
162 
164 {
165  Sampling info;
166  Values values;
167 
168  DataValues() = default;
169  DataValues(const dballe::Record& rec) : info(rec), values(rec) {}
170  void set_from_record(const Record& rec);
171  bool operator==(const DataValues& o) const
172  {
173  return info == o.info && values == o.values;
174  }
175  void clear_ids()
176  {
177  info.clear_ids();
178  values.clear_ids();
179  }
180 
181  void print(FILE* out) const;
182 };
183 
184 }
185 
186 #endif
Definition: values.h:31
Definition: values.h:12
Definition: values.h:55
Coordinates.
Definition: types.h:320
Definition: values.h:163
Information on how a value has been sampled or computed with regards to time.
Definition: types.h:565
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
Vertical level or layer.
Definition: types.h:515
A station identifier, that can be any string (including the empty string) or a missing value...
Definition: core/defs.h:19
Date and time.
Definition: types.h:147
Definition: values.h:142
Common definitions.
Implement ::dba_var, an encapsulation of a measured variable.
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
Definition: values.h:106
wreport::Varcode resolve_varcode(const char *name)
Resolve a variable name to a varcode proper, dealing with aliases and validation. ...