File: observable.h

package info (click to toggle)
kicad 5.0.2%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 234,592 kB
  • sloc: cpp: 505,330; ansic: 57,038; python: 4,886; sh: 879; awk: 294; makefile: 253; xml: 103; perl: 5
file content (252 lines) | stat: -rw-r--r-- 7,651 bytes parent folder | download | duplicates (2)
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
/*
* This program source code file is part of KICAD, a free EDA CAD application.
*
* Copyright (C) 2016 Kicad Developers, see change_log.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

#ifndef COMMON_OBSERVABLE_H__
#define COMMON_OBSERVABLE_H__

#include <cassert>
#include <memory>
#include <vector>
#include <utility>

/*
model subscriber implementation using links to represent connections.
subscribers can be removed during notification.
if no observers are registered, size is the size of a shared_ptr.
*/

namespace UTIL {

    class LINK;

    namespace DETAIL {

        struct OBSERVABLE_BASE {
        public:
            OBSERVABLE_BASE();
            OBSERVABLE_BASE( OBSERVABLE_BASE& other );

            ~OBSERVABLE_BASE();

            size_t size() const;

        private:
            friend class UTIL::LINK;

            struct IMPL
            {
                IMPL( OBSERVABLE_BASE* owned_by = nullptr );
                bool is_shared() const;
                void set_shared();
                ~IMPL();

                void add_observer( void* observer );
                void remove_observer( void* observer );
                void collect();

                bool is_iterating() const;

                void enter_iteration();
                void leave_iteration();

                std::vector<void*> observers_;
                unsigned int iteration_count_;
                OBSERVABLE_BASE* owned_by_;
            };

            void allocate_impl();
            void allocate_shared_impl();

            void deallocate_impl();

            std::shared_ptr<IMPL> get_shared_impl();

        protected:
            void on_observers_empty();

            void enter_iteration();
            void leave_iteration();

            void add_observer( void* observer );
            void remove_observer( void* observer );

            std::shared_ptr<IMPL> impl_;
        };

    }

    //
    // Simple RAII-handle to a subscription.
    //
    class LINK {
    public:
        LINK();
        LINK( std::shared_ptr<DETAIL::OBSERVABLE_BASE::IMPL> token, void* observer );
        LINK( LINK&& other );
        LINK( const LINK& ) = delete;

        void operator=( const LINK& ) = delete;
        LINK& operator=( LINK&& other );

        void reset();

        explicit operator bool() const;

        ~LINK();

    private:
        std::shared_ptr<DETAIL::OBSERVABLE_BASE::IMPL> token_;
        void* observer_;
    };

    //
    //
    //
    template<typename ObserverInterface>
    class OBSERVABLE :
        public DETAIL::OBSERVABLE_BASE
    {
    public:
        /**
        * Function Observable()
        * Constructor. Constructs an observable with empty non-shared subscription list.
        */
        OBSERVABLE() {}

        /**
        * Function Observable(OBSERVABLE&)
        * Constructor. Constructs an observable with a shared subscription list.
        * @param aInherit Observable to share the subscription list with.
        */
        OBSERVABLE( OBSERVABLE& aInherit )
            : OBSERVABLE_BASE( aInherit )
        {}

        /**
         * Function SubscribeUnmanaged
         * adds a subscription without RAII link.
         * @param aObserver Observer to subscribe
         */
        void SubscribeUnmanaged( ObserverInterface* aObserver )
        {
            OBSERVABLE_BASE::add_observer( static_cast<void*>(aObserver) );
        }

        /**
         * Function Subscribe
         * adds a subscription returning an RAII link
         * @param aObserver observer to subscribe
         * @return RAII link controlling the lifetime of the subscription
         */
        LINK Subscribe( ObserverInterface* aObserver ) {
            OBSERVABLE_BASE::add_observer( static_cast<void*>(aObserver) );
            return LINK( impl_, static_cast<void*>(aObserver) );
        }

        /**
         * Function Unsubscribe
         * cancels the subscription of a subscriber. Can be called during notification calls.
         * @param aObserver observer to remove from the subscription list
         */
        void Unsubscribe( ObserverInterface* aObserver )
        {
            OBSERVABLE_BASE::remove_observer( static_cast<void*>(aObserver) );
        }

        /**
         * Function Notify
         * Notifies event to all subscribed observers.
         * @param Ptr pointer to method of the Observer-interface
         * @param aArgs list of arguments to each notification call, will be perfectly forwarded.
         */
        template< typename... Args1, typename... Args2 >
        void Notify( void(ObserverInterface::*Ptr)(Args1...), Args2&&... aArgs )
        {
            static_assert(sizeof...(Args1) == sizeof...(Args2), "argument counts don't match");

            if( impl_ )
            {
                enter_iteration();
                try {
                    for( auto* void_ptr : impl_->observers_ )
                    {
                        if( void_ptr )
                        {
                            auto* typed_ptr = static_cast<ObserverInterface*>(void_ptr);
                            (typed_ptr->*Ptr)(std::forward<Args2>( aArgs )...);
                        }
                    }
                }
                catch(...)
                {
                    leave_iteration();
                    throw;
                }

                leave_iteration();
            }
        }

        /**
        * Function Notify
        * Notifies event to all subscribed observers but one to be ignore.
        * @param Ptr pointer to method of the Observer-interface
        * @param aIgnore observer to ignore during this notification
        * @param aArgs list of arguments to each notification call, will be perfectly forwarded.
        */
        template< typename... Args1, typename... Args2 >
        void NotifyIgnore( void(ObserverInterface::*Ptr)(Args1...), ObserverInterface* aIgnore,
                           Args2&&... aArgs )
        {
            static_assert(sizeof...(Args1) == sizeof...(Args2), "argument counts don't match");

            if( impl_ )
            {
                enter_iteration();

                try
                {
                    for(auto* void_ptr : impl_->observers_)
                    {
                        if( void_ptr && void_ptr != aIgnore )
                        {
                            auto* typed_ptr = static_cast<ObserverInterface*>(void_ptr);
                            (typed_ptr->*Ptr)(std::forward<Args2>( aArgs )...);
                        }
                    }
                }
                catch(...)
                {
                    leave_iteration();
                    throw;
                }

                leave_iteration();
            }
        }

    };

}

#endif