libdballe  7.29
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 #include <functional>
8 
9 namespace dballe {
10 
17 struct Record
18 {
19  virtual ~Record() {}
20 
22  virtual std::unique_ptr<Record> clone() const = 0;
23 
25  static std::unique_ptr<Record> create();
26 
28  virtual void clear() = 0;
29 
31  virtual void clear_vars() = 0;
32 
41  virtual void seti(const char* key, int val) = 0;
42 
46  virtual void setd(const char* key, double val) = 0;
47 
56  virtual void setc(const char* key, const char* val) = 0;
57 
66  virtual void sets(const char* key, const std::string& val) = 0;
67 
78  virtual void setf(const char* key, const char* val) = 0;
79 
81  virtual void set_datetime(const Datetime& dt) = 0;
83  virtual void set_coords(const Coords& c) = 0;
85  virtual void set_latrange(const LatRange& lr) = 0;
87  virtual void set_lonrange(const LonRange& lr) = 0;
89  virtual void set_datetimerange(const DatetimeRange& lr) = 0;
91  virtual void set_level(const Level& lev) = 0;
93  virtual void set_trange(const Trange& tr) = 0;
95  virtual void set_var(const wreport::Var& var) = 0;
97  virtual void set_var_acquire(std::unique_ptr<wreport::Var>&& var) = 0;
98 
99  void set(const char* key, int val) { seti(key, val); }
100  void set(const char* key, double val) { setd(key, val); }
101  void set(const char* key, const char* val) { setc(key, val); }
102  void set(const char* key, const std::string& val) { sets(key, val); }
103  void set(const Datetime& dt) { set_datetime(dt); }
104  void set(const DatetimeRange& dt) { set_datetimerange(dt); }
105  void set(const Coords& c) { set_coords(c); }
106  void set(const LatRange& lr) { set_latrange(lr); }
107  void set(const LonRange& lr) { set_lonrange(lr); }
108  void set(const Level& lev) { set_level(lev); }
109  void set(const Trange& tr) { set_trange(tr); }
110  void set(const wreport::Var& var) { set_var(var); }
111  void set(std::unique_ptr<wreport::Var>&& var) { set_var_acquire(std::move(var)); }
112 
114  virtual void unset(const char* key) = 0;
115 
117  virtual const wreport::Var* get(const char* key) const = 0;
118 
120  virtual bool isset(const char* key) const;
121 
123  virtual bool equals(const Record& rec) const = 0;
124 
126  bool operator==(const Record& rec) const;
127 
129  bool operator!=(const Record& rec) const;
130 
132  const wreport::Var& operator[](const char* key) const;
133 
134  const char* enq(const char* key, const char* def) const
135  {
136  if (const wreport::Var* var = get(key))
137  return var->enq(def);
138  return def;
139  }
140 
141  template<typename T>
142  T enq(const char* key, const T& def) const
143  {
144  if (const wreport::Var* var = get(key))
145  return var->enq(def);
146  return def;
147  }
148 
158  virtual void add(const Record& source) = 0;
159 
164  virtual bool contains(const Record& subset) const = 0;
165 
170  void foreach_key(std::function<void(const char*, const wreport::Var&)> dest) const { foreach_key_ref(dest); }
171 
176  void foreach_key(std::function<void(const char*, std::unique_ptr<wreport::Var>&&)> dest) const { foreach_key_copy(dest); }
177 
179  virtual void print(FILE* out) const = 0;
180 
187  static wreport::Varinfo key_info(const char* key);
188 
195  static wreport::Varinfo key_info(const std::string& key);
196 
197 protected:
198  virtual void foreach_key_ref(std::function<void(const char*, const wreport::Var&)> dest) const = 0;
199  virtual void foreach_key_copy(std::function<void(const char*, std::unique_ptr<wreport::Var>&&)> dest) const = 0;
200 };
201 
202 }
203 #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:337
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:587
Key/value store where keys are strings and values are wreport variables.
Definition: record.h:17
virtual void add(const Record &source)=0
Copy all data from the record source into dest.
Vertical level or layer.
Definition: types.h:532
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:272
virtual void set_latrange(const LatRange &lr)=0
Set latmin, latmax.
Range of latitudes.
Definition: types.h:407
T enq() const
virtual void print(FILE *out) const =0
Print the contents of this record to the given stream.
Date and time.
Definition: types.h:158
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&amp; for all the contents of the record.
Definition: record.h:170
void foreach_key(std::function< void(const char *, std::unique_ptr< wreport::Var > &&)> dest) const
Generate a sequence of key names and unique_ptr&lt;Var&gt; for all the contents of the record.
Definition: record.h:176
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:481
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.