Skip to main content
GameDev.net gamedev.net
🔒 Locked

Macro problem

Started by Irushian Aug 7, 2007 at 4:19 AM 4 replies 600+ views
Original Post
Irushian
Irushian
I'm attempting to use a macro to handle load variables into a class, however instead of using the variable directly what I have attempted to do is to call the function to set the variable within it, I have done a working one however I'm struggling to make it generic and I don't want to make a macro per class for it, could anyone give any advise on how to best achieve this?

   // The macro
   #define STRINGREAD( sKey, sFunc ) {                              if( CompareStr( strWord, sKey ) ) {                              ( MyClass1->*(sFunc) )( FileReadString( pFile ) );            bFound = true;                                             }                                                          }

   // To load a string and set i call
   STRINGREAD( "Name", &MyClass1::SetName );
So again in short, I'm wanting to use a macro but have it handle other classes aswell instead of just MyClass1.
Irushian
Irushian
Ah, the code tags ate my formatting, sorry about that =)
Captain_Thunder
Captain_Thunder
1) If you need to, you can have a macro span across multiple lines by putting a \ character at the end of each line.

2) It sounds like you need to substitute different names for MyClass1 in the macro. Remember that macros are just text substitution; you can make the class name a parameter to the macro and have it substituted in. Something like this:
#define STRINGREAD(sKey, sFunc, className) ... (className->*(sFunc)) ...
ToohrVyk
ToohrVyk
Your code:

// The macro#define STRINGREAD( sKey, sFunc ) {                    \   if( CompareStr( strWord, sKey ) ) {                  \     ( MyClass1->*(sFunc) )( FileReadString( pFile ) ); \     bFound = true;                                     \           }                                                    \ }


A C++ version:
template<typename Class>class StringReaderClass{  std::istream & file;  std::string word;  Class & myClass;public:  StringReader(    std::istream & file,    const std::string & word,    Class & myClass  ) : file(file), word(word), myClass(myClass) {}  template<typename Ret, typename Arg>  void operator()(    const std::string & key,     Ret (Class::*member)(Arg)) const  {    if (key == word) (myClass.*member)( FileReadString(file) );  }};template<typename Class>StringReaderClass<Class> StringReader(  std::istream & file,  const std::string & word,  Class & myClass){  return StringReaderClass<Class>(file,word,myClass);}


Single usage:
StringReader(file,"Name",myClass)("Name",&MyClass::SetName);


Multiple usage:
StringReaderClass<MyClass> stringRead(file,"Name",myClass);stringRead("Name",&MyClass::SetName);stringRead("Age",&MyClass::SetAge);stringRead("City",&MyClass::SetCity);


Irushian
Irushian
I did use \ to seperate them but when i pasted the code into the code tags it put the entire thing on one line hehe

Thank you very much guys!
SiCrane
SiCrane
The forum eats \ followed by a newline. It's a known bug. In order to get a trailing \ to display you need to put an extra space at the end.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.