Skip to content

Instantly share code, notes, and snippets.

@milan11
Created April 18, 2013 22:27
Show Gist options
  • Save milan11/5416729 to your computer and use it in GitHub Desktop.
Save milan11/5416729 to your computer and use it in GitHub Desktop.
macros for defining getters, setters and constructor arguments
#pragma once
#include <boost/config.hpp>
#if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
#define CONSTRUCTOR_PARAM(type, name) \
const type & name
#define TO_FIELD(name) \
name(name)
#else
#define CONSTRUCTOR_PARAM(type, name) \
type name
#define TO_FIELD(name) \
name(std::move(name))
#endif
#define GETTER(type, name) \
public: \
const type & get_##name() const { \
return name; \
}
#if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
#define SETTER(type, name) \
public: \
auto set_##name(const type & name) \
-> decltype(*this) { \
this->name = name; \
return *this; \
}
#else
#define SETTER(type, name) \
public: \
auto set_##name(type name) \
-> decltype(*this) { \
this->name = std::move(name); \
return *this; \
}
#endif
#define FIELD(type, name) \
private: \
type name;
#define FIELD_GET(type, name) \
FIELD(type, name) \
GETTER(type, name)
#define FIELD_SET(type, name) \
FIELD(type, name) \
SETTER(type, name)
#define FIELD_GET_SET(type, name) \
FIELD(type, name) \
GETTER(type, name) \
SETTER(type, name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment