SHARE
TWEET

Untitled

a guest Feb 27th, 2020 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Example program
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <memory>
  7.  
  8. struct IDisposableObserver;
  9.  
  10. struct IObservable
  11. {
  12.   virtual void add(IDisposableObserver* observer) = 0;
  13.   virtual void remove(IDisposableObserver* observer) = 0;
  14. };
  15.  
  16. struct IDisposableObserver
  17. {
  18.     IObservable* obs;
  19.     IDisposableObserver(IObservable* o) : obs(o) {}
  20.    
  21.     virtual void notify() = 0;
  22.    
  23.     virtual ~IDisposableObserver()
  24.     {
  25.         std::cout << "~IDisposableObserver(): Dispose observer" << std::endl;
  26.        
  27.         obs->remove(this);
  28.     }
  29. };
  30.  
  31. struct Endpoint : public IDisposableObserver
  32. {
  33.     Endpoint(const std::string& ldn, IObservable* obs) : IDisposableObserver(obs)
  34.     {
  35.         obs->add(this);    
  36.     }
  37.    
  38.     virtual void notify()
  39.     {
  40.         std::cout << "Notify " << std::endl;
  41.     }  
  42. };
  43.  
  44. struct Server
  45. {
  46.     std::vector<std::unique_ptr<Endpoint>> endpoints;
  47.     void publishEndpoint(const std::string& ldn, IObservable* observable)
  48.     {
  49.         auto endpoint = std::make_unique<Endpoint>(ldn, observable);
  50.         endpoints.push_back(std::move(endpoint));
  51.     }
  52. };
  53.  
  54.  
  55. struct Fru : public IObservable
  56. {
  57.     std::vector<IDisposableObserver*> observers;
  58.    
  59.     void add(IDisposableObserver* observer) override
  60.     {
  61.         auto it = std::find_if(observers.begin(), observers.end(), [&](const IDisposableObserver* ptr) { return ptr == observer; } );
  62.        
  63.         if (it != observers.end())
  64.         {
  65.             return;
  66.         }
  67.        
  68.         observers.push_back(observer);
  69.     }
  70.    
  71.     void remove(IDisposableObserver* observer) override
  72.     {
  73.         auto it = std::find_if(observers.begin(), observers.end(), [&](const IDisposableObserver* ptr) { return ptr == observer; } );
  74.        
  75.         if (it != observers.end())
  76.         {
  77.             return;
  78.         }
  79.        
  80.         observers.erase(it);
  81.     }
  82.    
  83.     void doIt()
  84.     {
  85.         for (const auto& x : observers)
  86.         {
  87.             x->notify();
  88.         }
  89.     }
  90. };
  91.  
  92.  
  93.  
  94.  
  95. int main()
  96. {
  97.     Fru fru{};
  98.    
  99.     {
  100.     Server usi{};
  101.     usi.publishEndpoint("ldn=1", &fru);
  102.     }
  103.    
  104.    
  105.     fru.doIt();
  106. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top