libdballe 9.13
msg/cursor.h
1#ifndef DBALLE_MSG_CURSOR_H
2#define DBALLE_MSG_CURSOR_H
3
4#include <dballe/core/cursor.h>
6#include <dballe/msg/msg.h>
7#include <dballe/types.h>
8
9namespace dballe {
10namespace impl {
11namespace msg {
12
13struct CursorStation : public impl::CursorStation
14{
15 std::shared_ptr<const impl::Message> msg;
16 dballe::DBStation station;
17 const Values& station_values;
18 bool at_start = true;
19
20 CursorStation(std::shared_ptr<const impl::Message> msg)
21 : msg(msg), station_values(msg->find_station_context())
22 {
23 station.report = msg->get_report();
24 station.coords = msg->get_coords();
25 station.ident = msg->get_ident();
26 }
27 ~CursorStation();
28
29 bool has_value() const override { return !at_start; }
30
31 int remaining() const override
32 {
33 if (at_start)
34 return 1;
35 return 0;
36 }
37
38 bool next() override
39 {
40 if (at_start)
41 {
42 at_start = false;
43 return true;
44 }
45 else
46 return false;
47 }
48
49 void discard() override { at_start = false; }
50
51 void enq(Enq& enq) const override;
52
53 DBStation get_station() const override { return station; }
54
55 DBValues get_values() const override { return DBValues(station_values); }
56
58 inline static std::shared_ptr<CursorStation>
59 downcast(std::shared_ptr<dballe::CursorStation> c)
60 {
61 auto res = std::dynamic_pointer_cast<CursorStation>(c);
62 if (!res)
63 throw std::runtime_error(
64 "Attempted to downcast the wrong kind of cursor");
65 return res;
66 }
67};
68
69struct CursorStationData : public impl::CursorStationData
70{
71 std::shared_ptr<const impl::Message> msg;
72 dballe::DBStation station;
73 const Values& station_values;
74 bool at_start = true;
75 Values::const_iterator cur;
76
77 CursorStationData(std::shared_ptr<const impl::Message> msg)
78 : msg(msg), station_values(msg->find_station_context())
79 {
80 station.report = msg->get_report();
81 station.coords = msg->get_coords();
82 station.ident = msg->get_ident();
83 }
84 ~CursorStationData();
85
86 bool has_value() const override
87 {
88 return !at_start && cur != station_values.end();
89 }
90
91 int remaining() const override
92 {
93 if (at_start)
94 return station_values.size();
95 return station_values.end() - cur;
96 }
97
98 bool next() override
99 {
100 if (at_start)
101 {
102 at_start = false;
103 cur = station_values.begin();
104 return cur != station_values.end();
105 }
106 else if (cur == station_values.end())
107 return false;
108 else
109 {
110 ++cur;
111 return cur != station_values.end();
112 }
113 }
114
115 void discard() override
116 {
117 at_start = false;
118 cur = station_values.end();
119 }
120
121 void enq(Enq& enq) const override;
122
123 DBStation get_station() const override { return station; }
124
125 wreport::Varcode get_varcode() const override { return (*cur)->code(); }
126 wreport::Var get_var() const override { return **cur; }
127
129 inline static std::shared_ptr<CursorStationData>
130 downcast(std::shared_ptr<dballe::CursorStationData> c)
131 {
132 auto res = std::dynamic_pointer_cast<CursorStationData>(c);
133 if (!res)
134 throw std::runtime_error(
135 "Attempted to downcast the wrong kind of cursor");
136 return res;
137 }
138};
139
140struct CursorDataRow
141{
142 Level level;
143 Trange trange;
144 Values::const_iterator var;
145
146 CursorDataRow(Values::const_iterator var) : var(var) {}
147
148 CursorDataRow(const Level& level, const Trange& trange,
149 Values::const_iterator var)
150 : level(level), trange(trange), var(var)
151 {
152 }
153};
154
155struct CursorData : public impl::CursorData
156{
157 dballe::DBStation station;
158 Datetime datetime;
159 std::vector<CursorDataRow> rows;
160 std::vector<CursorDataRow>::const_iterator cur;
161 bool at_start = true;
162
163 CursorData(const impl::Message& msg, bool merged = false)
164 {
165 station.report = msg.get_report();
166 station.coords = msg.get_coords();
167 station.ident = msg.get_ident();
168 datetime = msg.get_datetime();
169
170 for (const auto& ctx : msg.data)
171 for (Values::const_iterator cur = ctx.values.begin();
172 cur != ctx.values.end(); ++cur)
173 rows.emplace_back(ctx.level, ctx.trange, cur);
174
175 if (merged)
176 for (Values::const_iterator cur = msg.station_data.begin();
177 cur != msg.station_data.end(); ++cur)
178 if (WR_VAR_X((*cur)->code()) < 4 ||
179 WR_VAR_X((*cur)->code()) > 6)
180 rows.emplace_back(cur);
181 }
182 ~CursorData();
183
184 bool has_value() const override { return !at_start && cur != rows.end(); }
185
186 int remaining() const override
187 {
188 if (at_start)
189 return rows.size();
190 return rows.end() - cur;
191 }
192
193 bool next() override
194 {
195 if (at_start)
196 {
197 at_start = false;
198 cur = rows.begin();
199 return cur != rows.end();
200 }
201 else if (cur == rows.end())
202 {
203 return false;
204 }
205 else
206 {
207 ++cur;
208 return cur != rows.end();
209 }
210 }
211
212 void discard() override
213 {
214 at_start = false;
215 cur = rows.end();
216 }
217
218 void enq(Enq& enq) const override;
219
220 DBStation get_station() const override { return station; }
221
222 wreport::Varcode get_varcode() const override
223 {
224 return (*(cur->var))->code();
225 }
226 wreport::Var get_var() const override { return **(cur->var); }
227 Level get_level() const override { return cur->level; }
228 Trange get_trange() const override { return cur->trange; }
229 Datetime get_datetime() const override { return datetime; }
230
232 inline static std::shared_ptr<CursorData>
233 downcast(std::shared_ptr<dballe::CursorData> c)
234 {
235 auto res = std::dynamic_pointer_cast<CursorData>(c);
236 if (!res)
237 throw std::runtime_error(
238 "Attempted to downcast the wrong kind of cursor");
239 return res;
240 }
241};
242
243} // namespace msg
244} // namespace impl
245} // namespace dballe
246
247#endif
Storage for related physical data.
Definition msg.h:134
Sorted storage for all the dba_msg_datum present on one level.
Definition types.h:940
Collection of DBValue objects, indexed by wreport::Varcode.
Definition values.h:230
Date and time.
Definition types.h:164
Vertical level or layer.
Definition types.h:625
Information on how a value has been sampled or computed with regards to time.
Definition types.h:689
Collection of Value objects, indexed by wreport::Varcode.
Definition values.h:216
Cursor iterating over data values.
Definition core/cursor.h:53
Cursor iterating over station data values.
Definition core/cursor.h:33
Cursor iterating over stations.
Definition core/cursor.h:13
Class passed to key-value accessors to set values in an invoker-defined way.
Definition core/enq.h:18
bool next() override
Get a new item from the results of a query.
Definition msg/cursor.h:193
Level get_level() const override
Get the level.
Definition msg/cursor.h:227
void discard() override
Discard the results that have not been read yet.
Definition msg/cursor.h:212
DBStation get_station() const override
Get the whole station data in a single call.
Definition msg/cursor.h:220
Trange get_trange() const override
Get the time range.
Definition msg/cursor.h:228
wreport::Varcode get_varcode() const override
Get the variable code.
Definition msg/cursor.h:222
bool has_value() const override
Check if the cursor points to a valid value.
Definition msg/cursor.h:184
Datetime get_datetime() const override
Get the datetime.
Definition msg/cursor.h:229
wreport::Var get_var() const override
Get the variable.
Definition msg/cursor.h:226
int remaining() const override
Get the number of rows still to be fetched.
Definition msg/cursor.h:186
static std::shared_ptr< CursorData > downcast(std::shared_ptr< dballe::CursorData > c)
Downcast a unique_ptr pointer.
Definition msg/cursor.h:233
void discard() override
Discard the results that have not been read yet.
Definition msg/cursor.h:115
int remaining() const override
Get the number of rows still to be fetched.
Definition msg/cursor.h:91
bool has_value() const override
Check if the cursor points to a valid value.
Definition msg/cursor.h:86
wreport::Varcode get_varcode() const override
Get the variable code.
Definition msg/cursor.h:125
static std::shared_ptr< CursorStationData > downcast(std::shared_ptr< dballe::CursorStationData > c)
Downcast a unique_ptr pointer.
Definition msg/cursor.h:130
DBStation get_station() const override
Get the whole station data in a single call.
Definition msg/cursor.h:123
wreport::Var get_var() const override
Get the variable.
Definition msg/cursor.h:126
bool next() override
Get a new item from the results of a query.
Definition msg/cursor.h:98
DBValues get_values() const override
Get the station data values.
Definition msg/cursor.h:55
static std::shared_ptr< CursorStation > downcast(std::shared_ptr< dballe::CursorStation > c)
Downcast a unique_ptr pointer.
Definition msg/cursor.h:59
bool has_value() const override
Check if the cursor points to a valid value.
Definition msg/cursor.h:29
int remaining() const override
Get the number of rows still to be fetched.
Definition msg/cursor.h:31
bool next() override
Get a new item from the results of a query.
Definition msg/cursor.h:38
void discard() override
Discard the results that have not been read yet.
Definition msg/cursor.h:49
DBStation get_station() const override
Get the whole station data in a single call.
Definition msg/cursor.h:53
Common base types used by most of DB-All.e code.
#define WR_VAR_X(code)