Skip to content

Instantly share code, notes, and snippets.

@BreadFish64
Last active March 10, 2018 03:11
Show Gist options
  • Save BreadFish64/4bc03a94bd4ae10fb9a0c992235f4bae to your computer and use it in GitHub Desktop.
Save BreadFish64/4bc03a94bd4ae10fb9a0c992235f4bae to your computer and use it in GitHub Desktop.
#include <memory>
template <typename T>
class ValueGetter {
public:
constexpr explicit ValueGetter(const T& value) noexcept : m_value{value} {
}
constexpr const T& get() const {
return m_value;
}
private:
const T& m_value;
};
template <typename T>
class ValueSetter {
public:
constexpr explicit ValueSetter(T& value) noexcept : m_value{value} {
}
constexpr void set(T value) {
m_value = value;
}
private:
T& m_value;
};
template <typename T>
static std::unique_ptr<ValueGetter<T>> MakeGetter(const T& value) {
return std::make_unique<ValueGetter<T>>(value);
}
template <typename T>
static std::unique_ptr<ValueSetter<T>> MakeSetter(T& value) {
return std::make_unique<ValueSetter<T>>(value);
}
class BooleanFlipper {
public:
explicit BooleanFlipper(bool& val) : m_getter{MakeGetter<bool>(val)}, m_setter{MakeSetter<bool>(val)} {
}
bool flip() {
if (m_getter->get()) {
m_setter->set(false);
} else {
m_setter->set(true);
}
return m_getter->get();
}
private:
std::unique_ptr<ValueGetter<bool>> m_getter;
std::unique_ptr<ValueSetter<bool>> m_setter;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment