aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/utility/state.h
diff options
context:
space:
mode:
Diffstat (limited to 'meowpp/utility/state.h')
-rw-r--r--meowpp/utility/state.h108
1 files changed, 108 insertions, 0 deletions
diff --git a/meowpp/utility/state.h b/meowpp/utility/state.h
new file mode 100644
index 0000000..00b79dc
--- /dev/null
+++ b/meowpp/utility/state.h
@@ -0,0 +1,108 @@
+/*!
+ * @file state.h
+ * @brief Contains a base class for a state (in meowpp, most of all the return
+ * value of a function (or to say, an "operation") will be a state).
+ *
+ * @author cathook
+ */
+
+#ifndef __MEOWPP_UTILITY_STATE_H__
+#define __MEOWPP_UTILITY_STATE_H__
+
+#include "object.h"
+
+namespace meow {
+
+
+/*!
+ * @brief The base class for state.
+ *
+ * Some example code:
+ * @code{.cpp}
+ * #include <meowpp/utility/state.h>
+ * #include <cstdio>
+ *
+ * using namespace meow;
+ *
+ * class Func1State : public State {
+ * public:
+ * static const int SAME = 0;
+ * static const int DIFF = 1;
+ * };
+ *
+ * State Func1(int a, int b) {
+ * if (a == b) {
+ * return Func1State::SAME;
+ * } else {
+ * return Func1State::DIFF;
+ * }
+ * }
+ *
+ * int main() {
+ * if (Func1(3, 5) == Func1State::SAME) {
+ * printf("same!\n");
+ * } else {
+ * printf("diff\n");
+ * }
+ * return 0;
+ * }
+ * @endcode
+ */
+class State : public Object {
+ private:
+ int value_; //!< Stores the current state.
+
+ public:
+
+ /*!
+ * @brief Default constructor.
+ */
+ State() {}
+
+ /*!
+ * @brief Copy constructor.
+ */
+ State(State const& arg_another_state) : State(arg_another_state.value_) {}
+
+ /*!
+ * @brief Constructor.
+ */
+ State(int arg_init_value) : value_(arg_init_value) {}
+
+ /*!
+ * @brief Virtual destructor.
+ */
+ virtual ~State() {}
+
+ /*!
+ * @brief Gets the integer value of the state.
+ */
+ operator int() const {
+ return value_;
+ }
+
+ /*!
+ * @brief Sets the integer value of the state.
+ */
+ State& operator=(State const& arg_new_state) {
+ value_ = arg_new_state.value_;
+ return *this;
+ }
+
+ Object* Copy() const {
+ return new State(value_);
+ }
+
+ Object* CopyFrom(Object const* another_state) {
+ value_ = dynamic_cast<State const*>(another_state)->value_;
+ return this;
+ }
+
+ bool Equals(Object const* another_state) {
+ return (value_ == dynamic_cast<State const*>(another_state)->value_);
+ }
+};
+
+} // meow
+
+#endif // __MEOWPP_UTILITY_STATE_H__