libdballe  9.11
core/enq.h
1 #ifndef DBALLE_CORE_ENQ_H
2 #define DBALLE_CORE_ENQ_H
3 
4 #include <dballe/fwd.h>
5 #include <dballe/types.h>
6 #include <dballe/values.h>
7 #include <dballe/core/var.h>
8 #include <wreport/error.h>
9 #include <string>
10 
11 namespace dballe {
12 namespace impl {
13 
17 struct Enq
18 {
19  const char* key;
20  unsigned len;
21  bool missing = true;
22 
23  Enq(const char* key, unsigned len)
24  : key(key), len(len)
25  {
26  }
27  virtual ~Enq() {}
28 
29  [[noreturn]] void throw_notfound()
30  {
31  wreport::error_notfound::throwf("key %s not found on this query result", key);
32  }
33 
34  // Return the name of this access operation
35  virtual const char* name() const = 0;
36 
37  // Set a boolean value
38  virtual void set_bool(bool val) = 0;
39 
40  // Set an always defined int value
41  virtual void set_int(int val) = 0;
42 
43  // Set an int value that is undefined if it is MISSING_INT
44  virtual void set_dballe_int(int val) = 0;
45 
46  // Set a string
47  virtual void set_string(const std::string& val)
48  {
49  wreport::error_consistency::throwf("cannot %s `%s`", name(), key);
50  }
51 
52  // Set station identifier
53  virtual void set_ident(const Ident& ident)
54  {
55  wreport::error_consistency::throwf("cannot %s `%s`", name(), key);
56  }
57 
58  // Set variable code
59  virtual void set_varcode(wreport::Varcode val)
60  {
61  wreport::error_consistency::throwf("cannot %s `%s`", name(), key);
62  }
63 
64  // Set variable value
65  virtual void set_var(const wreport::Var* val)
66  {
67  wreport::error_consistency::throwf("cannot %s `%s`", name(), key);
68  }
69 
76  virtual void set_var_value(const wreport::Var& var) = 0;
77 
78  // Set variable attributes
79  virtual void set_attrs(const wreport::Var* val)
80  {
81  wreport::error_consistency::throwf("cannot %s `%s`", name(), key);
82  }
83 
84  // Set latitude
85  virtual void set_lat(int lat) { wreport::error_consistency::throwf("cannot %s `%s`", name(), key); }
86 
87  // Set longitude
88  virtual void set_lon(int lon) { wreport::error_consistency::throwf("cannot %s `%s`", name(), key); }
89 
90  // Set coordinates
91  virtual void set_coords(const Coords& c) { wreport::error_consistency::throwf("cannot %s `%s`", name(), key); }
92 
93  // Set station
94  virtual void set_station(const Station& s) { wreport::error_consistency::throwf("cannot %s `%s`", name(), key); }
95 
96  // Set station with DB info
97  virtual void set_dbstation(const DBStation& s) { wreport::error_consistency::throwf("cannot %s `%s`", name(), key); }
98 
99  // Set datetime
100  virtual void set_datetime(const Datetime& dt) { wreport::error_consistency::throwf("cannot %s `%s`", name(), key); }
101 
102  // Set level
103  virtual void set_level(const Level& dt) { wreport::error_consistency::throwf("cannot %s `%s`", name(), key); }
104 
105  // Set timerange
106  virtual void set_trange(const Trange& dt) { wreport::error_consistency::throwf("cannot %s `%s`", name(), key); }
107 
108  template<typename Values>
109  bool search_b_values(const Values& values)
110  {
111  if (key[0] != 'B' || len != 6)
112  return false;
113 
114  wreport::Varcode code = WR_STRING_TO_VAR(key + 1);
115  const wreport::Var* var = values.maybe_var(code);
116  if (var && var->isset())
117  set_var_value(*var);
118  return true;
119  }
120 
121  bool search_b_value(const dballe::Value& value)
122  {
123  if (key[0] != 'B' || len != 6)
124  return false;
125 
126  wreport::Varcode code = WR_STRING_TO_VAR(key + 1);
127  if (code != value.code())
128  throw_notfound();
129 
130  const wreport::Var* var = value.get();
131  if (var && var->isset())
132  set_var_value(*var);
133  return true;
134  }
135 
136  template<typename Values>
137  void search_alias_values(const Values& values)
138  {
140  const wreport::Var* var = values.maybe_var(code);
141  if (var && var->isset())
142  set_var_value(*var);
143  }
144 
145  void search_alias_value(const dballe::Value& value)
146  {
148  if (code != value.code())
149  throw_notfound();
150  const wreport::Var* var = value.get();
151  if (var && var->isset())
152  set_var_value(*var);
153  }
154 };
155 
156 struct Enqi : public Enq
157 {
158  using Enq::Enq;
159  int res;
160 
161  const char* name() const override { return "enqi"; }
162 
163  void set_bool(bool val) override
164  {
165  res = val ? 1 : 0;
166  missing = false;
167  }
168 
169  void set_int(int val) override
170  {
171  res = val;
172  missing = false;
173  }
174 
175  void set_dballe_int(int val) override
176  {
177  if (val == MISSING_INT)
178  return;
179  res = val;
180  missing = false;
181  }
182 
183  void set_lat(int lat) override
184  {
185  if (lat == MISSING_INT)
186  return;
187  res = lat;
188  missing = false;
189  }
190 
191  void set_lon(int lon) override
192  {
193  if (lon == MISSING_INT)
194  return;
195  res = lon;
196  missing = false;
197  }
198 
199  void set_var_value(const wreport::Var& var) override
200  {
201  missing = false;
202  res = var.enqi();
203  }
204 };
205 
206 struct Enqd : public Enq
207 {
208  using Enq::Enq;
209  double res;
210 
211  const char* name() const override { return "enqd"; }
212 
213  void set_bool(bool val) override
214  {
215  res = val ? 1 : 0;
216  missing = false;
217  }
218 
219  void set_int(int val) override
220  {
221  res = val;
222  missing = false;
223  }
224 
225  void set_dballe_int(int val) override
226  {
227  if (val == MISSING_INT)
228  return;
229  res = val;
230  missing = false;
231  }
232 
233  void set_lat(int lat) override
234  {
235  if (lat == MISSING_INT)
236  return;
237  res = Coords::lat_from_int(lat);
238  missing = false;
239  }
240 
241  void set_lon(int lon) override
242  {
243  if (lon == MISSING_INT)
244  return;
245  res = Coords::lon_from_int(lon);
246  missing = false;
247  }
248 
249  void set_var_value(const wreport::Var& var) override
250  {
251  missing = false;
252  res = var.enqd();
253  }
254 };
255 
256 }
257 }
258 
259 #endif
wreport::Varcode code() const
Return the varcode of the variable, or 0 if no variable has been set.
Common base types used by most of DB-All.e code.
Class passed to key-value accessors to set values in an invoker-defined way.
Definition: core/enq.h:17
void set_var_value(const wreport::Var &var) override
Set the value using the value of the given variable.
Definition: core/enq.h:249
Station information.
Definition: types.h:793
bool isset() const
wreport::Varcode resolve_varcode(const std::string &name)
Resolve a variable name to a varcode proper, dealing with aliases and validation. ...
const wreport::Var * maybe_var(wreport::Varcode code) const
Lookup a variable, returning nullptr if not found.
Coordinates.
Definition: types.h:368
Information on how a value has been sampled or computed with regards to time.
Definition: types.h:686
Definition: cmdline.h:18
Vertical level or layer.
Definition: types.h:624
A station identifier, that can be any string (including the empty string) or a missing value...
Definition: types.h:747
static double lon_from_int(int lon)
Convert a longitude from the internal integer representation.
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
uint16_t Varcode
virtual void set_var_value(const wreport::Var &var)=0
Set the value using the value of the given variable.
#define WR_STRING_TO_VAR(str)
Definition: core/enq.h:156
void set_var_value(const wreport::Var &var) override
Set the value using the value of the given variable.
Definition: core/enq.h:199
Date and time.
Definition: types.h:164
Container for a wreport::Var pointer.
Definition: value.h:18
int enqi() const
Definition: types.h:850
static double lat_from_int(int lat)
Convert a latitude from the internal integer representation.
double enqd() const
Structures used as input to database insert functions.
Shortcut functions to work with wreport::Var in DB-All.e.
Collection of Value objects, indexed by wreport::Varcode.
Definition: values.h:176
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Definition: core/enq.h:206