NCBI C++ ToolKit
publisher_subscriber.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef GUI_WIDGETS_EDIT___PUBLISHER_SUBSCRIBER__HPP
2 #define GUI_WIDGETS_EDIT___PUBLISHER_SUBSCRIBER__HPP
3 
4 /* $Id: publisher_subscriber.hpp 40298 2018-01-23 21:34:09Z asztalos $
5 * ===========================================================================
6 *
7 * PUBLIC DOMAIN NOTICE
8 * National Center for Biotechnology Information
9 *
10 * This software/database is a "United States Government Work" under the
11 * terms of the United States Copyright Act. It was written as part of
12 * the author's official duties as a United States Government employee and
13 * thus cannot be copyrighted. This software/database is freely available
14 * to the public for use. The National Library of Medicine and the U.S.
15 * Government have not placed any restriction on its use or reproduction.
16 *
17 * Although all reasonable efforts have been taken to ensure the accuracy
18 * and reliability of the software and data, the NLM and the U.S.
19 * Government do not and cannot warrant the performance or results that
20 * may be obtained by using this software or data. The NLM and the U.S.
21 * Government disclaim all warranties, express or implied, including
22 * warranties of performance, merchantability or fitness for any particular
23 * purpose.
24 *
25 * Please cite the author in any work or product based on this material.
26 *
27 * ===========================================================================
28 *
29 * Authors: Vladislav Evgeniev
30 */
31 
32 #include <vector>
33 #include <assert.h>
34 
36 
37 template<typename T>
38 class ISubscriberT {
39 public:
40  virtual void Update(const T& value) = 0;
41 };
42 
43 template<typename T>
45 {
46 public:
48  {
49  }
50 
51  CPublisherT(CPublisherT<T> const& publisher)
52  {
53  m_Subscribers = publisher.m_Subscribers;
54  m_Value = publisher.m_Value;
55  Notify();
56  }
57 
58  CPublisherT<T>& operator=(T const& rhs)
59  {
60  m_Value = rhs;
61  Notify();
62  return (*this);
63  }
64 
65  bool operator==(T const& rhs) const
66  {
67  return (m_Value == rhs);
68  }
69 
70  bool operator!=(T const& rhs) const
71  {
72  return (m_Value != rhs);
73  }
74 
75  operator T const&() const
76  {
77  return m_Value;
78  }
79 
80  T const& get() const
81  {
82  return m_Value;
83  }
84 
85  void Attach(ISubscriberT<T>* pSubscriber) const
86  {
87  assert(pSubscriber);
88  m_Subscribers.push_back(pSubscriber);
89  }
90 
91  void Detach(ISubscriberT<T>* pSubscriber) const
92  {
93  assert(pSubscriber);
94  typename TSubscribersVector::iterator itToBeDetached;
95  for (itToBeDetached = m_Subscribers.begin(); itToBeDetached != m_Subscribers.end(); ++itToBeDetached) {
96  if (pSubscriber != *itToBeDetached)
97  continue;
98 
99  m_Subscribers.erase(itToBeDetached);
100  break;
101  }
102  }
103 
104  void Notify()
105  {
106  typename TSubscribersVector::iterator itSubscriber;
107  for (itSubscriber = m_Subscribers.begin(); itSubscriber != m_Subscribers.end(); ++itSubscriber)
108  (*itSubscriber)->Update(m_Value);
109  }
110 private:
111  typedef std::vector<ISubscriberT<T>*> TSubscribersVector;
114 };
115 
117 
118 #endif // GUI_WIDGETS_EDIT___PUBLISHER_SUBSCRIBER__HPP
bool operator!=(T const &rhs) const
void Attach(ISubscriberT< T > *pSubscriber) const
void Detach(ISubscriberT< T > *pSubscriber) const
CPublisherT(CPublisherT< T > const &publisher)
TSubscribersVector m_Subscribers
T const & get() const
CPublisherT< T > & operator=(T const &rhs)
bool operator==(T const &rhs) const
std::vector< ISubscriberT< T > * > TSubscribersVector
virtual void Update(const T &value)=0
#define T(s)
Definition: common.h:230
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
#define assert(x)
Definition: srv_diag.hpp:58
Modified on Fri Sep 20 14:58:04 2024 by modify_doxy.py rev. 669887