Page 1 of 1

Get set property macros for C++

Posted: Fri Feb 10, 2006 7:23 pm
by Jorg
Hi all, here a set of handy GETSET macro's. For all people who are tired of writing property get set values like;

Code: Select all

const &SomeObj GetObject() {
    return SomeObj;
};

void SetObject(const SomeObject &obj) {
    m_obj = obj;
};
The following macro's will give;

- Get methods
- Get / Set methods
- Get methods by ref
- Set methods
- Guared variants of the above, meaning when a condition is not set (a bool or expression) the Get / Set method will raise an assertion.

Examples:

Code: Select all

class SomeClass 
{
public:
  GETSETR(wxString, m_name, MyName);
  SET(int, m_count, Count);

private:
  wxString m_name;
  int m_count;
}; 
Will expand in;

Code: Select all

class SomeClass 
{
public:
  const wxString &GetMyName() {
     return m_name;
  };

  void SetMyName(const wxString &value) {
    m_name = value;
  };  

  void SetCount(int value) {
    m_count = value;
  };

private:
  wxString m_name;
  int m_count;
}; 
So as you can see it will save a lot of code overhead. I am using it to great satisfaction for my own projects, reduces compile errors, stupid mistakes and makes my code a lot more readable.

Here is the dump:

Code: Select all

#ifndef __GETSET_DECL_
#define __GETSET_DECL_

#include "wx/wxprec.h"

#define GETSET(Type, MemberName, FaceName) \
        Type Get##FaceName() const { \
            return MemberName; \
		}; \
		void Set##FaceName(Type value) { \
            MemberName = value; \
		}

#define GETSETR(Type, MemberName, FaceName) \
		const Type &Get##FaceName() const { \
			return MemberName; \
		}; \
		void Set##FaceName(const Type &value) { \
			MemberName = value; \
		}

#define GET(Type, MemberName, FaceName) \
		Type Get##FaceName() const { \
			return MemberName; \
		}

#define GETR(Type, MemberName, FaceName) \
		const Type &Get##FaceName() const { \
			return MemberName; \
		}

#define GETRNC(Type, MemberName, FaceName) \
		Type &Get##FaceName() { \
			return MemberName; \
		}

#define SET(Type, MemberName, FaceName) \
        void Set##FaceName(const Type &value) { \
            MemberName = value; \
        }

// guarded versions

#define GETSETG(Type, MemberName, FaceName, GuardVal) \
        Type Get##FaceName() const { \
                wxASSERT(GuardVal); \
                return MemberName; \
		}; \
		void Set##FaceName(Type value) { \
            MemberName = value; \
		}

#define GETSETGR(Type, MemberName, FaceName, GuardVal) \
		const Type &Get##FaceName() const { \
			wxASSERT(GuardVal); \
			return MemberName; \
		}; \
		void Set##FaceName(const Type &value) { \
			MemberName = value; \
		}

#define GETG(Type, MemberName, FaceName, GuardVal) \
		Type Get##FaceName() const { \
			wxASSERT(GuardVal); \
            return MemberName; \
		}

#define GETGR(Type, MemberName, FaceName, GuardVal) \
		const Type &Get##FaceName() const { \
			wxASSERT(GuardVal); \
			return MemberName; \
		}

#define SETG(Type, MemberName, FaceName, GuardVal) \
        void Set##FaceName(const Type &value) { \
            wxASSERT(GuardVal); \
            MemberName = value; \
        }


#endif