Pothos  0.7.0-gf7fbae99
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 ValueType>
71 Proxy::operator ValueType(void) const
72 {
73  return this->convert<ValueType>();
74 }
75 
76 template <typename ReturnType, typename... ArgsType>
77 ReturnType Proxy::call(const std::string &name, ArgsType&&... args) const
78 {
79  Proxy ret = this->call(name, std::forward<ArgsType>(args)...);
80  return Detail::convertProxy<ReturnType>(ret);
81 }
82 
83 template <typename... ArgsType>
84 Proxy Proxy::call(const std::string &name, ArgsType&&... args) const
85 {
86  const std::array<Proxy, sizeof...(ArgsType)> proxyArgs{{Detail::makeProxy(this->getEnvironment(), std::forward<ArgsType>(args))...}};
87  auto handle = this->getHandle();
88  assert(handle);
89  return handle->call(name, proxyArgs.data(), sizeof...(args));
90 }
91 
92 template <typename... ArgsType>
93 Proxy Proxy::callProxy(const std::string &name, ArgsType&&... args) const
94 {
95  return this->call(name, std::forward<ArgsType>(args)...);
96 }
97 
98 template <typename... ArgsType>
99 void Proxy::callVoid(const std::string &name, ArgsType&&... args) const
100 {
101  this->call(name, std::forward<ArgsType>(args)...);
102 }
103 
104 template <typename ReturnType>
105 ReturnType Proxy::get(const std::string &name) const
106 {
107  return this->call<ReturnType>("get:"+name);
108 }
109 
110 template <typename ValueType>
111 void Proxy::set(const std::string &name, ValueType&& value) const
112 {
113  this->call("set:"+name, std::forward<ValueType>(value));
114 }
115 
116 template <typename... ArgsType>
117 Proxy Proxy::operator()(ArgsType&&... args) const
118 {
119  return this->call("()", std::forward<ArgsType>(args)...);
120 }
121 
122 } //namespace Pothos
ReturnType call(const std::string &name, ArgsType &&... args) const
Call a method with a return type and variable args.
Definition: ProxyImpl.hpp:77
Definition: ArchiveEntry.hpp:20
std::shared_ptr< ProxyEnvironment > Sptr
Definition: Environment.hpp:46
std::shared_ptr< ProxyEnvironment > getEnvironment(void) const
ValueType convert(void) const
Definition: ProxyImpl.hpp:25
void callVoid(const std::string &name, ArgsType &&... args) const
Definition: ProxyImpl.hpp:99
Proxy callProxy(const std::string &name, ArgsType &&... args) const
Definition: ProxyImpl.hpp:93
void set(const std::string &name, ValueType &&value) const
Call a field setter.
Definition: ProxyImpl.hpp:111
ReturnType get(const std::string &name) const
Call a field getter with specified return type.
Definition: ProxyImpl.hpp:105
std::shared_ptr< ProxyHandle > getHandle(void) const
Get the handle held in this proxy object.
Proxy operator()(ArgsType &&... args) const
Call the function operator() with a Proxy return and variable args.
Definition: ProxyImpl.hpp:117
Definition: Proxy.hpp:28