Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

[C++] Macro for set/get methods

Posted on 2014-09-25
7
Medium Priority
?
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

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

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 9

Accepted Solution

by:
Subrat (C++ windows/Linux) earned 1500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

722 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