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 template<
typename T,
size_t N>
74 int set(
const std::string &name,
const std::array<T,N> &val);
77 int set(
const std::string &name, std::initializer_list<T> val);
81 int set(
const std::string &name,
const std::vector<std::vector<T>> &val);
88 int set(
const std::string &name,
94 int append(
const std::string &name,
const T &val);
98 int update(
const std::string &name,
const T &val)
99 {
return this->update<T>(name, 0, val); }
104 int update(
const std::string &name,
unsigned int i,
const T &val);
109 int update(
const std::string &name,
const T *val,
unsigned int n);
113 int update(
const std::string &name,
const std::vector<T> &val);
116 template<
typename T,
size_t N>
117 int update(
const std::string &name,
const std::array<T,N> &val);
120 int update(
const std::string &name, std::initializer_list<T> val);
124 int update(
const std::string &name,
const std::set<T> &val);
132 int get(
const std::string &name, T &val)
const
133 {
return this->get<T>(name, (
unsigned int)(0), val); }
137 int get(
const std::string &name,
unsigned int i, T &val)
const;
142 int get(
const std::string &name,
143 T *val,
unsigned int n)
const;
145 template<
typename T,
unsigned int N>
146 int get(
const std::string &name, T (&val)[N])
const
147 {
return this->get(name, val, N); }
152 int get(
const std::string &name, std::vector<T> &val)
const;
156 template<
typename T,
size_t N>
157 int get(
const std::string &name, std::array<T,N> &val)
const;
162 int get(
const std::string &name, std::set<T> &val)
const;
175 int get_name(
unsigned long i, std::string &name)
const;
179 int get_names(std::vector<std::string> &names)
const;
182 int remove(
const std::string &name) noexcept;
189 int has(
const std::string &name)
const noexcept;
192 int empty()
const noexcept;
195 explicit operator bool()
const noexcept
203 int to_stream(std::ostream &os)
const;
204 int from_stream(std::ostream &) {
return -1; }
207 unsigned long long get_next_id()
const noexcept;
210 unsigned long long id;
211 using prop_map_t = std::map<std::string, p_teca_variant_array>;
232 {
return !(lhs == rhs); }
241 void teca_metadata::declare(
const std::string &name)
246 this->set(name, prop_val);
251 void teca_metadata::declare(
const std::string &name,
unsigned int n)
256 this->set(name, prop_val);
261 int teca_metadata::append(
const std::string &name,
const T &val)
263 prop_map_t::iterator it = this->props.find(name);
264 if (it == this->props.end())
266 return this->set(name, val);
269 it->second->append(val);
276 int teca_metadata::set(
const std::string &name,
const T &val)
281 return this->set(name, prop_val);
286 int teca_metadata::set(
const std::string &name,
const T *vals,
292 return this->set(name, prop_val);
297 int teca_metadata::set(
const std::string &name,
const std::set<T> &vals)
299 size_t n = vals.size();
301 std::vector<T> tmp(vals.begin(), vals.end());
306 return this->set(name, prop_val);
311 int teca_metadata::set(
const std::string &name,
312 std::initializer_list<T> vals)
314 return this->set(name, std::vector<T>(vals));
319 int teca_metadata::set(
const std::string &name,
320 const std::vector<T> &vals)
322 size_t n = vals.size();
327 return this->set(name, prop_val);
331 template<
typename T,
size_t N>
332 int teca_metadata::set(
const std::string &name,
const std::array<T,N> &vals)
337 return this->set(name, prop_val);
342 int teca_metadata::set(
const std::string &name,
343 const std::vector<std::vector<T>> &vals)
345 size_t n = vals.size();
350 for (
size_t i = 0; i < n; ++i)
354 prop_vals->append(prop_val);
357 return this->set(name, prop_vals);
362 int teca_metadata::set(
const std::string &name,
365 this->props[name] = prop_val;
371 int teca_metadata::update(
const std::string &name,
unsigned int i,
const T &val)
373 prop_map_t::iterator it = this->props.find(name);
374 if (it == this->props.end())
377 <<
"attempt to access non-existent property \""
378 << name <<
"\" ignored!")
382 it->second->set(i, val);
390 const T *vals,
unsigned int n_vals)
392 prop_map_t::iterator it = this->props.find(name);
393 if (it == this->props.end())
396 <<
"attempt to access non-existent property \""
397 << name <<
"\" ignored!")
401 it->second->set(0, n_vals-1, vals);
408 int teca_metadata::update(const std::
string &name, const std::vector<T> &vals)
410 prop_map_t::iterator it = this->props.find(name);
411 if (it == this->props.end())
414 <<
"attempt to access non-existent property \""
415 << name <<
"\" ignored!")
419 it->second->set(vals);
425 template<typename T,
size_t N>
426 int teca_metadata::update(const std::
string &name, const std::array<T,N> &vals)
428 return this->update(name, vals.data(), N);
433 int teca_metadata::update(
const std::string &name, std::initializer_list<T> vals)
435 return this->update(name, std::vector<T>(vals));
440 int teca_metadata::update(
const std::string &name,
const std::set<T> &vals)
442 prop_map_t::iterator it = this->props.find(name);
443 if (it == this->props.end())
446 <<
"attempt to access non-existent property \""
447 << name <<
"\" ignored!")
451 std::vector<T> tmp(vals.begin(), vals.end());
452 it->second->set(tmp);
459 int teca_metadata::get(const std::
string &name,
unsigned int i, T &val)
const
461 prop_map_t::const_iterator it = this->props.find(name);
463 if (it == this->props.end())
466 if (it->second->size() <= i)
468 TECA_ERROR(
"Requested element " << i <<
" in property \""
469 << name <<
"\" of length " << it->second->size())
473 it->second->get(i, val);
480 int teca_metadata::get(const std::
string &name, std::vector<T> &vals)
const
482 prop_map_t::const_iterator it = this->props.find(name);
484 if (it == this->props.end())
487 it->second->get(vals);
493 template<
typename T,
size_t N>
494 int teca_metadata::get(
const std::string &name, std::array<T,N> &vals)
const
496 return this->get(name, vals.data(), N);
501 int teca_metadata::get(
const std::string &name, std::set<T> &vals)
const
504 if (!this->get(name, tmp))
506 vals = std::set<T>(tmp.begin(), tmp.end());
514 int teca_metadata::get(
const std::string &name,
515 T *vals,
unsigned int n)
const
517 prop_map_t::const_iterator it = this->props.find(name);
519 if (it == this->props.end())
522 if (it->second->size() < n)
524 TECA_ERROR(
"Requested " << n <<
" values in property \""
525 << name <<
"\" of length " << it->second->size())
529 it->second->get(0, vals, 0, n);
Serialize objects into a binary stream.
Definition: teca_binary_stream.h:17
The concrete implementation of our type agnostic container for contiguous arrays.
Definition: teca_variant_array_impl.h:380
static std::shared_ptr< teca_variant_array_impl< T > > New()
Definition: teca_variant_array_impl.h:393
p_teca_error_handler error_handler TECA_EXPORT
The global error handler instance.
TECA_EXPORT bool append(teca_variant_array *varr, PyObject *obj)
Append values from the object to the variant array.
Definition: teca_py_array.h:221
TECA_EXPORT bool set(teca_variant_array *varr, unsigned long i, PyObject *obj)
Set i'th element of the variant array to the value of the object.
Definition: teca_py_array.h:305
#define TECA_ERROR(_msg)
Constructs an error message and sends it to the stderr stream.
Definition: teca_common.h:161
std::shared_ptr< teca_variant_array > p_teca_variant_array
Definition: teca_variant_array.h:27
std::shared_ptr< const teca_variant_array > const_p_teca_variant_array
Definition: teca_variant_array.h:27
std::shared_ptr< teca_variant_array_impl< T > > p_teca_variant_array_impl
Definition: teca_variant_array_impl.h:44