Skip to content

Commit 026d755

Browse files
authored
Variant: add inplace constructor (project-chip#13232)
1 parent 65ba5df commit 026d755

8 files changed

+52
-11
lines changed

src/credentials/FabricTable.h

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#pragma once
2323

24+
#include <algorithm>
25+
2426
#include <app/util/basic-types.h>
2527
#include <credentials/CHIPCert.h>
2628
#include <crypto/CHIPCryptoPAL.h>

src/credentials/GroupDataProvider.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
*/
1717
#pragma once
1818

19+
#include <algorithm>
20+
#include <stdint.h>
21+
#include <sys/types.h>
22+
1923
#include <app-common/zap-generated/cluster-objects.h>
2024
#include <app/util/basic-types.h>
2125
#include <crypto/CHIPCryptoPAL.h>
2226
#include <lib/core/CHIPError.h>
23-
#include <stdint.h>
24-
#include <sys/types.h>
2527
#include <transport/raw/MessageHeader.h>
2628

2729
namespace chip {

src/darwin/Framework/CHIP/CHIPOperationalCredentialsDelegate.h

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18+
#include <memory>
19+
1820
#import <Foundation/Foundation.h>
1921
#import <Security/Security.h>
2022

src/darwin/Framework/CHIP/CHIPOperationalCredentialsDelegate.mm

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18+
#include <algorithm>
19+
1820
#import "CHIPOperationalCredentialsDelegate.h"
1921

2022
#import <Security/Security.h>

src/lib/core/InPlace.h

+8
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,12 @@ struct InPlaceType
3333
};
3434
constexpr InPlaceType InPlace{};
3535

36+
template <class T>
37+
struct InPlaceTemplateType
38+
{
39+
explicit InPlaceTemplateType() = default;
40+
};
41+
template <class T>
42+
constexpr InPlaceTemplateType<T> InPlaceTemplate{};
43+
3644
} // namespace chip

src/lib/core/Optional.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
*/
2424
#pragma once
2525

26+
#include <new>
27+
2628
#include <lib/core/CHIPCore.h>
2729
#include <lib/core/InPlace.h>
28-
#include <lib/support/Variant.h>
2930

3031
namespace chip {
3132

src/lib/support/Variant.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include <typeinfo>
2828
#include <utility>
2929

30+
#include <lib/core/InPlace.h>
31+
3032
namespace chip {
3133

3234
namespace VariantInternal {
@@ -148,6 +150,13 @@ struct Variant
148150
public:
149151
Variant() : mTypeId(kInvalidType) {}
150152

153+
template <typename T, class... Args>
154+
constexpr explicit Variant(InPlaceTemplateType<T>, Args &&... args) :
155+
mTypeId(VariantInternal::TupleIndexOfType<T, std::tuple<Ts...>>::value)
156+
{
157+
new (&mData) T(std::forward<Args>(args)...);
158+
}
159+
151160
Variant(const Variant<Ts...> & that) : mTypeId(that.mTypeId) { Curry::Copy(that.mTypeId, &that.mData, &mData); }
152161

153162
Variant(Variant<Ts...> && that) : mTypeId(that.mTypeId)
@@ -193,9 +202,7 @@ struct Variant
193202
template <typename T, typename... Args>
194203
static Variant<Ts...> Create(Args &&... args)
195204
{
196-
Variant<Ts...> instance;
197-
instance.template Set<T>(std::forward<Args>(args)...);
198-
return instance;
205+
return Variant<Ts...>(InPlaceTemplate<T>, std::forward<Args>(args)...);
199206
}
200207

201208
template <typename T, typename... Args>

src/lib/support/tests/TestVariant.cpp

+22-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* limitations under the License.
1717
*/
1818

19+
#include <functional>
20+
1921
#include <lib/support/UnitTestRegistration.h>
2022
#include <lib/support/Variant.h>
2123

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

213+
void TestVariantInPlace(nlTestSuite * inSuite, void * inContext)
214+
{
215+
int i = 0;
216+
217+
Variant<std::reference_wrapper<int>> v1 = Variant<std::reference_wrapper<int>>(InPlaceTemplate<std::reference_wrapper<int>>, i);
218+
NL_TEST_ASSERT(inSuite, v1.Valid());
219+
NL_TEST_ASSERT(inSuite, v1.Is<std::reference_wrapper<int>>());
220+
NL_TEST_ASSERT(inSuite, &v1.Get<std::reference_wrapper<int>>().get() == &i);
221+
222+
Variant<std::reference_wrapper<int>> v2 = Variant<std::reference_wrapper<int>>::Create<std::reference_wrapper<int>>(i);
223+
NL_TEST_ASSERT(inSuite, v2.Valid());
224+
NL_TEST_ASSERT(inSuite, v2.Is<std::reference_wrapper<int>>());
225+
NL_TEST_ASSERT(inSuite, &v2.Get<std::reference_wrapper<int>>().get() == &i);
226+
}
227+
211228
void TestVariantCompare(nlTestSuite * inSuite, void * inContext)
212229
{
213230
Variant<Simple, Pod> v0;
@@ -268,11 +285,11 @@ int Teardown(void * inContext)
268285
/**
269286
* Test Suite. It lists all the test functions.
270287
*/
271-
static const nlTest sTests[] = {
272-
NL_TEST_DEF_FN(TestVariantSimple), NL_TEST_DEF_FN(TestVariantMovable), NL_TEST_DEF_FN(TestVariantCtorDtor),
273-
NL_TEST_DEF_FN(TestVariantCopy), NL_TEST_DEF_FN(TestVariantMove), NL_TEST_DEF_FN(TestVariantCopyAssign),
274-
NL_TEST_DEF_FN(TestVariantMoveAssign), NL_TEST_DEF_FN(TestVariantCompare), NL_TEST_SENTINEL()
275-
};
288+
static const nlTest sTests[] = { NL_TEST_DEF_FN(TestVariantSimple), NL_TEST_DEF_FN(TestVariantMovable),
289+
NL_TEST_DEF_FN(TestVariantCtorDtor), NL_TEST_DEF_FN(TestVariantCopy),
290+
NL_TEST_DEF_FN(TestVariantMove), NL_TEST_DEF_FN(TestVariantCopyAssign),
291+
NL_TEST_DEF_FN(TestVariantMoveAssign), NL_TEST_DEF_FN(TestVariantInPlace),
292+
NL_TEST_DEF_FN(TestVariantCompare), NL_TEST_SENTINEL() };
276293

277294
int TestVariant()
278295
{

0 commit comments

Comments
 (0)