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
|
///\file
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2023 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_DELEGATE_OBSERVER_INCLUDED
#define ETL_DELEGATE_OBSERVER_INCLUDED
#include "algorithm.h"
#include "platform.h"
#include "delegate.h"
#include "vector.h"
#include "exception.h"
#include "error_handler.h"
#include "utility.h"
namespace etl
{
//***************************************************************************
///\ingroup observer
/// The base class for delegate observer exceptions.
//***************************************************************************
class delegate_observer_exception : public exception
{
public:
delegate_observer_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
: exception(reason_, file_name_, line_number_)
{
}
};
//***************************************************************************
///\ingroup observer
/// The exception thrown when the delegate observer list is full.
//***************************************************************************
class delegate_observer_list_full : public delegate_observer_exception
{
public:
delegate_observer_list_full(string_type file_name_, numeric_type line_number_)
: delegate_observer_exception(ETL_ERROR_TEXT("delegate_observable:full", ETL_DELEGATE_OBSERVER_FILE_ID"A"), file_name_, line_number_)
{
}
};
//*********************************************************************
/// The object that is being observed.
///\tparam MAX_OBSERVERS The maximum number of observers that can be accommodated.
///\ingroup observer
//*********************************************************************
template <typename TNotification, const size_t MAX_OBSERVERS>
class delegate_observable
{
public:
typedef etl::delegate<void(TNotification)> observer_type;
private:
//***********************************
// Item stored in the observer list.
//***********************************
struct observer_item
{
observer_item(observer_type observer_)
: observer(observer_)
, enabled(true)
{
}
observer_type observer;
bool enabled;
};
//***********************************
// How to compare an observer with an observer list item.
//***********************************
struct compare_observers
{
compare_observers(observer_type& observer_)
: observer(&observer_)
{
}
bool operator ()(const observer_item& item) const
{
return observer == item.observer;
}
observer_type observer;
};
typedef etl::vector<observer_item, MAX_OBSERVERS> Observer_List;
public:
typedef size_t size_type;
typedef TNotification notification_type;
//*****************************************************************
/// Add an observer to the list.
/// If asserts or exceptions are enabled then an etl::observable_observer_list_full
/// is emitted if the observer list is already full.
///\param observer A reference to the observer.
//*****************************************************************
void add_observer(observer_type& observer)
{
// See if we already have it in our list.
typename Observer_List::iterator i_observer_item = find_observer(observer);
// Not there?
if (i_observer_item == observer_list.end())
{
// Is there enough room?
ETL_ASSERT_OR_RETURN(!observer_list.full(), ETL_ERROR(etl::observer_list_full));
// Add it.
observer_list.push_back(observer_item(observer));
}
}
//*****************************************************************
/// Remove a particular observer from the list.
///\param observer A reference to the observer.
///\return <b>true</b> if the observer was removed, <b>false</b> if not.
//*****************************************************************
bool remove_observer(observer_type& observer)
{
// See if we have it in our list.
typename Observer_List::iterator i_observer_item = find_observer(observer);
// Found it?
if (i_observer_item != observer_list.end())
{
// Erase it.
observer_list.erase(i_observer_item);
return true;
}
else
{
return false;
}
}
//*****************************************************************
/// Enable an observer
///\param observer A reference to the observer.
///\param state <b>true</b> to enable, <b>false</b> to disable. Default is enable.
//*****************************************************************
void enable_observer(observer_type& observer, bool state = true)
{
// See if we have it in our list.
typename Observer_List::iterator i_observer_item = find_observer(observer);
// Found it?
if (i_observer_item != observer_list.end())
{
i_observer_item->enabled = state;
}
}
//*****************************************************************
/// Disable an observer
//*****************************************************************
void disable_observer(observer_type& observer)
{
// See if we have it in our list.
typename Observer_List::iterator i_observer_item = find_observer(observer);
// Found it?
if (i_observer_item != observer_list.end())
{
i_observer_item->enabled = false;
}
}
//*****************************************************************
/// Clear all observers from the list.
//*****************************************************************
void clear_observers()
{
observer_list.clear();
}
//*****************************************************************
/// Returns the number of observers.
//*****************************************************************
size_type number_of_observers() const
{
return observer_list.size();
}
//*****************************************************************
/// Notify all of the observers, sending them the notification.
///\tparam TNotification The notification type.
///\param n The notification.
//*****************************************************************
void notify_observers(TNotification n)
{
typename Observer_List::iterator i_observer_item = observer_list.begin();
while (i_observer_item != observer_list.end())
{
if (i_observer_item->enabled)
{
i_observer_item->p_observer->notification(n);
}
++i_observer_item;
}
}
protected:
~observable()
{
}
private:
//*****************************************************************
/// Find an observer in the list.
/// Returns the end of the list if not found.
//*****************************************************************
typename Observer_List::iterator find_observer(observer_type& observer_)
{
return etl::find_if(observer_list.begin(), observer_list.end(), compare_observers(observer_));
}
/// The list of observers.
Observer_List observer_list;
};
}
#endif
|