libdballe  5.18
record.h
Go to the documentation of this file.
1 /*
2  * dballe/record - groups of related variables
3  *
4  * Copyright (C) 2005--2010 ARPA-SIM <urpsim@smr.arpa.emr.it>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Author: Enrico Zini <enrico@enricozini.com>
20  */
21 
22 #ifndef DBA_RECORD_H
23 #define DBA_RECORD_H
24 
30 #include <dballe/core/var.h>
31 #include <dballe/core/matcher.h>
32 #include <vector>
33 
34 namespace dballe {
35 
39 enum _dba_keyword {
40  DBA_KEY_ERROR = -1,
41  DBA_KEY_PRIORITY = 0,
42  DBA_KEY_PRIOMAX = 1,
43  DBA_KEY_PRIOMIN = 2,
44  DBA_KEY_REP_COD = 3,
45  DBA_KEY_REP_MEMO = 4,
46  DBA_KEY_ANA_ID = 5,
47  DBA_KEY_MOBILE = 6,
48  DBA_KEY_IDENT = 7,
49  DBA_KEY_LAT = 8,
50  DBA_KEY_LON = 9,
51  DBA_KEY_LATMAX = 10,
52  DBA_KEY_LATMIN = 11,
53  DBA_KEY_LONMAX = 12,
54  DBA_KEY_LONMIN = 13,
55  DBA_KEY_YEAR = 14,
56  DBA_KEY_MONTH = 15,
57  DBA_KEY_DAY = 16,
58  DBA_KEY_HOUR = 17,
59  DBA_KEY_MIN = 18,
60  DBA_KEY_SEC = 19,
61  DBA_KEY_YEARMAX = 20,
62  DBA_KEY_YEARMIN = 21,
63  DBA_KEY_MONTHMAX = 22,
64  DBA_KEY_MONTHMIN = 23,
65  DBA_KEY_DAYMAX = 24,
66  DBA_KEY_DAYMIN = 25,
67  DBA_KEY_HOURMAX = 26,
68  DBA_KEY_HOURMIN = 27,
69  DBA_KEY_MINUMAX = 28,
70  DBA_KEY_MINUMIN = 29,
71  DBA_KEY_SECMAX = 30,
72  DBA_KEY_SECMIN = 31,
73  DBA_KEY_LEVELTYPE1 = 32,
74  DBA_KEY_L1 = 33,
75  DBA_KEY_LEVELTYPE2 = 34,
76  DBA_KEY_L2 = 35,
77  DBA_KEY_PINDICATOR = 36,
78  DBA_KEY_P1 = 37,
79  DBA_KEY_P2 = 38,
80  DBA_KEY_VAR = 39,
81  DBA_KEY_VARLIST = 40,
82  DBA_KEY_CONTEXT_ID = 41,
83  DBA_KEY_QUERY = 42,
84  DBA_KEY_ANA_FILTER = 43,
85  DBA_KEY_DATA_FILTER = 44,
86  DBA_KEY_ATTR_FILTER = 45,
87  DBA_KEY_LIMIT = 46,
88  DBA_KEY_VAR_RELATED = 47,
89  DBA_KEY_COUNT = 48,
90 };
92 typedef enum _dba_keyword dba_keyword;
93 
100 class Record
101 {
102 protected:
103  /* The storage for the core keyword data */
104  wreport::Var* keydata[DBA_KEY_COUNT];
105 
106  /* The variables */
107  std::vector<wreport::Var*> m_vars;
108 
110  int find_item(wreport::Varcode code) const throw ();
111 
113  wreport::Var& get_item(wreport::Varcode code);
114 
116  const wreport::Var& get_item(wreport::Varcode code) const;
117 
119  void remove_item(wreport::Varcode code);
120 
121 public:
122  Record();
123  Record(const Record& rec);
124  ~Record();
125 
126  Record& operator=(const Record& rec);
127 
128  bool operator==(const Record& rec) const;
129  bool operator!=(const Record& rec) const { return !operator==(rec); }
130 
132  void clear();
133 
135  void clear_vars();
136 
146  void add(const Record& source);
147 
152  bool contains(const Record& subset) const;
153 
165  void set_to_difference(const Record& source1, const Record& source2);
166 
174  const wreport::Var* key_peek(dba_keyword parameter) const throw ();
175 
183  const wreport::Var* var_peek(wreport::Varcode code) const throw ();
184 
191  const wreport::Var* peek(const char* name) const;
192 
194  const wreport::Var* peek(dba_keyword parameter) const throw () { return key_peek(parameter); }
195 
197  const wreport::Var* peek(wreport::Varcode code) const throw () { return var_peek(code); }
198 
207  const char* key_peek_value(dba_keyword parameter) const throw ();
208 
217  const char* var_peek_value(wreport::Varcode code) const throw ();
218 
225  const char* peek_value(const char* name) const;
226 
228  const char* peek_value(dba_keyword parameter) const throw () { return key_peek_value(parameter); }
229 
231  const char* peek_value(wreport::Varcode code) const throw () { return var_peek_value(code); }
232 
236  const wreport::Var& key(dba_keyword parameter) const;
237 
241  const wreport::Var& var(wreport::Varcode code) const;
242 
246  wreport::Var& key(dba_keyword parameter);
247 
251  wreport::Var& var(wreport::Varcode code);
252 
254  // @{
255  const wreport::Var& get(dba_keyword parameter) const { return key(parameter); }
256  const wreport::Var& get(wreport::Varcode code) const { return var(code); }
257  const wreport::Var& get(const char* name) const;
258  wreport::Var& get(dba_keyword parameter) { return key(parameter); }
259  wreport::Var& get(wreport::Varcode code) { return var(code); }
260  wreport::Var& get(const char* name);
261  template<typename K, typename T>
262  T get(K name, T default_value) const
263  {
264  if (const wreport::Var* v = peek(name))
265  return v->enq(default_value);
266  else
267  return default_value;
268  }
269  const wreport::Var& operator[](dba_keyword parameter) const { return key(parameter); }
270  const wreport::Var& operator[](wreport::Varcode code) const { return var(code); }
271  const wreport::Var& operator[](const char* name) const { return get(name); }
272  wreport::Var& operator[](dba_keyword parameter) { return key(parameter); }
273  wreport::Var& operator[](wreport::Varcode code) { return var(code); }
274  wreport::Var& operator[](const char* name) { return get(name); }
275  template<typename P, typename V>
276  void set(const P& field, const V& val) { get(field).set(val); }
277  void set(const wreport::Var& var) { get(var.code()).set(var); }
278  void unset(dba_keyword parameter) { key_unset(parameter); }
279  void unset(wreport::Varcode code) { var_unset(code); }
280  void unset(const char* name);
281  // @}
282 
286  void set_ana_context();
287 
291  const std::vector<wreport::Var*>& vars() const;
292 
299  void key_unset(dba_keyword parameter);
300 
307  void var_unset(wreport::Varcode code);
308 
324  void parse_date_extremes(int* minvalues, int* maxvalues) const;
325 
336  void parse_date(int* values) const;
337 
351  void set_from_string(const char* str);
352 
359  void print(FILE* out) const;
360 
367  static const char* keyword_name(dba_keyword keyword);
368 
375  static wreport::Varinfo keyword_info(dba_keyword keyword);
376 
384  static dba_keyword keyword_byname(const char* tag);
385 
397  static dba_keyword keyword_byname_len(const char* tag, int len);
398 };
399 
400 #if 0
401 
414 void dba_record_diff(dba_record rec1, dba_record rec2, int* diffs, FILE* out);
415 #endif
416 
417 struct MatchedRecord : public Matched
418 {
419  const Record& r;
420 
421  MatchedRecord(const Record& r);
422  ~MatchedRecord();
423 
424  virtual matcher::Result match_var_id(int val) const;
425  virtual matcher::Result match_station_id(int val) const;
426  virtual matcher::Result match_station_wmo(int block, int station=-1) const;
427  virtual matcher::Result match_date(const int* min, const int* max) const;
428  virtual matcher::Result match_coords(int latmin, int latmax, int lonmin, int lonmax) const;
429  virtual matcher::Result match_rep_memo(const char* memo) const;
430 };
431 
432 }
433 
434 /* vim:set ts=4 sw=4: */
435 #endif