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