[locator] adding InstanceLocatorInit (#4569)

This commits adds a new class `InstanceLocatorInit` which is
similar to `InstanceLoator` but provides a default constructor
(instead of a parameterized one) and allows an inheriting class to
initialize the object (set the OpenThread Instance) post constructor
call using the `Init()` method. This class is intended for types that
require a default constructor and cannot use a parameterized one. An
example of such a class is one that is used as type of a C array
element (e.g., `Neighbor`/`Child`/`Router` classes which are used as
array in `ChildTable`/`RouterTable`).
This commit is contained in:
Abtin Keshavarzian
2020-02-20 09:36:34 -08:00
committed by Jonathan Hui
parent 0823114dff
commit 67c5081d14
+48 -5
View File
@@ -71,6 +71,8 @@ extern uint64_t gInstanceRaw[];
*/
class InstanceLocator
{
friend class InstanceLocatorInit;
public:
/**
* This method returns a reference to the parent OpenThread Instance.
@@ -79,7 +81,7 @@ public:
*
*/
#if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
Instance &GetInstance(void) const { return mInstance; }
Instance &GetInstance(void) const { return *mInstance; }
#else
Instance &GetInstance(void) const { return *reinterpret_cast<Instance *>(&gInstanceRaw); }
#endif
@@ -101,23 +103,64 @@ protected:
/**
* This constructor initializes the object.
*
* @param[in] aInstance A pointer to the otInstance.
* @param[in] aInstance A reference to the OpenThread Instance.
*
*/
explicit InstanceLocator(Instance &aInstance)
#if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
: mInstance(aInstance)
: mInstance(&aInstance)
#endif
{
OT_UNUSED_VARIABLE(aInstance);
}
#if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
private:
Instance &mInstance;
InstanceLocator(void) {}
#if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
Instance *mInstance;
#endif
};
/**
* This class implements a locator for an OpenThread Instance object.
*
* The `InstanceLocatorInit` is similar to `InstanceLoator` but provides a default constructor (instead of a
* parameterized one) and allows an inheriting class to initialize the object (set the OpenThread Instance) post
* constructor call using the `Init()` method. This class is intended for types that require a default constructor and
* cannot use a parameterized one. (e.g., `Neighbor`/`Child`/`Router` classes which are used as a C array element type
* in`ChildTable`/`RouterTable`).
*
* The inheriting class from `InstanceLocatorInit` should ensure that object is properly initialized after the object
* is created and more importantly that it is re-initialized when/if it is cleared or reset.
*
*/
class InstanceLocatorInit : public InstanceLocator
{
protected:
/**
* This is the default constructor for the `InstanceLocatorInit` object.
*
*/
InstanceLocatorInit(void)
: InstanceLocator()
{
}
/**
* This method (re)initializes the object and sets the OpenThread Instance.
*
* @param[in] aInstance A reference to the OpenThread Instance.
*/
void Init(Instance &aInstance)
{
#if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
mInstance = &aInstance;
#endif
OT_UNUSED_VARIABLE(aInstance);
}
};
/**
* This class implements a locator for owner of an object.
*