1 #ifndef teca_metadata_h
2 #define teca_metadata_h
7 #include <initializer_list>
10 #include "teca_variant_array.h"
29 unsigned int size()
const {
return props.size(); }
32 int size(
const std::string &name,
33 unsigned int &size)
const noexcept;
36 void resize(
const std::string &name,
unsigned int n);
41 void declare(
const std::string &name);
44 void declare(
const std::string &name,
unsigned int n);
49 int set(
const std::string &name,
const T &val);
53 int set(
const std::string &name,
const T *val,
unsigned int n);
55 template<
typename T,
unsigned int N>
56 int set(
const std::string &name,
const T (&val)[N])
57 {
return this->set(name, val, N); }
61 int set(
const std::string &name,
const std::set<T> &val);
65 int set(
const std::string &name,
const std::vector<T> &val);
68 int set(
const std::string &name, std::initializer_list<T> val);
72 int set(
const std::string &name,
const std::vector<std::vector<T>> &val);
76 int set(
const std::string &name,
const p_teca_variant_array &prop_val);
79 int set(
const std::string &name,
80 const p_teca_variant_array_impl<T> &prop_val);
85 int append(
const std::string &name,
const T &val);
89 int update(
const std::string &name,
const T &val)
90 {
return this->update<T>(name, 0, val); }
95 int update(
const std::string &name,
unsigned int i,
const T &val);
100 int update(
const std::string &name,
const T *val,
unsigned int n);
104 int update(
const std::string &name,
const std::vector<T> &val);
107 int update(
const std::string &name, std::initializer_list<T> val);
111 int update(
const std::string &name,
const std::set<T> &val);
115 int update(
const std::string &name, p_teca_variant_array prop_val);
119 int get(
const std::string &name, T &val)
const
120 {
return this->get<T>(name, (
unsigned int)(0), val); }
124 int get(
const std::string &name,
unsigned int i, T &val)
const;
129 int get(
const std::string &name,
130 T *val,
unsigned int n)
const;
135 int get(
const std::string &name, std::vector<T> &val)
const;
140 int get(
const std::string &name, std::set<T> &val)
const;
144 int get(
const std::string &name, p_teca_variant_array val)
const;
148 p_teca_variant_array get(
const std::string &name);
149 const_p_teca_variant_array get(
const std::string &name)
const;
153 int get_name(
unsigned long i, std::string &name)
const;
157 int get_names(std::vector<std::string> &names)
const;
160 int remove(
const std::string &name) noexcept;
167 int has(
const std::string &name)
const noexcept;
170 int empty()
const noexcept;
173 explicit operator bool()
const noexcept
181 int to_stream(std::ostream &os)
const;
182 int from_stream(std::ostream &) {
return -1; }
185 unsigned long long get_next_id()
const noexcept;
188 unsigned long long id;
189 using prop_map_t = std::map<std::string, p_teca_variant_array>;
208 {
return !(lhs == rhs); }
216 void teca_metadata::declare(
const std::string &name)
218 p_teca_variant_array prop_val
221 this->set(name, prop_val);
226 void teca_metadata::declare(
const std::string &name,
unsigned int n)
228 p_teca_variant_array prop_val
231 this->set(name, prop_val);
236 int teca_metadata::append(
const std::string &name,
const T &val)
238 prop_map_t::iterator it = this->props.find(name);
239 if (it == this->props.end())
241 return this->set(name, val);
244 it->second->append(val);
251 int teca_metadata::set(
const std::string &name,
const T &val)
253 p_teca_variant_array prop_val
256 return this->set(name, prop_val);
261 int teca_metadata::set(
const std::string &name,
const T *vals,
264 p_teca_variant_array prop_val
267 return this->set(name, prop_val);
272 int teca_metadata::set(
const std::string &name,
const std::set<T> &vals)
274 size_t n = vals.size();
276 std::vector<T> tmp(vals.begin(), vals.end());
278 p_teca_variant_array prop_val
281 return this->set(name, prop_val);
286 int teca_metadata::set(
const std::string &name,
287 std::initializer_list<T> vals)
289 return this->set(name, std::vector<T>(vals));
294 int teca_metadata::set(
const std::string &name,
295 const std::vector<T> &vals)
297 size_t n = vals.size();
299 p_teca_variant_array prop_val
302 return this->set(name, prop_val);
307 int teca_metadata::set(
const std::string &name,
308 const std::vector<std::vector<T>> &vals)
310 size_t n = vals.size();
312 p_teca_variant_array prop_vals
315 for (
size_t i = 0; i < n; ++i)
317 p_teca_variant_array prop_val
319 prop_vals->append(prop_val);
322 return this->set(name, prop_vals);
327 int teca_metadata::set(
const std::string &name,
328 const p_teca_variant_array_impl<T> &prop_val)
330 this->props[name] = prop_val;
336 int teca_metadata::update(
const std::string &name,
unsigned int i,
const T &val)
338 prop_map_t::iterator it = this->props.find(name);
339 if (it == this->props.end())
342 <<
"attempt to access non-existent property \""
343 << name <<
"\" ignored!")
347 it->second->set(i, val);
355 const T *vals,
unsigned int n_vals)
357 prop_map_t::iterator it = this->props.find(name);
358 if (it == this->props.end())
361 <<
"attempt to access non-existent property \""
362 << name <<
"\" ignored!")
366 it->second->set(0, n_vals-1, vals);
373 int teca_metadata::update(const std::
string &name, const std::vector<T> &vals)
375 prop_map_t::iterator it = this->props.find(name);
376 if (it == this->props.end())
379 <<
"attempt to access non-existent property \""
380 << name <<
"\" ignored!")
384 it->second->set(vals);
391 int teca_metadata::update(const std::
string &name, std::initializer_list<T> vals)
393 return this->update(name, std::vector<T>(vals));
398 int teca_metadata::update(
const std::string &name,
const std::set<T> &vals)
400 prop_map_t::iterator it = this->props.find(name);
401 if (it == this->props.end())
404 <<
"attempt to access non-existent property \""
405 << name <<
"\" ignored!")
409 std::vector<T> tmp(vals.begin(), vals.end());
410 it->second->set(tmp);
417 int teca_metadata::get(const std::
string &name,
unsigned int i, T &val)
const
419 prop_map_t::const_iterator it = this->props.find(name);
421 if (it == this->props.end())
424 if (it->second->size() <= i)
426 TECA_ERROR(
"Requested element " << i <<
" in property \""
427 << name <<
"\" of length " << it->second->size())
431 it->second->get(i, val);
438 int teca_metadata::get(const std::
string &name, std::vector<T> &vals)
const
440 prop_map_t::const_iterator it = this->props.find(name);
442 if (it == this->props.end())
445 it->second->get(vals);
452 int teca_metadata::get(
const std::string &name, std::set<T> &vals)
const
455 if (!this->get(name, tmp))
457 vals = std::set<T>(tmp.begin(), tmp.end());
465 int teca_metadata::get(
const std::string &name,
466 T *vals,
unsigned int n)
const
468 prop_map_t::const_iterator it = this->props.find(name);
470 if (it == this->props.end())
473 if (it->second->size() < n)
475 TECA_ERROR(
"Requested " << n <<
" values in property \""
476 << name <<
"\" of length " << it->second->size())
480 it->second->get(0, n-1, vals);