Solved

[C++] Macro for set/get methods

Posted on 2014-09-25
7
198 Views
Last Modified: 2014-10-15
I'm accessing a class XMLConfiguration  with a handful of get and set  methods.

    std::stringstream iss ( in );
    Poco::AutoPtr < Poco::Util::XMLConfiguration > ptrCfg
	    = new Poco::Util::XMLConfiguration ( iss );

//methods
	void setString(const std::string& key, const std::string& value);
	void setInt(const std::string& key, int value);
	void setDouble(const std::string& key, double value);
        // more ex: setBool

	double getDouble(const std::string& key) const;
	double getDouble(const std::string& key, double defaultValue) const;
	bool getBool(const std::string& key) const;		
	bool getBool(const std::string& key, bool defaultValue) const;
     //more

Open in new window


Need help (sample code) in creating macros such that I could do:
  SET ( key, value )
  double = GET ( key, value )
0
Comment
Question by:forums_mp
  • 3
  • 2
  • 2
7 Comments
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 40345408
you will have to use an XML parser to read and write XML files. There are quite a few out there and it is up for debate which is the best one.

Here are a couple I'm familiar with

http://xerces.apache.org/xerces-c/
http://libxmlplusplus.sourceforge.net/docs/manual/html/


And a few more which I haven't tried
http://rapidxml.sourceforge.net/
http://sourceforge.net/projects/tinyxml/

I suggest you pick the one most suited to your needs after writing out simple sample programs
0
 

Author Comment

by:forums_mp
ID: 40345685
I already have an XML parser.
0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 40346158
well then use it and write the function you want
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:forums_mp
ID: 40346388
I'm asking for sample code reflecting macros around set/get methods
0
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 40348239
If I understood your Q. correctly, then you can do it as follows.
 
  #define GET_DBL(key, value) value = getDouble(key);
  #define SET_DBL(key, value) setDouble(key, value);
 
  #define GET_STR(key, value) value = getString(key);
  #define SET_STR(key, value) setString(key, value);
 
  #define GET_INT(key, value) value = getDouble(key);
  #define SET_INT(key, value) setInt(key, value);
 
  ..... likewise
0
 
LVL 8

Accepted Solution

by:
Subrat (C++ windows/Linux) earned 500 total points
ID: 40348243
#define GET_DBL(key, value) value = obj.getDouble(key);

class Sample {
public:
  double getDouble(const std::string& key) const {
      return 2.5;
  }
};

Sample obj;

int _tmain(int argc, _TCHAR* argv[])
{
      double d = 0.0;
            GET_DBL("Key", d);
            std::cout << d;
      return 0;
}
######################################################
If functions are static, you can do as follows...
#define GET_DBL(key, value) value = Sample::getDouble(key);

class Sample {
public:
  static double getDouble(const std::string& key) const {
      return 2.5;
  }
};

int _tmain(int argc, _TCHAR* argv[])
{
      double d = 0.0;
            GET_DBL("Key", d);
            std::cout << d;
      return 0;
}
0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 40348523
I misunderstood your question as one concerning XML parsing.

Asking experts to write code without attempting it yourself is generally not the norm on EE
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question