A parameter processing class

Source: Internet
Author: User

In recent projects, using the plug-in architecture, each feature of the plug-in requires user input parameters (the required parameter values are configured in the XML file), and the previous practice is to define all the parameters required for each function as structs
At the beginning of the program, the XML file is parsed, and the parameter values of the corresponding names are populated into the corresponding fields. If you have a large number of arguments, there will be a lot of fields in the parameter structure, which can be cumbersome to write, and when
When a module is added to a new parameter (added in an XML file), the parameter structure must be modified to be detrimental to the program extension.
So write a parameter class that stores all parameters in the XML as parameter name (string) key value pairs in the map, when a parameter is required, by parameter name
Extract (without distinguishing the case of the parameter name), the user controls what form (integer, float, etc.) is resolved to the parameter, and when a new parameter is added, only the XML can be added according to certain rules (parameter value) without modifying the parameter class. Use the time to directly extract by name.

The records are as follows:

The parameter configuration XML file format is simplified to:

<params><item name= "Networktype" value= "UMTS" ></item><item name= "Gridcntthreshold" Value= "100 "></Item></Params>

The parameter classes are as follows:

Param.h

#ifndef param_h_#define param_h_#include <map> #include <string> #include <algorithm> #include < sstream>using std::map;using std::string;using std::stringstream;//Parameter classes class Cparam{public:struct NoCaseCompare{ BOOL Operator () (const string & str1, const string & str2) {string upper_str1 (str1); string upper_str2 (STR2); std:: Transform (Upper_str1.begin (), Upper_str1.end (), Upper_str1.begin (), ToUpper), Std::transform (Upper_str2.begin (), Upper_str2.end (), Upper_str2.begin (), toupper); return Upper_str1.compare (UPPER_STR2) >= 0? False:true;}}; typedef map<string,string,nocasecompare> STRMAP; Cparam (); virtual ~cparam ();//Set parameter value template <typename t>void setparam (const string & param_name, const T & P Aram_val);//Get double type argument double getparamdouble (const string & param_name)//get integer parameter value int getparamint (const string & Param_name);//Get string parameter value string getparamstring (const string & param_name);//get bool type parameter value bool Getparambool (const String & Param_name);//Load parameterA number of configuration files that store bool Loadparamxml (const string & Param_file_path) in the form of parameter name (string), all parameters, and/or clear all parameters void clear ( );p rivate:const char* findparam (const string & param_name);p rivate:strmap _params;}; Template <typename t>void cparam::setparam (const string & param_name, const T & param_val) {StringStream ss; SS << Param_val;_params[param_name] = Ss.str ();} #endif

Param.cpp

#define Tixml_use_stl#include "param.h" #include <tinyxml.h>cparam::cparam () {}cparam::~cparam () {}double Cparam::getparamdouble (const string & param_name) {Double val = 0;const char * pStr = This->findparam (param_name); if (NULL! = pStr) {val = atof (PSTR);} return Val;} const char * CPARAM::FINDPARAM (const string & param_name) {Strmap::const_iterator it = _params.find (param_name); It! = _params.end ()) {return it->second.c_str ();} return NULL;}  int Cparam::getparamint (const string & param_name) {int val = 0;const char * pStr = Findparam (param_name); if (NULL! = PSTR) {val = atoi (PSTR);} return Val;} String cparam::getparamstring (const string & param_name) {Strmap::const_iterator it = _params.find (param_name); It! = _params.end ()) {return it->second;} Return "";} BOOL Cparam::getparambool (const string & param_name) {bool val = false;const char * pStr = Findparam (param_name); if ( NULL! = pStr) {string str (PSTR); Std::transform (Str.begin (), Str.end (), Str.begin (), Tolower); if (str = = "T" | | str = = "true" | | atoi (STR.C_STR ()) > 0) {val = true;}} return Val;} BOOL Cparam::loadparamxml (const string & Param_file_path) {//Read all parameters from the parameter configuration file Tixmldocument doc;if (!doc. LoadFile (Param_file_path)) {return false;} Tixmlhandle handle (&doc); Tixmlelement *pchild = handle. FirstChild ("Params"). FirstChild ("Item"). Element (); for (; NULL! = pchild; pchild = Pchild->nextsiblingelement ()) {string name;string val;pchild->querystringattribute ("name", &name) ;p Child->querystringattribute ("Value", &val); This->setparam (name,val);} return true;} void Cparam::clear () {_params.clear ();}

The test code is as follows:

Cparam Param;param. Loadparamxml ("D:\\test\\param.xml"); cout << Param. Getparamdouble ("Gridcntthreshold") << endl;cout << param. Getparamstring ("Networktype") << Endl;

A parameter processing class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.