1 /*
2  *  The ManaPlus Client
3  *  Copyright (C) 2014-2019  The ManaPlus Developers
4  *  Copyright (C) 2019-2021  Andrei Karas
5  *
6  *  This file is part of The ManaPlus Client.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef LISTENERS_BASELISTENER_HPP
23 #define LISTENERS_BASELISTENER_HPP
24 
25 #include "utils/vector.h"
26 
27 #define defineListener(name) \
28     STD_VECTOR<name*> name::mListeners; \
29     \
30     name::name() \
31     { \
32         addListener(this); \
33     } \
34     \
35     name::~name() \
36     { \
37         removeListener(this); \
38     } \
39     \
40     void name::addListener(name *const listener) \
41     { \
42         if (listener) \
43             mListeners.push_back(listener); \
44     } \
45     \
46     void name::removeListener(const name *const listener) \
47     { \
48         STD_VECTOR<name*>::iterator it = mListeners.begin(); \
49         while (it != mListeners.end()) \
50         { \
51             if (*it == listener) \
52                 it = mListeners.erase(it); \
53             else \
54                 ++ it; \
55         } \
56     }
57 
58 #define defineListenerHeader(name) \
59     public: \
60         name(); \
61         \
62         virtual ~name(); \
63         \
64         static void addListener(name *const listener); \
65         \
66         static void removeListener(const name *const listener); \
67         \
68     private: \
69         static STD_VECTOR<name*> mListeners;
70 
71 #endif  // LISTENERS_BASELISTENER_HPP
72