Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C++] Add setter extensions #47

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
54 changes: 54 additions & 0 deletions cpp/Platform.Setters.Tests/SetterExtensionsTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using TLinkAddress = std::int32_t;

namespace Platform::Setters::Tests
{
TEST(SetterExtensionsTests, SetFirstFromFirstListAndReturnTrue)
{
TLinkAddress list1[] { 1,2,3 };
TLinkAddress list2[] { 4,5,6 };
Setter<TLinkAddress> setter {};
bool decision { SetterExtensions::SetFirstFromFirstListAndReturnTrue(setter, list1, list2) };
ASSERT_EQ(setter.GetTrue(), decision);
ASSERT_EQ(list1[0], setter.Result());
}

TEST(SetterExtensionsTests, SetSecondFromFirstListAndReturnTrue)
{
TLinkAddress list1[] { 1,2,3 };
TLinkAddress list2[] { 4,5,6 };
Setter<TLinkAddress> setter {};
bool decision { SetterExtensions::SetSecondFromFirstListAndReturnTrue(setter, list1, list2) };
ASSERT_EQ(setter.GetTrue(), decision);
ASSERT_EQ(list1[1], setter.Result());
}

TEST(SetterExtensionsTests, SetFirstFromSecondListAndReturnTrue)
{
TLinkAddress list1[] { 1,2,3 };
TLinkAddress list2[] { 4,5,6 };
Setter<TLinkAddress> setter {};
bool decision { SetterExtensions::SetFirstFromSecondListAndReturnTrue(setter, list1, list2) };
ASSERT_EQ(setter.GetTrue(), decision);
ASSERT_EQ(list2[0], setter.Result());
}

TEST(SetterExtensionsTests, SetSecondFromSecondListAndReturnTrue)
{
TLinkAddress list1[] { 1,2,3 };
TLinkAddress list2[] { 4,5,6 };
Setter<TLinkAddress> setter {};
bool decision { SetterExtensions::SetSecondFromSecondListAndReturnTrue(setter, list1, list2) };
ASSERT_EQ(setter.GetTrue(), decision);
ASSERT_EQ(list2[1], setter.Result());
}

TEST(SetterExtensionsTests, SetThirdFromSecondListAndReturnTrue)
{
TLinkAddress list1[] { 1,2,3 };
TLinkAddress list2[] { 4,5,6 };
Setter<TLinkAddress> setter {};
bool decision { SetterExtensions::SetThirdFromSecondListAndReturnTrue(setter, list1, list2) };
ASSERT_EQ(setter.GetTrue(), decision);
ASSERT_EQ(list2[2], setter.Result());
}
}
1 change: 1 addition & 0 deletions cpp/Platform.Setters.Tests/SetterTests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <gtest/gtest.h>
#include <Platform.Setters.h>
#include "SetterExtensionsTests.cpp"

namespace Platform::Setters::Tests
{
Expand Down
1 change: 1 addition & 0 deletions cpp/Platform.Setters/Platform.Setters.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
#include "SetterBase.h"
#include "Setter[TResult].h"
#include "Setter[TResult, TDecision].h"
#include "SetterExtensions.h"
2 changes: 1 addition & 1 deletion cpp/Platform.Setters/SetterBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@

public: void Set(TResult value) { _result = value; }

public: ~SetterBase() requires Interfaces::ISetter<decltype(*this), TResult> = default;
public: ~SetterBase() requires Interfaces::CSetter<decltype(*this), TResult> = default;
};
}
48 changes: 48 additions & 0 deletions cpp/Platform.Setters/SetterExtensions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace Platform::Setters
{
class SetterExtensions
{
public:
template<typename TResult, typename TDecision>
static TDecision SetFirstFromFirstListAndReturnTrue(Setter<TResult, TDecision>& setter, Interfaces::CArray<TResult> auto&& list1, Interfaces::CArray<TResult> auto&& list2)
{
setter.Set(list1[0]);
return setter.GetTrue();
}

template<typename TResult, typename TDecision>
static TDecision SetSecondFromFirstListAndReturnTrue(Setter<TResult, TDecision>& setter, Interfaces::CArray<TResult> auto&& list1, Interfaces::CArray<TResult> auto&& list2)
{
setter.Set(list1[1]);
return setter.GetTrue();
}

template<typename TResult, typename TDecision>
static TDecision SetThirdFromFirstListAndReturnTrue(Setter<TResult, TDecision>& setter, Interfaces::CArray<TResult> auto&& list1, Interfaces::CArray<TResult> auto&& list2)
{
setter.Set(list1[2]);
return setter.GetTrue();
}

template<typename TResult, typename TDecision>
static TDecision SetFirstFromSecondListAndReturnTrue(Setter<TResult, TDecision>& setter, Interfaces::CArray<TResult> auto&& list1, Interfaces::CArray<TResult> auto&& list2)
{
setter.Set(list2[0]);
return setter.GetTrue();
}

template<typename TResult, typename TDecision>
static TDecision SetSecondFromSecondListAndReturnTrue(Setter<TResult, TDecision>& setter, Interfaces::CArray<TResult> auto&& list1, Interfaces::CArray<TResult> auto&& list2)
{
setter.Set(list2[1]);
return setter.GetTrue();
}

template<typename TResult, typename TDecision>
static TDecision SetThirdFromSecondListAndReturnTrue(Setter<TResult, TDecision>& setter, Interfaces::CArray<TResult> auto&& list1, Interfaces::CArray<TResult> auto&& list2)
{
setter.Set(list2[2]);
return setter.GetTrue();
}
};
}
26 changes: 19 additions & 7 deletions cpp/Platform.Setters/Setter[TResult, TDecision].h
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,32 @@
template <typename TResult, typename TDecision> class Setter<TResult, TDecision> : public SetterBase<TResult>
{
using base = SetterBase<TResult>;
private: TDecision _trueValue {};
private: TDecision _falseValue {};
private:
TDecision _trueValue {};
TDecision _falseValue {};

public: Setter(TDecision trueValue, TDecision falseValue, TResult defaultValue)
: _trueValue(trueValue), _falseValue(falseValue), base(defaultValue)
public: Setter(TDecision _trueValue, TDecision _falseValue, TResult defaultValue)
: _trueValue(_trueValue), _falseValue(_falseValue), base(defaultValue)
{
}

public: Setter(TDecision trueValue, TDecision falseValue) : Setter(trueValue, falseValue, {}) { }
public: Setter(TDecision _trueValue, TDecision _falseValue) : Setter(_trueValue, _falseValue, {}) { }

public: explicit Setter(TResult defaultValue) : base(defaultValue) { }

public: Setter() = default;

public:
TDecision GetTrue()
{
return _trueValue;
}

TDecision GetFalse()
{
return _falseValue;
}

public: TDecision SetAndReturnTrue(TResult value)
{
base::_result = value;
Expand All @@ -30,13 +42,13 @@
return _falseValue;
}

public: TDecision SetFirstAndReturnTrue(Interfaces::IArray<TResult> auto&& list)
public: TDecision SetFirstAndReturnTrue(Interfaces::CArray<TResult> auto&& list)
{
base::_result = list[0];
return _trueValue;
}

public: TDecision SetFirstAndReturnFalse(Interfaces::IArray<TResult> auto&& list)
public: TDecision SetFirstAndReturnFalse(Interfaces::CArray<TResult> auto&& list)
{
base::_result = list[0];
return _falseValue;
Expand Down
4 changes: 2 additions & 2 deletions cpp/conanfile.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[requires]
gtest/cci.20210126
platform.interfaces/0.1.3
platform.interfaces/0.2.5

[generators]
cmake
cmake