• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

MyGUI_DocType.cpp

Go to the documentation of this file.
00001 
00007 /*
00008     This file is part of MyGUI.
00009     
00010     MyGUI is free software: you can redistribute it and/or modify
00011     it under the terms of the GNU Lesser General Public License as published by
00012     the Free Software Foundation, either version 3 of the License, or
00013     (at your option) any later version.
00014     
00015     MyGUI is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018     GNU Lesser General Public License for more details.
00019     
00020     You should have received a copy of the GNU Lesser General Public License
00021     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
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         // example
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     } // namespace demonstrate
00113 } // namespace MyGUI

Generated on Sun Jan 30 2011 for MyGUI by  doxygen 1.7.1