libdballe  7.7
record.h
1 #ifndef DBALLE_RECORD_H
2 #define DBALLE_RECORD_H
3 
4 #include <wreport/var.h>
5 #include <dballe/types.h>
6 #include <memory>
7 
8 namespace dballe {
9 
16 struct Record
17 {
18  virtual ~Record() {}
19 
21  virtual std::unique_ptr<Record> clone() const = 0;
22 
24  static std::unique_ptr<Record> create();
25 
27  virtual void clear() = 0;
28 
30  virtual void clear_vars() = 0;
31 
40  virtual void seti(const char* key, int val) = 0;
41 
45  virtual void setd(const char* key, double val) = 0;
46 
55  virtual void setc(const char* key, const char* val) = 0;
56 
65  virtual void sets(const char* key, const std::string& val) = 0;
66 
77  virtual void setf(const char* key, const char* val) = 0;
78 
80  virtual void set_datetime(const Datetime& dt) = 0;
82  virtual void set_coords(const Coords& c) = 0;
84  virtual void set_latrange(const LatRange& lr) = 0;
86  virtual void set_lonrange(const LonRange& lr) = 0;
88  virtual void set_datetimerange(const DatetimeRange& lr) = 0;
90  virtual void set_level(const Level& lev) = 0;
92  virtual void set_trange(const Trange& tr) = 0;
94  virtual void set_var(const wreport::Var& var) = 0;
96  virtual void set_var_acquire(std::unique_ptr<wreport::Var>&& var) = 0;
97 
98  void set(const char* key, int val) { seti(key, val); }
99  void set(const char* key, double val) { setd(key, val); }
100  void set(const char* key, const char* val) { setc(key, val); }
101  void set(const char* key, const std::string& val) { sets(key, val); }
102  void set(const Datetime& dt) { set_datetime(dt); }
103  void set(const DatetimeRange& dt) { set_datetimerange(dt); }
104  void set(const Coords& c) { set_coords(c); }
105  void set(const LatRange& lr) { set_latrange(lr); }
106  void set(const LonRange& lr) { set_lonrange(lr); }
107  void set(const Level& lev) { set_level(lev); }
108  void set(const Trange& tr) { set_trange(tr); }
109  void set(const wreport::Var& var) { set_var(var); }
110  void set(std::unique_ptr<wreport::Var>&& var) { set_var_acquire(std::move(var)); }
111 
113  virtual void unset(const char* key) = 0;
114 
116  virtual const wreport::Var* get(const char* key) const = 0;
117 
119  virtual bool isset(const char* key) const;
120 
122  virtual bool equals(const Record& rec) const = 0;
123 
125  bool operator==(const Record& rec) const;
126 
128  bool operator!=(const Record& rec) const;
129 
131  const wreport::Var& operator[](const char* key) const;
132 
133  const char* enq(const char* key, const char* def) const
134  {
135  if (const wreport::Var* var = get(key))
136  return var->enq(def);
137  return def;
138  }
139 
140  template<typename T>
141  T enq(const char* key, const T& def) const
142  {
143  if (const wreport::Var* var = get(key))
144  return var->enq(def);
145  return def;
146  }
147 
157  virtual void add(const Record& source) = 0;
158 
163  virtual bool contains(const Record& subset) const = 0;
164 
169  void foreach_key(std::function<void(const char*, const wreport::Var&)> dest) const { foreach_key_ref(dest); }
170 
175  void foreach_key(std::function<void(const char*, std::unique_ptr<wreport::Var>&&)> dest) const { foreach_key_copy(dest); }
176 
178  virtual void print(FILE* out) const = 0;
179 
186  static wreport::Varinfo key_info(const char* key);
187 
194  static wreport::Varinfo key_info(const std::string& key);
195 
196 protected:
197  virtual void foreach_key_ref(std::function<void(const char*, const wreport::Var&)> dest) const = 0;
198  virtual void foreach_key_copy(std::function<void(const char*, std::unique_ptr<wreport::Var>&&)> dest) const = 0;
199 };
200 
201 }
202 #endif
virtual void set_trange(const Trange &tr)=0
Set pindicator, p1, p2.
virtual void sets(const char *key, const std::string &val)=0
Set a key to a string value.
virtual void setd(const char *key, double val)=0
Set a key to a double value.
virtual bool isset(const char *key) const
Check if a value is set.
Common base types used by most of DB-All.e code.
bool operator!=(const Record &rec) const
Check if two records differ.
static wreport::Varinfo key_info(const char *key)
Return informations about a key.
virtual void set_var_acquire(std::unique_ptr< wreport::Var > &&var)=0
Set var.code() == var.
virtual void setc(const char *key, const char *val)=0
Set a key to a string value.
virtual void setf(const char *key, const char *val)=0
Set a key to a string value.
virtual void clear_vars()=0
Remove all Bxxyyy keys from the record, leaving the rest intact.
virtual void set_datetime(const Datetime &dt)=0
Set year, month, day, hour, min, sec.
Coordinates.
Definition: types.h:320
virtual void set_coords(const Coords &c)=0
Set lat, lon.
virtual void set_datetimerange(const DatetimeRange &lr)=0
Set datetime-min and datetime-max values.
virtual void clear()=0
Remove all contents from the record.
Information on how a value has been sampled or computed with regards to time.
Definition: types.h:565
Key/value store where keys are strings and values are wreport variables.
Definition: record.h:16
virtual void add(const Record &source)=0
Copy all data from the record source into dest.
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
Vertical level or layer.
Definition: types.h:515
virtual bool equals(const Record &rec) const =0
Check if two records are the same.
static std::unique_ptr< Record > create()
Create a new Record.
virtual void set_var(const wreport::Var &var)=0
Set var.code() == var.value()
Range of datetimes.
Definition: types.h:255
virtual void set_latrange(const LatRange &lr)=0
Set latmin, latmax.
Range of latitudes.
Definition: types.h:390
virtual void print(FILE *out) const =0
Print the contents of this record to the given stream.
Date and time.
Definition: types.h:147
virtual void seti(const char *key, int val)=0
Set a key to an integer value.
bool operator==(const Record &rec) const
Check if two records are the same.
virtual bool contains(const Record &subset) const =0
Return true if all elements of subset are present in this record, with the same value.
virtual std::unique_ptr< Record > clone() const =0
Return a copy of this record.
void foreach_key(std::function< void(const char *, const wreport::Var &)> dest) const
Generate a sequence of key names and const Var& for all the contents of the record.
Definition: record.h:169
void foreach_key(std::function< void(const char *, std::unique_ptr< wreport::Var > &&)> dest) const
Generate a sequence of key names and unique_ptr<Var> for all the contents of the record.
Definition: record.h:175
const wreport::Var & operator[](const char *key) const
Get a value, if set, or throw an exception if not.
Range of longitudes.
Definition: types.h:464
virtual void unset(const char *key)=0
Remove/unset a key from the record.
virtual void set_level(const Level &lev)=0
Set leveltype1, l1, leveltype2, l2.
virtual void set_lonrange(const LonRange &lr)=0
Set lonmin, lonmax.