Get set property macros for C++

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Get set property macros for C++

Post 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
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Post Reply