00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef _PMVCPP_H
00012 #define _PMVCPP_H
00013
00014 #include<string>
00015 #include<vector>
00016 #include<map>
00017 #include<iostream>
00018 #include<cstdlib>
00019 #include<stdint.h>
00020
00024 namespace PureMVC {
00031 template <class T>
00032 class Multiton
00033 {
00034 public:
00040 static T* instance()
00041 {
00042 std::string key = "key";
00043 return Multiton<T>::instance(key);
00044 }
00050 static T* instance(std::string key)
00051 {
00052 if(Multiton<T>::exists(key))
00053 return Multiton<T>::instanceMap[key];
00054
00055 T* inst = new T();
00056
00057 Multiton<T>::instanceMap[key] = inst;
00058 return inst;
00059 }
00065 static bool exists(std::string key)
00066 {
00067 return Multiton<T>::instanceMap.find(key) != Multiton<T>::instanceMap.end();
00068 }
00073 static size_t size()
00074 {
00075 return Multiton<T>::instanceMap.size();
00076 }
00082 static void erase(std::string key)
00083 {
00084 Multiton<T>::instanceMap.erase(key);
00085 }
00090 static void clear()
00091 {
00092 Multiton<T>::instanceMap.clear();
00093 }
00094 private:
00095 Multiton();
00096 ~Multiton();
00097 Multiton(Multiton const&);
00098 Multiton& operator=(Multiton const&);
00099 static std::map<std::string, T*> instanceMap;
00100 };
00101
00102 template <class T>
00103 std::map<std::string, T*> Multiton<T>::instanceMap;
00104
00105
00106
00111 class IMultitonKeyHeir
00112 {
00113 public:
00119 virtual void setMultitonKey(std::string key) = 0;
00125 virtual std::string getMultitonKey() = 0;
00126 };
00127
00128
00129
00153 class INotifier
00154 {
00155 public:
00167 virtual void sendNotification ( int notificationName, void* body, int notificationType) = 0;
00178 virtual void sendNotification ( int notificationName, int notificationType ) = 0;
00189 virtual void sendNotification ( int notificationName, void* body ) = 0;
00199 virtual void sendNotification ( int notificationName ) = 0;
00210 virtual void initializeNotifier ( std::string key ) = 0;
00211 virtual ~INotifier (){};
00212 };
00248 class INotification
00249 {
00250 public:
00255 virtual int getName () = 0;
00259 virtual void setBody( void* body ) = 0;
00263 virtual void* getBody() = 0;
00267 virtual void setType( int notificationType ) = 0;
00271 virtual int getType() = 0;
00275 virtual ~INotification(){};
00276 };
00282 class ICommand : public virtual INotifier
00283 {
00284 public:
00290 virtual void execute( INotification* notification ) = 0;
00291 virtual ~ICommand (){};
00292 };
00315 class IController : public virtual IMultitonKeyHeir
00316 {
00317 public:
00318 IController(){}
00326
00333
00339 virtual void removeCommand( int notificationName ) = 0;
00346 virtual bool hasCommand( int notificationName ) = 0;
00347 virtual ~IController(){};
00348 };
00355 class INotificationHandler
00356 {
00357 public:
00363 virtual void handleNotification ( INotification* notification ) = 0;
00364 };
00372 class IObserverRestricted
00373 {
00374 public:
00381 virtual void notifyObserver(INotification* notification) = 0;
00388 virtual bool compareNotifyContext( intptr_t memoryAddress ) = 0;
00389 };
00429 template<class T>
00430 class IObserverTemplated : public IObserverRestricted
00431 {
00432 public:
00441 virtual void setNotifyMethod( void(T::*fptr)(INotification*) ) = 0;
00447 virtual void setNotifyContext( T* notifyContext ) = 0;
00448
00449 virtual ~IObserverTemplated(){};
00450 };
00468 class IProxy : public virtual INotifier
00469 {
00470 public:
00476 virtual std::string getProxyName() = 0;
00480 virtual void onRegister() = 0;
00484 virtual void onRemove() = 0;
00490 virtual void setData( void* data ) = 0;
00496 virtual void* getData() = 0;
00497 };
00513 class IModel : public virtual IMultitonKeyHeir
00514 {
00515 public:
00522 virtual void registerProxy( IProxy* proxy ) = 0;
00529 virtual IProxy* retrieveProxy( std::string proxyName ) = 0;
00536 virtual IProxy* removeProxy( std::string proxyName ) = 0;
00543 virtual bool hasProxy( std::string proxyName ) = 0;
00544 virtual ~IModel(){};
00545 };
00546
00587 class IMediator : public virtual INotifier, public INotificationHandler
00588 {
00589 public:
00595 virtual std::string getMediatorName() = 0;
00601 virtual std::vector<int> listNotificationInterests() = 0;
00607 virtual void* getViewComponent() = 0;
00613 virtual void setViewComponent( void* viewComponent ) = 0;
00617 virtual void onRegister() = 0;
00621 virtual void onRemove() = 0;
00622
00623 virtual ~IMediator(){};
00624 };
00646 class IView : public virtual IMultitonKeyHeir
00647 {
00648 public:
00656 virtual void registerObserver( int notificationName, IObserverRestricted* observer ) = 0;
00664 virtual void removeObserver( int notificationName, intptr_t contextAddress ) = 0;
00675 virtual void notifyObservers( INotification* note ) = 0;
00693 virtual void registerMediator( IMediator* mediator ) = 0;
00700 virtual IMediator* retrieveMediator( std::string mediatorName ) = 0;
00707 virtual IMediator* removeMediator( std::string mediatorName ) = 0;
00714 virtual bool hasMediator( std::string mediatorName ) = 0;
00715 virtual ~IView(){};
00716 };
00736 class IFacade : public INotifier, public virtual IMultitonKeyHeir
00737 {
00738 public:
00744 virtual void registerProxy( IProxy* proxy ) = 0;
00751 virtual IProxy* retrieveProxy( std::string proxyName ) = 0;
00758 virtual IProxy* removeProxy( std::string proxyName ) = 0;
00765 virtual bool hasProxy( std::string proxyName ) = 0;
00772
00778 virtual void removeCommand( int notificationName ) = 0;
00785 virtual bool hasCommand( int notificationName ) = 0;
00791 virtual void registerMediator( IMediator* mediator ) = 0;
00798 virtual IMediator* retrieveMediator( std::string mediatorName ) = 0;
00805 virtual IMediator* removeMediator( std::string mediatorName ) = 0;
00812 virtual bool hasMediator( std::string mediatorName ) = 0;
00826 virtual void notifyObservers( INotification* notification ) = 0;
00827 };
00828
00829
00830
00831
00836 class MultitonKeyHeir : public virtual IMultitonKeyHeir
00837 {
00838 public:
00844 virtual void setMultitonKey(std::string key);
00850 virtual std::string getMultitonKey();
00851 private:
00852 std::string _multitonKey;
00853 };
00854
00855
00856
00882 class Notification : public INotification
00883 {
00884 public:
00885 int name;
00886 int type;
00887 void* body;
00895 Notification(int notificationName, void* body, int notificationType);
00896 Notification(int notificationName, void* body);
00897 Notification(int notificationName, int notificationType);
00898 Notification(int notificationName);
00904 int getName();
00908 void setBody( void* body );
00914 void* getBody();
00918 void setType( int notificationType );
00924 int getType();
00925 };
00926
00927
00928
00964 class Notifier : public MultitonKeyHeir, public virtual INotifier
00965 {
00966 public:
00977 void sendNotification ( int notificationName, void* body, int notificationType);
00978 void sendNotification ( int notificationName, int notificationType );
00979 void sendNotification ( int notificationName, void* body );
00980 void sendNotification ( int notificationName );
00999 void initializeNotifier(std::string key);
01000
01001 protected:
01002 IFacade* getFacade();
01003 };
01004
01005
01006
01019 class SimpleCommand : public Notifier, public ICommand
01020 {
01021 public:
01022 virtual void execute(INotification* notification) = 0;
01023 };
01024
01025
01026
01027
01055 class MacroCommand : public SimpleCommand
01056 {
01057 public:
01084 MacroCommand();
01094 void execute(INotification* notification);
01095 protected:
01119 void addSubCommand( ICommand* command );
01125 std::vector<ICommand*> subCommands;
01126 };
01147 template<class T>
01148 class Observer : public IObserverTemplated<T>
01149 {
01150 private:
01151 typedef void(T::*NotifyMethod)(INotification*);
01152 typedef T* NotifyContext;
01153 NotifyMethod notifyMethod;
01154 NotifyContext notifyContext;
01155 public:
01166 Observer( NotifyMethod method, NotifyContext context )
01167 {
01168 this->setNotifyMethod(method);
01169 this->setNotifyContext(context);
01170 }
01179 void setNotifyMethod( NotifyMethod method )
01180 {
01181 this->notifyMethod = method;
01182 }
01188 void setNotifyContext( NotifyContext context )
01189 {
01190 this->notifyContext = context;
01191 }
01197 NotifyMethod getNotifyMethod()
01198 {
01199 return this->notifyMethod;
01200 }
01206 NotifyContext getNotifyContext()
01207 {
01208 return this->notifyContext;
01209 }
01215 void notifyObserver( INotification* notification )
01216 {
01217 (this->notifyContext->*notifyMethod)(notification);
01218 }
01225 bool compareNotifyContext( intptr_t compareContextMemoryAddress )
01226 {
01227 return compareContextMemoryAddress == (intptr_t) &*this->getNotifyContext();
01228 }
01229 };
01230
01231
01232
01233 class Proxy : public IProxy, public Notifier
01234 {
01235 public:
01261 Proxy()
01262 {
01263 }
01264 Proxy(std::string proxyName, void* data)
01265 {
01266 this->proxyName = proxyName;
01267 this->data = data;
01268 }
01269 Proxy(std::string proxyName)
01270 {
01271 this->proxyName = proxyName;
01272 }
01273 Proxy(void* data)
01274 {
01275 this->data = data;
01276 }
01280 std::string getProxyName()
01281 {
01282 return this->proxyName;
01283 }
01284
01288 void setData( void* data )
01289 {
01290 this->data = data;
01291 }
01292
01296 void* getData()
01297 {
01298 return this->data;
01299 }
01300
01301 static std::string NAME;
01302
01303 protected:
01304
01305 std::string proxyName;
01306
01307
01308 void* data;
01309 };
01310
01311
01312
01313 class Mediator : public IMediator, public Notifier
01314 {
01315 public:
01319 Mediator( std::string mediatorName, void* viewComponent )
01320 {
01321 this->mediatorName = mediatorName;
01322 this->setViewComponent(viewComponent);
01323 }
01324 Mediator( std::string mediatorName )
01325 {
01326 this->mediatorName = mediatorName;
01327 }
01328 Mediator( void* viewComponent )
01329 {
01330 this->setViewComponent(viewComponent);
01331 }
01336 std::string getMediatorName()
01337 {
01338 return this->mediatorName;
01339 }
01340
01346 void setViewComponent( void* viewComponent )
01347 {
01348 this->viewComponent = viewComponent;
01349 }
01350
01368 void* getViewComponent()
01369 {
01370 return this->viewComponent;
01371 }
01372
01379 virtual std::vector<int> listNotificationInterests() = 0;
01380
01389 virtual void handleNotification( INotification* notification ) = 0;
01390
01394 virtual void onRegister() = 0;
01395
01399 virtual void onRemove() = 0;
01400
01406 std::string getName()
01407 {
01408 return this->mediatorName;
01409 }
01410
01411 protected:
01412
01413 std::string mediatorName;
01414
01415
01416 void* viewComponent;
01417 };
01418
01419
01420
01447 class Model : public MultitonKeyHeir, public IModel
01448 {
01449 public:
01465 Model();
01466
01472 static IModel* getInstance(std::string key);
01478 void registerProxy( IProxy* proxy );
01485 IProxy* retrieveProxy( std::string proxyName );
01486
01493 bool hasProxy( std::string proxyName );
01494
01501 IProxy* removeProxy( std::string proxyName );
01502
01508 static void removeModel( std::string key );
01509
01510 protected:
01511
01512 std::map <std::string, IProxy*> proxyMap;
01513 };
01514
01515
01516
01517
01537 class View : public MultitonKeyHeir, public IView
01538 {
01539 public:
01556 View( );
01562 static IView* getInstance( std::string key );
01570 void registerObserver ( int notificationName, IObserverRestricted* observer );
01581 void notifyObservers( INotification* notification );
01582
01589 void removeObserver( int notificationName, intptr_t contextAddress );
01590
01608 void registerMediator( IMediator* mediator );
01609
01616 IMediator* retrieveMediator( std::string mediatorName );
01617
01630 IMediator* removeMediator( std::string mediatorName );
01631
01638 bool hasMediator( std::string mediatorName );
01639
01645 static void removeView( std::string key );
01646
01647 protected:
01648
01649 std::map<std::string, IMediator*> mediatorMap;
01650
01651
01652 std::map<int, std::vector<IObserverRestricted*> > observerMap;
01653
01654 private:
01655 bool existsObserversInterestedIn(int notificationName);
01656 };
01657
01658
01659
01660
01694 class Controller : public MultitonKeyHeir, public IController
01695 {
01696 public:
01710 Controller();
01716 static IController* getInstance( std::string key );
01723 template<class T>
01724 void executeCommand( INotification* note )
01725 {
01726
01727
01728
01729
01730
01731
01732
01733
01734 int name = note->getName();
01735
01736 if(! this->hasCommand(name))
01737 return;
01738
01739 ICommand* command = new T();
01740 command->initializeNotifier(this->getMultitonKey());
01741 command->execute(note);
01742
01743 delete command;
01744 };
01760
01761 template<class T>
01762 void registerCommand(int notificationName)
01763 {
01764
01765
01766 if(this->hasCommand(notificationName))
01767 return;
01768
01769 Observer<Controller>* observer = new Observer<Controller>(&Controller::executeCommand<T>, this);
01770
01771 this->view->registerObserver(notificationName, observer);
01772
01773 commandMap[notificationName] = (T*) 0;
01774 };
01781 bool hasCommand( int notificationName );
01782
01788 void removeCommand( int notificationName );
01789
01795 static void removeController( std::string key );
01796
01797 protected:
01805 void initializeController();
01806
01807
01808 IView* view;
01809
01810
01811 std::map<int, ICommand*> commandMap;
01812 };
01813
01814
01815
01823 class Facade : public IFacade, public MultitonKeyHeir
01824 {
01825 public:
01839 Facade();
01845 static Facade* getInstance(std::string);
01852 template<class T>
01853 void registerCommand( int notificationName )
01854 {
01855 dynamic_cast<Controller*>(this->controller)->registerCommand<T>(notificationName);
01856 };
01857
01863 void removeCommand( int notificationName );
01864
01871 bool hasCommand( int notificationName );
01872
01879 void registerProxy ( IProxy* proxy );
01886 IProxy* retrieveProxy ( std::string proxyName );
01887
01894 IProxy* removeProxy ( std::string proxyName );
01895
01902 bool hasProxy( std::string proxyName );
01903
01910 void registerMediator( IMediator* mediator );
01911
01918 IMediator* retrieveMediator( std::string mediatorName );
01919
01926 IMediator* removeMediator( std::string mediatorName );
01927
01934 bool hasMediator( std::string mediatorName );
01935
01946 void sendNotification( int notificationName, void* body, int notificationType );
01947 void sendNotification( int notificationName, void* body );
01948 void sendNotification( int notificationName, int notificationType );
01949 void sendNotification( int notificationName );
01963 void notifyObservers ( INotification* notification );
01964
01973 void initializeNotifier( std::string key );
01974
01981 static bool hasCore( std::string key );
01982
01991 static void removeCore( std::string key );
01992 protected:
01999 void initializeFacade();
02000
02017 void initializeController( );
02018
02042 void initializeModel( );
02043
02044
02067 void initializeView( );
02068
02069
02070
02071 IController* controller;
02072 IModel* model;
02073 IView* view;
02074 };
02075
02076 }
02077
02078 #endif