diff options
Diffstat (limited to 'meowpp/meow/bases/object.h')
-rw-r--r-- | meowpp/meow/bases/object.h | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/meowpp/meow/bases/object.h b/meowpp/meow/bases/object.h new file mode 100644 index 0000000..7953cd5 --- /dev/null +++ b/meowpp/meow/bases/object.h @@ -0,0 +1,126 @@ +/*! + * @file object.h + * @brief Contains a base class for most of all the classes in meowpp. + * + * @author cathook + */ + +#ifndef __MEOW_BASES_OBJECT_H__ +#define __MEOW_BASES_OBJECT_H__ + +#include <cstdlib> + +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 Returns whether this object is an non-copyable type or not. + * @return false + * + * Default is no. + */ + virtual bool copyable() const { + return false; + } + + /*! + * @brief A simple way for user to setup whether the class is copyable or not. + * @param [in] x true or false. + */ +#define SET_MEOW_OBJECT_COPYABLE(x) bool copyable() const { return x; } + + /*! + * @brief Creates a copy of itself and return the pointer to it. + * + * It will return NULL in default if you don't implement it. + */ + virtual Object* Copy() const { + return NULL; + } + + /*! + * @brief A simple way for user to implement the method "Copy". + * @param [in] X The class name. + * + * By calling the copy constructor. + */ +#define IMPL_MEOW_OBJECT_COPY_BY_COPYCONSTRUCTOR(X) \ + Object* Copy() const { \ + return dynamic_cast<Object*>(NewEntry<X>(*this)); \ + } + + /*! + * @brief Copies data from another object. + * @param [in] ptr Points to another object. + * + * It will return NULL in default if you don't implement it. + */ + virtual Object* CopyFrom(Object const* ptr) { + return NULL; + } + + /*! + * @brief A simple way for user to implement the method "CopyFrom". + * @param [in] X The class name. + * + * By calling the CopyFrom method in the user defined class. + */ +#define DELEGATE_MEOW_OBJECT_COPYFROM(X) \ + Object* CopyFrom(Object const* b) { \ + return dynamic_cast<Object*>(&CopyFrom(*dynamic_cast<X const*>(b))); \ + } + + /*! + * @brief Returns whether it equals to another object or not. + * @param [in] ptr Points to another object. + * + * It will always return false if you don't implement it. + */ + virtual bool Equals(Object const* ptr) const { + return false; + } + + /*! + * @brief A simple way for user to implement the method "Equals". + * @param [in] X The class name. + * + * By calling the Equals method in the user defined class. + */ +#define DELEGATE_MEOW_OBJECT_EQUALS(X) \ + bool Equals(Object const* b) const { \ + return Equals(*dynamic_cast<X const*>(b)); \ + } + + /*! + * @brief Disable the copy operator. + */ + Object& operator=(Object const& b); +}; + +} // meow + +#endif // __MEOW_BASES_OBJECT_H__ |