Go to the documentation of this file.00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "MyGUI_Precompiled.h"
00025 #include "MyGUI_Common.h"
00026
00027 namespace MyGUI
00028 {
00029 namespace demonstrate
00030 {
00031
00032
00033
00034
00036 struct StateType
00037 {
00038 enum Enum
00039 {
00040 Disabled,
00041 Normal,
00042 Pushed,
00043 MAX
00044 };
00045
00046 static StateType parse(const std::string& _value)
00047 {
00048 StateType type;
00049 int value = 0;
00050 while (true)
00051 {
00052 const char * name = type.getValueName(value);
00053 if (strcmp(name, "") == 0 || name == _value) break;
00054 value++;
00055 };
00056 type.value = Enum(value);
00057 return type;
00058 }
00059
00060 StateType(Enum _value = MAX) : value(_value) { }
00061
00062 friend bool operator == (StateType const& a, StateType const& b) { return a.value == b.value; }
00063 friend bool operator != (StateType const& a, StateType const& b) { return a.value != b.value; }
00064
00065 friend std::ostream& operator << ( std::ostream& _stream, const StateType& _value )
00066 {
00067 _stream << _value.getValueName(_value.value);
00068 return _stream;
00069 }
00070
00071 friend std::istream& operator >> ( std::istream& _stream, StateType& _value )
00072 {
00073 std::string value;
00074 _stream >> value;
00075 _value = StateType::parse(value);
00076 return _stream;
00077 }
00078
00079 std::string print() const { return getValueName(value); }
00080
00081 private:
00082 const char * getValueName(int _index) const
00083 {
00084 static const char * values[MAX + 1] = { "Disabled", "Normal", "Pushed", "" };
00085 return values[(_index < MAX && _index >= 0) ? _index : MAX];
00086 }
00087
00088 private:
00089 Enum value;
00090 };
00091
00092
00093
00094
00095 void test()
00096 {
00097 StateType type1 = StateType::Normal;
00098 StateType type2(StateType::Pushed);
00099
00100 if (type1 == type2) { }
00101 if (type2 != type1) { }
00102 if (type1 == StateType::Disabled) { }
00103 if (StateType::Normal == type2) { }
00104
00105 type1 = type2;
00106 type2 = StateType::Normal;
00107
00108 StateType type3 = StateType::parse("Disabled");
00109 std::string name = type3.print();
00110 }
00111
00112 }
00113 }