MAST3
Multidisciplinary-design Adaptation and Sensitivity Toolkit (MAST)
parameter_data.hpp
Go to the documentation of this file.
1 /*
2 * MAST: Multidisciplinary-design Adaptation and Sensitivity Toolkit
3 * Copyright (C) 2013-2020 Manav Bhatia and MAST authors
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 #ifndef __mast_parameter_data_h__
21 #define __mast_parameter_data_h__
22 
23 // MAST includes
25 #include <mast/base/exceptions.hpp>
26 
27 
28 namespace MAST {
29 namespace Base {
30 
32 
33 public:
34 
36 
37 
38  virtual ~ParameterData() { }
39 
40 
41  template <typename T>
42  inline T&
43  add(const std::string& nm) {
44 
45  return _add_to_map<T>(nm, _get_map(T()));
46  }
47 
48 
49 
50  template <typename T>
51  inline T get(const std::string& nm) const {
52 
53  return _get_from_map<T>(nm, _get_map(T()));
54  }
55 
56 
57 private:
58 
59 
60  inline std::map<const std::string, int_t>&
61  _get_map(int v) {
62 
63  return _int_data;
64  }
65 
66 
67  inline std::map<const std::string, real_t>&
69 
70  return _real_data;
71  }
72 
73 
74  inline const std::map<const std::string, int_t>&
75  _get_map(int v) const {
76 
77  return _int_data;
78  }
79 
80 
81  inline const std::map<const std::string, real_t>&
82  _get_map(real_t v) const {
83 
84  return _real_data;
85  }
86 
87 
88 
89  template <typename T>
90  inline T& _add_to_map(const std::string &nm,
91  std::map<const std::string, T> &m) {
92 
93  typename std::map<const std::string, T>::iterator
94  it = m.find(nm);
95 
96  Assert0(it == m.end(), "Data already exists for name: " + nm);
97 
98  m[nm] = T();
99  return m[nm];
100  }
101 
102 
103  template <typename T>
104  inline T _get_from_map(const std::string &nm,
105  const std::map<const std::string, T> &m) const {
106 
107  typename std::map<const std::string, T>::const_iterator
108  it = m.find(nm);
109 
110  Assert0(it != m.end(), "Data does not exist for name: " + nm);
111 
112  return it->second;
113  }
114 
115 
116  std::map<const std::string, int_t> _int_data;
117  std::map<const std::string, real_t> _real_data;
118 };
119 
120 } // namespace Base
121 } // namespace MAST
122 
123 
124 #endif // __mast_parameter_data_h__
std::map< const std::string, real_t > & _get_map(real_t v)
T _get_from_map(const std::string &nm, const std::map< const std::string, T > &m) const
std::map< const std::string, int_t > & _get_map(int v)
std::map< const std::string, real_t > _real_data
const std::map< const std::string, int_t > & _get_map(int v) const
T & add(const std::string &nm)
const std::map< const std::string, real_t > & _get_map(real_t v) const
T & _add_to_map(const std::string &nm, std::map< const std::string, T > &m)
std::map< const std::string, int_t > _int_data
#define Assert0(cond, msg)
Definition: exceptions.hpp:134
double real_t