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