libdballe  6.8
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/defs.h>
31 #include <dballe/core/var.h>
32 #include <dballe/core/matcher.h>
33 #include <vector>
34 
35 namespace dballe {
36 
40 enum _dba_keyword {
41  DBA_KEY_ERROR = -1,
42  DBA_KEY_PRIORITY = 0,
43  DBA_KEY_PRIOMAX = 1,
44  DBA_KEY_PRIOMIN = 2,
45  DBA_KEY_REP_MEMO = 3,
46  DBA_KEY_ANA_ID = 4,
47  DBA_KEY_MOBILE = 5,
48  DBA_KEY_IDENT = 6,
49  DBA_KEY_LAT = 7,
50  DBA_KEY_LON = 8,
51  DBA_KEY_LATMAX = 9,
52  DBA_KEY_LATMIN = 10,
53  DBA_KEY_LONMAX = 11,
54  DBA_KEY_LONMIN = 12,
55  DBA_KEY_YEAR = 13,
56  DBA_KEY_MONTH = 14,
57  DBA_KEY_DAY = 15,
58  DBA_KEY_HOUR = 16,
59  DBA_KEY_MIN = 17,
60  DBA_KEY_SEC = 18,
61  DBA_KEY_YEARMAX = 19,
62  DBA_KEY_YEARMIN = 20,
63  DBA_KEY_MONTHMAX = 21,
64  DBA_KEY_MONTHMIN = 22,
65  DBA_KEY_DAYMAX = 23,
66  DBA_KEY_DAYMIN = 24,
67  DBA_KEY_HOURMAX = 25,
68  DBA_KEY_HOURMIN = 26,
69  DBA_KEY_MINUMAX = 27,
70  DBA_KEY_MINUMIN = 28,
71  DBA_KEY_SECMAX = 29,
72  DBA_KEY_SECMIN = 30,
73  DBA_KEY_LEVELTYPE1 = 31,
74  DBA_KEY_L1 = 32,
75  DBA_KEY_LEVELTYPE2 = 33,
76  DBA_KEY_L2 = 34,
77  DBA_KEY_PINDICATOR = 35,
78  DBA_KEY_P1 = 36,
79  DBA_KEY_P2 = 37,
80  DBA_KEY_VAR = 38,
81  DBA_KEY_VARLIST = 39,
82  DBA_KEY_CONTEXT_ID = 40,
83  DBA_KEY_QUERY = 41,
84  DBA_KEY_ANA_FILTER = 42,
85  DBA_KEY_DATA_FILTER = 43,
86  DBA_KEY_ATTR_FILTER = 44,
87  DBA_KEY_LIMIT = 45,
88  DBA_KEY_VAR_RELATED = 46,
89  DBA_KEY_COUNT = 47,
90 };
92 typedef enum _dba_keyword dba_keyword;
93 
94 std::ostream& operator<<(std::ostream& o, dba_keyword k);
95 
102 class Record
103 {
104 protected:
105  /* The storage for the core keyword data */
106  wreport::Var* keydata[DBA_KEY_COUNT];
107 
108  // The variables, sorted by varcode
109  std::vector<wreport::Var*> m_vars;
110 
112  int find_item(wreport::Varcode code) const throw ();
113 
115  wreport::Var& get_item(wreport::Varcode code);
116 
118  const wreport::Var& get_item(wreport::Varcode code) const;
119 
121  void remove_item(wreport::Varcode code);
122 
123 public:
124  Record();
125  Record(const Record& rec);
126  ~Record();
127 
128  Record& operator=(const Record& rec);
129 
130  bool operator==(const Record& rec) const;
131  bool operator!=(const Record& rec) const { return !operator==(rec); }
132 
134  void clear();
135 
137  void clear_vars();
138 
148  void add(const Record& source);
149 
154  bool contains(const Record& subset) const;
155 
159  bool contains(dba_keyword parameter) const throw();
160 
164  bool contains(wreport::Varcode parameter) const throw();
165 
167  bool contains_level() const throw();
168 
170  bool contains_trange() const throw();
171 
173  bool contains_datetime() const throw();
174 
186  void set_to_difference(const Record& source1, const Record& source2);
187 
195  const wreport::Var* key_peek(dba_keyword parameter) const throw ();
196 
204  const wreport::Var* var_peek(wreport::Varcode code) const throw ();
205 
212  const wreport::Var* peek(const char* name) const;
213 
215  const wreport::Var* peek(dba_keyword parameter) const throw () { return key_peek(parameter); }
216 
218  const wreport::Var* peek(wreport::Varcode code) const throw () { return var_peek(code); }
219 
228  const char* key_peek_value(dba_keyword parameter) const throw ();
229 
238  const char* var_peek_value(wreport::Varcode code) const throw ();
239 
246  const char* peek_value(const char* name) const;
247 
249  const char* peek_value(dba_keyword parameter) const throw () { return key_peek_value(parameter); }
250 
252  const char* peek_value(wreport::Varcode code) const throw () { return var_peek_value(code); }
253 
257  const wreport::Var& key(dba_keyword parameter) const;
258 
262  const wreport::Var& var(wreport::Varcode code) const;
263 
267  wreport::Var& key(dba_keyword parameter);
268 
272  wreport::Var& var(wreport::Varcode code);
273 
275  // @{
276  const wreport::Var& get(dba_keyword parameter) const { return key(parameter); }
277  const wreport::Var& get(wreport::Varcode code) const { return var(code); }
278  const wreport::Var& get(const char* name) const;
279  wreport::Var& get(dba_keyword parameter) { return key(parameter); }
280  wreport::Var& get(wreport::Varcode code) { return var(code); }
281  wreport::Var& get(const char* name);
282  template<typename K, typename T>
283  T get(K name, T default_value) const
284  {
285  if (const wreport::Var* v = peek(name))
286  return v->enq(default_value);
287  else
288  return default_value;
289  }
290  const wreport::Var& operator[](dba_keyword parameter) const { return key(parameter); }
291  const wreport::Var& operator[](wreport::Varcode code) const { return var(code); }
292  const wreport::Var& operator[](const char* name) const { return get(name); }
293  wreport::Var& operator[](dba_keyword parameter) { return key(parameter); }
294  wreport::Var& operator[](wreport::Varcode code) { return var(code); }
295  wreport::Var& operator[](const char* name) { return get(name); }
296  template<typename P, typename V>
297  void set(const P& field, const V& val) { get(field).set(val); }
298  void set(const wreport::Var& var) { get(var.code()).copy_val(var); }
299  void set(const Level& lev);
300  void set(const Trange& tr);
301  void unset(dba_keyword parameter) { key_unset(parameter); }
302  void unset(wreport::Varcode code) { var_unset(code); }
303  void unset(const char* name);
304  template<typename K>
305  void copy(const Record& rec, K parameter)
306  {
307  const wreport::Var* val = rec.peek(parameter);
308  if (!val)
309  unset(parameter);
310  else
311  set(parameter, *val);
312  }
313  template<typename K>
314  bool contains(const Record& rec, K parameter)
315  {
316  const wreport::Var* mine = peek(parameter);
317  const wreport::Var* theirs = rec.peek(parameter);
318  if (!mine and !theirs) return true;
319  if (!mine or !theirs) return false;
320  return *mine == *theirs;
321  }
322  // @}
323 
324  Level get_level() const;
325  Trange get_trange() const;
326  Datetime get_datetime() const;
327  void set(const Datetime& dt);
328  void setmin(const Datetime& dt);
329  void setmax(const Datetime& dt);
330  void unset_datetime();
331  void unset_datetimemin();
332  void unset_datetimemax();
333  void get_datetime(int (&val)[6]) const;
334  void get_datetimemin(int (&val)[6]) const;
335  void get_datetimemax(int (&val)[6]) const;
336  void set_datetime(const int (&val)[6]);
337  void set_datetimemin(const int (&val)[6]);
338  void set_datetimemax(const int (&val)[6]);
339  void set_datetime(int ye, int mo=1, int da=1, int ho=0, int mi=0, int se=0);
340  void set(const Coord& c);
341 
345  void set_ana_context();
346 
348  bool is_ana_context() const;
349 
351  const std::vector<wreport::Var*>& vars() const;
352 
359  void key_unset(dba_keyword parameter);
360 
367  void var_unset(wreport::Varcode code);
368 
384  void parse_date_extremes(int* minvalues, int* maxvalues) const;
385 
396  void parse_date(int* values) const;
397 
414  void set_from_string(const char* str);
415 
417  void set_from_string(const char* key, const char* val);
418 
422  std::string to_string() const;
423 
430  void print(FILE* out) const;
431 
438  static const char* keyword_name(dba_keyword keyword);
439 
446  static wreport::Varinfo keyword_info(dba_keyword keyword);
447 
455  static dba_keyword keyword_byname(const char* tag);
456 
468  static dba_keyword keyword_byname_len(const char* tag, int len);
469 };
470 
471 #if 0
472 
485 void dba_record_diff(dba_record rec1, dba_record rec2, int* diffs, FILE* out);
486 #endif
487 
488 struct MatchedRecord : public Matched
489 {
490  const Record& r;
491 
492  MatchedRecord(const Record& r);
493  ~MatchedRecord();
494 
495  virtual matcher::Result match_var_id(int val) const;
496  virtual matcher::Result match_station_id(int val) const;
497  virtual matcher::Result match_station_wmo(int block, int station=-1) const;
498  virtual matcher::Result match_date(const int* min, const int* max) const;
499  virtual matcher::Result match_coords(int latmin, int latmax, int lonmin, int lonmax) const;
500  virtual matcher::Result match_rep_memo(const char* memo) const;
501 };
502 
503 }
504 
505 /* vim:set ts=4 sw=4: */
506 #endif
wreport::Var & operator[](wreport::Varcode code)
Shortcuts.
Definition: record.h:294
const char * var_peek_value(wreport::Varcode code) const
Look at the raw value of a variable in the record, without raising errors.
const wreport::Var * peek(dba_keyword parameter) const
Shortcut for key_peek.
Definition: record.h:215
const wreport::Var * key_peek(dba_keyword parameter) const
Look at the value of a parameter.
bool contains(const Record &rec, K parameter)
Shortcuts.
Definition: record.h:314
const wreport::Var & operator[](dba_keyword parameter) const
Shortcuts.
Definition: record.h:290
Common interface for things that are matched.
Definition: matcher.h:48
Implement ::dba_var, an encapsulation of a measured variable.
void set_ana_context()
Set the date, level and timerange values to match the anagraphical context.
bool contains_level() const
Return true if some level attribute is set in this record.
const char * key_peek_value(dba_keyword parameter) const
Look at the raw value of a keyword in the record, without raising errors.
Definition: defs.h:113
void parse_date(int *values) const
Parse the date set in the Record.
Common definitions.
const wreport::Var & operator[](const char *name) const
Shortcuts.
Definition: record.h:292
virtual matcher::Result match_station_id(int val) const
Match station ID.
void unset(wreport::Varcode code)
Shortcuts.
Definition: record.h:302
DB-All.E record.
Definition: record.h:102
const char * peek_value(dba_keyword parameter) const
Shortcut for key_peek_value.
Definition: record.h:249
Definition: cmdline.h:34
bool contains_trange() const
Return true if some time range attribute is set in this record.
bool contains_datetime() const
Return true if some datetime information is set in this record.
wreport::Var & operator[](const char *name)
Shortcuts.
Definition: record.h:295
wreport::Var & operator[](dba_keyword parameter)
Shortcuts.
Definition: record.h:293
virtual matcher::Result match_date(const int *min, const int *max) const
Match date.
void set(const wreport::Var &var)
Shortcuts.
Definition: record.h:298
void var_unset(wreport::Varcode code)
Remove a parameter from the record.
int find_item(wreport::Varcode code) const
Find an item by wreport::Varcode, returning -1 if not found.
const wreport::Var & key(dba_keyword parameter) const
Return the Var for a key, throwing an error it if it missing.
Definition: defs.h:54
const std::vector< wreport::Var * > & vars() const
Return the varcode-sorted vector with the variables.
Definition: record.h:488
wreport::Var & get_item(wreport::Varcode code)
Find an item by wreport::Varcode, raising an exception if not found.
void parse_date_extremes(int *minvalues, int *maxvalues) const
Parse the date extremes set in Record.
const wreport::Var * var_peek(wreport::Varcode code) const
Look at the value of a variable.
void print(FILE *out) const
Print the contents of this record to the given file descriptor.
const wreport::Var & operator[](wreport::Varcode code) const
Shortcuts.
Definition: record.h:291
static dba_keyword keyword_byname_len(const char *tag, int len)
Get the dba_keyword corresponding to the given name.
const char * peek_value(const char *name) const
Get the string value for an item.
static const char * keyword_name(dba_keyword keyword)
Return the name of a dba_keyword.
void clear()
Remove all data from the record.
bool contains(const Record &subset) const
Return true if all elements of subset are present in this record, with the same value.
Coordinates.
Definition: defs.h:167
void unset(dba_keyword parameter)
Shortcuts.
Definition: record.h:301
virtual matcher::Result match_station_wmo(int block, int station=-1) const
Match station WMO code.
static dba_keyword keyword_byname(const char *tag)
Get the dba_keyword corresponding to the given name.
void copy(const Record &rec, K parameter)
Shortcuts.
Definition: record.h:305
Simple datetime structure.
Definition: defs.h:276
static wreport::Varinfo keyword_info(dba_keyword keyword)
Return informations about a keyword.
const wreport::Var * peek(const char *name) const
Get the variable for an item.
virtual matcher::Result match_rep_memo(const char *memo) const
Match rep_memo.
void set_from_string(const char *str)
Set a value in the record according to an assignment encoded in a string.
bool is_ana_context() const
Check if this record is set to the ana context.
void remove_item(wreport::Varcode code)
Remove an item by wreport::Varcode.
void set_to_difference(const Record &source1, const Record &source2)
Set the record to contain only those fields that change source1 into source2.
void clear_vars()
Remove all variables from the record, leaving the keywords intact.
const char * peek_value(wreport::Varcode code) const
Shortcut for var_peek_value.
Definition: record.h:252
void set(const P &field, const V &val)
Shortcuts.
Definition: record.h:297
void add(const Record &source)
Copy all data from the record source into dest.
void key_unset(dba_keyword parameter)
Remove a parameter from the record.
virtual matcher::Result match_coords(int latmin, int latmax, int lonmin, int lonmax) const
Match coordinates, with bounds in 1/100000 of degree.
virtual matcher::Result match_var_id(int val) const
Match variable ID.
const wreport::Var & var(wreport::Varcode code) const
Return the Var for a variable, throwing an error it if it missing.
const wreport::Var * peek(wreport::Varcode code) const
Shortcut for var_peek.
Definition: record.h:218
std::string to_string() const
Encode in a one-liner of comma-separated assignments.