MyGUI 3.0.1
MyGUI_WidgetUserData.h
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 #ifndef __MYGUI_WIDGET_USER_DATA_H__
00024 #define __MYGUI_WIDGET_USER_DATA_H__
00025 
00026 #include "MyGUI_Prerequest.h"
00027 #include "MyGUI_WidgetDefines.h"
00028 #include "MyGUI_Any.h"
00029 
00030 namespace MyGUI
00031 {
00033     class MYGUI_EXPORT UserData
00034     {
00035     public:
00036         UserData() { }
00037         virtual ~UserData() { }
00038 
00040         void setUserString(const std::string& _key, const std::string& _value)
00041         {
00042             mMapUserString[_key] = _value;
00043         }
00044 
00046         const std::string& getUserString(const std::string& _key)
00047         {
00048             MapString::iterator iter = mMapUserString.find(_key);
00049             if (iter == mMapUserString.end())
00050             {
00051                 static std::string empty;
00052                 return empty;
00053             }
00054             return iter->second;
00055         }
00056 
00058         bool clearUserString(const std::string& _key)
00059         {
00060             MapString::iterator iter = mMapUserString.find(_key);
00061             if (iter != mMapUserString.end())
00062             {
00063                 mMapUserString.erase(iter);
00064                 return true;
00065             }
00066             return false;
00067         }
00068 
00070         bool isUserString(const std::string& _key)
00071         {
00072             return mMapUserString.find(_key) != mMapUserString.end();
00073         }
00074 
00076         void clearUserStrings()
00077         {
00078             mMapUserString.clear();
00079         }
00080 
00082         void setUserData(Any _data) { mUserData = _data; }
00083 
00085         template <typename ValueType>
00086         ValueType * getUserData(bool _throw = true)
00087         {
00088             return mUserData.castType<ValueType>(_throw);
00089         }
00090 
00091     /*internal:*/
00092         void _setInternalData(Any _data) { mInternalData = _data; }
00093 
00094         template <typename ValueType>
00095         ValueType * _getInternalData(bool _throw = true)
00096         {
00097             return mInternalData.castType<ValueType>(_throw);
00098         }
00099 
00100     /*obsolete:*/
00101 #ifndef MYGUI_DONT_USE_OBSOLETE
00102 
00103         MYGUI_OBSOLETE("use : template <typename ValueType> ValueType * UserData::getUserData(bool _throw)")
00104         void * getUserData()
00105         {
00106             return mUserData.castUnsafe();
00107         }
00108 
00109 #endif // MYGUI_DONT_USE_OBSOLETE
00110 
00111     private:
00112         // пользовательские данные
00113         MapString mMapUserString;
00114         Any mUserData;
00115 
00116         // для внутренниего использования
00117         Any mInternalData;
00118 
00119     };
00120 
00121 } // namespace MyGUI
00122 
00123 #endif // __MYGUI_WIDGET_USER_DATA_H__