Solved

[C++] Macro for set/get methods

Posted on 2014-09-25
7
199 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
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.

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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
Need some help with listview limit text 20 109
gdb doesn't stop on breakpoint 2 92
designing in object programming 12 94
Where are the c++ header files to use in Eclipse? 4 24
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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

808 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