Solved

[C++] Macro for set/get methods

Posted on 2014-09-25
7
201 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
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!

 

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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TCP/IP Socket - connection close results in data lost 14 152
Compile GLUT with Visual Studio 2015 1 206
Find Visual Studio Tools 2 123
How can i compile this github project?? 2 99
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

733 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