d6828818cea962e8c0fdcbd19d6a9ee4872af3cd
[cvc5.git] / src / base / listener.h
1 /********************* */
2 /*! \file listener.h
3 ** \verbatim
4 ** Original author: Tim King
5 ** Major contributors: none
6 ** Minor contributors (to current version): none
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2009-2016 New York University and The University of Iowa
9 ** See the file COPYING in the top-level source directory for licensing
10 ** information.\endverbatim
11 **
12 ** \brief Utility classes for listeners and collections of listeners.
13 **
14 ** Utilities for the development of a Listener interface class. This class
15 ** provides a single notification that must be overwritten. This file also
16 ** provides a utility class for a collection of listeners and an RAII style
17 ** RegisterListener class for managing the relationship between Listeners
18 ** and the manager.
19 **/
20
21 #include "cvc4_public.h"
22
23 #ifndef __CVC4__LISTENER_H
24 #define __CVC4__LISTENER_H
25
26 #include <list>
27
28 namespace CVC4 {
29
30 /**
31 * Listener interface class.
32 *
33 * The interface provides a notify() function.
34 */
35 class CVC4_PUBLIC Listener {
36 public:
37 Listener();
38 virtual ~Listener();
39
40 /** Note that notify may throw arbitrary exceptions. */
41 virtual void notify() = 0;
42 };
43
44 /**
45 * ListenerCollection is a list of Listener instances.
46 * One can add and remove Listeners.
47 *
48 * ListenerCollection does not own the memory of the Listeners.
49 * As a sanity check, it asserted that all of its listeners
50 * have been removed.
51 */
52 class CVC4_PUBLIC ListenerCollection {
53 public:
54 typedef std::list<Listener*> ListenerList;
55 typedef ListenerList::iterator iterator;
56
57 ListenerCollection();
58
59 ~ListenerCollection();
60
61 iterator addListener(Listener* listener);
62
63 void removeListener(iterator position);
64
65 void notify();
66
67 bool empty() const;
68
69 private:
70 ListenerList d_listeners;
71 };
72
73 /**
74 * RegisterListener is an RAII utility function for using Listener
75 * with ListenerCollection.
76 *
77 * On construction, the RegisterListener takes a ListenerCollection, collection,
78 * and a Listener*, listener. It takes over the memory for listener. It then
79 * add listener to collection. On destruction it removes listener and calls
80 * delete on listener.
81 *
82 * Because of this usage, a RegisterListener must be destroyed before
83 * collection.
84 */
85 class CVC4_PUBLIC RegisterListener {
86 public:
87 RegisterListener(ListenerCollection* collection, Listener* listener);
88 ~RegisterListener();
89
90 private:
91 Listener* d_listener;
92 ListenerCollection::iterator d_position;
93 ListenerCollection* d_collection;
94 };
95
96
97 }/* CVC4 namespace */
98
99 #endif /* __CVC4__LISTENER_H */