PureMVC PureMVC
|
00001 // Model.hpp 00002 // PureMVC_C++ 00003 // 00004 // PureMVC Port to C++ by Tang Khai Phuong <phuong.tang@puremvc.org> 00005 // PureMVC - Copyright(c) 2006-2011 Futurescale, Inc., Some rights reserved. 00006 // Your reuse is governed by the Creative Commons Attribution 3.0 License 00007 // 00008 00009 #if !defined(__PUREMVC_CORE_MODEL_HPP__) 00010 #define __PUREMVC_CORE_MODEL_HPP__ 00011 00012 // STL include 00013 #include <string> 00014 #include <map> 00015 #include <exception> 00016 #include <stdexcept> 00017 #include <cassert> 00018 // PureMVC include 00019 #if !defined(__PUREMVC_HPP__) 00020 #define __PUREMVC_INCLUDE__ 00021 #include "../PureMVC.hpp" 00022 #endif /* __PUREMVC_HPP__ */ 00023 00024 #include "../Interfaces/IModel.hpp" 00025 #include "../Interfaces/IProxy.hpp" 00026 #include "../Patterns/Iterator/Iterator.hpp" 00027 00028 namespace PureMVC 00029 { 00030 namespace Core 00031 { 00032 using Interfaces::IModel; 00033 using Interfaces::IProxy; 00034 using Interfaces::IAggregate; 00035 using Interfaces::IIterator; 00036 00063 class PUREMVC_API Model : public virtual IModel 00064 { 00065 protected: 00066 typedef std::map<std::string, IProxy*> ProxyMap; 00067 protected: 00068 /* 00069 * Define iterator converter for getting proxy name only. 00070 */ 00071 struct IteratorConverter 00072 #if !defined(__DMC__) // The C++ compiler of Digital Mars cannot resolve this case 00073 : public std::unary_function<ProxyMap::const_iterator, ProxyMap::key_type> 00074 #endif 00075 { 00076 /* 00077 * Converting operator. 00078 * 00079 * @param iterator the iterator of internal container. 00080 * @return the name of proxy inside iterator. 00081 */ 00082 ProxyMap::key_type const& operator()(ProxyMap::const_iterator const& iterator) const; 00083 }; 00084 /* 00085 * Define iterator range of internal container. 00086 */ 00087 struct IteratorRange { 00088 /* 00089 * Get begin point. 00090 * 00091 * @return the begin of iterator. 00092 */ 00093 ProxyMap::const_iterator begin(ProxyMap const* const& iterator) const; 00094 /* 00095 * Get end point. 00096 * 00097 * @return the end of iterator. 00098 */ 00099 ProxyMap::const_iterator end(ProxyMap const* const& iterator) const; 00100 }; 00101 public: 00102 static char const* const DEFAULT_KEY; 00103 protected: 00104 // Message Constants 00105 static char const* const MULTITON_MSG; 00106 // The Multiton Key for this Core 00107 std::string const _multiton_key; 00108 // Mapping of proxyNames to IProxy instances 00109 ProxyMap _proxy_map; 00110 // Synchronous access 00111 mutable Mutex _synchronous_access; 00112 private: 00113 Model(Model const&); 00114 Model(IModel const&); 00115 Model& operator=(Model const&); 00116 Model& operator=(IModel const&); 00117 protected: 00118 static IModel* find(std::string const& key); 00119 static void insert(std::string const& key, IModel* model); 00120 public: 00133 explicit Model(std::string const& key = PureMVC::Core::Model::DEFAULT_KEY); 00134 00135 protected: 00159 template<typename _DerivedType> 00160 explicit Model(_DerivedType* instance, std::string const& key = PureMVC::Core::Model::DEFAULT_KEY) 00161 :_multiton_key(key) 00162 { 00163 if (find(key)) 00164 throw std::runtime_error(MULTITON_MSG); 00165 insert(_multiton_key, this); 00166 instance->_DerivedType::initializeModel(); 00167 } 00168 00180 virtual void initializeModel(void); 00181 00182 public: 00188 static IModel& getInstance(std::string const& key = PureMVC::Core::Model::DEFAULT_KEY); 00189 00195 virtual void registerProxy(IProxy* proxy); 00196 00203 virtual IProxy const& retrieveProxy(std::string const& proxy_name) const; 00204 00211 virtual IProxy& retrieveProxy(std::string const& proxy_name); 00212 00219 virtual bool hasProxy(std::string const& proxy_name) const; 00220 00227 virtual IProxy* removeProxy(std::string const& proxy_name); 00228 00234 virtual ProxyNames listProxyNames(void) const; 00235 00241 static void removeModel(std::string const& key); 00242 00246 virtual ~Model(void); 00247 }; 00248 } 00249 } 00250 00251 #endif /* __PUREMVC_CORE_MODEL_HPP__ */