Changeset 176 in pacpusframework for trunk/src/PacpusLib/ComponentBase.cpp
- Timestamp:
- Oct 11, 2013, 2:10:06 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/PacpusLib/ComponentBase.cpp
r152 r176 9 9 #include <Pacpus/kernel/Log.h> 10 10 11 #include <boost/program_options/parsers.hpp> 12 #include <boost/program_options/variables_map.hpp> 13 #include <string> 14 #include <vector> 15 16 namespace po = boost::program_options; 11 17 using namespace pacpus; 18 using namespace std; 19 20 std::vector<std::string> convertAttributesToArgumentVector(const QDomNamedNodeMap & attributes); 21 void logVariablesMap(boost::program_options::variables_map vm); 12 22 13 23 DECLARE_STATIC_LOGGER("pacpus.core.ComponentBase"); … … 20 30 , m_ui(NULL) 21 31 , m_componentState(NOT_MONITORED) 32 , mOptionsDescription("Component parameters") 22 33 { 23 34 LOG_TRACE("constructor"); 24 35 // Get a pointer on the instance of ComponentManager. 25 36 m_manager = ComponentManager::getInstance(); 26 LOG_INFO("component " << name() << " was created"); 37 LOG_INFO("component " << getName() << " was created"); 38 39 addParameters() 40 ("name", po::value<string>(&mName)->required(), "component name") 41 ("type", po::value<string>(&mTypeName)->required(), "component type") 42 ("ui", po::value<bool>(&mHasGui)->default_value(false), "whether to show GUI") 43 ("verbose", po::value<bool>(&mVerbose)->default_value(false), "set output verbose") 44 ("verbosity-level", po::value<int>(&mVerbosityLevel)->default_value(0), "set verbosity level") 45 ("recording", po::value<bool>(&m_isRecording)->default_value(false), "whether to record data") 46 ; 27 47 } 28 48 … … 113 133 } 114 134 115 QString ComponentBase::name() const116 {117 return m_componentName;118 }119 120 135 QString ComponentBase::getName() const 121 136 { … … 148 163 return inputs()[inputName]; 149 164 } 150 LOG_WARN("Component " << name() << " does not contain input " << inputName);165 LOG_WARN("Component " << getName() << " does not contain input " << inputName); 151 166 return NULL; 152 167 } … … 161 176 return outputs()[outputName]; 162 177 } 163 LOG_WARN("Component " << name() << " does not containt output " << outputName);178 LOG_WARN("Component " << getName() << " does not containt output " << outputName); 164 179 return NULL; 165 180 } 181 182 po::options_description_easy_init ComponentBase::addParameters() 183 { 184 return mOptionsDescription.add_options(); 185 } 186 187 void ComponentBase::parseParameters(const XmlComponentConfig & cfg) 188 { 189 LOG_INFO("Parsing parameters..."); 190 LOG_INFO(mOptionsDescription); 191 192 vector<string> xargs = convertAttributesToArgumentVector(cfg.getProperties()); 193 194 boost::program_options::variables_map vm; 195 try { 196 po::store( 197 po::command_line_parser(xargs) 198 .options(mOptionsDescription) 199 //.allow_unregistered() 200 .run() 201 , vm); 202 po::notify(vm); 203 } catch (po::error & e) { 204 LOG_ERROR(e.what()); 205 throw; 206 } 207 208 logVariablesMap(vm); 209 } 210 211 void logVariablesMap(boost::program_options::variables_map vm) 212 { 213 for (po::variables_map::iterator i = vm.begin(); i != vm.end(); ++i) { 214 const po::variable_value& v = i->second; 215 if (v.empty()) { 216 continue; 217 } 218 const type_info & type = v.value().type(); 219 if (type == typeid(string)) { 220 const string & val = v.as<string>(); 221 LOG_INFO(i->first << "=" << val); 222 } else if (type == typeid(int)) { 223 int val = v.as<int>(); 224 LOG_INFO(i->first << "=" << val); 225 } else if (type == typeid(bool)) { 226 int val = v.as<bool>(); 227 LOG_INFO(i->first << "=" << val); 228 } 229 } 230 } 231 232 vector<string> convertAttributesToArgumentVector(const QDomNamedNodeMap & attributes) 233 { 234 vector<string> xargs; 235 xargs.reserve(attributes.size()); 236 237 for (int i = 0; i < attributes.size(); ++i) { 238 QDomAttr parameter = attributes.item(i).toAttr(); 239 if (parameter.isNull()) { 240 LOG_WARN("node is not a parameter"); 241 continue; 242 } 243 244 QString arg = QString("--") + parameter.name() + "="; 245 246 bool shouldAddQuotes = parameter.value().contains(' '); 247 if (shouldAddQuotes) { 248 arg += '\"'; 249 arg += parameter.value(); 250 arg += '\"'; 251 } else { 252 arg += parameter.value(); 253 } 254 255 LOG_DEBUG("parameter: " << arg); 256 xargs.push_back(arg.toStdString()); 257 } 258 259 return xargs; 260 }
Note:
See TracChangeset
for help on using the changeset viewer.