TECA
teca_table_collection.h
1 #ifndef teca_table_collection_h
2 #define teca_table_collection_h
3 
4 #include "teca_table_collection_fwd.h"
5 #include "teca_table.h"
6 #include <map>
7 #include <vector>
8 #include <string>
9 
10 // a collection of named tables
15 {
16 public:
17  // construct on heap
18  static
19  p_teca_table_collection New()
20  { return p_teca_table_collection(new teca_table_collection()); }
21 
22  // reset to empty state
23  void clear();
24 
25  // declare a set of tables, from a list of names
26  template<typename nT, typename... oT>
27  void declare_set(nT &&a_name, oT &&...args);
28 
29  // declare a single array
30  template<typename nT>
31  void declare(nT &&a_name);
32 
33  // add, return the index of the new entry.
34  // or -1 if the array name already exists.
35  int append(p_teca_table array);
36  int append(const std::string &name, p_teca_table array);
37 
38  // set, return 0 on success.
39  int set(unsigned int i, p_teca_table array);
40  int set(const std::string &name, p_teca_table array);
41 
42  // remove
43  int remove(unsigned int i);
44  int remove(const std::string &name);
45 
46  // number of
47  unsigned int size() const noexcept
48  { return m_tables.size(); }
49 
50  // access by id
51  p_teca_table get(unsigned int i)
52  { return m_tables[i]; }
53 
54  const_p_teca_table get(unsigned int i) const
55  { return m_tables[i]; }
56 
57  // test for array
58  bool has(const std::string &name) const;
59 
60  // access by name
61  p_teca_table get(const std::string &name);
62  const_p_teca_table get(const std::string &name) const;
63 
64  p_teca_table operator[](const std::string &name)
65  { return this->get(name); }
66 
67  const_p_teca_table operator[](const std::string &name) const
68  { return this->get(name); }
69 
70  // access names
71  std::string &get_name(unsigned int i)
72  { return m_names[i]; }
73 
74  const std::string &get_name(unsigned int i) const
75  { return m_names[i]; }
76 
77  // return a unique string identifier
78  std::string get_class_name() const
79  { return "teca_table_collection"; }
80 
81  // return an integer identifier uniquely naming the dataset type
82  int get_type_code() const
83  { return -1; }
84 
85  // copy
86  void copy(const const_p_teca_table_collection &other);
87  void shallow_copy(const p_teca_table_collection &other);
88 
89  // swap
90  void swap(p_teca_table_collection &other);
91 
92  // serialize the data to/from the given stream
93  // for I/O or communication
94  int to_stream(teca_binary_stream &s) const;
95  int from_stream(teca_binary_stream &s);
96 
97  // stream to/from human readable representation
98  int to_stream(std::ostream &) const;
99  int from_stream(std::istream &) { return -1; }
100 
101 protected:
102  teca_table_collection() = default;
104  teca_table_collection(const teca_table_collection &&) = delete;
105  void operator=(const teca_table_collection &) = delete;
106  void operator=(const teca_table_collection &&) = delete;
107  void declare_set(){}
108 
109 private:
110  using name_vector_t = std::vector<std::string>;
111  using array_vector_t = std::vector<p_teca_table>;
112  using name_array_map_t = std::map<std::string,unsigned int>;
113 
114  name_vector_t m_names;
115  array_vector_t m_tables;
116  name_array_map_t m_name_array_map;
117 };
118 
119 // --------------------------------------------------------------------------
120 template<typename nT, typename... oT>
121 void teca_table_collection::declare_set(nT &&a_name, oT &&... args)
122 {
123  this->declare(std::forward<nT>(a_name));
124  this->declare_set(args...);
125 }
126 
127 // --------------------------------------------------------------------------
128 template<typename nT>
129 void teca_table_collection::declare(nT &&a_name)
130 {
131  unsigned int id = m_tables.size();
132  m_names.emplace_back(std::forward<nT>(a_name));
133  m_tables.emplace_back(teca_table::New());
134  m_name_array_map.emplace(std::forward<nT>(a_name), id);
135 }
136 
137 #endif
teca_binary_stream
Definition: teca_binary_stream.h:16
teca_table_collection
Definition: teca_table_collection.h:15