/* * File: Reference.h * Author: jgaebler * * Created on January 30, 2014, 7:39 PM */ #pragma once #ifndef REFERENCE_H #define REFERENCE_H #include namespace ubeeme { namespace moversight { /** * @brief Provides a basic reference implementation. * @class Reference * @ingroup Moversight * @author Jan Gäbler */ template class Reference : public Serializable { template friend std::ostream & operator<<(std::ostream & stream, const Reference & ref); public: Reference(); Reference(const T & value); Reference(const Reference & orig); virtual ~Reference(); Reference & operator=(const Reference & rhs); Reference operator+(const unsigned int & rhs); Reference operator+(const Reference & rhs); Reference operator-(const Reference & rhs); Reference operator*(const unsigned int & rhs); bool operator==(const Reference & rhs) const; bool operator!=(const Reference & rhs) const; bool operator<(const Reference & rhs) const; bool operator>(const Reference & rhs) const; Reference & operator++(); //++a Reference operator++(int); //a++ Reference & operator--(); //--a Reference operator--(int); //a-- void set(Archive &archive); virtual T getValue() const; protected: T value; }; /** * @brief Constructor */ template Reference::Reference() : value(0) { } /** * @brief Constructor * @param aValue The initial reference value. */ template Reference::Reference(const T & aValue) : value(aValue) { } /** * @brief Constructor * @param orig The instance to copy */ template Reference::Reference(const Reference & orig) : Serializable(orig) { operator =(orig); } /** * @brief Destructor */ template Reference::~Reference() { } /** * @brief Returns a integer representation of the current reference instance. * @return A integer representation of the current reference instance. */ template T Reference::getValue() const { return value; } /** * @brief Archive method. * @param archive The archive to which the attributes are to add. */ template void Reference::set(Archive &archive) { archive(value); } /** * @brief Addition operation. * @param rhs the value to add * @returns The result of the addition. */ template Reference Reference::operator+(const unsigned int & rhs) { return Reference(value + rhs); } /** * @brief Addition operation. * @param rhs the value to add * @returns The result of the addition. */ template Reference Reference::operator+(const Reference & rhs) { return Reference(value + rhs.value); } /** * @brief Subtraction operation. * @param rhs the value to add * @returns The result of the subtraction. */ template Reference Reference::operator-(const Reference & rhs) { return Reference(value - rhs.value); } /** * @brief Multiplication operation. * @param rhs the factor to multiply with * @returns The result of the multiplication. */ template Reference Reference::operator*(const unsigned int & rhs) { return Reference(value * rhs); } /** * @brief Assignment operator. * @param rhs The right hand value. * @return A reference to the new created object. */ template Reference & Reference::operator=(const Reference & rhs) { if (this == &rhs) { return *this; } Serializable::operator =(rhs); value = rhs.value; return *this; } /** * @brief Less operator. * @param rhs The value to compare. * @return True, if the local value less than the given one, false otherwise. */ template bool Reference::operator<(const Reference & rhs) const { return value < rhs.value; } /** * @brief Greater operator. * @param rhs The value to compare. * @return True, if the local value greater than the given one, false otherwise. */ template bool Reference::operator>(const Reference & rhs) const { return value > rhs.value; } /** * @brief Equality operator. * @param o The object to compare. * @return True, if the given reference the same than the local, false otherwise. */ template bool Reference::operator==(const Reference & o) const { return value == o.value; } /** * @brief Not equal operator. * @param rhs The value to compare. * @return True, if the local reference not equal to the given, false otherwise. */ template bool Reference::operator!=(const Reference & rhs) const { return value != rhs.value; } /** * @brief Increments the reference (++a). * @return A reference to the local instance. */ template Reference & Reference::operator++() { value++; return *this; } /** * @brief Increments the reference (a++). * @return The result of the operation. */ template Reference Reference::operator++(int) { Reference result(*this); ++(*this); return result; } /** * @brief Decrements the reference (--a). * @return A reference to the local instance. */ template Reference & Reference::operator--() { value--; return *this; } /** * @brief Decrements the reference (a--). * @return The result of the operation. */ template Reference Reference::operator--(int) { Reference result(*this); --(*this); return result; } /** * @brief Output operator. Prints a textual representation of the current reference. * @param stream The output stream to print the textual representation. * @param mRef The Reference to print. */ template std::ostream & operator<<(std::ostream& stream, const Reference & ref) { return stream << ref.value; } } } #endif /* REFERENCE_H */