Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

128 lines (106 sloc) 3.028 kb
// Copyright (C) 2012 Tomasz Dudziak
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#pragma once
namespace properties {
template<typename T, typename TClass>
class ValueProxy
{
inline TClass& obj() {
return *static_cast<TClass*>(this);
}
public:
#define _PROPERTIES_DECLARE_ASOP(op) \
inline TClass& operator op##= (const T& x) { \
return obj() = obj() op x; \
}
_PROPERTIES_DECLARE_ASOP(+)
_PROPERTIES_DECLARE_ASOP(-)
_PROPERTIES_DECLARE_ASOP(*)
_PROPERTIES_DECLARE_ASOP(/)
_PROPERTIES_DECLARE_ASOP(%)
_PROPERTIES_DECLARE_ASOP(&)
_PROPERTIES_DECLARE_ASOP(|)
_PROPERTIES_DECLARE_ASOP(^)
_PROPERTIES_DECLARE_ASOP(<<)
_PROPERTIES_DECLARE_ASOP(>>)
inline TClass& operator++() {
T value = T(obj());
obj() = ++value;
return obj();
}
inline T operator++(int) {
T value = T(obj());
T copy = value;
value++;
obj() = value;
return copy;
}
inline TClass& operator--() {
T value = T(obj());
obj() = --value;
return obj();
}
inline T operator--(int) {
T value = T(obj());
T copy = value;
value--;
obj() = value;
return copy;
}
inline T operator->() {
return T(obj());
}
};
template<
typename T,
typename Object,
T (Object::* getter)() const,
void (Object::* setter)(T)
>
class GetSet : public ValueProxy<T, GetSet<T, Object, getter, setter> >
{
Object* const m_owner;
// FIXME: Is there a way to avoid making this noncopyable?
GetSet& operator=(const GetSet&);
GetSet(const GetSet&);
public:
GetSet(Object* owner)
: m_owner(owner)
{}
inline operator T() const { return (m_owner->*getter)(); }
inline GetSet& operator=(T new_value) {
(m_owner->*setter)(new_value);
return *this;
}
};
template<typename T>
class Fieldlike : public ValueProxy<T, Fieldlike<T> >
{
T m_value;
public:
Fieldlike() : m_value() {}
Fieldlike(T init) : m_value(init) {}
inline operator T() const { return m_value; }
inline Fieldlike& operator=(T new_value) {
m_value = new_value;
return *this;
}
};
}
Jump to Line
Something went wrong with that request. Please try again.