1 #ifndef teca_metadata_h
2 #define teca_metadata_h
4 #include "teca_config.h"
11 #include <initializer_list>
34 unsigned int size()
const {
return props.size(); }
37 int size(
const std::string &name,
38 unsigned int &size)
const noexcept;
41 void resize(
const std::string &name,
unsigned int n);
46 void declare(
const std::string &name);
49 void declare(
const std::string &name,
unsigned int n);
54 int set(
const std::string &name,
const T &val);
58 int set(
const std::string &name,
const T *val,
unsigned int n);
60 template<
typename T,
unsigned int N>
61 int set(
const std::string &name,
const T (&val)[N])
62 {
return this->
set(name, val, N); }
66 int set(
const std::string &name,
const std::set<T> &val);
70 int set(
const std::string &name,
const std::vector<T> &val);
73 int set(
const std::string &name, std::initializer_list<T> val);
77 int set(
const std::string &name,
const std::vector<std::vector<T>> &val);
84 int set(
const std::string &name,
90 int append(
const std::string &name,
const T &val);
94 int update(
const std::string &name,
const T &val)
95 {
return this->update<T>(name, 0, val); }
100 int update(
const std::string &name,
unsigned int i,
const T &val);
105 int update(
const std::string &name,
const T *val,
unsigned int n);
109 int update(
const std::string &name,
const std::vector<T> &val);
112 int update(
const std::string &name, std::initializer_list<T> val);
116 int update(
const std::string &name,
const std::set<T> &val);
124 int get(
const std::string &name, T &val)
const
125 {
return this->get<T>(name, (
unsigned int)(0), val); }
129 int get(
const std::string &name,
unsigned int i, T &val)
const;
134 int get(
const std::string &name,
135 T *val,
unsigned int n)
const;
137 template<
typename T,
unsigned int N>
138 int get(
const std::string &name, T (&val)[N])
const
139 {
return this->get(name, val, N); }
144 int get(
const std::string &name, std::vector<T> &val)
const;
149 int get(
const std::string &name, std::set<T> &val)
const;
162 int get_name(
unsigned long i, std::string &name)
const;
166 int get_names(std::vector<std::string> &names)
const;
169 int remove(
const std::string &name) noexcept;
176 int has(
const std::string &name)
const noexcept;
179 int empty()
const noexcept;
182 explicit operator bool()
const noexcept
190 int to_stream(std::ostream &os)
const;
191 int from_stream(std::ostream &) {
return -1; }
194 unsigned long long get_next_id()
const noexcept;
197 unsigned long long id;
198 using prop_map_t = std::map<std::string, p_teca_variant_array>;
219 {
return !(lhs == rhs); }
228 void teca_metadata::declare(
const std::string &name)
233 this->set(name, prop_val);
238 void teca_metadata::declare(
const std::string &name,
unsigned int n)
243 this->set(name, prop_val);
248 int teca_metadata::append(
const std::string &name,
const T &val)
250 prop_map_t::iterator it = this->props.find(name);
251 if (it == this->props.end())
253 return this->set(name, val);
256 it->second->append(val);
263 int teca_metadata::set(
const std::string &name,
const T &val)
268 return this->set(name, prop_val);
273 int teca_metadata::set(
const std::string &name,
const T *vals,
279 return this->set(name, prop_val);
284 int teca_metadata::set(
const std::string &name,
const std::set<T> &vals)
286 size_t n = vals.size();
288 std::vector<T> tmp(vals.begin(), vals.end());
293 return this->set(name, prop_val);
298 int teca_metadata::set(
const std::string &name,
299 std::initializer_list<T> vals)
301 return this->set(name, std::vector<T>(vals));
306 int teca_metadata::set(
const std::string &name,
307 const std::vector<T> &vals)
309 size_t n = vals.size();
314 return this->set(name, prop_val);
319 int teca_metadata::set(
const std::string &name,
320 const std::vector<std::vector<T>> &vals)
322 size_t n = vals.size();
327 for (
size_t i = 0; i < n; ++i)
331 prop_vals->append(prop_val);
334 return this->set(name, prop_vals);
339 int teca_metadata::set(
const std::string &name,
342 this->props[name] = prop_val;
348 int teca_metadata::update(
const std::string &name,
unsigned int i,
const T &val)
350 prop_map_t::iterator it = this->props.find(name);
351 if (it == this->props.end())
354 <<
"attempt to access non-existent property \""
355 << name <<
"\" ignored!")
359 it->second->set(i, val);
367 const T *vals,
unsigned int n_vals)
369 prop_map_t::iterator it = this->props.find(name);
370 if (it == this->props.end())
373 <<
"attempt to access non-existent property \""
374 << name <<
"\" ignored!")
378 it->second->set(0, n_vals-1, vals);
385 int teca_metadata::update(const std::
string &name, const std::vector<T> &vals)
387 prop_map_t::iterator it = this->props.find(name);
388 if (it == this->props.end())
391 <<
"attempt to access non-existent property \""
392 << name <<
"\" ignored!")
396 it->second->set(vals);
403 int teca_metadata::update(const std::
string &name, std::initializer_list<T> vals)
405 return this->update(name, std::vector<T>(vals));
410 int teca_metadata::update(
const std::string &name,
const std::set<T> &vals)
412 prop_map_t::iterator it = this->props.find(name);
413 if (it == this->props.end())
416 <<
"attempt to access non-existent property \""
417 << name <<
"\" ignored!")
421 std::vector<T> tmp(vals.begin(), vals.end());
422 it->second->set(tmp);
429 int teca_metadata::get(const std::
string &name,
unsigned int i, T &val)
const
431 prop_map_t::const_iterator it = this->props.find(name);
433 if (it == this->props.end())
436 if (it->second->size() <= i)
438 TECA_ERROR(
"Requested element " << i <<
" in property \""
439 << name <<
"\" of length " << it->second->size())
443 it->second->get(i, val);
450 int teca_metadata::get(const std::
string &name, std::vector<T> &vals)
const
452 prop_map_t::const_iterator it = this->props.find(name);
454 if (it == this->props.end())
457 it->second->get(vals);
464 int teca_metadata::get(
const std::string &name, std::set<T> &vals)
const
467 if (!this->get(name, tmp))
469 vals = std::set<T>(tmp.begin(), tmp.end());
477 int teca_metadata::get(
const std::string &name,
478 T *vals,
unsigned int n)
const
480 prop_map_t::const_iterator it = this->props.find(name);
482 if (it == this->props.end())
485 if (it->second->size() < n)
487 TECA_ERROR(
"Requested " << n <<
" values in property \""
488 << name <<
"\" of length " << it->second->size())
492 it->second->get(0, vals, 0, n);