C++ Unified Device Interface
Common.cpp
Go to the documentation of this file.
1#include "Common.h"
2#include "json_helpers.h"
3
4
5SOIL::Common::Common(std::shared_ptr<Common> 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->insert(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::Common::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::Common> SOIL::Common::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
79bool SOIL::Common::insert(std::string uuid, std::shared_ptr<Common>& child)
80{
81 bool exists = children.count(uuid) > 0;
82 children[uuid] = child;
83 return exists;
84}
85
86bool SOIL::Common::insert(std::string uuid, Common * child)
87{
88 return insert(uuid, std::shared_ptr<Common>(child));
89}
90
91bool SOIL::Common::remove(std::string uuid)
92{
93 bool exists = children.count(uuid) > 0;
94 children.erase(uuid);
95 return exists;
96}
97
99{
100 return (uuid.substr(0, 3) == "OBJ");
101}
102
104{
105 return (uuid.substr(0, 3) == "VAR");
106}
107
109{
110 return (uuid.substr(0, 3) == "FUN");
111}
112
114{
115 return (uuid.substr(0, 3) == "PAR");
116}
117
119{
120 HTTP::Json json_root;
121 json_root[L"uuid"] = SOIL::to_json(uuid);
122 json_root[L"name"] = SOIL::to_json(name);
123 json_root[L"description"] = SOIL::to_json(description);
124 if (ontology == "")
125 {
126 json_root[L"ontology"] = HTTP::Json::null();
127 }
128 else
129 {
130 json_root[L"ontology"] = SOIL::to_json(ontology);
131 }
132
133 return json_root;
134}
135
136std::string SOIL::Common::json(void)
137{
138 HTTP::Json content = wjson();
139 return utility::conversions::to_utf8string(content.serialize());
140}
141
142
144{
145 try
146 {
147 std::string path = "";
148 if (match.size() > 0)
149 {
150 path = utility::conversions::to_utf8string(match.str(0));
151 }
152
153 size_t query_delimiter = path.find("?");
154 std::shared_ptr<Common> resource = (*self)[path.substr(0, query_delimiter)];
155
156 if (resource == self)
157 {
158 return Resource::handle(request, match);
159 }
160 else
161 {
162 return resource->Resource::handle(request, match);
163 }
164
165 }
166 catch (std::exception& exception)
167 {
168 return Resource::handle_exception(request, exception, match);
169
170 }
171
172}
virtual std::string json(void)
Definition: Common.cpp:136
bool is_function(void) const
Definition: Common.cpp:108
bool is_parameter(void) const
Definition: Common.cpp:113
virtual HTTP::Json wjson(void)
Definition: Common.cpp:118
std::vector< std::string > fqid(void)
Definition: Common.cpp:26
bool insert(std::string uuid, std::shared_ptr< Common > &child)
Definition: Common.cpp:79
bool is_object(void) const
Definition: Common.cpp:98
Common(std::shared_ptr< Common > parent, std::string uuid, std::string name, std::string description, std::string ontology="")
Definition: Common.cpp:5
bool remove(std::string uuid)
Definition: Common.cpp:91
std::shared_ptr< Common > operator[](std::string fqid)
Definition: Common.cpp:38
std::shared_ptr< Common > parent
Definition: Common.h:19
bool is_variable(void) const
Definition: Common.cpp:103
virtual ~Common()
Definition: Common.cpp:19
HTTP::Response handle(HTTP::Request request, std::wsmatch match=std::wsmatch())
HTTP Handler.
Definition: Common.cpp:143
std::shared_ptr< Common > self
Definition: Common.h:20
std::string uuid
Definition: Common.h:21
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
void null_deleter(SOIL::Common *)
Definition: Common.h:54
DLL web::json::value to_json(const T &value)