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