PureMVC PureMVC
|
00001 // Controller.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_CONTROLLER_HPP__) 00010 #define __PUREMVC_CORE_CONTROLLER_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/ICommand.hpp" 00026 #include "../Interfaces/IObserver.hpp" 00027 #include "../Patterns/Iterator/Iterator.hpp" 00028 00029 namespace PureMVC 00030 { 00031 namespace Core 00032 { 00033 using Interfaces::IModel; 00034 using Interfaces::IController; 00035 using Interfaces::ICommand; 00036 using Interfaces::IObserver; 00037 using Interfaces::IAggregate; 00038 using Interfaces::IIterator; 00039 00072 class PUREMVC_API Controller : public virtual IController 00073 { 00074 protected: 00075 typedef std::map<std::string, ICommand*> CommandMap; 00076 protected: 00077 /* 00078 * Define iterator converter for getting notification name only. 00079 */ 00080 struct IteratorConverter 00081 #if !defined(__DMC__) // The C++ compiler of Digital Mars cannot resolve this case 00082 : public std::unary_function<CommandMap::const_iterator, CommandMap::key_type> 00083 #endif 00084 { 00085 /* 00086 * Converting operator. 00087 * 00088 * @param iterator the iterator of internal container. 00089 * @return the name of notification inside iterator. 00090 */ 00091 CommandMap::key_type const& operator()(CommandMap::const_iterator const& iterator) const; 00092 }; 00093 /* 00094 * Define iterator range of internal container. 00095 */ 00096 struct IteratorRange { 00097 /* 00098 * Get begin point. 00099 * 00100 * @return the begin of iterator. 00101 */ 00102 CommandMap::const_iterator begin(CommandMap const* const& iterator) const; 00103 /* 00104 * Get end point. 00105 * 00106 * @return the end of iterator. 00107 */ 00108 CommandMap::const_iterator end(CommandMap const* const& iterator) const; 00109 }; 00110 public: 00111 static char const* const DEFAULT_KEY; 00112 protected: 00113 // Message Constants 00114 static char const* const MULTITON_MSG; 00115 protected: 00116 // The Multiton Key for this Core 00117 std::string const _multiton_key; 00118 // Local reference to View 00119 IView* _view; 00120 // Mapping of Notification names to Command Class references 00121 CommandMap _command_map; 00122 // Synchronous access 00123 mutable Mutex _synchronous_access; 00124 private: 00125 Controller(Controller const&); 00126 Controller(IController const&); 00127 Controller& operator=(Controller const&); 00128 Controller& operator=(IController const&); 00129 protected: 00130 static IController* find(std::string const& key); 00131 static void insert(std::string const& key, IController* controller); 00132 public: 00146 explicit Controller(std::string const& key = Controller::DEFAULT_KEY); 00147 00148 protected: 00149 // Support call virtual method in constructor of base class (Controller). 00150 // Condition: Derived class (class inherit from Controller) must implement 00151 // method: initializeController 00176 template<typename _DerivedType> 00177 explicit Controller(_DerivedType* instance, std::string const& key = Controller::DEFAULT_KEY) 00178 :_multiton_key(key) 00179 ,_view(NULL) 00180 { 00181 if (find(_multiton_key)) 00182 throw std::runtime_error(MULTITON_MSG); 00183 insert(_multiton_key, this); 00184 instance->_DerivedType::initializeController(); 00185 } 00186 00207 virtual void initializeController(void); 00208 00209 public: 00215 static IController& getInstance(std::string const& key = Controller::DEFAULT_KEY); 00216 00223 virtual void executeCommand(INotification const& notification); 00224 00240 virtual void registerCommand(std::string const& notification_name, ICommand* command); 00241 00248 virtual bool hasCommand(std::string const& notification_name) const; 00249 00256 virtual ICommand const& retrieveCommand(std::string const& notification_name) const; 00257 00264 virtual ICommand& retrieveCommand(std::string const& notification_name); 00265 00272 virtual ICommand* removeCommand(std::string const& notification_name); 00273 00279 virtual NotificationNames listNotificationNames(void) const; 00280 00286 static void removeController(std::string const& key); 00287 00291 virtual ~Controller(void); 00292 }; 00293 } 00294 } 00295 00296 #endif /* __PUREMVC_CORE_CONTROLLER_HPP__ */