Solved

[C++] Macro for set/get methods

Posted on 2014-09-25
7
196 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now