summaryrefslogtreecommitdiff
path: root/thirdparty/windows/include/coin/IpObserver.hpp
blob: 55ef7389b88f377f879b75f8bf608b1e2dbdf660 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright (C) 2004, 2006 International Business Machines and others.
// All Rights Reserved.
// This code is published under the Common Public License.
//
// $Id: IpObserver.hpp 1019 2007-06-24 03:52:34Z andreasw $
//
// Authors:  Carl Laird, Andreas Waechter     IBM    2004-08-13

#ifndef __IPOBSERVER_HPP__
#define __IPOBSERVER_HPP__

#include "IpUtils.hpp"
#include <vector>
#include <algorithm>

//#define IP_DEBUG_OBSERVER
#if COIN_IPOPT_CHECKLEVEL > 2
# define IP_DEBUG_OBSERVER
#endif
#ifdef IP_DEBUG_OBSERVER
# include "IpDebug.hpp"
#endif

namespace Ipopt
{
  /** Forward declarations */
  class Subject;

  /** Slight Variation of the Observer Design Pattern.
   *  This class implements the Observer class of the
   *  Observer Design Pattern. An Observer "Attach"es
   *  to a Subject, indicating that it would like to
   *  be notified of changes in the Subject.
   *  Any derived class wishing to recieve notifications
   *  from a Subject should inherit off of 
   *  Observer and overload the protected method,
   *  RecieveNotification_(...).
   */
  class Observer
  {
  public:
#ifdef IP_DEBUG_OBSERVER

    static const Index dbg_verbosity;
#endif

    /**@name Constructors/Destructors */
    //@{
    /** Default Constructor */
    Observer()
    {}

    /** Default destructor */
    virtual ~Observer();
    //@}

    /** Enumeration specifying the type of notification */
    enum NotifyType
    {
      NT_All,
      NT_BeingDestroyed,
      NT_Changed
    };

  protected:
    /** Derived classes should call this method
     * to request an "Attach" to a Subject. Do 
     * not call "Attach" explicitly on the Subject
     * since further processing is done here
     */
    void RequestAttach(NotifyType notify_type, const Subject* subject);

    /** Derived classes should call this method
     * to request a "Detach" to a Subject. Do 
     * not call "Detach" explicitly on the Subject
     * since further processing is done here
     */
    void RequestDetach(NotifyType notify_type, const Subject* subject);

    /** Derived classes should overload this method to
     * recieve the requested notification from 
     * attached Subjects
     */
    virtual void RecieveNotification(NotifyType notify_type, const Subject* subject)=0;

  private:
    /**@name Default Compiler Generated Methods
     * (Hidden to avoid implicit creation/calling).
     * These methods are not implemented and 
     * we do not want the compiler to implement
     * them for us, so we declare them private
     * and do not define them. This ensures that
     * they will not be implicitly created/called. */
    //@{
    /** Copy Constructor */
    Observer(const Observer&);

    /** Overloaded Equals Operator */
    void operator=(const Observer&);
    //@}

    /** A list of the subjects currently being
     *  observed. */
    std::vector<const Subject*> subjects_;

    /** Private Method for Recieving Notification
     *  should only be called by the friend class
     *  Subject. This method will, in turn, call
     *  the overloaded RecieveNotification method
     *  for the derived class to process.
     */
    void ProcessNotification(NotifyType notify_type, const Subject* subject);

    friend class Subject;
  };

  /** Slight Variation of the Observer Design Pattern (Subject part).
   *  This class implements the Subject class of the Observer Design
   *  Pattern. An Observer "Attach"es to a Subject, indicating that it
   *  would like to be notified of changes in the Subject.  Any
   *  derived class that is to be observed has to inherit off the
   *  Subject base class.  If the subject needs to notify the
   *  Observer, it calls the Notify method.
   */
  class Subject
  {
  public:
#ifdef IP_DEBUG_OBSERVER

    static const Index dbg_verbosity;
#endif

    /**@name Constructors/Destructors */
    //@{
    /** Default Constructor */
    Subject()
    {}

    /** Default destructor */
    virtual ~Subject();
    //@}

    /**@name Methods to Add and Remove Observers.
     *  Currently, the notify_type flags are not used,
     *  and Observers are attached in general and will
     *  recieve all notifications (of the type requested
     *  and possibly of types not requested). It is 
     *  up to the observer to ignore the types they
     *  are not interested in. The NotifyType in the
     *  parameter list is so a more efficient mechanism
     *  depending on type could be implemented later if
     *  necessary.*/
    //@{

    /** Attach the specified observer
     *  (i.e., begin recieving notifications). */
    void AttachObserver(Observer::NotifyType notify_type, Observer* observer) const;

    /** Detach the specified observer
     *  (i.e., no longer recieve notifications). */
    void DetachObserver(Observer::NotifyType notify_type, Observer* observer) const;
    //@}

  protected:

    void Notify(Observer::NotifyType notify_type) const;

  private:
    /**@name Default Compiler Generated Methods
     * (Hidden to avoid implicit creation/calling).
     * These methods are not implemented and 
     * we do not want the compiler to implement
     * them for us, so we declare them private
     * and do not define them. This ensures that
     * they will not be implicitly created/called. */
    //@{
    /** Copy Constructor */
    Subject(const Subject&);

    /** Overloaded Equals Operator */
    void operator=(const Subject&);
    //@}

    mutable std::vector<Observer*> observers_;

  };

  /* inline methods */
  inline
  Observer::~Observer()
  {
#ifdef IP_DEBUG_OBSERVER
    DBG_START_METH("Observer::~Observer", dbg_verbosity);
    if (DBG_VERBOSITY()>=1) {
      for (Index i=0; i<(Index)subjects_.size(); i++) {
        DBG_PRINT((1,"subjects_[%d] = 0x%x\n", i, subjects_[i]));
      }
    }
#endif
    // Detach all subjects
    for (Int i=(Int)(subjects_.size()-1); i>=0; i--) {
#ifdef IP_DEBUG_OBSERVER
      DBG_PRINT((1,"About to detach subjects_[%d] = 0x%x\n", i, subjects_[i]));
#endif

      RequestDetach(NT_All, subjects_[i]);
    }
  }

  inline
  void Observer::RequestAttach(NotifyType notify_type, const Subject* subject)
  {
#ifdef IP_DEBUG_OBSERVER
    DBG_START_METH("Observer::RequestAttach", dbg_verbosity);

    // Add the subject to the list if it does not already exist
    std::vector<const Subject*>::iterator attached_subject;
    attached_subject = std::find(subjects_.begin(), subjects_.end(), subject);
    DBG_ASSERT(attached_subject == subjects_.end());
    DBG_ASSERT(subject);
#endif

    // add the subject to the list
    subjects_.push_back(subject);
    // Attach the observer to the subject
    subject->AttachObserver(notify_type, this);
  }

  inline
  void Observer::RequestDetach(NotifyType notify_type, const Subject* subject)
  {
#ifdef IP_DEBUG_OBSERVER
    DBG_START_METH("Observer::RequestDetach", dbg_verbosity);
    DBG_PRINT((1, "Requesting detach of subject: 0x%x\n", subject));
    DBG_ASSERT(subject);
#endif

    if (subject) {
      std::vector<const Subject*>::iterator attached_subject;
      attached_subject = std::find(subjects_.begin(), subjects_.end(), subject);
#ifdef IP_DEBUG_OBSERVER

      DBG_ASSERT(attached_subject != subjects_.end());
#endif

      if (attached_subject != subjects_.end()) {
#ifdef IP_DEBUG_OBSERVER
        DBG_PRINT((1, "Removing subject: 0x%x from the list\n", subject));
#endif

        subjects_.erase(attached_subject);
      }

      // Detach the observer from the subject
      subject->DetachObserver(notify_type, this);
    }
  }

  inline
  void Observer::ProcessNotification(NotifyType notify_type, const Subject* subject)
  {
#ifdef IP_DEBUG_OBSERVER
    DBG_START_METH("Observer::ProcessNotification", dbg_verbosity);
    DBG_ASSERT(subject);
#endif

    if (subject) {
      std::vector<const Subject*>::iterator attached_subject;
      attached_subject = std::find(subjects_.begin(), subjects_.end(), subject);

      // We must be processing a notification for a
      // subject that was previously attached.
#ifdef IP_DEBUG_OBSERVER

      DBG_ASSERT(attached_subject != subjects_.end());
#endif

      this->RecieveNotification(notify_type, subject);

      if (notify_type == NT_BeingDestroyed) {
        // the subject is going away, remove it from our list
        subjects_.erase(attached_subject);
      }
    }
  }

  inline
  Subject::~Subject()
  {
#ifdef IP_DEBUG_OBSERVER
    DBG_START_METH("Subject::~Subject", dbg_verbosity);
#endif

    std::vector<Observer*>::iterator iter;
    for (iter = observers_.begin(); iter != observers_.end(); iter++) {
      (*iter)->ProcessNotification(Observer::NT_BeingDestroyed, this);
    }
  }

  inline
  void Subject::AttachObserver(Observer::NotifyType notify_type, Observer* observer) const
  {
#ifdef IP_DEBUG_OBSERVER
    DBG_START_METH("Subject::AttachObserver", dbg_verbosity);
    // current implementation notifies all observers of everything
    // they must filter the notifications that they are not interested
    // in (i.e. a hub, not a router)
    DBG_ASSERT(observer);

    std::vector<Observer*>::iterator attached_observer;
    attached_observer = std::find(observers_.begin(), observers_.end(), observer);
    DBG_ASSERT(attached_observer == observers_.end());

    DBG_ASSERT(observer);
#endif

    observers_.push_back(observer);
  }

  inline
  void Subject::DetachObserver(Observer::NotifyType notify_type, Observer* observer) const
  {
#ifdef IP_DEBUG_OBSERVER
    DBG_START_METH("Subject::DetachObserver", dbg_verbosity);
    DBG_ASSERT(observer);
#endif

    if (observer) {
      std::vector<Observer*>::iterator attached_observer;
      attached_observer = std::find(observers_.begin(), observers_.end(), observer);
#ifdef IP_DEBUG_OBSERVER

      DBG_ASSERT(attached_observer != observers_.end());
#endif

      if (attached_observer != observers_.end()) {
        observers_.erase(attached_observer);
      }
    }
  }

  inline
  void Subject::Notify(Observer::NotifyType notify_type) const
  {
#ifdef IP_DEBUG_OBSERVER
    DBG_START_METH("Subject::Notify", dbg_verbosity);
#endif

    std::vector<Observer*>::iterator iter;
    for (iter = observers_.begin(); iter != observers_.end(); iter++) {
      (*iter)->ProcessNotification(notify_type, this);
    }
  }


} // namespace Ipopt

#endif