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