aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/utility/state.h
blob: 00b79dc3637b9ed2e61b7e3dd6c1ecaa53bdc84c (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
/*!
 * @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__