SOIL C++
C++ Unified Device Interface
Function.h
Go to the documentation of this file.
1#pragma once
2#include "constants.h"
3#include "Element.h"
4#include "Container.h"
5#include "Range.h"
6#include "Parameter.h"
7
8namespace SOIL
9{
21 class Function :
22 public Element
23 {
24 private:
32 std::map<std::string, std::shared_ptr<Element>> arguments;
33
41 std::map<std::string, std::shared_ptr<Element>> returns;
42 public:
56 DLL Function(std::shared_ptr<Element> parent, std::string uuid, std::string name, std::string description, std::string ontology = "");
57
63 DLL ~Function();
64
77 static std::shared_ptr<Function> create(std::shared_ptr<Element> parent, std::string uuid, std::string name, std::string description, std::string ontology = "");
78
94 template<typename T, int x = -1, int y = -1>
95 void add_argument(std::string uuid, std::string name, std::string description, std::string unit, SOIL::Range<T> range = SOIL::Range<T>(), std::string ontology = "");
112 template<typename T, int x = -1, int y =-1>
113 void add_argument(std::string uuid, std::string name, std::string description, std::string unit, SOIL::Range<T> range, const Container<T, x, y>& default_value, std::string ontology = "");
114
130 template<typename T, int x = -1, int y = -1>
131 void add_return(std::string uuid, std::string name, std::string description, std::string unit, SOIL::Range<T> range = SOIL::Range<T>(), std::string ontology = "");
132
133
147 template<typename T, int x = -1, int y = -1>
148 HTTP::Json make_return(std::string uuid, Container<T,x,y> value);
149
163 template<typename T, int x = -1, int y = -1>
164 Container<T, x, y> make_argument(std::string uuid, HTTP::Json external_json);
165
172 DLL HTTP::Json wjson(void);
173
183 DLL virtual HTTP::Response handle_get(HTTP::Request request, std::smatch match = std::smatch());
184
196 DLL virtual HTTP::Response handle_post(HTTP::Request request, std::smatch match = std::smatch());
197
215 DLL virtual std::map<std::string, HTTP::Json> invoke(std::map<std::string, HTTP::Json> arguments);
216
223 DLL inline std::shared_ptr<Function> ptr(void)
224 {
225 return std::dynamic_pointer_cast<Function>(Element::self);
226 }
227 };
228 template<typename T, int x, int y>
229 void Function::add_argument(std::string uuid, std::string name, std::string description, std::string unit, SOIL::Range<T> range, std::string ontology)
230 {
231 arguments[uuid].reset(new Parameter<T, x, y>(Element::self, uuid, name, description, unit, false, ontology, range));
232 }
233 template<typename T, int x, int y>
234 void Function::add_argument(std::string uuid, std::string name, std::string description, std::string unit, SOIL::Range<T> range, const Container<T,x,y>& default_value, std::string ontology)
235 {
237 arguments[uuid].reset(par);
238 *par = default_value;
239 }
240
241
242 template<typename T, int x,int y>
243 void Function::add_return(std::string uuid, std::string name, std::string description, std::string unit, SOIL::Range<T> range, std::string ontology)
244 {
245 returns[uuid].reset(new Parameter<T, x, y>(Element::self, uuid, name, description, unit, false, ontology, range));
246 }
247 template<typename T, int x, int y>
249 {
250 HTTP::Json json_root = HTTP::Json::object();
251 json_root[U("uuid")] = to_json<std::string>(uuid);
252 json_root[U("value")] = value.json()[U("value")];
253 return json_root;
254 }
255 template<typename T, int x, int y>
257 {
258 for (auto& a : external_json.as_array())
259 {
260 if (to_value<std::string>(a[U("uuid")]) == uuid)
261 {
262 return Container<T, x, y>(a[U("value")]);
263 }
264 }
265 return Container<T, x, y>();
266 }
267}
268
SOIL Base Element.
Definition: Element.h:24
std::string uuid
Local UUID.
Definition: Element.h:53
std::string description
Description.
Definition: Element.h:67
std::string ontology
Ontology identifier.
Definition: Element.h:76
std::string name
Name.
Definition: Element.h:60
std::shared_ptr< Element > parent
Parent Pointer.
Definition: Element.h:39
std::shared_ptr< Element > self
Self Pointer.
Definition: Element.h:46
Function Class.
Definition: Function.h:23
DLL std::shared_ptr< Function > ptr(void)
Get Pointer.
Definition: Function.h:223
HTTP::Json make_return(std::string uuid, Container< T, x, y > value)
Make JSON Return.
Definition: Function.h:248
static std::shared_ptr< Function > create(std::shared_ptr< Element > parent, std::string uuid, std::string name, std::string description, std::string ontology="")
Create new Function.
Definition: Function.cpp:18
Container< T, x, y > make_argument(std::string uuid, HTTP::Json external_json)
Make Argument from JSON.
Definition: Function.h:256
virtual DLL HTTP::Response handle_post(HTTP::Request request, std::smatch match=std::smatch())
Handle HTTP POST request.
Definition: Function.cpp:54
virtual DLL HTTP::Response handle_get(HTTP::Request request, std::smatch match=std::smatch())
Handle HTTP GET request.
Definition: Function.cpp:46
void add_argument(std::string uuid, std::string name, std::string description, std::string unit, SOIL::Range< T > range=SOIL::Range< T >(), std::string ontology="")
Add argument.
Definition: Function.h:229
DLL ~Function()
Destructor.
Definition: Function.cpp:14
void add_return(std::string uuid, std::string name, std::string description, std::string unit, SOIL::Range< T > range=SOIL::Range< T >(), std::string ontology="")
Add return value.
Definition: Function.h:243
virtual DLL std::map< std::string, HTTP::Json > invoke(std::map< std::string, HTTP::Json > arguments)
Core Invocation.
Definition: Function.cpp:95
DLL Function(std::shared_ptr< Element > parent, std::string uuid, std::string name, std::string description, std::string ontology="")
Constructor.
Definition: Function.cpp:4
DLL HTTP::Json wjson(void)
HTTP JSON.
Definition: Function.cpp:24
Parameter Class.
Definition: Parameter.h:35
Range Helper Class.
Definition: Range.h:25
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
Type definitions.
Definition: Container.h:7