Getter is used to read value.
Usually you should use Accessor instead of Getter.
accessorpp/getter.h
template <typename Type>
class Getter;
Type
: the underlying value type.
Getter();
Default constructor. Note: if the Type
is a reference, the default constructor won't compile.
template <typename U>
explicit Getter(const U * address);
When reading from the getter, the getter gets value from address
and cast the value to Type
.
Example code,
int value = 3;
accessorpp::Getter<int> getter(&value);
// output 3
std::cout << (int)getter << std::endl;
value = 5;
// output 5
std::cout << (int)getter << std::endl;
template <typename U, typename C>
Getter(const U C::* address, const C * instance);
When reading from the getter, the getter gets value from member field instance->*address
and cast the value to Type
.
Example code,
struct MyClass
{
int value;
};
MyClass instance{ 8 };
accessorpp::Getter<int> getter(&MyClass::value, &instance);
// output 8
std::cout << (int)getter << std::endl;
instance.value = 9;
// output 9
std::cout << (int)getter << std::endl;
template <typename U>
Getter(const U C::* address);
To read the value, call Getter::get(instance)
with the object instance.
Example code,
struct MyClass
{
int value;
};
MyClass instance{ 8 };
accessorpp::Getter<int> getter(&MyClass::value);
// output 8, can't use (int)getter here
std::cout << getter.get(&instance) << std::endl;
instance.value = 9;
// output 9
std::cout << getter.get(&instance) << std::endl;
Getter(const Type & value);
When reading from the getter, the getter always returns 'value', that's to say, the getter returns constant 'value'. Example code,
int n = 9;
// note n is passed by value
accessorpp::Getter<int> getter(n);
// output 9
std::cout << (int)getter << std::endl;
n = 8;
// still output 9
std::cout << (int)getter << std::endl;
template <typename F>
explicit Getter(F func);
'func' is a callable, such as function pointer, std::function, function object, or lambda. The prototype is Type ()
.
When reading from the getter, the getter returns func()
.
Example code,
int n = 9;
accessorpp::Getter<int> getter([&n]() -> int { return n++; });
// output 9
std::cout << (int)getter << std::endl;
// output 10
std::cout << (int)getter << std::endl;
template <typename F, typename C>
explicit Getter(F func, C * instance);
func
is a member function which prototype is Type ()
, instance
is the object pointer.
When reading from the getter, the getter returns instance->*func()
.
Example code,
struct MyClass
{
int getValue() const {
return value;
}
int value;
};
MyClass instance{ 18 };
accessorpp::Getter<int> getter(&MyClass::getValue, &instance);
// output 18
std::cout << (int)getter << std::endl;
instance.value = 19;
// output 19
std::cout << (int)getter << std::endl;
template <typename F>
explicit Getter(F func);
func
is a member function which prototype is Type ()
.
To read the value, call Getter::get(instance)
with the object instance.
Example code,
struct MyClass
{
int getValue() const {
return value;
}
int value;
};
MyClass instance{ 18 };
accessorpp::Getter<int> getter(&MyClass::getValue);
// output 18
std::cout << getter.get(&instance) << std::endl;
instance.value = 19;
// output 19
std::cout << getter.get(&instance) << std::endl;
Getter(const Getter & other);
Copy constructor.
Getter(Getter && other);
Move constructor.
Type get(const void * instance = nullptr) const;
operator Type() const;
Both returns the underlying value.
If the getter is a class member and instance is not passed in constructor, the instance
in the get
function must be a valid object instance. Otherwise, instance
can be nullptr.
template <typename Type>
std::ostream & operator << (std::ostream & stream, const Getter<Type> & getter);
Overloaded output stream operator.