torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ParameterMap.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 #ifndef TORC_GENERIC_PARAMETERMAP_HPP
17 #define TORC_GENERIC_PARAMETERMAP_HPP
18 
20 #include <string>
21 
22 //BOOST
23 #include <boost/shared_ptr.hpp>
24 
26 #include "torc/generic/SymTab.hpp"
27 
28 namespace torc { namespace generic { class Parameter; } }
29 
30 namespace torc {
31 namespace generic {
32 
33 /**
34  * Stores Name-Parameter mappings. This map can be used to get and set values for different
35  * parameters.
36  */
37 class ParameterMap {
38 public:
40  typedef boost::shared_ptr<TabParams> TabParamsPtr;
41  /**
42  * Get a named parameter or null if none exists
43  * @return The parameter
44  */
45  ParameterSharedPtr get(const ParameterContext& inContext, const std::string& inName) const;
46 
47  /**
48  * Saves a parameter by name. Overrides existing parameters if it exists.
49  * @param[in] inContext the context for which the param is valid
50  * @param[in] inName name of param
51  * @param[in] inParam pointer to the parameter
52  */
53  void set(const ParameterContext& inContext, const std::string& inName,
54  const ParameterSharedPtr& inParam);
55 
56  /**
57  * Get all parameter name-value-pairs
58  * @param[in] inContext the context for which the param is valid
59  * @param[out] outParams Map to be populated
60  */
61  void getAllParameters(const ParameterContext& inContext,
62  std::map<std::string, ParameterSharedPtr>& outParams) const;
63 
64  /**
65  * Get all parameter name-value-pairs that have been overridden in this context. For a view
66  * context this will return an empty map.
67  * @param[in] inContext the context for which the param is valid
68  * @param[out] outParams Map to be populated
69  */
70  void getOverriddenParameters(const ParameterContext& inContext,
71  std::map<std::string, ParameterSharedPtr>& outParams) const;
72 
73  /**
74  * Apply action on all parameters.
75  * @param[in] inContext the context for which the param is valid
76  * @param[in] action Action to be applied
77  *
78  */
79  template <typename _Action> inline void applyOnAllParameters(const ParameterContext& inContext,
80  const _Action& action) throw (Error);
81 
82  /**
83  * Apply action on overridden parameters
84  * @param[in] inContext the context for which the param is valid
85  * @param[in] action Action to be applied
86  *
87  */
88  template <typename _Action> inline void applyOnOverriddenParameters(
89  const ParameterContext& inContext, const _Action& action) throw (Error);
90 
92 
93  void registerContext(const ParameterContext& inContext,
94  const ParameterContext& inParentContext = ParameterContext()) throw (Error);
95 
96  bool isContextRegistered(const ParameterContext& inContext) const;
97 
98  void unregisterContext(const ParameterContext& inContext);
99 
100  ParameterMap();
101 
102  ~ParameterMap() throw ();
103 
104  ParameterMap(const ParameterMap& source);
105 
106  ParameterMap& operator=(const ParameterMap& source);
107 
108 private:
109  struct ParamData {
112  ParamData() : mParams(), mParentContext() {
113  }
114  };
115  typedef boost::shared_ptr<ParamData> ParamDataPtr;
118 };
119 
120 template <typename _Action> inline void ParameterMap::applyOnAllParameters(
121  const ParameterContext& inContext, const _Action& action) throw (Error) {
122  if(!inContext) {
123  return;
124  }
125  try {
126  ParamDataPtr data;
127  mParameters.get(inContext, data);
128  if(!data) {
129  return;
130  } else {
131  data->mParams.applyOnAll(action);
132  applyOnAllParameters(data->mParentContext, action);
133  }
134  } catch(Error& e) {
135  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
136  throw;
137  }
138 }
139 
140 template <typename _Action> inline void ParameterMap::applyOnOverriddenParameters(
141  const ParameterContext& inContext, const _Action& action) throw (Error) {
142  if(!inContext) {
143  return;
144  }
145  try {
146  ParamDataPtr data;
147  mParameters.get(inContext, data);
148  if(!data) {
149  return;
150  } else {
151  data->mParams.applyOnAll(action);
152  }
153  } catch(Error& e) {
154  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
155  throw;
156  }
157 }
158 
159 } // namespace generic
160 } // namespace torc
161 
162 #endif // TORC_GENERIC_PARAMETERMAP_HPP
void applyOnAllParameters(const ParameterContext &inContext, const _Action &action)
boost::shared_ptr< TabParams > TabParamsPtr
boost::shared_ptr< Parameter > ParameterSharedPtr
void registerContext(const ParameterContext &inContext, const ParameterContext &inParentContext=ParameterContext())
std::string string
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
ParameterContext getNewContext()
SymTab< ParameterContext, ParamDataPtr > mParameters
void set(const ParameterContext &inContext, const std::string &inName, const ParameterSharedPtr &inParam)
void applyOnOverriddenParameters(const ParameterContext &inContext, const _Action &action)
void unregisterContext(const ParameterContext &inContext)
void getAllParameters(const ParameterContext &inContext, std::map< std::string, ParameterSharedPtr > &outParams) const
boost::shared_ptr< ParamData > ParamDataPtr
void getOverriddenParameters(const ParameterContext &inContext, std::map< std::string, ParameterSharedPtr > &outParams) const
SymTab< std::string, ParameterSharedPtr, true > TabParams
bool isContextRegistered(const ParameterContext &inContext) const
ParameterContext mNextContext
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73