295 lines
7.6 KiB
C++
295 lines
7.6 KiB
C++
/*
|
|
* File: Reference.h
|
|
* Author: jgaebler
|
|
*
|
|
* Created on January 30, 2014, 7:39 PM
|
|
*/
|
|
#pragma once
|
|
|
|
#ifndef REFERENCE_H
|
|
#define REFERENCE_H
|
|
|
|
#include <serializable.h>
|
|
|
|
namespace ubeeme {
|
|
namespace moversight {
|
|
|
|
/**
|
|
* @brief Provides a basic reference implementation.
|
|
* @class Reference
|
|
* @ingroup Moversight
|
|
* @author Jan Gäbler
|
|
*/
|
|
template <typename T>
|
|
class Reference : public Serializable {
|
|
template <typename S>
|
|
friend std::ostream & operator<<(std::ostream & stream, const Reference<S> & ref);
|
|
|
|
public:
|
|
|
|
Reference();
|
|
|
|
Reference(const T & value);
|
|
|
|
Reference(const Reference<T> & orig);
|
|
|
|
virtual ~Reference();
|
|
|
|
Reference<T> & operator=(const Reference<T> & rhs);
|
|
|
|
Reference<T> operator+(const unsigned int & rhs);
|
|
|
|
Reference<T> operator+(const Reference<T> & rhs);
|
|
|
|
Reference<T> operator-(const Reference<T> & rhs);
|
|
|
|
Reference<T> operator*(const unsigned int & rhs);
|
|
|
|
bool operator==(const Reference<T> & rhs) const;
|
|
|
|
bool operator!=(const Reference<T> & rhs) const;
|
|
|
|
bool operator<(const Reference<T> & rhs) const;
|
|
|
|
bool operator>(const Reference<T> & rhs) const;
|
|
|
|
|
|
Reference<T> & operator++(); //++a
|
|
Reference<T> operator++(int); //a++
|
|
Reference<T> & operator--(); //--a
|
|
Reference<T> operator--(int); //a--
|
|
|
|
void set(Archive &archive);
|
|
|
|
virtual T getValue() const;
|
|
|
|
protected:
|
|
T value;
|
|
};
|
|
|
|
/**
|
|
* @brief Constructor
|
|
*/
|
|
template <typename T>
|
|
Reference<T>::Reference() : value(0) {
|
|
}
|
|
|
|
/**
|
|
* @brief Constructor
|
|
* @param aValue The initial reference value.
|
|
*/
|
|
template <typename T>
|
|
Reference<T>::Reference(const T & aValue) : value(aValue) {
|
|
}
|
|
|
|
/**
|
|
* @brief Constructor
|
|
* @param orig The instance to copy
|
|
*/
|
|
template <typename T>
|
|
Reference<T>::Reference(const Reference<T> & orig) : Serializable(orig) {
|
|
operator =(orig);
|
|
}
|
|
|
|
/**
|
|
* @brief Destructor
|
|
*/
|
|
template <typename T>
|
|
Reference<T>::~Reference() {
|
|
}
|
|
|
|
/**
|
|
* @brief Returns a integer representation of the current reference instance.
|
|
* @return A integer representation of the current reference instance.
|
|
*/
|
|
template <typename T>
|
|
T
|
|
Reference<T>::getValue() const {
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* @brief Archive method.
|
|
* @param archive The archive to which the attributes are to add.
|
|
*/
|
|
template <typename T>
|
|
void
|
|
Reference<T>::set(Archive &archive) {
|
|
archive(value);
|
|
}
|
|
|
|
/**
|
|
* @brief Addition operation.
|
|
* @param rhs the value to add
|
|
* @returns The result of the addition.
|
|
*/
|
|
template <typename T>
|
|
Reference<T>
|
|
Reference<T>::operator+(const unsigned int & rhs) {
|
|
return Reference<T>(value + rhs);
|
|
}
|
|
|
|
/**
|
|
* @brief Addition operation.
|
|
* @param rhs the value to add
|
|
* @returns The result of the addition.
|
|
*/
|
|
template <typename T>
|
|
Reference<T>
|
|
Reference<T>::operator+(const Reference<T> & rhs) {
|
|
return Reference<T>(value + rhs.value);
|
|
}
|
|
|
|
/**
|
|
* @brief Subtraction operation.
|
|
* @param rhs the value to add
|
|
* @returns The result of the subtraction.
|
|
*/
|
|
template <typename T>
|
|
Reference<T>
|
|
Reference<T>::operator-(const Reference<T> & rhs) {
|
|
return Reference<T>(value - rhs.value);
|
|
}
|
|
|
|
/**
|
|
* @brief Multiplication operation.
|
|
* @param rhs the factor to multiply with
|
|
* @returns The result of the multiplication.
|
|
*/
|
|
template <typename T>
|
|
Reference<T>
|
|
Reference<T>::operator*(const unsigned int & rhs) {
|
|
return Reference<T>(value * rhs);
|
|
}
|
|
|
|
/**
|
|
* @brief Assignment operator.
|
|
* @param rhs The right hand value.
|
|
* @return A reference to the new created object.
|
|
*/
|
|
template <typename T>
|
|
Reference<T> &
|
|
Reference<T>::operator=(const Reference<T> & 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 <typename T>
|
|
bool
|
|
Reference<T>::operator<(const Reference<T> & 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 <typename T>
|
|
bool
|
|
Reference<T>::operator>(const Reference<T> & 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 <typename T>
|
|
bool
|
|
Reference<T>::operator==(const Reference<T> & 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 <typename T>
|
|
bool
|
|
Reference<T>::operator!=(const Reference<T> & rhs) const {
|
|
return value != rhs.value;
|
|
}
|
|
|
|
/**
|
|
* @brief Increments the reference (++a).
|
|
* @return A reference to the local instance.
|
|
*/
|
|
template <typename T>
|
|
Reference<T> &
|
|
Reference<T>::operator++() {
|
|
|
|
value++;
|
|
return *this;
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief Increments the reference (a++).
|
|
* @return The result of the operation.
|
|
*/
|
|
template <typename T>
|
|
Reference<T>
|
|
Reference<T>::operator++(int) {
|
|
|
|
Reference<T> result(*this);
|
|
++(*this);
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* @brief Decrements the reference (--a).
|
|
* @return A reference to the local instance.
|
|
*/
|
|
template <typename T>
|
|
Reference<T> &
|
|
Reference<T>::operator--() {
|
|
|
|
value--;
|
|
return *this;
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief Decrements the reference (a--).
|
|
* @return The result of the operation.
|
|
*/
|
|
template <typename T>
|
|
Reference<T>
|
|
Reference<T>::operator--(int) {
|
|
|
|
Reference<T> 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 <typename S>
|
|
std::ostream & operator<<(std::ostream& stream, const Reference<S> & ref) {
|
|
return stream << ref.value;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
#endif /* REFERENCE_H */
|
|
|