SOIL C++
C++ Unified Device Interface
Element.cpp
Go to the documentation of this file.
1#include "Element.h"
2#include "json_helpers.h"
3
4
5SOIL::Element::Element(std::shared_ptr<Element> parent, std::string uuid, std::string name, std::string description, std::string ontology) : parent(parent), uuid(uuid), name(name), description(description), ontology(ontology)
6{
7 if (parent.use_count() > 0)
8 {
9 parent->add(uuid, this);
10 self = (*parent)[uuid];
11 }
12 else
13 {
14 self.reset(this, &SOIL::null_deleter);
15 }
16}
17
18
20{
21 children.clear();
22}
23
24#pragma warning( push )
25#pragma warning( disable : 4717)
26std::vector<std::string> SOIL::Element::fqid(void)
27{
28 std::vector<std::string> parent_fqid;
29 if (parent.use_count() > 0)
30 {
31 parent_fqid = parent->fqid();
32 }
33 parent_fqid.push_back(uuid);
34 return parent_fqid;
35}
36#pragma warning(pop)
37
38std::shared_ptr<SOIL::Element> SOIL::Element::operator[](std::string fqid)
39{
40 if (fqid == "" || fqid == "/")
41 {
42 return self;
43 }
44 size_t path_length = fqid.size();
45 if (fqid[0] == '/')
46 {
47 fqid = fqid.substr(1, path_length-1);
48 path_length--;
49 }
50 size_t first_delimiter = fqid.find("/");
51 if (first_delimiter == std::string::npos)
52 {
53 if (children.count(fqid) == 0)
54 {
55 if (this->uuid == fqid)
56 {
57 return self;
58 }
59 throw std::runtime_error(fqid + " does not match any children of " + uuid);
60 }
61 return children[fqid];
62 }
63 else
64 {
65 std::string dock_off_path = fqid.substr(0, first_delimiter);
66 std::string continue_path = fqid.substr(first_delimiter + 1, path_length - first_delimiter - 1);
67 if (children.count(dock_off_path) == 0)
68 {
69 if (this->uuid == dock_off_path)
70 {
71 return (*self)[continue_path];
72 }
73 throw std::runtime_error(dock_off_path + " does not match any children of " + uuid);
74 }
75 return (*children[dock_off_path])[continue_path];
76 }
77}
78
79std::shared_ptr<SOIL::Element> SOIL::Element::add(std::string uuid, std::shared_ptr<Element> child)
80{
81 bool exists = children.count(uuid) > 0;
82 children[uuid] = child;
83 return child;
84}
85
86std::shared_ptr<SOIL::Element> SOIL::Element::add(std::string uuid, Element * child)
87{
88 return add(uuid, std::shared_ptr<Element>(child));
89}
90
91bool SOIL::Element::insert(std::string uuid, std::shared_ptr<Element> child)
92{
93 bool exists = children.count(uuid) > 0;
94 this->add(uuid, child);
95 return exists;
96}
97
98bool SOIL::Element::insert(std::string uuid, Element* child)
99{
100 return insert(uuid, std::shared_ptr<Element>(child));
101}
102
103bool SOIL::Element::remove(std::string uuid)
104{
105 bool exists = children.count(uuid) > 0;
106 children.erase(uuid);
107 return exists;
108}
109
111{
112 return (uuid.substr(0, 3) == "OBJ");
113}
114
116{
117 return (uuid.substr(0, 3) == "VAR");
118}
119
121{
122 return (uuid.substr(0, 3) == "FUN");
123}
124
126{
127 return (uuid.substr(0, 3) == "PAR");
128}
129
131{
132 HTTP::Json json_root;
133 json_root[U("uuid")] = SOIL::to_json(uuid);
134 json_root[U("name")] = SOIL::to_json(name);
135 json_root[U("description")] = SOIL::to_json(description);
136 if (ontology == "")
137 {
138 json_root[U("ontology")] = HTTP::Json::null();
139 }
140 else
141 {
142 json_root[U("ontology")] = SOIL::to_json(ontology);
143 }
144
145 return json_root;
146}
147
148std::string SOIL::Element::json(void)
149{
150 HTTP::Json content = wjson();
151 return utility::conversions::to_utf8string(content.serialize());
152}
153
154
156{
157 try
158 {
159 std::string path = "";
160 if (match.size() > 0)
161 {
162 path = match.str(0);//utility::conversions::to_utf8string(match.str(0));
163 }
164
165 size_t query_delimiter = path.find("?");
166 std::shared_ptr<Element> resource = (*self)[path.substr(0, query_delimiter)];
167
168 if (resource == self)
169 {
170 return Resource::handle(request, match);
171 }
172 else
173 {
174 return resource->Resource::handle(request, match);
175 }
176
177 }
178 catch (std::exception& exception)
179 {
180 return Resource::handle_exception(request, exception, match);
181
182 }
183
184}
SOIL Base Element.
Definition: Element.h:24
bool insert(std::string uuid, std::shared_ptr< Element > child)
Add Child Element.
Definition: Element.cpp:91
HTTP::Response handle(HTTP::Request request, std::smatch match=std::smatch())
HTTP Handler.
Definition: Element.cpp:155
std::shared_ptr< Element > operator[](std::string fqid)
Access Operator.
Definition: Element.cpp:38
virtual ~Element()
Destructor.
Definition: Element.cpp:19
bool is_parameter(void) const
Is Parameter?
Definition: Element.cpp:125
std::shared_ptr< Element > add(std::string uuid, std::shared_ptr< Element > child)
Add Child Element.
Definition: Element.cpp:79
std::string uuid
Local UUID.
Definition: Element.h:53
bool is_object(void) const
Is Object?
Definition: Element.cpp:110
virtual HTTP::Json wjson(void)
HTTP JSON.
Definition: Element.cpp:130
bool is_variable(void) const
Is Variable?
Definition: Element.cpp:115
bool is_function(void) const
Is Function?
Definition: Element.cpp:120
std::vector< std::string > fqid(void)
FQID.
Definition: Element.cpp:26
virtual std::string json(void)
JSON string.
Definition: Element.cpp:148
Element(std::shared_ptr< Element > parent, std::string uuid, std::string name, std::string description, std::string ontology="")
Constructor.
Definition: Element.cpp:5
bool remove(std::string uuid)
Remove Child element.
Definition: Element.cpp:103
std::shared_ptr< Element > parent
Parent Pointer.
Definition: Element.h:39
std::shared_ptr< Element > self
Self Pointer.
Definition: Element.h:46
web::json::value Json
HTTP JSON.
Definition: Types.h:39
web::http::http_request Request
HTTP Request.
Definition: Types.h:11
web::http::http_response Response
HTTP Response.
Definition: Types.h:18
DLL web::json::value to_json(const T &value)
Value to JSON.
void null_deleter(SOIL::Element *ptr)
Null deleter.
Definition: Element.h:278