aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/utility/object.h
blob: 630ab6e0b1f33614ecba693ae00f0e1bdd9e8fc0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*!
 * @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__