Pothos  0.4.1-gb758ed46
The Pothos dataflow programming software suite
ProxyImpl.hpp
Go to the documentation of this file.
1 
11 #pragma once
12 #include <Pothos/Config.hpp>
14 #include <Pothos/Proxy/Proxy.hpp>
15 #include <Pothos/Proxy/Handle.hpp>
17 #include <type_traits> //enable_if
18 #include <utility> //std::forward
19 #include <cassert>
20 #include <array>
21 
22 namespace Pothos {
23 
24 template <typename ValueType>
25 ValueType Proxy::convert(void) const
26 {
27  return this->getEnvironment()->convertProxyToObject(*this).convert<ValueType>();
28 }
29 
30 namespace Detail {
31 
32 /***********************************************************************
33  * convertProxy either converts a proxy to a desired type
34  * or returns a proxy if the requested type was a proxy
35  **********************************************************************/
36 template <typename T>
37 typename std::enable_if<!std::is_same<T, Proxy>::value, T>::type
38 convertProxy(const Proxy &p)
39 {
40  return p.convert<T>();
41 }
42 
43 template <typename T>
44 typename std::enable_if<std::is_same<T, Proxy>::value, T>::type
45 convertProxy(const Proxy &p)
46 {
47  return p;
48 }
49 
50 /***********************************************************************
51  * makeProxy either makes a proxy from an arbitrary type
52  * or returns a proxy if the type is already a proxy
53  **********************************************************************/
54 template <typename T>
55 typename std::enable_if<!std::is_same<typename std::decay<T>::type, Proxy>::value, Proxy>::type
56 makeProxy(const ProxyEnvironment::Sptr &env, T &&value)
57 {
58  return env->makeProxy(std::forward<T>(value));
59 }
60 
61 template <typename T>
62 typename std::enable_if<std::is_same<typename std::decay<T>::type, Proxy>::value, Proxy>::type
63 makeProxy(const ProxyEnvironment::Sptr &, T &&value)
64 {
65  return value;
66 }
67 
68 } //namespace Detail
69 
70 template <typename ReturnType, typename... ArgsType>
71 ReturnType Proxy::call(const std::string &name, ArgsType&&... args) const
72 {
73  Proxy ret = this->callProxy(name, std::forward<ArgsType>(args)...);
74  return Detail::convertProxy<ReturnType>(ret);
75 }
76 
77 template <typename... ArgsType>
78 Proxy Proxy::callProxy(const std::string &name, ArgsType&&... args) const
79 {
80  const std::array<Proxy, sizeof...(ArgsType)> proxyArgs{{Detail::makeProxy(this->getEnvironment(), std::forward<ArgsType>(args))...}};
81  auto handle = this->getHandle();
82  assert(handle);
83  return handle->call(name, proxyArgs.data(), sizeof...(args));
84 }
85 
86 template <typename... ArgsType>
87 void Proxy::callVoid(const std::string &name, ArgsType&&... args) const
88 {
89  this->callProxy(name, std::forward<ArgsType>(args)...);
90 }
91 
92 template <typename ReturnType>
93 ReturnType Proxy::get(const std::string &name) const
94 {
95  return this->call<ReturnType>("get:"+name);
96 }
97 
98 template <typename ValueType>
99 void Proxy::set(const std::string &name, ValueType&& value) const
100 {
101  this->callVoid("set:"+name, std::forward<ValueType>(value));
102 }
103 
104 template <typename... ArgsType>
105 Proxy Proxy::operator()(ArgsType&&... args) const
106 {
107  return this->callProxy("()", std::forward<ArgsType>(args)...);
108 }
109 
110 } //namespace Pothos
void set(const std::string &name, ValueType &&value) const
Call a field setter.
Definition: ProxyImpl.hpp:99
void callVoid(const std::string &name, ArgsType &&...args) const
Call a method with a void return and variable args.
Definition: ProxyImpl.hpp:87
Definition: Testing.hpp:134
std::shared_ptr< ProxyEnvironment > Sptr
Definition: Environment.hpp:46
Proxy callProxy(const std::string &name, ArgsType &&...args) const
Call a method with a Proxy return and variable args.
Definition: ProxyImpl.hpp:78
std::shared_ptr< ProxyEnvironment > getEnvironment(void) const
ReturnType call(const std::string &name, ArgsType &&...args) const
Call a method with a return type and variable args.
Definition: ProxyImpl.hpp:71
std::shared_ptr< ProxyHandle > getHandle(void) const
Get the handle held in this proxy object.
ReturnType get(const std::string &name) const
Call a field getter with specified return type.
Definition: ProxyImpl.hpp:93
ValueType convert(void) const
Definition: ProxyImpl.hpp:25
Proxy operator()(ArgsType &&...args) const
Call the function operator() with a Proxy return and variable args.
Definition: ProxyImpl.hpp:105
Definition: Proxy.hpp:28