aboutsummaryrefslogblamecommitdiffstats
path: root/meowpp/utility/object.h
blob: 630ab6e0b1f33614ecba693ae00f0e1bdd9e8fc0 (plain) (tree)

























































































































































                                                                                
/*!
 * @file object.h
 * @brief Contains a base class for most of all the classes in meowpp.
 *
 * @author cathook
 */

#ifndef __MEOWPP_UTILITY_OBJECT_H__
#define __MEOWPP_UTILITY_OBJECT_H__

#include <cstdlib>
#include <cstdint>

namespace meow {


/*!
 * @brief The base class.
 */
class Object {
 protected:

  /*!
   * @brief A protected constructor to prevent developers create an instance of
   *     Object directly.
   */
  Object() {}
  
  /*!
   * @brief Disable the copy operation.
   */
  Object(Object const& b);

 public:

  /*!
   * @brief Virtual destructor.
   */
  virtual ~Object() {}

  /*!
   * @brief Creates a copy of itself and return the pointer to it.
   */
  virtual Object* Copy() const {
    return NULL;
  }

  /*!
   * @brief Copies data from another object.
   * @param [in] ptr Points to another object.
   */
  virtual Object* CopyFrom(Object const* ptr) {
    return NULL;
  }

  /*!
   * @brief Returns whether it equals to another object or not.
   * @param [in] ptr Points to another object.
   */
  virtual bool Equals(Object const* ptr) const {
    return false;
  }
  
  /*!
   * @brief Disable the copy operator.
   */
  Object& operator=(Object const& b);
};


/*!
 * @brief A class for number base object.
 * 
 * It contains bitwise operations.
 */
template<typename DataType>
class BaseNumberType : public Object {
 private:
  DataType value_;
 public:
  BaseNumberType() : BaseNumberType(static_cast<DataType>(0)) {}
  BaseNumberType(BaseNumberType const& b) : BaseNumberType(b.value_) {}
  BaseNumberType(DataType const& arg_init_value) : value_(arg_init_value) {}
  ~BaseNumberType() {}
  Object* Copy() const { return new BaseNumberType(value_); }
  Object* CopyFrom(Object const* ptr) {
    value_ = static_cast<BaseNumberType const*>(ptr)->value_;
    return this;
  }
  bool Equals(Object const* ptr) const {
    return (value_ == static_cast<BaseNumberType const*>(ptr)->value_);
  }
  operator DataType() const { return value_; }
  BaseNumberType& operator=(DataType const& b) { value_ = b; return *this; }
  BaseNumberType& operator+=(DataType const& b) { value_ += b; return *this; }
  BaseNumberType& operator-=(DataType const& b) { value_ -= b; return *this; }
  BaseNumberType& operator*=(DataType const& b) { value_ *= b; return *this; }
  BaseNumberType& operator/=(DataType const& b) { value_ /= b; return *this; }
  BaseNumberType& operator&=(DataType const& b) { value_ &= b; return *this; }
  BaseNumberType& operator|=(DataType const& b) { value_ |= b; return *this; }
  BaseNumberType& operator^=(DataType const& b) { value_ ^= b; return *this; }
  BaseNumberType& operator<<=(int64_t const& b) { value_ <<= b; return *this; }
  BaseNumberType& operator>>=(int64_t const& b) { value_ >>= b; return *this; }
};


/*!
 * @brief A class for floating-point base object.
 * 
 * It don't contain bitwise operations.
 */
template<typename DataType>
class BaseFloatingType : public Object {
 private:
  DataType value_;
 public:
  BaseFloatingType() : BaseFloatingType(static_cast<DataType>(0)) {}
  BaseFloatingType(BaseFloatingType const& b) : BaseFloatingType(b.value_) {}
  BaseFloatingType(DataType const& arg_init_value) : value_(arg_init_value) {}
  ~BaseFloatingType() {}
  Object* Copy() const { return new BaseFloatingType(value_); }
  Object* CopyFrom(Object const* ptr) {
    value_ = static_cast<BaseFloatingType const*>(ptr)->value_;
    return this;
  }
  bool Equals(Object const* ptr) const {
    return (value_ == static_cast<BaseFloatingType const*>(ptr)->value_);
  }
  operator DataType() const { return value_; }
  BaseFloatingType& operator=(DataType const& b) { value_ = b; return *this; }
  BaseFloatingType& operator+=(DataType const& b) { value_ += b; return *this; }
  BaseFloatingType& operator-=(DataType const& b) { value_ -= b; return *this; }
  BaseFloatingType& operator*=(DataType const& b) { value_ *= b; return *this; }
  BaseFloatingType& operator/=(DataType const& b) { value_ /= b; return *this; }
};


typedef BaseNumberType<int8_t> Int8;  //!< 8 bits integer.
typedef BaseNumberType<int16_t> Int16;  //!< 16 bits integer.
typedef BaseNumberType<int32_t> Int32;  //!< 32 bits integer.
typedef BaseNumberType<int64_t> Int64;  //!< 64 bits integer.

typedef BaseNumberType<uint8_t> UInt8;  //!< 8 bits unsigned integer.
typedef BaseNumberType<uint16_t> UInt16;  //!< 16 bits unsigned integer.
typedef BaseNumberType<uint32_t> UInt32;  //!< 32 bits unsigned integer.
typedef BaseNumberType<uint64_t> UInt64;  //!< 64 bits unsigned integer.

typedef BaseFloatingType<float> Float;  //!< Float.
typedef BaseFloatingType<double> Double;  //!< Double.
typedef BaseFloatingType<long double> LDouble;  //!< Long double.

}  // meow

#endif  // __MEOWPP_UTILITY_OBJECT_H__