52 class AbstractLifeCycleManager;
53 template<
typename T>
class LifeCycle;
158 return register_impl_(instance);
181 if ((slot ==
nullptr) || (*slot ==
nullptr))
185 source.manager_ =
nullptr;
228 if ((slot ==
nullptr) || (*slot ==
nullptr))
return false;
231 dest.id_ = source.id_;
232 dest.manager_ =
this;
234 source.manager_ =
nullptr;
271 : size_{size}, slots_{slots}, free_slots_{size}
275 ~AbstractLifeCycleManager() =
default;
276 AbstractLifeCycleManager(
const AbstractLifeCycleManager&) =
delete;
277 AbstractLifeCycleManager& operator=(
const AbstractLifeCycleManager&) =
delete;
281 uint8_t register_impl_(AbstractLifeCycle& instance)
284 if (free_slots_ == 0)
return 0;
286 if (instance.id_ != 0)
return 0;
288 AbstractLifeCycle** slot = &slots_[0];
289 for (uint8_t i = 0; i < size_; ++i)
291 if (*slot ==
nullptr)
293 const uint8_t
id = i + 1;
295 instance.manager_ =
this;
306 AbstractLifeCycle* find_impl_(uint8_t
id)
const
308 AbstractLifeCycle** slot = find_slot_(
id);
315 AbstractLifeCycle** find_slot_(uint8_t
id)
const
317 if ((
id == 0) || (
id > size_))
320 return &slots_[
id - 1];
324 AbstractLifeCycle** slots_;
433 template<
typename T>
class Proxy;
466 : ptr_{reinterpret_cast<uintptr_t>(&dest)}, is_dynamic_{false} {}
480 : id_{dest.
id()}, ptr_{reinterpret_cast<uintptr_t>(dest.
manager())}, is_dynamic_{true}
487 constexpr
Proxy() =
default;
489 : id_{that.id_}, ptr_{that.ptr_}, is_dynamic_{that.is_dynamic_}
495 template<
typename U> Proxy& operator=(
const Proxy<U>& that)
499 if (
this == &that)
return *
this;
502 is_dynamic_ = that.is_dynamic_;
552 return (is_dynamic_ ?
nullptr :
reinterpret_cast<T*
>(ptr_));
570 return reinterpret_cast<T*
>(ptr_);
577 bool is_dynamic_ : 1;
580 template<
typename U>
friend class Proxy;
581 friend bool operator==<T>(
const Proxy<T>&,
const Proxy<T>&);
582 friend bool operator!=<T>(
const Proxy<T>&,
const Proxy<T>&);
613 bool operator==(
const Proxy<T>& a,
const Proxy<T>& b)
615 if (&a == &b)
return true;
616 return (a.is_dynamic_ == b.is_dynamic_) && (a.ptr_ == b.ptr_) && (a.id_ == b.id_);
620 bool operator!=(
const Proxy<T>& a,
const Proxy<T>& b)
622 if (&a == &b)
return false;
623 return (a.is_dynamic_ != b.is_dynamic_) || (a.ptr_ != b.ptr_) || (a.id_ != b.id_);
649 LightProxy(
const T& dest) : ptr_{reinterpret_cast<uintptr_t>(&dest)}, is_dynamic_{false} {}
683 ptr_ =
reinterpret_cast<uintptr_t
>(proxy.
destination());
690 template<
typename U> LightProxy(
const LightProxy<U>& that) : content_{that.content_}
695 template<
typename U> LightProxy& operator=(
const LightProxy<U>& that)
699 if (
this == (
const LightProxy<T>*) &that)
return *
this;
700 content_ = that.content_;
715 return manager->find_<T>(id_);
717 return reinterpret_cast<T*
>(ptr_);
748 return (is_dynamic_ ?
nullptr :
reinterpret_cast<T*
>(ptr_));
758 uint16_t is_dynamic_ : 1;
763 uint8_t reserved_ : 7;
764 uint8_t is_dynamic2_ : 1;
768 template<
typename U>
friend class LightProxy;
769 friend bool operator==<T>(
const LightProxy<T>&,
const LightProxy<T>&);
770 friend bool operator!=<T>(
const LightProxy<T>&,
const LightProxy<T>&);
801 bool operator==(
const LightProxy<T>& a,
const LightProxy<T>& b)
803 if (&a == &b)
return true;
804 return (a.is_dynamic_ == b.is_dynamic_) && (a.ptr_ == b.ptr_);
808 bool operator!=(
const LightProxy<T>& a,
const LightProxy<T>& b)
810 if (&a == &b)
return false;
811 return (a.is_dynamic_ != b.is_dynamic_) || (a.ptr_ != b.ptr_);
832 DirectProxy(
const T& dest) : ptr_{reinterpret_cast<uintptr_t>(&dest)} {}
841 template<
typename U> DirectProxy& operator=(
const DirectProxy<U>& that)
845 if (
this == &that)
return *
this;
858 return reinterpret_cast<T*
>(ptr_);
886 return reinterpret_cast<T*
>(ptr_);
912 bool operator==(
const DirectProxy<T>& a,
const DirectProxy<T>& b)
914 if (&a == &b)
return true;
915 return (a.ptr_ == b.ptr_);
919 bool operator!=(
const DirectProxy<T>& a,
const DirectProxy<T>& b)
921 if (&a == &b)
return false;
922 return (a.ptr_ != b.ptr_);
The abstract base class of all LifeCycle<T>.
AbstractLifeCycleManager * manager() const
A pointer to the AbstractLifeCycleManager handling this instance.
uint8_t id() const
The unique identifier of this lifecycle-managed instance, as provided by the LifeCycleManager it was ...
The abstract base class of all LifeCycleManager.
bool move_(uint8_t id, AbstractLifeCycle &dest)
Move an already registered LifeCycle<T> instance (identified by id) to a new location,...
LifeCycle< T > * find_(uint8_t id) const
Find an existing LifeCycle<T> registered with this LifeCycleManager and identified by id.
bool unregister_(uint8_t id)
Unregisters a LifeCycle<T> instance, identified by id, already registered with this LifeCycleManager.
uint8_t register_(LifeCycle< T > &instance)
Register a LifeCycle<T> instance with this LifeCycleManager.
uint8_t available_() const
Return the number of available "slots" for registration of new LifeCycle<T> instances.
A kind of proxy class that encapsulates access to a fixed T instance.
DirectProxy(const T &dest)
Create a DirectProxy<T> to a static reference.
bool is_dynamic() const
Tell if this DirectProxy is dynamic or static.
T * destination() const
A pointer to the static instance proxified.
uint8_t id() const
The identifier of the proxified LifeCycle<U> or 0 if a static instance was proxified.
T * operator()(UNUSED const AbstractLifeCycleManager *manager=nullptr) const
Return a pointer to the proxified T instance.
A mixin class allowing lifecycle management of any object of any type T.
LifeCycle & operator=(LifeCycle &&that)=default
Assign this LifeCycle<T> with that, by moving that already existing LifeCycle<T> instance.
LifeCycle()
Create a new LifeCycle<T> mixin for an object of type T.
LifeCycle(LifeCycle &&that)=default
Crate a new LifeCycle<T> mixin object of type T, by moving an already existing LifeCycle<T> instance ...
LifeCycle(const T &value)
Create a new LifeCycle<T> mixin for object value of type T.
LifeCycle(T &&value)
Create a new LifeCycle<T> mixin for object value of type T.
~LifeCycle()=default
Destroy this LifeCycle<T> instance.
An actual LifeCycleManager implementation, based on AbstractLifeCycleManager, adding static storage f...
A light proxy class that encapsulates access to a fixed T instance, or to a dynamic LifeCycle<T> inst...
uint8_t id() const
The identifier of the proxified LifeCycle<U> or 0 if a static instance was proxified.
bool is_dynamic() const
Tell if this LightProxy is dynamic or static.
LightProxy(const Proxy< T > &proxy)
Create a LightProxy<T> from a Proxy<T>.
T * destination() const
A pointer to the static instance proxified, or nullptr if a dynamic instance (a LifeCycle<U>) was pro...
T * operator()(const AbstractLifeCycleManager *manager=nullptr) const
Return a pointer to the proxified T instance.
LightProxy(const LifeCycle< U > &dest)
Create a LightProxy<T> to a LifeCycle<U> instance (dynamic reference).
LightProxy(const T &dest)
Create a LightProxy<T> to a static reference.
A proxy class that encapsulates access to a fixed T instance, or to a dynamic LifeCycle<T> instance.
Proxy(const T &dest)
Create a Proxy<T> to a static reference.
Proxy(const LifeCycle< U > &dest)
Create a Proxy<T> to a LifeCycle<U> instance (dynamic reference).
bool is_dynamic() const
Tell if this Proxy is dynamic or static.
uint8_t id() const
The identifier of the proxified LifeCycle<U> or 0 if a static instance was proxified.
T * destination() const
A pointer to the static instance proxified, or nullptr if a dynamic instance (a LifeCycle<U>) was pro...
T * operator->()
Overloaded -> operator, allowing access to proxified type T instance members.
AbstractLifeCycleManager * manager() const
The LifeCycleManager managing the proxified LifeCycle<U>, or nullptr if a static instance was proxifi...
T & operator*()
Return a reference to the proxified T instance.
#define UNUSED
Specific GCC attribute to declare an argument or variable unused, so that the compiler does not emit ...
Contains the API around LifeCycle management implementation.
DirectProxy< T > make_direct_proxy(const T &dest)
Utility template function to create a DirectProxy<T> from dest without the need to speicify T.
LightProxy< T > make_light_proxy(const T &dest)
Utility template function to create a LightProxy<T> from dest without the need to speicify T.
Proxy< T > make_proxy(const T &dest)
Utility template function to create a Proxy<T> from dest without the need to speicify T.
Similar to standard C++ std namespace, this namespace is used by FastArduino library to implement var...
bool operator==(const RTTTime &a, const RTTTime &b)
Compare 2 RTTTime instances.
bool operator!=(const RTTTime &a, const RTTTime &b)
Compare 2 RTTTime instances.
Utility class that checks, at compile-time, that type T is a subtype of type B.
Useful traits for common types.