[settings] enhance Settings::ChildInfoIterator (#5272)

This commit updates `ChildInfoIterator` in `Settings` class to
enable range-based `for` loop.
This commit is contained in:
Abtin Keshavarzian
2020-07-22 08:48:12 -07:00
committed by GitHub
parent df2fa4c548
commit 812eeee027
3 changed files with 87 additions and 27 deletions
+1 -8
View File
@@ -336,7 +336,7 @@ exit:
return error;
}
otError Settings::DeleteChildInfo(void)
otError Settings::DeleteAllChildInfo(void)
{
otError error;
@@ -353,13 +353,6 @@ Settings::ChildInfoIterator::ChildInfoIterator(Instance &aInstance)
, mIndex(0)
, mIsDone(false)
{
Reset();
}
void Settings::ChildInfoIterator::Reset(void)
{
mIndex = 0;
mIsDone = false;
Read();
}
+83 -15
View File
@@ -153,7 +153,7 @@ private:
* This class provides structure definitions for different settings keys.
*
*/
class SettingsBase : public InstanceLocator, private NonCopyable
class SettingsBase : public InstanceLocator
{
public:
/**
@@ -622,8 +622,10 @@ protected:
* This class defines methods related to non-volatile storage of settings.
*
*/
class Settings : public SettingsBase
class Settings : public SettingsBase, private NonCopyable
{
class ChildInfoIteratorBuilder;
public:
/**
* This constructor initializes a `Settings` object.
@@ -824,7 +826,20 @@ public:
* @retval OT_ERROR_NOT_IMPLEMENTED The platform does not implement settings functionality.
*
*/
otError DeleteChildInfo(void);
otError DeleteAllChildInfo(void);
/**
* This method enables range-based `for` loop iteration over all child info entries in the `Settings`.
*
* This method should be used as follows:
*
* for (const ChildInfo &childInfo : Get<Settings>().IterateChildInfo()) { ... }
*
*
* @returns A ChildInfoIteratorBuilder instance.
*
*/
ChildInfoIteratorBuilder IterateChildInfo(void) { return ChildInfoIteratorBuilder(GetInstance()); }
/**
* This class defines an iterator to access all Child Info entries in the settings.
@@ -832,6 +847,8 @@ public:
*/
class ChildInfoIterator : public SettingsBase
{
friend class ChildInfoIteratorBuilder;
public:
/**
* This constructor initializes a `ChildInfoInterator` object.
@@ -841,12 +858,6 @@ public:
*/
explicit ChildInfoIterator(Instance &aInstance);
/**
* This method resets the iterator to start from the first Child Info entry in the list.
*
*/
void Reset(void);
/**
* This method indicates whether there are no more Child Info entries in the list (iterator has reached end of
* the list), or the current entry is valid.
@@ -857,12 +868,6 @@ public:
*/
bool IsDone(void) const { return mIsDone; }
/**
* This method advances the iterator to move to the next Child Info entry in the list (if any).
*
*/
void Advance(void);
/**
* This method overloads operator `++` (pre-increment) to advance the iterator to move to the next Child Info
* entry in the list (if any).
@@ -898,7 +903,58 @@ public:
*/
otError Delete(void);
/**
* This method overloads the `*` dereference operator and gets a reference to `ChildInfo` entry to which the
* iterator is currently pointing.
*
* @note This method should be used only if `IsDone()` is returning FALSE indicating that the iterator is
* pointing to a valid entry.
*
*
* @returns A reference to the `ChildInfo` entry currently pointed by the iterator.
*
*/
const ChildInfo &operator*(void)const { return mChildInfo; }
/**
* This method overloads operator `==` to evaluate whether or not two iterator instances are equal.
*
* @param[in] aOther The other iterator to compare with.
*
* @retval TRUE If the two iterator objects are equal
* @retval FALSE If the two iterator objects are not equal.
*
*/
bool operator==(const ChildInfoIterator &aOther) const
{
return (mIsDone && aOther.mIsDone) || (!mIsDone && !aOther.mIsDone && (mIndex == aOther.mIndex));
}
/**
* This method overloads operator `!=` to evaluate whether or not two iterator instances are unequal.
*
* @param[in] aOther The other iterator to compare with.
*
* @retval TRUE If the two iterator objects are unequal.
* @retval FALSE If the two iterator objects are not unequal.
*
*/
bool operator!=(const ChildInfoIterator &aOther) const { return !(*this == aOther); }
private:
enum IteratorType
{
kEndIterator,
};
ChildInfoIterator(Instance &aInstance, IteratorType)
: SettingsBase(aInstance)
, mIndex(0)
, mIsDone(true)
{
}
void Advance(void);
void Read(void);
ChildInfo mChildInfo;
@@ -943,6 +999,18 @@ public:
#endif // OPENTHREAD_CONFIG_DUA_ENABLE
private:
class ChildInfoIteratorBuilder : public InstanceLocator
{
public:
ChildInfoIteratorBuilder(Instance &aInstance)
: InstanceLocator(aInstance)
{
}
ChildInfoIterator begin(void) { return ChildInfoIterator(GetInstance()); }
ChildInfoIterator end(void) { return ChildInfoIterator(GetInstance(), ChildInfoIterator::kEndIterator); }
};
otError Read(Key aKey, void *aBuffer, uint16_t &aSize) const;
otError Save(Key aKey, const void *aValue, uint16_t aSize);
otError Add(Key aKey, const void *aValue, uint16_t aSize);
+3 -4
View File
@@ -3663,10 +3663,9 @@ void MleRouter::RestoreChildren(void)
bool foundDuplicate = false;
uint16_t numChildren = 0;
for (Settings::ChildInfoIterator iter(GetInstance()); !iter.IsDone(); iter++)
for (const Settings::ChildInfo &childInfo : Get<Settings>().IterateChildInfo())
{
Child * child;
const Settings::ChildInfo &childInfo = iter.GetChildInfo();
Child *child;
child = mChildTable.FindChild(childInfo.GetExtAddress(), Child::kInStateAnyExceptInvalid);
@@ -3742,7 +3741,7 @@ otError MleRouter::StoreChild(const Child &aChild)
void MleRouter::RefreshStoredChildren(void)
{
SuccessOrExit(Get<Settings>().DeleteChildInfo());
SuccessOrExit(Get<Settings>().DeleteAllChildInfo());
for (Child &child : Get<ChildTable>().Iterate(Child::kInStateAnyExceptInvalid))
{