Templates -- Meow  2.0.-1
A C++ template contains kinds of interesting classes and functions
object.h
Go to the documentation of this file.
1 
8 #ifndef __MEOWPP_UTILITY_OBJECT_H__
9 #define __MEOWPP_UTILITY_OBJECT_H__
10 
11 #include <cstdlib>
12 #include <cstdint>
13 
14 namespace meow {
15 
16 
20 class Object {
21  protected:
22 
27  Object() {}
28 
32  Object(Object const& b);
33 
34  public:
35 
39  virtual ~Object() {}
40 
44  virtual Object* Copy() const {
45  return NULL;
46  }
47 
52  virtual Object* CopyFrom(Object const* ptr) {
53  return NULL;
54  }
55 
60  virtual bool Equals(Object const* ptr) const {
61  return false;
62  }
63 
67  Object& operator=(Object const& b);
68 };
69 
70 
76 template<typename DataType>
77 class BaseNumberType : public Object {
78  private:
79  DataType value_;
80  public:
81  BaseNumberType() : BaseNumberType(static_cast<DataType>(0)) {}
82  BaseNumberType(BaseNumberType const& b) : BaseNumberType(b.value_) {}
83  BaseNumberType(DataType const& arg_init_value) : value_(arg_init_value) {}
85  Object* Copy() const { return new BaseNumberType(value_); }
86  Object* CopyFrom(Object const* ptr) {
87  value_ = static_cast<BaseNumberType const*>(ptr)->value_;
88  return this;
89  }
90  bool Equals(Object const* ptr) const {
91  return (value_ == static_cast<BaseNumberType const*>(ptr)->value_);
92  }
93  operator DataType() const { return value_; }
94  BaseNumberType& operator=(DataType const& b) { value_ = b; return *this; }
95  BaseNumberType& operator+=(DataType const& b) { value_ += b; return *this; }
96  BaseNumberType& operator-=(DataType const& b) { value_ -= b; return *this; }
97  BaseNumberType& operator*=(DataType const& b) { value_ *= b; return *this; }
98  BaseNumberType& operator/=(DataType const& b) { value_ /= b; return *this; }
99  BaseNumberType& operator&=(DataType const& b) { value_ &= b; return *this; }
100  BaseNumberType& operator|=(DataType const& b) { value_ |= b; return *this; }
101  BaseNumberType& operator^=(DataType const& b) { value_ ^= b; return *this; }
102  BaseNumberType& operator<<=(int64_t const& b) { value_ <<= b; return *this; }
103  BaseNumberType& operator>>=(int64_t const& b) { value_ >>= b; return *this; }
104 };
105 
106 
112 template<typename DataType>
113 class BaseFloatingType : public Object {
114  private:
115  DataType value_;
116  public:
117  BaseFloatingType() : BaseFloatingType(static_cast<DataType>(0)) {}
119  BaseFloatingType(DataType const& arg_init_value) : value_(arg_init_value) {}
121  Object* Copy() const { return new BaseFloatingType(value_); }
122  Object* CopyFrom(Object const* ptr) {
123  value_ = static_cast<BaseFloatingType const*>(ptr)->value_;
124  return this;
125  }
126  bool Equals(Object const* ptr) const {
127  return (value_ == static_cast<BaseFloatingType const*>(ptr)->value_);
128  }
129  operator DataType() const { return value_; }
130  BaseFloatingType& operator=(DataType const& b) { value_ = b; return *this; }
131  BaseFloatingType& operator+=(DataType const& b) { value_ += b; return *this; }
132  BaseFloatingType& operator-=(DataType const& b) { value_ -= b; return *this; }
133  BaseFloatingType& operator*=(DataType const& b) { value_ *= b; return *this; }
134  BaseFloatingType& operator/=(DataType const& b) { value_ /= b; return *this; }
135 };
136 
137 
142 
147 
151 
152 } // meow
153 
154 #endif // __MEOWPP_UTILITY_OBJECT_H__
BaseFloatingType & operator/=(DataType const &b)
Definition: object.h:134
A class for floating-point base object.
Definition: object.h:113
Object * CopyFrom(Object const *ptr)
Copies data from another object.
Definition: object.h:86
BaseNumberType & operator&=(DataType const &b)
Definition: object.h:99
BaseNumberType< uint64_t > UInt64
64 bits unsigned integer.
Definition: object.h:146
Object()
A protected constructor to prevent developers create an instance of Object directly.
Definition: object.h:27
BaseNumberType & operator>>=(int64_t const &b)
Definition: object.h:103
BaseNumberType< int64_t > Int64
64 bits integer.
Definition: object.h:141
BaseNumberType< int16_t > Int16
16 bits integer.
Definition: object.h:139
A class for number base object.
Definition: object.h:77
Object * Copy() const
Creates a copy of itself and return the pointer to it.
Definition: object.h:121
BaseNumberType< uint32_t > UInt32
32 bits unsigned integer.
Definition: object.h:145
virtual bool Equals(Object const *ptr) const
Returns whether it equals to another object or not.
Definition: object.h:60
BaseNumberType< uint8_t > UInt8
8 bits unsigned integer.
Definition: object.h:143
BaseFloatingType & operator*=(DataType const &b)
Definition: object.h:133
BaseFloatingType(BaseFloatingType const &b)
Definition: object.h:118
The base class.
Definition: object.h:20
BaseNumberType & operator|=(DataType const &b)
Definition: object.h:100
BaseFloatingType & operator=(DataType const &b)
Definition: object.h:130
virtual Object * CopyFrom(Object const *ptr)
Copies data from another object.
Definition: object.h:52
BaseNumberType & operator/=(DataType const &b)
Definition: object.h:98
BaseNumberType & operator-=(DataType const &b)
Definition: object.h:96
BaseNumberType< int32_t > Int32
32 bits integer.
Definition: object.h:140
virtual Object * Copy() const
Creates a copy of itself and return the pointer to it.
Definition: object.h:44
Object & operator=(Object const &b)
Disable the copy operator.
Object * CopyFrom(Object const *ptr)
Copies data from another object.
Definition: object.h:122
bool Equals(Object const *ptr) const
Returns whether it equals to another object or not.
Definition: object.h:90
BaseFloatingType & operator+=(DataType const &b)
Definition: object.h:131
BaseNumberType & operator=(DataType const &b)
Definition: object.h:94
BaseFloatingType & operator-=(DataType const &b)
Definition: object.h:132
virtual ~Object()
Virtual destructor.
Definition: object.h:39
BaseNumberType(DataType const &arg_init_value)
Definition: object.h:83
BaseNumberType & operator<<=(int64_t const &b)
Definition: object.h:102
BaseNumberType< int8_t > Int8
8 bits integer.
Definition: object.h:138
BaseFloatingType< long double > LDouble
Long double.
Definition: object.h:150
BaseFloatingType< float > Float
Float.
Definition: object.h:148
BaseNumberType & operator*=(DataType const &b)
Definition: object.h:97
BaseNumberType & operator+=(DataType const &b)
Definition: object.h:95
BaseFloatingType(DataType const &arg_init_value)
Definition: object.h:119
BaseNumberType & operator^=(DataType const &b)
Definition: object.h:101
BaseNumberType< uint16_t > UInt16
16 bits unsigned integer.
Definition: object.h:144
BaseFloatingType< double > Double
Double.
Definition: object.h:149
Object * Copy() const
Creates a copy of itself and return the pointer to it.
Definition: object.h:85
BaseNumberType(BaseNumberType const &b)
Definition: object.h:82
bool Equals(Object const *ptr) const
Returns whether it equals to another object or not.
Definition: object.h:126