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
711namespace 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