Skip to content

Commit 3d77807

Browse files
Refactoring
1 parent 2cd6d5b commit 3d77807

48 files changed

Lines changed: 885 additions & 698 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CSAPI-lib/APIRequest.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
#include <string>
44
#include <iostream>
55
#include <stdexcept>
6-
#include <curl/curl.h>
76
#include <map>
7+
#include <vector>
8+
#include <curl/curl.h>
9+
#include <curl/easy.h>
10+
811
#include "APIResponse.h"
912

1013
namespace ConnectedSystemsAPI {
@@ -86,10 +89,9 @@ namespace ConnectedSystemsAPI {
8689
static size_t HeaderCallback(char* buffer, size_t size, size_t nitems, void* userdata) {
8790
size_t totalSize = size * nitems;
8891
auto* response = static_cast<RawHttpResponse*>(userdata);
89-
std::string headerLine(buffer, totalSize);
9092

9193
// Check for the status line (e.g., "HTTP/1.1 200 OK\r\n")
92-
if (headerLine.find("HTTP/") == 0) {
94+
if (std::string headerLine(buffer, totalSize); headerLine.find("HTTP/") == 0) {
9395
// Remove trailing \r\n
9496
headerLine.erase(headerLine.find_last_not_of("\r\n") + 1);
9597
// Remove "HTTP/1.1 " prefix to get the status message

CSAPI-lib/APIResponse.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <vector>
55
#include <map>
66
#include <nlohmann/json.hpp>
7+
#include <nlohmann/json_fwd.hpp>
78

89
namespace ConnectedSystemsAPI {
910
template <typename T>

CSAPI-lib/ControlStreamsAPI.h

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

33
#include <string>
4+
#include <optional>
5+
#include <nlohmann/json_fwd.hpp>
46

57
#include "APIRequest.h"
68
#include "APIResponse.h"

CSAPI-lib/DataModels/CommandSchema.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
#pragma once
22

33
#include <string>
4+
#include <memory>
5+
#include <ostream>
6+
#include <utility>
47
#include <nlohmann/json.hpp>
8+
#include <nlohmann/json_fwd.hpp>
59

610
#include "Component/DataComponent.h"
711
#include "Component/DataComponentRegistry.h"
812

913
namespace ConnectedSystemsAPI::DataModels {
14+
class CommandSchema;
15+
void to_json(nlohmann::ordered_json& j, const CommandSchema& v);
16+
1017
class CommandSchema {
1118
private:
1219
std::string commandFormat;
@@ -30,6 +37,13 @@ namespace ConnectedSystemsAPI::DataModels {
3037
CommandSchema& operator=(const CommandSchema&) = delete;
3138
CommandSchema(CommandSchema&&) noexcept = default;
3239
CommandSchema& operator=(CommandSchema&&) noexcept = default;
40+
~CommandSchema() = default;
41+
42+
nlohmann::ordered_json toJson() const {
43+
nlohmann::ordered_json j;
44+
to_json(j, *this);
45+
return j;
46+
}
3347

3448
/// <summary>
3549
/// Encoding format of the command.
@@ -53,12 +67,13 @@ namespace ConnectedSystemsAPI::DataModels {
5367

5468
friend void from_json(const nlohmann::json& j, CommandSchema& v);
5569
friend void to_json(nlohmann::ordered_json& j, const CommandSchema& v);
56-
friend std::ostream& operator<<(std::ostream& os, const CommandSchema& v);
70+
71+
friend std::ostream& operator<<(std::ostream& os, const CommandSchema& v) {
72+
return os << v.toJson().dump(2);
73+
}
5774
};
5875

5976
inline void from_json(const nlohmann::json& j, CommandSchema& v) {
60-
// Print the json for debugging
61-
std::cout << "Deserializing CommandSchema from JSON: " << j.dump(2) << std::endl;
6277
v.commandFormat = j.at("commandFormat").get<std::string>();
6378
v.parametersSchema = Component::DataComponentRegistry::createDataComponent(j.at("paramsSchema"));
6479
if (j.contains("resultSchema"))
@@ -75,10 +90,4 @@ namespace ConnectedSystemsAPI::DataModels {
7590
if (v.resultSchema) j["resultSchema"] = v.getResultSchema()->toJson();
7691
if (v.feasibilityResultSchema) j["feasibilityResultSchema"] = v.getFeasibilityResultSchema()->toJson();
7792
}
78-
79-
inline std::ostream& operator<<(std::ostream& os, const CommandSchema& v) {
80-
nlohmann::ordered_json j;
81-
to_json(j, v);
82-
return os << j.dump(2);
83-
}
8493
}

CSAPI-lib/DataModels/CommandSchemaBuilder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <optional>
66
#include <memory>
77
#include <utility>
8+
#include <vector>
9+
#include <nlohmann/json_fwd.hpp>
810

911
#include "CommandSchema.h"
1012
#include "Component/DataComponent.h"

CSAPI-lib/DataModels/Component/Boolean.h

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
#include <string>
44
#include <optional>
55
#include <ostream>
6+
#include <utility>
67
#include <nlohmann/json.hpp>
8+
#include <nlohmann/json_fwd.hpp>
9+
710
#include "ScalarComponent.h"
811
#include "Util/JsonUtils.h"
12+
#include "DataComponent.h"
13+
#include "SimpleComponent.h"
914

1015
namespace ConnectedSystemsAPI::DataModels::Component {
1116
class Boolean;
@@ -23,10 +28,6 @@ namespace ConnectedSystemsAPI::DataModels::Component {
2328
Boolean& operator=(Boolean&&) noexcept = default;
2429
~Boolean() override = default;
2530

26-
void validate() const override {
27-
ScalarComponent::validate();
28-
}
29-
3031
nlohmann::ordered_json toJson() const override {
3132
nlohmann::ordered_json j;
3233
to_json(j, *this);
@@ -42,27 +43,29 @@ namespace ConnectedSystemsAPI::DataModels::Component {
4243
void setValue(bool v) noexcept { value = v; }
4344
bool hasValue() const noexcept { return value.has_value(); }
4445
void clearValue() noexcept { value.reset(); }
46+
47+
friend void from_json(const nlohmann::json& j, Boolean& v);
48+
friend void to_json(nlohmann::ordered_json& j, const Boolean& v);
49+
50+
friend bool operator==(const Boolean& a, const Boolean& b) { return a.toJson() == b.toJson(); }
51+
friend bool operator!=(const Boolean& a, const Boolean& b) { return !(a == b); }
52+
53+
friend std::ostream& operator<<(std::ostream& os, const Boolean& v) {
54+
return os << v.toJson().dump(2);
55+
}
4556
};
4657

47-
inline DataComponent::Registrar<Boolean> registerBoolean{ "Boolean" };
48-
inline bool operator==(const Boolean& a, const Boolean& b) { return a.toJson() == b.toJson(); }
49-
inline bool operator!=(const Boolean& a, const Boolean& b) { return !(a == b); }
58+
const inline DataComponent::Registrar<Boolean> registerBoolean{ "Boolean" };
5059

5160
inline void from_json(const nlohmann::json& j, Boolean& v) {
5261
from_json(j, static_cast<ScalarComponent&>(v));
5362

54-
v.setValue(ConnectedSystemsAPI::JsonUtils::tryParseBoolean(j, "value"));
63+
v.value = ConnectedSystemsAPI::JsonUtils::tryParseBoolean(j, "value");
5564
}
5665

5766
inline void to_json(nlohmann::ordered_json& j, const Boolean& v) {
5867
to_json(j, static_cast<const ScalarComponent&>(v));
5968

60-
if (v.getValue()) j["value"] = v.getValue().value();
61-
}
62-
63-
inline std::ostream& operator<<(std::ostream& os, const Boolean& v) {
64-
nlohmann::ordered_json j;
65-
to_json(j, v);
66-
return os << j.dump(2);
69+
if (v.hasValue()) j["value"] = v.value;
6770
}
6871
}

CSAPI-lib/DataModels/Component/BooleanBuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <optional>
4+
#include <utility>
45

56
#include "Boolean.h"
67
#include "DataComponentBuilder.h"

CSAPI-lib/DataModels/Component/Category.h

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
#include <string>
44
#include <optional>
55
#include <ostream>
6+
#include <utility>
67
#include <nlohmann/json.hpp>
8+
#include <nlohmann/json_fwd.hpp>
9+
710
#include "ScalarComponent.h"
811
#include "Util/JsonUtils.h"
12+
#include "DataComponent.h"
13+
#include "SimpleComponent.h"
914

1015
namespace ConnectedSystemsAPI::DataModels::Component {
1116
class Category;
@@ -24,10 +29,6 @@ namespace ConnectedSystemsAPI::DataModels::Component {
2429
Category& operator=(Category&&) noexcept = default;
2530
~Category() override = default;
2631

27-
void validate() const override {
28-
ScalarComponent::validate();
29-
}
30-
3132
nlohmann::ordered_json toJson() const override {
3233
nlohmann::ordered_json j;
3334
to_json(j, *this);
@@ -39,49 +40,44 @@ namespace ConnectedSystemsAPI::DataModels::Component {
3940
/// This property is optional to enable structure to act as a schema for values provided separately (e.g., in a datastream)
4041
/// </summary>
4142
const std::optional<std::string>& getValue() const noexcept { return value; }
42-
void setValue(const std::optional<std::string>& v) { value = v; }
43-
void setValue(std::optional<std::string>&& v) { value = std::move(v); }
44-
void setValue(const std::string& v) { value = v; }
45-
void setValue(std::string&& v) { value = std::move(v); }
46-
void setValue(const char* v) { value = v ? std::optional<std::string>(v) : std::nullopt; }
43+
void setValue(std::optional<std::string> v) { value = std::move(v); }
4744
bool hasValue() const noexcept { return value.has_value(); }
4845
void clearValue() noexcept { value.reset(); }
4946

5047
/// <summary>
5148
/// Name of the dictionary where the possible values for this component are listed and defined.
5249
/// </summary>
5350
const std::optional<std::string>& getCodeSpace() const noexcept { return codeSpace; }
54-
void setCodeSpace(const std::optional<std::string>& cs) { codeSpace = cs; }
55-
void setCodeSpace(std::optional<std::string>&& cs) { codeSpace = std::move(cs); }
56-
void setCodeSpace(const std::string& cs) { codeSpace = cs; }
57-
void setCodeSpace(std::string&& cs) { codeSpace = std::move(cs); }
58-
void setCodeSpace(const char* cs) { codeSpace = cs ? std::optional<std::string>(cs) : std::nullopt; }
51+
void setCodeSpace(std::optional<std::string> cs) { codeSpace = std::move(cs); }
5952
bool hasCodeSpace() const noexcept { return codeSpace.has_value(); }
6053
void clearCodeSpace() noexcept { codeSpace.reset(); }
6154

55+
friend void from_json(const nlohmann::json& j, Category& v);
56+
friend void to_json(nlohmann::ordered_json& j, const Category& v);
57+
58+
friend bool operator==(const Category& a, const Category& b) { return a.toJson() == b.toJson(); }
59+
friend bool operator!=(const Category& a, const Category& b) { return !(a == b); }
60+
6261
friend std::ostream& operator<<(std::ostream& os, const Category& v) {
6362
nlohmann::ordered_json j;
6463
to_json(j, v);
6564
return os << j.dump(2);
6665
}
67-
68-
friend bool operator==(const Category& a, const Category& b) { return a.toJson() == b.toJson(); }
69-
friend bool operator!=(const Category& a, const Category& b) { return !(a == b); }
7066
};
7167

72-
inline DataComponent::Registrar<Category> registerCategory{ "Category" };
68+
const inline DataComponent::Registrar<Category> registerCategory{ "Category" };
7369

7470
inline void from_json(const nlohmann::json& j, Category& v) {
7571
from_json(j, static_cast<ScalarComponent&>(v));
7672

77-
v.setValue(ConnectedSystemsAPI::JsonUtils::tryParseString(j, "value"));
78-
v.setCodeSpace(ConnectedSystemsAPI::JsonUtils::tryParseString(j, "codeSpace"));
73+
v.value = ConnectedSystemsAPI::JsonUtils::tryParseString(j, "value");
74+
v.codeSpace = ConnectedSystemsAPI::JsonUtils::tryParseString(j, "codeSpace");
7975
}
8076

8177
inline void to_json(nlohmann::ordered_json& j, const Category& v) {
8278
to_json(j, static_cast<const ScalarComponent&>(v));
8379

84-
if (v.getValue()) j["value"] = v.getValue().value();
85-
if (v.getCodeSpace()) j["codeSpace"] = v.getCodeSpace().value();
80+
if (v.hasValue()) j["value"] = v.value.value();
81+
if (v.hasCodeSpace()) j["codeSpace"] = v.codeSpace.value();
8682
}
8783
}

CSAPI-lib/DataModels/Component/CategoryRange.h

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
#include <optional>
55
#include <ostream>
66
#include <vector>
7+
#include <initializer_list>
8+
#include <utility>
79
#include <nlohmann/json.hpp>
10+
#include <nlohmann/json_fwd.hpp>
11+
812
#include "SimpleComponent.h"
913
#include "Util/JsonUtils.h"
14+
#include "DataComponent.h"
1015

1116
namespace ConnectedSystemsAPI::DataModels::Component {
1217
class CategoryRange;
@@ -25,10 +30,6 @@ namespace ConnectedSystemsAPI::DataModels::Component {
2530
CategoryRange& operator=(CategoryRange&&) noexcept = default;
2631
~CategoryRange() override = default;
2732

28-
void validate() const override {
29-
SimpleComponent::validate();
30-
}
31-
3233
nlohmann::ordered_json toJson() const override {
3334
nlohmann::ordered_json j;
3435
to_json(j, *this);
@@ -62,29 +63,33 @@ namespace ConnectedSystemsAPI::DataModels::Component {
6263
void setCodeSpace(std::string cs) { codeSpace = std::move(cs); }
6364
bool hasCodeSpace() const noexcept { return codeSpace.has_value(); }
6465
void clearCodeSpace() noexcept { codeSpace = std::nullopt; }
66+
67+
friend void from_json(const nlohmann::json& j, CategoryRange& v);
68+
friend void to_json(nlohmann::ordered_json& j, const CategoryRange& v);
69+
70+
friend bool operator==(const CategoryRange& a, const CategoryRange& b) { return a.toJson() == b.toJson(); }
71+
friend bool operator!=(const CategoryRange& a, const CategoryRange& b) { return !(a == b); }
72+
73+
friend std::ostream& operator<<(std::ostream& os, const CategoryRange& v) {
74+
nlohmann::ordered_json j;
75+
to_json(j, v);
76+
return os << j.dump(2);
77+
}
6578
};
6679

67-
inline DataComponent::Registrar<CategoryRange> registerCategoryRange{ "CategoryRange" };
68-
inline bool operator==(const CategoryRange& a, const CategoryRange& b) { return a.toJson() == b.toJson(); }
69-
inline bool operator!=(const CategoryRange& a, const CategoryRange& b) { return !(a == b); }
80+
const inline DataComponent::Registrar<CategoryRange> registerCategoryRange{ "CategoryRange" };
7081

7182
inline void from_json(const nlohmann::json& j, CategoryRange& v) {
7283
from_json(j, static_cast<SimpleComponent&>(v));
7384

74-
v.setValue(ConnectedSystemsAPI::JsonUtils::tryParseStringArray(j, "value"));
75-
v.setCodeSpace(ConnectedSystemsAPI::JsonUtils::tryParseString(j, "codeSpace"));
85+
v.value = ConnectedSystemsAPI::JsonUtils::tryParseStringArray(j, "value");
86+
v.codeSpace = ConnectedSystemsAPI::JsonUtils::tryParseString(j, "codeSpace");
7687
}
7788

7889
inline void to_json(nlohmann::ordered_json& j, const CategoryRange& v) {
7990
to_json(j, static_cast<const SimpleComponent&>(v));
8091

81-
if (v.getValue()) j["value"] = v.getValue().value();
82-
if (v.getCodeSpace()) j["codeSpace"] = v.getCodeSpace().value();
83-
}
84-
85-
inline std::ostream& operator<<(std::ostream& os, const CategoryRange& v) {
86-
nlohmann::ordered_json j;
87-
to_json(j, v);
88-
return os << j.dump(2);
92+
if (v.hasValue()) j["value"] = v.value.value();
93+
if (v.hasCodeSpace()) j["codeSpace"] = v.codeSpace.value();
8994
}
9095
}

0 commit comments

Comments
 (0)