Skip to content

Commit 37442a2

Browse files
Refactoring
1 parent f582a39 commit 37442a2

22 files changed

Lines changed: 867 additions & 577 deletions

CSAPI-lib/CSAPI-lib.vcxproj

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,26 @@
2929
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
3030
<ConfigurationType>StaticLibrary</ConfigurationType>
3131
<UseDebugLibraries>true</UseDebugLibraries>
32-
<PlatformToolset>v143</PlatformToolset>
32+
<PlatformToolset>v145</PlatformToolset>
3333
<CharacterSet>Unicode</CharacterSet>
3434
</PropertyGroup>
3535
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
3636
<ConfigurationType>StaticLibrary</ConfigurationType>
3737
<UseDebugLibraries>false</UseDebugLibraries>
38-
<PlatformToolset>v143</PlatformToolset>
38+
<PlatformToolset>v145</PlatformToolset>
3939
<WholeProgramOptimization>true</WholeProgramOptimization>
4040
<CharacterSet>Unicode</CharacterSet>
4141
</PropertyGroup>
4242
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
4343
<ConfigurationType>StaticLibrary</ConfigurationType>
4444
<UseDebugLibraries>true</UseDebugLibraries>
45-
<PlatformToolset>v143</PlatformToolset>
45+
<PlatformToolset>v145</PlatformToolset>
4646
<CharacterSet>Unicode</CharacterSet>
4747
</PropertyGroup>
4848
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
4949
<ConfigurationType>StaticLibrary</ConfigurationType>
5050
<UseDebugLibraries>false</UseDebugLibraries>
51-
<PlatformToolset>v143</PlatformToolset>
51+
<PlatformToolset>v145</PlatformToolset>
5252
<WholeProgramOptimization>true</WholeProgramOptimization>
5353
<CharacterSet>Unicode</CharacterSet>
5454
</PropertyGroup>
@@ -70,6 +70,9 @@
7070
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
7171
</ImportGroup>
7272
<PropertyGroup Label="UserMacros" />
73+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
74+
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);$(SolutionDir)CSAPI-lib;</IncludePath>
75+
</PropertyGroup>
7376
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
7477
<ClCompile>
7578
<WarningLevel>Level3</WarningLevel>
@@ -170,6 +173,7 @@
170173
<ClInclude Include="RegistryInit.h" />
171174
<ClInclude Include="SystemsAPI.h" />
172175
<ClInclude Include="TimeUtils.h" />
176+
<ClInclude Include="Util\JsonUtils.h" />
173177
</ItemGroup>
174178
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
175179
<ImportGroup Label="ExtensionTargets">

CSAPI-lib/CSAPI-lib.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,8 @@
150150
<ClInclude Include="DataModels\Component\DataChoice.h">
151151
<Filter>Header Files\DataModels\Component</Filter>
152152
</ClInclude>
153+
<ClInclude Include="Util\JsonUtils.h">
154+
<Filter>Header Files\Util</Filter>
155+
</ClInclude>
153156
</ItemGroup>
154157
</Project>

CSAPI-lib/DataModels/Component/Boolean.h

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
#pragma once
22

33
#include <string>
4+
#include <optional>
5+
#include <ostream>
46
#include <nlohmann/json.hpp>
57
#include "ScalarComponent.h"
8+
#include "Util/JsonUtils.h"
69

710
namespace ConnectedSystemsAPI {
811
namespace DataModels {
912
namespace Component {
1013
class Boolean;
11-
void to_json(nlohmann::ordered_json& j, const Boolean& q);
14+
void to_json(nlohmann::ordered_json& j, const Boolean& v);
1215

1316
class Boolean : public ScalarComponent {
1417
private:
15-
std::optional<bool> value;
18+
std::optional<bool> value = std::nullopt;
1619

1720
public:
1821
Boolean() = default;
22+
Boolean(const Boolean&) = default;
23+
Boolean(Boolean&&) noexcept = default;
24+
Boolean& operator=(const Boolean&) = default;
25+
Boolean& operator=(Boolean&&) noexcept = default;
26+
~Boolean() override = default;
1927

20-
void validate() const {
28+
void validate() const override {
2129
ScalarComponent::validate();
2230
}
2331

@@ -27,38 +35,31 @@ namespace ConnectedSystemsAPI {
2735
return j;
2836
}
2937

30-
/// <summary>Inline value(s) for the component.
31-
/// This property is optional to enable structure to act as a schema for values provided separately (e.g., in a datastream)</summary>
32-
std::optional<int> getValue() const { return value; }
33-
/// <summary>Inline value(s) for the component.
34-
/// This property is optional to enable structure to act as a schema for values provided separately (e.g., in a datastream)</summary>
35-
void setValue(const std::optional<int>& value) { this->value = value; }
38+
/// <summary>
39+
/// Inline value(s) for the component.
40+
/// This property is optional to enable structure to act as a schema for values provided separately (e.g., in a datastream)
41+
/// </summary>
42+
const std::optional<bool>& getValue() const noexcept { return value; }
43+
void setValue(std::optional<bool> v) noexcept { value = std::move(v); }
44+
void setValue(bool v) noexcept { value = v; }
45+
bool hasValue() const noexcept { return value.has_value(); }
46+
void clearValue() noexcept { value.reset(); }
3647
};
3748

38-
// Register with the DataComponentRegistry
39-
struct BooleanRegistrar {
40-
BooleanRegistrar() {
41-
ConnectedSystemsAPI::DataModels::Component::DataComponentRegistry::registerType(
42-
"Boolean", [](const nlohmann::json& j) {
43-
return std::make_unique<ConnectedSystemsAPI::DataModels::Component::Boolean>(j.get<ConnectedSystemsAPI::DataModels::Component::Boolean>());
44-
}
45-
);
46-
}
47-
};
48-
static BooleanRegistrar booleanRegistrar;
49+
inline DataComponent::Registrar<Boolean> registerBoolean{ "Boolean" };
50+
inline bool operator==(const Boolean& a, const Boolean& b) { return a.toJson() == b.toJson(); }
51+
inline bool operator!=(const Boolean& a, const Boolean& b) { return !(a == b); }
4952

5053
inline void from_json(const nlohmann::json& j, Boolean& v) {
5154
from_json(j, static_cast<ScalarComponent&>(v));
52-
if (j.contains("value") && j["value"].is_boolean())
53-
v.setValue(j["value"].get<bool>());
54-
else
55-
v.setValue(std::nullopt);
55+
56+
v.setValue(ConnectedSystemsAPI::JsonUtils::tryParseBoolean(j, "value"));
5657
}
5758

5859
inline void to_json(nlohmann::ordered_json& j, const Boolean& v) {
5960
to_json(j, static_cast<const ScalarComponent&>(v));
60-
if (v.getValue())
61-
j["value"] = v.getValue().value();
61+
62+
if (v.getValue()) j["value"] = v.getValue().value();
6263
}
6364

6465
inline std::ostream& operator<<(std::ostream& os, const Boolean& v) {

CSAPI-lib/DataModels/Component/Category.h

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#pragma once
22

33
#include <string>
4+
#include <optional>
5+
#include <ostream>
46
#include <nlohmann/json.hpp>
57
#include "ScalarComponent.h"
8+
#include "Util/JsonUtils.h"
69

710
namespace ConnectedSystemsAPI {
811
namespace DataModels {
@@ -17,8 +20,13 @@ namespace ConnectedSystemsAPI {
1720

1821
public:
1922
Category() = default;
23+
Category(const Category&) = default;
24+
Category(Category&&) noexcept = default;
25+
Category& operator=(const Category&) = default;
26+
Category& operator=(Category&&) noexcept = default;
27+
~Category() override = default;
2028

21-
void validate() const {
29+
void validate() const override {
2230
ScalarComponent::validate();
2331
}
2432

@@ -28,52 +36,48 @@ namespace ConnectedSystemsAPI {
2836
return j;
2937
}
3038

31-
/// <summary>Inline value(s) for the component.
32-
/// This property is optional to enable structure to act as a schema for values provided separately (e.g., in a datastream)</summary>
33-
std::optional<std::string> getValue() const { return value; }
34-
/// <summary>Inline value(s) for the component.
35-
/// This property is optional to enable structure to act as a schema for values provided separately (e.g., in a datastream)</summary>
36-
void setValue(const std::optional<std::string>& value) { this->value = value; }
37-
/// <summary>Name of the dictionary where the possible values for this component are listed and defined.</summary>
38-
std::optional<std::string> getCodeSpace() const { return codeSpace; }
39-
/// <summary>Name of the dictionary where the possible values for this component are listed and defined.</summary>
40-
void setCodeSpace(const std::optional<std::string>& codeSpace) { this->codeSpace = codeSpace; }
41-
};
39+
/// <summary>
40+
/// Inline value(s) for the component.
41+
/// This property is optional to enable structure to act as a schema for values provided separately (e.g., in a datastream)
42+
/// </summary>
43+
const std::optional<std::string>& getValue() const noexcept { return value; }
44+
void setValue(const std::optional<std::string>& v) { value = v; }
45+
void setValue(std::optional<std::string>&& v) { value = std::move(v); }
46+
void setValue(const std::string& v) { value = v; }
47+
void setValue(std::string&& v) { value = std::move(v); }
48+
void setValue(const char* v) { value = v ? std::optional<std::string>(v) : std::nullopt; }
49+
bool hasValue() const noexcept { return value.has_value(); }
50+
void clearValue() noexcept { value.reset(); }
4251

43-
// Register with the DataComponentRegistry
44-
struct CategoryRegistrar {
45-
CategoryRegistrar() {
46-
ConnectedSystemsAPI::DataModels::Component::DataComponentRegistry::registerType(
47-
"Category", [](const nlohmann::json& j) {
48-
return std::make_unique<ConnectedSystemsAPI::DataModels::Component::Category>(j.get<ConnectedSystemsAPI::DataModels::Component::Category>());
49-
}
50-
);
51-
}
52+
/// <summary>
53+
/// Name of the dictionary where the possible values for this component are listed and defined.
54+
/// </summary>
55+
const std::optional<std::string>& getCodeSpace() const noexcept { return codeSpace; }
56+
void setCodeSpace(const std::optional<std::string>& cs) { codeSpace = cs; }
57+
void setCodeSpace(std::optional<std::string>&& cs) { codeSpace = std::move(cs); }
58+
void setCodeSpace(const std::string& cs) { codeSpace = cs; }
59+
void setCodeSpace(std::string&& cs) { codeSpace = std::move(cs); }
60+
void setCodeSpace(const char* cs) { codeSpace = cs ? std::optional<std::string>(cs) : std::nullopt; }
61+
bool hasCodeSpace() const noexcept { return codeSpace.has_value(); }
62+
void clearCodeSpace() noexcept { codeSpace.reset(); }
5263
};
53-
static CategoryRegistrar categoryRegistrar;
64+
65+
inline DataComponent::Registrar<Category> registerCategory{ "Category" };
66+
inline bool operator==(const Category& a, const Category& b) { return a.toJson() == b.toJson(); }
67+
inline bool operator!=(const Category& a, const Category& b) { return !(a == b); }
5468

5569
inline void from_json(const nlohmann::json& j, Category& v) {
5670
from_json(j, static_cast<ScalarComponent&>(v));
5771

58-
if (j.contains("value") && j["value"].is_string())
59-
v.setValue(j["value"].get<std::string>());
60-
else
61-
v.setValue(std::nullopt);
62-
63-
if (j.contains("codeSpace") && j["codeSpace"].is_string())
64-
v.setCodeSpace(j["codeSpace"].get<std::string>());
65-
else
66-
v.setCodeSpace(std::nullopt);
72+
v.setValue(ConnectedSystemsAPI::JsonUtils::tryParseString(j, "value"));
73+
v.setCodeSpace(ConnectedSystemsAPI::JsonUtils::tryParseString(j, "codeSpace"));
6774
}
6875

6976
inline void to_json(nlohmann::ordered_json& j, const Category& v) {
7077
to_json(j, static_cast<const ScalarComponent&>(v));
7178

72-
if (v.getValue())
73-
j["value"] = v.getValue().value();
74-
75-
if (v.getCodeSpace())
76-
j["codeSpace"] = v.getCodeSpace().value();
79+
if (v.getValue()) j["value"] = v.getValue().value();
80+
if (v.getCodeSpace()) j["codeSpace"] = v.getCodeSpace().value();
7781
}
7882

7983
inline std::ostream& operator<<(std::ostream& os, const Category& v) {

CSAPI-lib/DataModels/Component/CategoryRange.h

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#pragma once
22

33
#include <string>
4+
#include <optional>
5+
#include <ostream>
6+
#include <vector>
47
#include <nlohmann/json.hpp>
58
#include "SimpleComponent.h"
9+
#include "Util/JsonUtils.h"
610

711
namespace ConnectedSystemsAPI {
812
namespace DataModels {
@@ -17,8 +21,13 @@ namespace ConnectedSystemsAPI {
1721

1822
public:
1923
CategoryRange() = default;
24+
CategoryRange(const CategoryRange&) = default;
25+
CategoryRange(CategoryRange&&) noexcept = default;
26+
CategoryRange& operator=(const CategoryRange&) = default;
27+
CategoryRange& operator=(CategoryRange&&) noexcept = default;
28+
~CategoryRange() override = default;
2029

21-
void validate() const {
30+
void validate() const override {
2231
SimpleComponent::validate();
2332
}
2433

@@ -28,52 +37,51 @@ namespace ConnectedSystemsAPI {
2837
return j;
2938
}
3039

31-
/// <summary>Inline value(s) for the component.
32-
/// This property is optional to enable structure to act as a schema for values provided separately (e.g., in a datastream)</summary>
33-
std::optional<std::vector<std::string>> getValue() const { return value; }
34-
/// <summary>Inline value(s) for the component.
35-
/// This property is optional to enable structure to act as a schema for values provided separately (e.g., in a datastream)</summary>
36-
void setValue(const std::optional<std::vector<std::string>>& value) { this->value = value; }
37-
/// <summary>Name of the dictionary where the possible values for this component are listed and defined.</summary>
38-
std::optional<std::string> getCodeSpace() const { return codeSpace; }
39-
/// <summary>Name of the dictionary where the possible values for this component are listed and defined.</summary>
40-
void setCodeSpace(const std::optional<std::string>& codeSpace) { this->codeSpace = codeSpace; }
41-
};
42-
43-
// Register with the DataComponentRegistry
44-
struct CategoryRangeRegistrar {
45-
CategoryRangeRegistrar() {
46-
ConnectedSystemsAPI::DataModels::Component::DataComponentRegistry::registerType(
47-
"CategoryRange", [](const nlohmann::json& j) {
48-
return std::make_unique<ConnectedSystemsAPI::DataModels::Component::CategoryRange>(j.get<ConnectedSystemsAPI::DataModels::Component::CategoryRange>());
49-
}
50-
);
40+
/// <summary>
41+
/// Inline value(s) for the component.
42+
/// This property is optional to enable structure to act as a schema for values provided separately (e.g., in a datastream)
43+
/// </summary>
44+
const std::optional<std::vector<std::string>>& getValue() const noexcept { return value; }
45+
void setValue(std::optional<std::vector<std::string>> v) noexcept { value = std::move(v); }
46+
void setValue(std::vector<std::string> v) noexcept { value = std::move(v); }
47+
void setValue(std::initializer_list<std::string> il) { value = std::vector<std::string>(il); }
48+
bool hasValue() const noexcept { return value.has_value(); }
49+
void clearValue() noexcept { value = std::nullopt; }
50+
void addValue(const std::string& v) {
51+
if (!value) value = std::vector<std::string>{};
52+
value->push_back(v);
5153
}
54+
void addValue(std::string&& v) {
55+
if (!value) value = std::vector<std::string>{};
56+
value->push_back(std::move(v));
57+
}
58+
59+
/// <summary>
60+
/// Name of the dictionary where the possible values for this component are listed and defined.
61+
/// </summary>
62+
const std::optional<std::string>& getCodeSpace() const noexcept { return codeSpace; }
63+
void setCodeSpace(std::optional<std::string> cs) noexcept { codeSpace = std::move(cs); }
64+
void setCodeSpace(std::string cs) { codeSpace = std::move(cs); }
65+
bool hasCodeSpace() const noexcept { return codeSpace.has_value(); }
66+
void clearCodeSpace() noexcept { codeSpace = std::nullopt; }
5267
};
53-
static CategoryRangeRegistrar categoryRangeRegistrar;
68+
69+
inline DataComponent::Registrar<CategoryRange> registerCategoryRange{ "CategoryRange" };
70+
inline bool operator==(const CategoryRange& a, const CategoryRange& b) { return a.toJson() == b.toJson(); }
71+
inline bool operator!=(const CategoryRange& a, const CategoryRange& b) { return !(a == b); }
5472

5573
inline void from_json(const nlohmann::json& j, CategoryRange& v) {
5674
from_json(j, static_cast<SimpleComponent&>(v));
5775

58-
if (j.contains("value") && j["value"].is_array())
59-
v.setValue(j["value"].get<std::vector<std::string>>());
60-
else
61-
v.setValue(std::nullopt);
62-
63-
if (j.contains("codeSpace") && j["codeSpace"].is_string())
64-
v.setCodeSpace(j["codeSpace"].get<std::string>());
65-
else
66-
v.setCodeSpace(std::nullopt);
76+
v.setValue(ConnectedSystemsAPI::JsonUtils::tryParseStringArray(j, "value"));
77+
v.setCodeSpace(ConnectedSystemsAPI::JsonUtils::tryParseString(j, "codeSpace"));
6778
}
6879

6980
inline void to_json(nlohmann::ordered_json& j, const CategoryRange& v) {
7081
to_json(j, static_cast<const SimpleComponent&>(v));
7182

72-
if (v.getValue())
73-
j["value"] = v.getValue().value();
74-
75-
if (v.getCodeSpace())
76-
j["codeSpace"] = v.getCodeSpace().value();
83+
if (v.getValue()) j["value"] = v.getValue().value();
84+
if (v.getCodeSpace()) j["codeSpace"] = v.getCodeSpace().value();
7785
}
7886

7987
inline std::ostream& operator<<(std::ostream& os, const CategoryRange& v) {

0 commit comments

Comments
 (0)