1 #ifndef teca_metadata_h
2 #define teca_metadata_h
7 #include <initializer_list>
31 unsigned int size()
const {
return props.size(); }
34 int size(
const std::string &name,
35 unsigned int &size)
const noexcept;
38 void resize(
const std::string &name,
unsigned int n);
43 void declare(
const std::string &name);
46 void declare(
const std::string &name,
unsigned int n);
51 int set(
const std::string &name,
const T &val);
55 int set(
const std::string &name,
const T *val,
unsigned int n);
57 template<
typename T,
unsigned int N>
58 int set(
const std::string &name,
const T (&val)[N])
59 {
return this->set(name, val, N); }
63 int set(
const std::string &name,
const std::set<T> &val);
67 int set(
const std::string &name,
const std::vector<T> &val);
70 int set(
const std::string &name, std::initializer_list<T> val);
74 int set(
const std::string &name,
const std::vector<std::vector<T>> &val);
81 int set(
const std::string &name,
87 int append(
const std::string &name,
const T &val);
91 int update(
const std::string &name,
const T &val)
92 {
return this->update<T>(name, 0, val); }
97 int update(
const std::string &name,
unsigned int i,
const T &val);
102 int update(
const std::string &name,
const T *val,
unsigned int n);
106 int update(
const std::string &name,
const std::vector<T> &val);
109 int update(
const std::string &name, std::initializer_list<T> val);
113 int update(
const std::string &name,
const std::set<T> &val);
121 int get(
const std::string &name, T &val)
const
122 {
return this->get<T>(name, (
unsigned int)(0), val); }
126 int get(
const std::string &name,
unsigned int i, T &val)
const;
131 int get(
const std::string &name,
132 T *val,
unsigned int n)
const;
134 template<
typename T,
unsigned int N>
135 int get(
const std::string &name, T (&val)[N])
const
136 {
return this->get(name, val, N); }
141 int get(
const std::string &name, std::vector<T> &val)
const;
146 int get(
const std::string &name, std::set<T> &val)
const;
159 int get_name(
unsigned long i, std::string &name)
const;
163 int get_names(std::vector<std::string> &names)
const;
166 int remove(
const std::string &name) noexcept;
173 int has(
const std::string &name)
const noexcept;
176 int empty()
const noexcept;
179 explicit operator bool()
const noexcept
187 int to_stream(std::ostream &os)
const;
188 int from_stream(std::ostream &) {
return -1; }
191 unsigned long long get_next_id()
const noexcept;
194 unsigned long long id;
195 using prop_map_t = std::map<std::string, p_teca_variant_array>;
214 {
return !(lhs == rhs); }
222 void teca_metadata::declare(
const std::string &name)
227 this->set(name, prop_val);
232 void teca_metadata::declare(
const std::string &name,
unsigned int n)
237 this->set(name, prop_val);
242 int teca_metadata::append(
const std::string &name,
const T &val)
244 prop_map_t::iterator it = this->props.find(name);
245 if (it == this->props.end())
247 return this->set(name, val);
250 it->second->append(val);
257 int teca_metadata::set(
const std::string &name,
const T &val)
262 return this->set(name, prop_val);
267 int teca_metadata::set(
const std::string &name,
const T *vals,
273 return this->set(name, prop_val);
278 int teca_metadata::set(
const std::string &name,
const std::set<T> &vals)
280 size_t n = vals.size();
282 std::vector<T> tmp(vals.begin(), vals.end());
287 return this->set(name, prop_val);
292 int teca_metadata::set(
const std::string &name,
293 std::initializer_list<T> vals)
295 return this->set(name, std::vector<T>(vals));
300 int teca_metadata::set(
const std::string &name,
301 const std::vector<T> &vals)
303 size_t n = vals.size();
308 return this->set(name, prop_val);
313 int teca_metadata::set(
const std::string &name,
314 const std::vector<std::vector<T>> &vals)
316 size_t n = vals.size();
321 for (
size_t i = 0; i < n; ++i)
325 prop_vals->append(prop_val);
328 return this->set(name, prop_vals);
333 int teca_metadata::set(
const std::string &name,
336 this->props[name] = prop_val;
342 int teca_metadata::update(
const std::string &name,
unsigned int i,
const T &val)
344 prop_map_t::iterator it = this->props.find(name);
345 if (it == this->props.end())
348 <<
"attempt to access non-existent property \""
349 << name <<
"\" ignored!")
353 it->second->set(i, val);
361 const T *vals,
unsigned int n_vals)
363 prop_map_t::iterator it = this->props.find(name);
364 if (it == this->props.end())
367 <<
"attempt to access non-existent property \""
368 << name <<
"\" ignored!")
372 it->second->set(0, n_vals-1, vals);
379 int teca_metadata::update(const std::
string &name, const std::vector<T> &vals)
381 prop_map_t::iterator it = this->props.find(name);
382 if (it == this->props.end())
385 <<
"attempt to access non-existent property \""
386 << name <<
"\" ignored!")
390 it->second->set(vals);
397 int teca_metadata::update(const std::
string &name, std::initializer_list<T> vals)
399 return this->update(name, std::vector<T>(vals));
404 int teca_metadata::update(
const std::string &name,
const std::set<T> &vals)
406 prop_map_t::iterator it = this->props.find(name);
407 if (it == this->props.end())
410 <<
"attempt to access non-existent property \""
411 << name <<
"\" ignored!")
415 std::vector<T> tmp(vals.begin(), vals.end());
416 it->second->set(tmp);
423 int teca_metadata::get(const std::
string &name,
unsigned int i, T &val)
const
425 prop_map_t::const_iterator it = this->props.find(name);
427 if (it == this->props.end())
430 if (it->second->size() <= i)
432 TECA_ERROR(
"Requested element " << i <<
" in property \""
433 << name <<
"\" of length " << it->second->size())
437 it->second->get(i, val);
444 int teca_metadata::get(const std::
string &name, std::vector<T> &vals)
const
446 prop_map_t::const_iterator it = this->props.find(name);
448 if (it == this->props.end())
451 it->second->get(vals);
458 int teca_metadata::get(
const std::string &name, std::set<T> &vals)
const
461 if (!this->get(name, tmp))
463 vals = std::set<T>(tmp.begin(), tmp.end());
471 int teca_metadata::get(
const std::string &name,
472 T *vals,
unsigned int n)
const
474 prop_map_t::const_iterator it = this->props.find(name);
476 if (it == this->props.end())
479 if (it->second->size() < n)
481 TECA_ERROR(
"Requested " << n <<
" values in property \""
482 << name <<
"\" of length " << it->second->size())
486 it->second->get(0, n-1, vals);