libdballe  7.6
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 
74  virtual void setf(const char* key, const char* val) = 0;
75 
77  virtual void set_datetime(const Datetime& dt) = 0;
79  virtual void set_coords(const Coords& c) = 0;
81  virtual void set_latrange(const LatRange& lr) = 0;
83  virtual void set_lonrange(const LonRange& lr) = 0;
85  virtual void set_datetimerange(const DatetimeRange& lr) = 0;
87  virtual void set_level(const Level& lev) = 0;
89  virtual void set_trange(const Trange& tr) = 0;
91  virtual void set_var(const wreport::Var& var) = 0;
93  virtual void set_var_acquire(std::unique_ptr<wreport::Var>&& var) = 0;
94 
95  void set(const char* key, int val) { seti(key, val); }
96  void set(const char* key, double val) { setd(key, val); }
97  void set(const char* key, const char* val) { setc(key, val); }
98  void set(const char* key, const std::string& val) { sets(key, val); }
99  void set(const Datetime& dt) { set_datetime(dt); }
100  void set(const DatetimeRange& dt) { set_datetimerange(dt); }
101  void set(const Coords& c) { set_coords(c); }
102  void set(const LatRange& lr) { set_latrange(lr); }
103  void set(const LonRange& lr) { set_lonrange(lr); }
104  void set(const Level& lev) { set_level(lev); }
105  void set(const Trange& tr) { set_trange(tr); }
106  void set(const wreport::Var& var) { set_var(var); }
107  void set(std::unique_ptr<wreport::Var>&& var) { set_var_acquire(std::move(var)); }
108 
110  virtual void unset(const char* key) = 0;
111 
113  virtual const wreport::Var* get(const char* key) const = 0;
114 
116  virtual bool isset(const char* key) const;
117 
119  virtual bool equals(const Record& rec) const = 0;
120 
122  bool operator==(const Record& rec) const;
123 
125  bool operator!=(const Record& rec) const;
126 
128  const wreport::Var& operator[](const char* key) const;
129 
130  const char* enq(const char* key, const char* def) const
131  {
132  if (const wreport::Var* var = get(key))
133  return var->enq(def);
134  return def;
135  }
136 
137  template<typename T>
138  T enq(const char* key, const T& def) const
139  {
140  if (const wreport::Var* var = get(key))
141  return var->enq(def);
142  return def;
143  }
144 
154  virtual void add(const Record& source) = 0;
155 
160  virtual bool contains(const Record& subset) const = 0;
161 
166  void foreach_key(std::function<void(const char*, const wreport::Var&)> dest) const { foreach_key_ref(dest); }
167 
172  void foreach_key(std::function<void(const char*, std::unique_ptr<wreport::Var>&&)> dest) const { foreach_key_copy(dest); }
173 
175  virtual void print(FILE* out) const = 0;
176 
183  static wreport::Varinfo key_info(const char* key);
184 
191  static wreport::Varinfo key_info(const std::string& key);
192 
193 protected:
194  virtual void foreach_key_ref(std::function<void(const char*, const wreport::Var&)> dest) const = 0;
195  virtual void foreach_key_copy(std::function<void(const char*, std::unique_ptr<wreport::Var>&&)> dest) const = 0;
196 };
197 
198 }
199 #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:166
void foreach_key(std::function< void(const char *, std::unique_ptr< wreport::Var > &&)> dest) const
Generate a sequence of key names and unique_ptr for all the contents of the record.
Definition: record.h:172
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.