Cheetah Software  1.0
ControlParameterCollection Class Reference

#include <ControlParameters.h>

+ Collaboration diagram for ControlParameterCollection:

Public Member Functions

 ControlParameterCollection (const std::string &name)
 
void addParameter (ControlParameter *param, const std::string &name)
 
ControlParameterlookup (const std::string &name)
 
std::string printToIniString ()
 print all control parameters in the INI file format More...
 
std::string printToYamlString ()
 
bool checkIfAllSet ()
 are all the control parameters initialized? More...
 
void clearAllSet ()
 
void deleteAll ()
 

Public Attributes

std::map< std::string, ControlParameter * > _map
 

Private Attributes

std::string _name
 

Detailed Description

ControlParameterCollections contains a map of all the control parameters.

Definition at line 71 of file ControlParameters.h.

Constructor & Destructor Documentation

ControlParameterCollection::ControlParameterCollection ( const std::string &  name)
inlineexplicit

Definition at line 73 of file ControlParameters.h.

73 : _name(name) {}

Member Function Documentation

void ControlParameterCollection::addParameter ( ControlParameter param,
const std::string &  name 
)
inline

Use this to add a parameter for the first time in the RobotControlParameters or SimulatorControlParameters This should only be used during initialization of a ControlParameter

Definition at line 80 of file ControlParameters.h.

References mapContains().

80  {
81  if (mapContains(_map, name)) {
82  printf(
83  "[ERROR] ControlParameterCollection %s: tried to add parameter %s "
84  "twice!\n",
85  _name.c_str(), name.c_str());
86  throw std::runtime_error("control parameter error");
87  }
88  _map[name] = param;
89  }
std::map< std::string, ControlParameter * > _map
bool mapContains(const std::map< T1, T2 > &set, T1 key)
Definition: utilities.h:120

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool ControlParameterCollection::checkIfAllSet ( )

are all the control parameters initialized?

Definition at line 67 of file ControlParameters.cpp.

References _map.

67  {
68  for (auto& kv : _map) {
69  if (!kv.second->_set) {
70  return false;
71  }
72  }
73  return true;
74 }
std::map< std::string, ControlParameter * > _map

+ Here is the caller graph for this function:

void ControlParameterCollection::clearAllSet ( )

Definition at line 76 of file ControlParameters.cpp.

References _map.

76  {
77  for (auto& kv : _map) {
78  kv.second->_set = false;
79  }
80 }
std::map< std::string, ControlParameter * > _map

+ Here is the caller graph for this function:

void ControlParameterCollection::deleteAll ( )

Definition at line 82 of file ControlParameters.cpp.

References _map.

82  {
83  for(auto& kv : _map) {
84  delete kv.second;
85  }
86  _map.clear();
87 }
std::map< std::string, ControlParameter * > _map

+ Here is the caller graph for this function:

ControlParameter& ControlParameterCollection::lookup ( const std::string &  name)
inline

Lookup a control parameter by its name. This does not modify the set field of the control parameter!

Definition at line 95 of file ControlParameters.h.

References mapContains().

95  {
96  if (mapContains(_map, name)) {
97  return *_map[name];
98  } else {
99  // for now:
100  throw std::runtime_error("parameter " + name +
101  " wasn't found in parameter collection " +
102  _name);
103  }
104  }
std::map< std::string, ControlParameter * > _map
bool mapContains(const std::map< T1, T2 > &set, T1 key)
Definition: utilities.h:120

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::string ControlParameterCollection::printToIniString ( )

print all control parameters in the INI file format

Definition at line 100 of file ControlParameters.cpp.

References _map, _name, and getCurrentTimeAndDate().

100  {
101  std::string result = ";; Generated on " + getCurrentTimeAndDate() + "\n";
102 
103  // ini section
104  result += "[";
105  result += _name + "]\n\n";
106 
107  std::vector<std::string> lines;
108 
109  // names (and max name length)
110  int maxLength_name = 0;
111  for (auto& kv : _map) {
112  maxLength_name = std::max(maxLength_name, (int)kv.first.length());
113  lines.push_back(kv.first);
114  }
115 
116  // pad, equals sign, and number
117  size_t i = 0;
118  int maxLength_number = 0;
119  for (auto& kv : _map) {
120  int charsToAdd = maxLength_name - (int)lines[i].length();
121  assert(charsToAdd >= 0);
122  for (int j = 0; j < charsToAdd; j++) {
123  lines[i].push_back(' ');
124  }
125  lines[i] += " = ";
126  lines[i] += kv.second->toString();
127  maxLength_number = std::max(maxLength_number, (int)lines[i].length());
128  i++;
129  }
130 
131  // pad, ;;, and units
132  i = 0;
133  for (auto& kv : _map) {
134  int charsToAdd = maxLength_number - (int)lines[i].length();
135  assert(charsToAdd >= 0);
136  for (int j = 0; j < charsToAdd; j++) {
137  lines[i].push_back(' ');
138  }
139  lines[i] += " ;; ";
140  if (kv.second->_units.empty()) {
141  lines[i] += "No units specified. Add them!";
142  } else {
143  lines[i] += kv.second->_units;
144  }
145 
146  i++;
147  }
148 
149  // combine lines
150  for (auto& line : lines) {
151  result += line + "\n";
152  }
153 
154  return result;
155 }
std::string getCurrentTimeAndDate()
Definition: utilities.cpp:18
std::map< std::string, ControlParameter * > _map

+ Here is the call graph for this function:

std::string ControlParameterCollection::printToYamlString ( )

print all control parameters in the YAML file format

Definition at line 157 of file ControlParameters.cpp.

References _map, _name, getCurrentTimeAndDate(), and YAML_COLLECTION_NAME_KEY.

157  {
158  std::string result = "# Generated on " + getCurrentTimeAndDate() + "\n";
159 
160  result += YAML_COLLECTION_NAME_KEY;
161  result += ": ";
162  result += _name + "\n\n";
163 
164  std::vector<std::string> lines;
165 
166  // names
167  int maxLength_name = 0;
168  for (auto& kv : _map) {
169  maxLength_name = std::max(maxLength_name, (int)kv.first.length());
170  lines.push_back(kv.first);
171  }
172 
173  // name pad, :, and number
174  size_t i = 0;
175  int maxLength_number = 0;
176  for (auto& kv : _map) {
177  int charsToAdd = maxLength_name - (int)lines[i].length();
178  assert(charsToAdd >= 0);
179  for (int j = 0; j < charsToAdd; j++) {
180  lines[i].push_back(' ');
181  }
182  lines[i] += ": ";
183  lines[i] += kv.second->toString();
184  maxLength_number = std::max(maxLength_number, (int)lines[i].length());
185  i++;
186  }
187 
188  // combine lines
189  for (auto& line : lines) {
190  result += line + "\n";
191  }
192 
193  return result;
194 }
std::string getCurrentTimeAndDate()
Definition: utilities.cpp:18
std::map< std::string, ControlParameter * > _map
#define YAML_COLLECTION_NAME_KEY

+ Here is the call graph for this function:

Member Data Documentation

std::map<std::string, ControlParameter*> ControlParameterCollection::_map

Definition at line 114 of file ControlParameters.h.

std::string ControlParameterCollection::_name
private

Definition at line 117 of file ControlParameters.h.


The documentation for this class was generated from the following files: