Skip to content

Commit

Permalink
Variant: add inplace constructor (#13232)
Browse files Browse the repository at this point in the history
  • Loading branch information
kghost authored and pull[bot] committed Sep 1, 2023
1 parent 0187252 commit 1590820
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/credentials/FabricTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#pragma once

#include <algorithm>

#include <app/util/basic-types.h>
#include <credentials/CHIPCert.h>
#include <crypto/CHIPCryptoPAL.h>
Expand Down
6 changes: 4 additions & 2 deletions src/credentials/GroupDataProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
*/
#pragma once

#include <algorithm>
#include <stdint.h>
#include <sys/types.h>

#include <app-common/zap-generated/cluster-objects.h>
#include <app/util/basic-types.h>
#include <crypto/CHIPCryptoPAL.h>
#include <lib/core/CHIPError.h>
#include <stdint.h>
#include <sys/types.h>
#include <transport/raw/MessageHeader.h>

namespace chip {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

#include <memory>

#import <Foundation/Foundation.h>
#import <Security/Security.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

#include <algorithm>

#import "CHIPOperationalCredentialsDelegate.h"

#import <Security/Security.h>
Expand Down
8 changes: 8 additions & 0 deletions src/lib/core/InPlace.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ struct InPlaceType
};
constexpr InPlaceType InPlace{};

template <class T>
struct InPlaceTemplateType
{
explicit InPlaceTemplateType() = default;
};
template <class T>
constexpr InPlaceTemplateType<T> InPlaceTemplate{};

} // namespace chip
3 changes: 2 additions & 1 deletion src/lib/core/Optional.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
*/
#pragma once

#include <new>

#include <lib/core/CHIPCore.h>
#include <lib/core/InPlace.h>
#include <lib/support/Variant.h>

namespace chip {

Expand Down
13 changes: 10 additions & 3 deletions src/lib/support/Variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <typeinfo>
#include <utility>

#include <lib/core/InPlace.h>

namespace chip {

namespace VariantInternal {
Expand Down Expand Up @@ -148,6 +150,13 @@ struct Variant
public:
Variant() : mTypeId(kInvalidType) {}

template <typename T, class... Args>
constexpr explicit Variant(InPlaceTemplateType<T>, Args &&... args) :
mTypeId(VariantInternal::TupleIndexOfType<T, std::tuple<Ts...>>::value)
{
new (&mData) T(std::forward<Args>(args)...);
}

Variant(const Variant<Ts...> & that) : mTypeId(that.mTypeId) { Curry::Copy(that.mTypeId, &that.mData, &mData); }

Variant(Variant<Ts...> && that) : mTypeId(that.mTypeId)
Expand Down Expand Up @@ -193,9 +202,7 @@ struct Variant
template <typename T, typename... Args>
static Variant<Ts...> Create(Args &&... args)
{
Variant<Ts...> instance;
instance.template Set<T>(std::forward<Args>(args)...);
return instance;
return Variant<Ts...>(InPlaceTemplate<T>, std::forward<Args>(args)...);
}

template <typename T, typename... Args>
Expand Down
27 changes: 22 additions & 5 deletions src/lib/support/tests/TestVariant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* limitations under the License.
*/

#include <functional>

#include <lib/support/UnitTestRegistration.h>
#include <lib/support/Variant.h>

Expand Down Expand Up @@ -208,6 +210,21 @@ void TestVariantMoveAssign(nlTestSuite * inSuite, void * inContext)
NL_TEST_ASSERT(inSuite, v2.Get<Pod>().m2 == 10);
}

void TestVariantInPlace(nlTestSuite * inSuite, void * inContext)
{
int i = 0;

Variant<std::reference_wrapper<int>> v1 = Variant<std::reference_wrapper<int>>(InPlaceTemplate<std::reference_wrapper<int>>, i);
NL_TEST_ASSERT(inSuite, v1.Valid());
NL_TEST_ASSERT(inSuite, v1.Is<std::reference_wrapper<int>>());
NL_TEST_ASSERT(inSuite, &v1.Get<std::reference_wrapper<int>>().get() == &i);

Variant<std::reference_wrapper<int>> v2 = Variant<std::reference_wrapper<int>>::Create<std::reference_wrapper<int>>(i);
NL_TEST_ASSERT(inSuite, v2.Valid());
NL_TEST_ASSERT(inSuite, v2.Is<std::reference_wrapper<int>>());
NL_TEST_ASSERT(inSuite, &v2.Get<std::reference_wrapper<int>>().get() == &i);
}

void TestVariantCompare(nlTestSuite * inSuite, void * inContext)
{
Variant<Simple, Pod> v0;
Expand Down Expand Up @@ -268,11 +285,11 @@ int Teardown(void * inContext)
/**
* Test Suite. It lists all the test functions.
*/
static const nlTest sTests[] = {
NL_TEST_DEF_FN(TestVariantSimple), NL_TEST_DEF_FN(TestVariantMovable), NL_TEST_DEF_FN(TestVariantCtorDtor),
NL_TEST_DEF_FN(TestVariantCopy), NL_TEST_DEF_FN(TestVariantMove), NL_TEST_DEF_FN(TestVariantCopyAssign),
NL_TEST_DEF_FN(TestVariantMoveAssign), NL_TEST_DEF_FN(TestVariantCompare), NL_TEST_SENTINEL()
};
static const nlTest sTests[] = { NL_TEST_DEF_FN(TestVariantSimple), NL_TEST_DEF_FN(TestVariantMovable),
NL_TEST_DEF_FN(TestVariantCtorDtor), NL_TEST_DEF_FN(TestVariantCopy),
NL_TEST_DEF_FN(TestVariantMove), NL_TEST_DEF_FN(TestVariantCopyAssign),
NL_TEST_DEF_FN(TestVariantMoveAssign), NL_TEST_DEF_FN(TestVariantInPlace),
NL_TEST_DEF_FN(TestVariantCompare), NL_TEST_SENTINEL() };

int TestVariant()
{
Expand Down

0 comments on commit 1590820

Please sign in to comment.