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 {
00025
00026
00031 class Object
00032 {
00033 public:
00038 virtual void function(){}
00039 };
00040
00047 template <class T>
00048 class Multiton
00049 {
00050 public:
00056 static T* instance()
00057 {
00058 std::string key = "key";
00059 return Multiton<T>::instance(key);
00060 }
00066 static T* instance(std::string key)
00067 {
00068 if(Multiton<T>::exists(key))
00069 return Multiton<T>::instanceMap[key];
00070
00071 T* inst = new T();
00072
00073 Multiton<T>::instanceMap[key] = inst;
00074 return inst;
00075 }
00081 static bool exists(std::string key)
00082 {
00083 return Multiton<T>::instanceMap.find(key) != Multiton<T>::instanceMap.end();
00084 }
00089 static size_t size()
00090 {
00091 return Multiton<T>::instanceMap.size();
00092 }
00098 static void erase(std::string key)
00099 {
00100 Multiton<T>::instanceMap.erase(key);
00101 }
00106 static void clear()
00107 {
00108 Multiton<T>::instanceMap.clear();
00109 }
00110 private:
00111 Multiton();
00112 ~Multiton();
00113 Multiton(Multiton const&);
00114 Multiton& operator=(Multiton const&);
00115 static std::map<std::string, T*> instanceMap;
00116 };
00117
00118 template <class T>
00119 std::map<std::string, T*> Multiton<T>::instanceMap;
00120
00121
00122
00127 class IMultitonKeyHeir
00128 {
00129 public:
00135 virtual void setMultitonKey(std::string key) = 0;
00141 virtual std::string getMultitonKey() = 0;
00142 };
00143
00144
00145
00169 class INotifier
00170 {
00171 public:
00183 virtual void sendNotification ( int notificationName, Object* body, int notificationType) = 0;
00184 virtual void sendNotification ( int notificationName, int notificationType ) = 0;
00185 virtual void sendNotification ( int notificationName, Object* body ) = 0;
00186 virtual void sendNotification ( int notificationName ) = 0;
00197 virtual void initializeNotifier ( std::string key ) = 0;
00198 virtual ~INotifier (){};
00199 };
00235 class INotification
00236 {
00237 public:
00242 virtual int getName () = 0;
00246 virtual void setBody( Object* body ) = 0;
00250 virtual Object* getBody() = 0;
00254 virtual void setType( int notificationType ) = 0;
00258 virtual int getType() = 0;
00262 virtual ~INotification(){};
00263 };
00269 class ICommand : public virtual INotifier
00270 {
00271 public:
00277 virtual void execute( INotification* notification ) = 0;
00278 virtual ~ICommand (){};
00279 };
00302 class IController : public virtual IMultitonKeyHeir
00303 {
00304 public:
00305 IController(){}
00313
00320
00326 virtual void removeCommand( int notificationName ) = 0;
00333 virtual bool hasCommand( int notificationName ) = 0;
00334 virtual ~IController(){};
00335 };
00342 class INotificationHandler
00343 {
00344 public:
00350 virtual void handleNotification ( INotification* notification ) = 0;
00351 };
00359 class IObserverRestricted
00360 {
00361 public:
00368 virtual void notifyObserver(INotification* notification) = 0;
00375 virtual bool compareNotifyContext( intptr_t memoryAddress ) = 0;
00376 };
00416 template<class T>
00417 class IObserverTemplated : public IObserverRestricted
00418 {
00419 public:
00428 virtual void setNotifyMethod( void(T::*fptr)(INotification*) ) = 0;
00434 virtual void setNotifyContext( T* notifyContext ) = 0;
00435
00436 virtual ~IObserverTemplated(){};
00437 };
00460 class IProxyRestricted : public virtual INotifier
00461 {
00462 public:
00468 virtual std::string getProxyName() = 0;
00472 virtual void onRegister() = 0;
00476 virtual void onRemove() = 0;
00477 };
00482 template<class T>
00483 class IProxyTemplated : public IProxyRestricted
00484 {
00485 public:
00491 virtual void setData( T data ) = 0;
00497 virtual T getData() = 0;
00498 };
00514 class IModel : public virtual IMultitonKeyHeir
00515 {
00516 public:
00523 virtual void registerProxy( IProxyRestricted* proxy ) = 0;
00530 virtual IProxyRestricted* retrieveProxy( std::string proxyName ) = 0;
00537 virtual IProxyRestricted* removeProxy( std::string proxyName ) = 0;
00544 virtual bool hasProxy( std::string proxyName ) = 0;
00545 virtual ~IModel(){};
00546 };
00547
00590 class IMediatorRestricted : public virtual INotifier, public INotificationHandler
00591 {
00592 public:
00598 virtual std::string getMediatorName() = 0;
00604 virtual std::vector<int> listNotificationInterests() = 0;
00608 virtual void onRegister() = 0;
00612 virtual void onRemove() = 0;
00613
00614 virtual ~IMediatorRestricted(){};
00615 };
00623 template<class T>
00624 class IMediatorTemplated : public IMediatorRestricted
00625 {
00626 public:
00632 virtual T getViewComponent() = 0;
00638 virtual void setViewComponent( T viewComponent ) = 0;
00639
00640 virtual ~IMediatorTemplated(){};
00641
00642 };
00664 class IView : public virtual IMultitonKeyHeir
00665 {
00666 public:
00674 virtual void registerObserver( int notificationName, IObserverRestricted* observer ) = 0;
00682 virtual void removeObserver( int notificationName, intptr_t contextAddress ) = 0;
00693 virtual void notifyObservers( INotification* note ) = 0;
00711 virtual void registerMediator( IMediatorRestricted* mediator ) = 0;
00718 virtual IMediatorRestricted* retrieveMediator( std::string mediatorName ) = 0;
00725 virtual IMediatorRestricted* removeMediator( std::string mediatorName ) = 0;
00732 virtual bool hasMediator( std::string mediatorName ) = 0;
00733 virtual ~IView(){};
00734 };
00754 class IFacade : public INotifier, public virtual IMultitonKeyHeir
00755 {
00756 public:
00762 virtual void registerProxy( IProxyRestricted* proxy ) = 0;
00769 virtual IProxyRestricted* retrieveProxy( std::string proxyName ) = 0;
00776 virtual IProxyRestricted* removeProxy( std::string proxyName ) = 0;
00783 virtual bool hasProxy( std::string proxyName ) = 0;
00790
00796 virtual void removeCommand( int notificationName ) = 0;
00803 virtual bool hasCommand( int notificationName ) = 0;
00809 virtual void registerMediator( IMediatorRestricted* mediator ) = 0;
00816 virtual IMediatorRestricted* retrieveMediator( std::string mediatorName ) = 0;
00823 virtual IMediatorRestricted* removeMediator( std::string mediatorName ) = 0;
00830 virtual bool hasMediator( std::string mediatorName ) = 0;
00844 virtual void notifyObservers( INotification* notification ) = 0;
00845 };
00846
00847
00848
00849
00854 class MultitonKeyHeir : public virtual IMultitonKeyHeir
00855 {
00856 public:
00862 virtual void setMultitonKey(std::string key);
00868 virtual std::string getMultitonKey();
00869 private:
00870 std::string _multitonKey;
00871 };
00872
00873
00874
00900 class Notification : public INotification
00901 {
00902 public:
00903 int name;
00904 int type;
00905 Object* body;
00913 Notification(int notificationName, Object* body, int notificationType);
00914 Notification(int notificationName, Object* body);
00915 Notification(int notificationName, int notificationType);
00916 Notification(int notificationName);
00922 int getName();
00926 void setBody( Object* body );
00932 Object* getBody();
00936 void setType( int notificationType );
00942 int getType();
00943 };
00944
00945
00946
00982 class Notifier : public MultitonKeyHeir, public virtual INotifier
00983 {
00984 public:
00995 void sendNotification ( int notificationName, Object* body, int notificationType);
00996 void sendNotification ( int notificationName, int notificationType );
00997 void sendNotification ( int notificationName, Object* body );
00998 void sendNotification ( int notificationName );
01017 void initializeNotifier(std::string key);
01018
01019 protected:
01020 IFacade* getFacade();
01021 };
01022
01023
01024
01037 class SimpleCommand : public Notifier, public ICommand
01038 {
01039 public:
01040 virtual void execute(INotification* notification) = 0;
01041 };
01042
01043
01044
01045
01073 class MacroCommand : public SimpleCommand
01074 {
01075 public:
01102 MacroCommand();
01112 void execute(INotification* notification);
01113 protected:
01137 void addSubCommand( ICommand* command );
01143 std::vector<ICommand*> subCommands;
01144 };
01165 template<class T>
01166 class Observer : public IObserverTemplated<T>
01167 {
01168 private:
01169 typedef void(T::*NotifyMethod)(INotification*);
01170 typedef T* NotifyContext;
01171 NotifyMethod notifyMethod;
01172 NotifyContext notifyContext;
01173 public:
01184 Observer( NotifyMethod method, NotifyContext context )
01185 {
01186 this->setNotifyMethod(method);
01187 this->setNotifyContext(context);
01188 }
01197 void setNotifyMethod( NotifyMethod method )
01198 {
01199 this->notifyMethod = method;
01200 }
01206 void setNotifyContext( NotifyContext context )
01207 {
01208 this->notifyContext = context;
01209 }
01215 NotifyMethod getNotifyMethod()
01216 {
01217 return this->notifyMethod;
01218 }
01224 NotifyContext getNotifyContext()
01225 {
01226 return this->notifyContext;
01227 }
01233 void notifyObserver( INotification* notification )
01234 {
01235 (this->notifyContext->*notifyMethod)(notification);
01236 }
01243 bool compareNotifyContext( intptr_t compareContextMemoryAddress )
01244 {
01245 return compareContextMemoryAddress == (intptr_t) &*this->getNotifyContext();
01246 }
01247 };
01248
01249
01250
01251 template<class T>
01252 class Proxy : public IProxyTemplated<T>, public Notifier
01253 {
01254 public:
01280 Proxy()
01281 {
01282 }
01283 Proxy(std::string proxyName, T data)
01284 {
01285 this->proxyName = proxyName;
01286 this->data = data;
01287 }
01288 Proxy(std::string proxyName)
01289 {
01290 this->proxyName = proxyName;
01291 }
01292 Proxy(T data)
01293 {
01294 this->data = data;
01295 }
01299 std::string getProxyName()
01300 {
01301 return this->proxyName;
01302 }
01303
01307 void setData( T data )
01308 {
01309 this->data = data;
01310 }
01311
01315 T getData()
01316 {
01317 return this->data;
01318 }
01319
01320 static std::string NAME;
01321
01322 protected:
01323
01324 std::string proxyName;
01325
01326
01327 T data;
01328 };
01329
01330
01331
01332 template<class T>
01333 class Mediator : public IMediatorTemplated<T>, public Notifier
01334 {
01335 public:
01339 Mediator( std::string mediatorName, T viewComponent )
01340 {
01341 this->mediatorName = mediatorName;
01342 this->setViewComponent(viewComponent);
01343 }
01344 Mediator( std::string mediatorName )
01345 {
01346 this->mediatorName = mediatorName;
01347 }
01348 Mediator( T viewComponent )
01349 {
01350 this->setViewComponent(viewComponent);
01351 }
01356 std::string getMediatorName()
01357 {
01358 return this->mediatorName;
01359 }
01360
01366 void setViewComponent( T viewComponent )
01367 {
01368 this->viewComponent = viewComponent;
01369 }
01370
01388 T getViewComponent()
01389 {
01390 return this->viewComponent;
01391 }
01392
01399 virtual std::vector<int> listNotificationInterests() = 0;
01400
01409 virtual void handleNotification( INotification* notification ) = 0;
01410
01414 virtual void onRegister() = 0;
01415
01419 virtual void onRemove() = 0;
01420
01426 std::string getName()
01427 {
01428 return this->mediatorName;
01429 }
01430
01439 static const std::string NAME;
01440
01441 protected:
01442
01443 std::string mediatorName;
01444
01445
01446 T viewComponent;
01447 };
01448 template<class T>
01449 const typename std::string Mediator<T>::NAME = "Mediator";
01450
01451
01452
01479 class Model : public MultitonKeyHeir, public IModel
01480 {
01481 public:
01497 Model();
01498
01504 static IModel* getInstance(std::string key);
01510 void registerProxy( IProxyRestricted* proxy );
01517 IProxyRestricted* retrieveProxy( std::string proxyName );
01518
01525 bool hasProxy( std::string proxyName );
01526
01533 IProxyRestricted* removeProxy( std::string proxyName );
01534
01540 static void removeModel( std::string key );
01541
01542 protected:
01543
01544 std::map <std::string, IProxyRestricted*> proxyMap;
01545 };
01546
01547
01548
01549
01569 class View : public MultitonKeyHeir, public IView
01570 {
01571 public:
01588 View( );
01594 static IView* getInstance( std::string key );
01602 void registerObserver ( int notificationName, IObserverRestricted* observer );
01613 void notifyObservers( INotification* notification );
01614
01621 void removeObserver( int notificationName, intptr_t contextAddress );
01622
01640 void registerMediator( IMediatorRestricted* mediator );
01641
01648 IMediatorRestricted* retrieveMediator( std::string mediatorName );
01649
01662 IMediatorRestricted* removeMediator( std::string mediatorName );
01663
01670 bool hasMediator( std::string mediatorName );
01671
01677 static void removeView( std::string key );
01678
01679 protected:
01680
01681 std::map<std::string, IMediatorRestricted*> mediatorMap;
01682
01683
01684 std::map<int, std::vector<IObserverRestricted*> > observerMap;
01685
01686 private:
01687 bool existsObserversInterestedIn(int notificationName);
01688 };
01689
01690
01691
01692
01726 class Controller : public MultitonKeyHeir, public IController
01727 {
01728 public:
01742 Controller();
01748 static IController* getInstance( std::string key );
01755 template<class T>
01756 void executeCommand( INotification* note )
01757 {
01758
01759
01760
01761
01762
01763
01764
01765
01766 int name = note->getName();
01767
01768 if(! this->hasCommand(name))
01769 return;
01770
01771 ICommand* command = new T();
01772 command->initializeNotifier(this->getMultitonKey());
01773 command->execute(note);
01774
01775 delete command;
01776 };
01792
01793 template<class T>
01794 void registerCommand(int notificationName)
01795 {
01796
01797
01798 if(this->hasCommand(notificationName))
01799 return;
01800
01801 Observer<Controller>* observer = new Observer<Controller>(&Controller::executeCommand<T>, this);
01802
01803 this->view->registerObserver(notificationName, observer);
01804
01805 commandMap[notificationName] = (T*) 0;
01806 };
01813 bool hasCommand( int notificationName );
01814
01820 void removeCommand( int notificationName );
01821
01827 static void removeController( std::string key );
01828
01829 protected:
01837 void initializeController();
01838
01839
01840 IView* view;
01841
01842
01843 std::map<int, ICommand*> commandMap;
01844 };
01845
01846
01847
01855 class Facade : public IFacade, public MultitonKeyHeir
01856 {
01857 public:
01871 Facade();
01877 static Facade* getInstance(std::string);
01884 template<class T>
01885 void registerCommand( int notificationName )
01886 {
01887 dynamic_cast<Controller*>(this->controller)->registerCommand<T>(notificationName);
01888 };
01889
01895 void removeCommand( int notificationName );
01896
01903 bool hasCommand( int notificationName );
01904
01911 void registerProxy ( IProxyRestricted* proxy );
01918 IProxyRestricted* retrieveProxy ( std::string proxyName );
01919
01926 IProxyRestricted* removeProxy ( std::string proxyName );
01927
01934 bool hasProxy( std::string proxyName );
01935
01942 void registerMediator( IMediatorRestricted* mediator );
01943
01950 IMediatorRestricted* retrieveMediator( std::string mediatorName );
01951
01958 IMediatorRestricted* removeMediator( std::string mediatorName );
01959
01966 bool hasMediator( std::string mediatorName );
01967
01978 void sendNotification( int notificationName, Object* body, int notificationType );
01979 void sendNotification( int notificationName, Object* body );
01980 void sendNotification( int notificationName, int notificationType );
01981 void sendNotification( int notificationName );
01995 void notifyObservers ( INotification* notification );
01996
02005 void initializeNotifier( std::string key );
02006
02013 static bool hasCore( std::string key );
02014
02023 static void removeCore( std::string key );
02024 protected:
02031 void initializeFacade();
02032
02049 void initializeController( );
02050
02074 void initializeModel( );
02075
02076
02099 void initializeView( );
02100
02101
02102
02103 IController* controller;
02104 IModel* model;
02105 IView* view;
02106 };
02107
02108 }
02109
02110 #endif