PureMVC PureMVC

include/PureMVC/Core/View.hpp

Go to the documentation of this file.
00001 //  View.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_VIEW_HPP__)
00010 #define __PUREMVC_CORE_VIEW_HPP__
00011 
00012 // STL include
00013 #include <string>
00014 #include <map>
00015 #include <set>
00016 #include <exception>
00017 #include <cassert>
00018 #include <stdexcept>
00019 // PureMVC include
00020 #if !defined(__PUREMVC_HPP__)
00021 #define __PUREMVC_INCLUDE__
00022 #include "../PureMVC.hpp"
00023 #endif /* __PUREMVC_HPP__ */
00024 
00025 #include "../Interfaces/IView.hpp"
00026 #include "../Interfaces/IObserver.hpp"
00027 #include "../Interfaces/INotification.hpp"
00028 #include "../Interfaces/IMediator.hpp"
00029 #include "../Interfaces/IAggregate.hpp"
00030 #include "../Interfaces/IIterator.hpp"
00031 #include "../Patterns/Iterator/Iterator.hpp"
00032 
00033 namespace PureMVC
00034 {
00035     namespace Core
00036     {
00037         using Interfaces::IObserver;
00038         using Interfaces::IView;
00039         using Interfaces::INotification;
00040         using Interfaces::IMediator;
00041         using Interfaces::IAggregate;
00042         using Interfaces::IIterator;
00043 
00063         class PUREMVC_API View : public virtual IView
00064         {
00065         protected:
00066             typedef std::map<std::string, IMediator*> MediatorMap;
00067             typedef std::multimap<std::string, IObserver*> ObserverMap;
00068         protected:
00069             /*
00070              * Define iterator converter for getting mediator name only.
00071              */
00072             struct IteratorConverter
00073 #if !defined(__DMC__) // The C++ compiler of Digital Mars cannot resolve this case
00074             : public std::unary_function<MediatorMap::const_iterator, MediatorMap::key_type>
00075 #endif
00076             {
00077             /*
00078              * Converting operator.
00079              *
00080              * @param iterator the iterator of internal container.
00081              * @return the name of mediator inside iterator.
00082              */
00083                 MediatorMap::key_type const& operator()(MediatorMap::const_iterator const& iterator) const;
00084             };
00085             /*
00086              * Define iterator range of internal container.
00087              */
00088             struct IteratorRange {
00089                 /*
00090                  * Get begin point.
00091                  *
00092                  * @return the begin of iterator.
00093                  */
00094                 MediatorMap::const_iterator begin(MediatorMap const* const& iterator) const;
00095                 /*
00096                  * Get end point.
00097                  *
00098                  * @return the end of iterator.
00099                  */
00100                 MediatorMap::const_iterator end(MediatorMap const* const& iterator) const;
00101             };
00102         protected:
00103             struct IPassiveObserver {
00104                 virtual ~IPassiveObserver(void);
00105             };
00106             class RemovingObserver : public IPassiveObserver {
00107             private:
00108                 ObserverMap::iterator _item;
00109             public:
00110                 explicit RemovingObserver(ObserverMap::iterator const& item);
00111                 ObserverMap::iterator const& operator()(void) const;
00112             private:
00113                 RemovingObserver(RemovingObserver const&);
00114                 RemovingObserver& operator=(RemovingObserver const&);
00115             public:
00116                 virtual ~RemovingObserver(void);
00117             };
00118             class InsertedObserver : public IPassiveObserver {
00119             private:
00120                 ObserverMap::value_type _item;
00121             public:
00122                 explicit InsertedObserver(ObserverMap::value_type const& item);
00123                 ObserverMap::value_type const& operator()(void) const;
00124             private:
00125                 InsertedObserver(InsertedObserver const&);
00126                 InsertedObserver& operator=(InsertedObserver const&);
00127             public:
00128                 virtual ~InsertedObserver(void);
00129             };
00130         public:
00131             static char const* const DEFAULT_KEY;
00132         protected:
00133             // Message Constants
00134             static char const* const MULTITON_MSG;
00135         protected:
00136             // The Multiton Key for this Core
00137             std::string const _multiton_key;
00138             // Mapping of Mediator names to Mediator instances
00139             MediatorMap _mediator_map;
00140             // Mapping of Notification names to Observer lists
00141             ObserverMap _observer_map;
00142             // In loop flag
00143             int _inloop_counter;
00144             typedef std::list<IPassiveObserver*> PassiveObserverList;
00145             // Being remove observer
00146             PassiveObserverList _passive_observer_list;
00147             // Synchronous access
00148             mutable Mutex _synchronous_access;
00149         private:
00150             View(View const&);
00151             View(IView const&);
00152             View& operator=(View const&);
00153             View& operator=(IView const&);
00154         protected:
00155             static IView* find(std::string const& key);
00156             static void insert(std::string const& key, IView* view);
00157         public:
00170             explicit View(std::string const& key = PureMVC::Core::View::DEFAULT_KEY);
00171         protected:
00195             template<typename _DerivedType>
00196             explicit View(_DerivedType* instance, std::string const& key = PureMVC::Core::View::DEFAULT_KEY)
00197             :_multiton_key(key)
00198             {
00199                 if (find(key))
00200                     throw std::runtime_error(MULTITON_MSG);
00201                 insert(_multiton_key, this);
00202                 instance->_DerivedType::initializeView();
00203             }
00204 
00216             virtual void initializeView(void);
00217 
00218         public:
00224             static IView& getInstance(std::string const& key = PureMVC::Core::View::DEFAULT_KEY);
00225 
00233             virtual void registerObserver (std::string const& notification_name, IObserver* observer);
00234 
00245             virtual void notifyObservers(INotification const& notification);
00246 
00253             virtual void removeObserver(std::string const& notification_name, IObserver const* notify_context);
00254 
00271             virtual void registerMediator(IMediator* mediator);
00272 
00279             virtual IMediator const& retrieveMediator(std::string const& mediator_name) const;
00280 
00287             virtual IMediator& retrieveMediator(std::string const& mediator_name);
00288 
00295             virtual IMediator* removeMediator(std::string const& mediator_name);
00296 
00303             virtual bool hasMediator(std::string const& mediator_name) const;
00304 
00310             virtual MediatorNames listMediatorNames(void) const;
00311 
00317             static void removeView(std::string const& key);
00318 
00322             virtual ~View(void);
00323 
00324             void joinAllPassiveObserver(void);
00325         };
00326     }
00327 }
00328 
00329 #endif /* __PUREMVC_CORE_VIEW_HPP__ */