RemoteHandle.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18 * @package ArmarXCore
19 * @author Raphael Grimm ( raphael dot grimm at kit dot edu)
20 * @date 2016
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 #pragma once
25 
26 #include <Ice/ProxyHandle.h>
27 
28 #include <ArmarXCore/interface/core/util/distributed/RemoteHandle/RemoteHandleControlBlock.h>
29 #include <ArmarXCore/interface/core/util/distributed/RemoteHandle/ClientSideRemoteHandleControlBlock.h>
30 #include "../../TemplateMetaProgramming.h"
31 
32 /**
33  * \defgroup Distributed
34  * \ingroup core-utility
35  */
36 namespace armarx
37 {
38  /**
39  * @brief The RemoteHandle class wrapps a ClientSideRemoteHandleControlBlock and can be used just as a Ice proxy.
40  * It can be copied, casted, assigned, compared and dereferenced.
41  * (for more info: \ref ArmarXCore-Tutorials-RemoteHandles "RemoteHandle tutorial")
42  * \ingroup Distributed
43  */
44  template<class PrxType>
46  {
47  static_assert(meta::TypeTemplateTraits::IsInstanceOf<IceInternal::ProxyHandle, PrxType>::value, "PrxType has to be an Ice proxy");
48  public:
49  //constructing
50  RemoteHandle() = default;
51  RemoteHandle(const RemoteHandle& other) = default;
52  RemoteHandle(RemoteHandle&& other) = default;
53  RemoteHandle(const ClientSideRemoteHandleControlBlockBasePtr& controlBlock);
54  RemoteHandle(std::nullptr_t);
55  template<class PrxTypeB> RemoteHandle(const RemoteHandle<PrxTypeB>& other);
56  ~RemoteHandle() = default;
57 
58  //assigning
59  inline RemoteHandle<PrxType>& operator =(const RemoteHandle<PrxType>& other) = default;
60  inline RemoteHandle<PrxType>& operator =(RemoteHandle<PrxType>&& other) = default;
61  inline RemoteHandle<PrxType>& operator =(const ClientSideRemoteHandleControlBlockBasePtr& controlBlock);
62  inline RemoteHandle<PrxType>& operator =(std::nullptr_t);
63  template<class PrxTypeB>
65 
66  //casting
67  template<class SourceType>
68  inline static RemoteHandle<PrxType> uncheckedCast(const SourceType& proxyToCast);
69 
70  template<class SourceType>
71  inline static RemoteHandle<PrxType> checkedCast(const SourceType& proxyToCast);
72 
73  template<class TargetPrxType> inline RemoteHandle<TargetPrxType> uncheckedCast() const;
74  template<class TargetPrxType> inline RemoteHandle<TargetPrxType> checkedCast() const;
75 
76  explicit inline operator bool() const;
77 
78  //access
79  inline PrxType operator *();
80  inline const PrxType operator *() const;
81  inline PrxType operator ->();
82  inline const PrxType operator ->() const;
83  inline PrxType get();
84  inline const PrxType get() const;
85 
86  //compare
87  template<class PrxTA, class PrxTB> friend bool operator==(const RemoteHandle<PrxTA>& fst, const RemoteHandle<PrxTB>& snd);
88  template<class PrxTA, class PrxTB> friend bool operator!=(const RemoteHandle<PrxTA>& fst, const RemoteHandle<PrxTB>& snd);
89  template<class PrxTA, class PrxTB> friend bool operator< (const RemoteHandle<PrxTA>& fst, const RemoteHandle<PrxTB>& snd);
90  template<class PrxTA, class PrxTB> friend bool operator<=(const RemoteHandle<PrxTA>& fst, const RemoteHandle<PrxTB>& snd);
91  template<class PrxTA, class PrxTB> friend bool operator >(const RemoteHandle<PrxTA>& fst, const RemoteHandle<PrxTB>& snd);
92  template<class PrxTA, class PrxTB> friend bool operator>=(const RemoteHandle<PrxTA>& fst, const RemoteHandle<PrxTB>& snd);
93 
94  /**
95  * @return Returns the internal ClientSideRemoteHandleControlBlock.
96  * This should only be used when it has to be send per ice.
97  */
99 
100  private:
101  template<class PrxTA> friend class armarx::RemoteHandle;
102 
103  PrxType objectProxy;
104  ClientSideRemoteHandleControlBlockBasePtr clientSideControlBlock;
105  };
106 
107  template<class PrxType>
108  RemoteHandle<PrxType>::RemoteHandle(const ClientSideRemoteHandleControlBlockBasePtr& controlBlock):
109  objectProxy {controlBlock ? PrxType::checkedCast(controlBlock->getManagedObjectProxy()) : nullptr},
110  clientSideControlBlock {objectProxy ? controlBlock : nullptr}
111  {
112  assert(objectProxy ? clientSideControlBlock : !clientSideControlBlock);
113  }
114 
115  template<class PrxType> template<class PrxTypeB>
116  RemoteHandle<PrxType>::RemoteHandle(const RemoteHandle<PrxTypeB>& other): RemoteHandle(other.clientSideControlBlock)
117  {
118  }
119 
120  template<class PrxType>
121  RemoteHandle<PrxType>& RemoteHandle<PrxType>::operator =(const ClientSideRemoteHandleControlBlockBasePtr& controlBlock)
122  {
123  objectProxy = controlBlock ? PrxType::checkedCast(controlBlock->getManagedObjectProxy()) : nullptr;
124  clientSideControlBlock = objectProxy ? controlBlock : nullptr;
125  return *this;
126  }
127 
128  template<class PrxType>
130  {
131  objectProxy = nullptr;
132  clientSideControlBlock = nullptr;
133  return *this;
134  }
135 
136  template<class PrxType> template<class PrxTypeB>
138  {
139  this = other.clientSideControlBlock;
140  return this;
141  }
142 
143  template<class PrxType>
145 
146  template<class PrxType> template<class SourceType>
148  {
149  static_assert(meta::TypeTemplateTraits::IsInstanceOf<::armarx::RemoteHandle, SourceType>::value, "proxyToCast has to be a RemoteHandle");
150  return proxyToCast.template uncheckedCast<PrxType>();
151  }
152 
153  template<class PrxType> template<class SourceType>
155  {
156  static_assert(meta::TypeTemplateTraits::IsInstanceOf<::armarx::RemoteHandle, SourceType>::value, "proxyToCast has to be a RemoteHandle");
157  return proxyToCast.template checkedCast<PrxType>();
158  }
159 
160  template<class PrxType> template<class TargetPrxType>
162  {
163  static_assert(meta::TypeTemplateTraits::IsInstanceOf<IceInternal::ProxyHandle, TargetPrxType>::value, "TargetPrxType has to be an Ice proxy");
165  casted.clientSideControlBlock = clientSideControlBlock;
166  casted.objectProxy = TargetPrxType::uncheckedCast(objectProxy);
167  return casted;
168  }
169 
170  template<class PrxType> template<class TargetPrxType>
172  {
173  static_assert(meta::TypeTemplateTraits::IsInstanceOf<IceInternal::ProxyHandle, TargetPrxType>::value, "TargetPrxType has to be an Ice proxy");
175  casted.objectProxy = TargetPrxType::checkedCast(objectProxy);
176  casted.clientSideControlBlock = casted.objectProxy ? clientSideControlBlock : nullptr;
177  return casted;
178  }
179 
180  template<class PrxType> PrxType RemoteHandle<PrxType>::operator *()
181  {
182  return objectProxy;
183  }
184  template<class PrxType> const PrxType RemoteHandle<PrxType>::operator *() const
185  {
186  return objectProxy;
187  }
188  template<class PrxType> PrxType RemoteHandle<PrxType>::operator ->()
189  {
190  return objectProxy;
191  }
192  template<class PrxType> const PrxType RemoteHandle<PrxType>::operator ->() const
193  {
194  return objectProxy;
195  }
196  template<class PrxType> PrxType RemoteHandle<PrxType>::get()
197  {
198  return objectProxy;
199  }
200  template<class PrxType> const PrxType RemoteHandle<PrxType>::get() const
201  {
202  return objectProxy;
203  }
204 
205  template<class PrxType> RemoteHandle<PrxType>::operator bool() const
206  {
207  return objectProxy;
208  }
209 
210  template<class PrxType>
212  {
213  return clientSideControlBlock;
214  }
215 
216  template<class PrxTA, class PrxTB> bool operator==(const RemoteHandle<PrxTA>& fst, const RemoteHandle<PrxTB>& snd)
217  {
218  return fst.clientSideControlBlock->getManagedObjectProxy() == snd.clientSideControlBlock->getManagedObjectProxy();
219  }
220  template<class PrxTA, class PrxTB> bool operator!=(const RemoteHandle<PrxTA>& fst, const RemoteHandle<PrxTB>& snd)
221  {
222  return fst.clientSideControlBlock->getManagedObjectProxy() != snd.clientSideControlBlock->getManagedObjectProxy();
223  }
224  template<class PrxTA, class PrxTB> bool operator<(const RemoteHandle<PrxTA>& fst, const RemoteHandle<PrxTB>& snd)
225  {
226  return fst.clientSideControlBlock->getManagedObjectProxy() < snd.clientSideControlBlock->getManagedObjectProxy();
227  }
228  template<class PrxTA, class PrxTB> bool operator<=(const RemoteHandle<PrxTA>& fst, const RemoteHandle<PrxTB>& snd)
229  {
230  return fst.clientSideControlBlock->getManagedObjectProxy() <= snd.clientSideControlBlock->getManagedObjectProxy();
231  }
232  template<class PrxTA, class PrxTB> bool operator>(const RemoteHandle<PrxTA>& fst, const RemoteHandle<PrxTB>& snd)
233  {
234  return fst.clientSideControlBlock->getManagedObjectProxy() > snd.clientSideControlBlock->getManagedObjectProxy();
235  }
236  template<class PrxTA, class PrxTB> bool operator>=(const RemoteHandle<PrxTA>& fst, const RemoteHandle<PrxTB>& snd)
237  {
238  return fst.clientSideControlBlock->getManagedObjectProxy() >= snd.clientSideControlBlock->getManagedObjectProxy();
239  }
240 }
armarx::operator!=
bool operator!=(const RemoteHandle< PrxTA > &fst, const RemoteHandle< PrxTB > &snd)
Definition: RemoteHandle.h:220
armarx::RemoteHandle::RemoteHandle
RemoteHandle()=default
armarx::RemoteHandle::operator*
PrxType operator*()
Definition: RemoteHandle.h:180
armarx::RemoteHandle::operator<
friend bool operator<(const RemoteHandle< PrxTA > &fst, const RemoteHandle< PrxTB > &snd)
Definition: RemoteHandle.h:224
armarx::RemoteHandle::operator>=
friend bool operator>=(const RemoteHandle< PrxTA > &fst, const RemoteHandle< PrxTB > &snd)
Definition: RemoteHandle.h:236
armarx::RemoteHandle::get
PrxType get()
Definition: RemoteHandle.h:196
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::RemoteHandle::operator==
friend bool operator==(const RemoteHandle< PrxTA > &fst, const RemoteHandle< PrxTB > &snd)
Definition: RemoteHandle.h:216
armarx::detail::operator*
StreamPrinter< Fnc > operator*(StreamPrinterTag, Fnc &&f)
Definition: LoggingUtil.h:44
armarx::RemoteHandle::operator>
friend bool operator>(const RemoteHandle< PrxTA > &fst, const RemoteHandle< PrxTB > &snd)
Definition: RemoteHandle.h:232
armarx::operator>=
bool operator>=(const RemoteHandle< PrxTA > &fst, const RemoteHandle< PrxTB > &snd)
Definition: RemoteHandle.h:236
armarx::operator==
bool operator==(const RemoteHandle< PrxTA > &fst, const RemoteHandle< PrxTB > &snd)
Definition: RemoteHandle.h:216
armarx::RemoteHandle::getclientSideControlBlock
const IceInternal::Handle< const ClientSideRemoteHandleControlBlockBase > getclientSideControlBlock() const
Definition: RemoteHandle.h:211
armarx::RemoteHandle::operator->
PrxType operator->()
Definition: RemoteHandle.h:188
armarx::RemoteHandle::uncheckedCast
RemoteHandle< TargetPrxType > uncheckedCast() const
Definition: RemoteHandle.h:161
armarx::RemoteHandle::operator!=
friend bool operator!=(const RemoteHandle< PrxTA > &fst, const RemoteHandle< PrxTB > &snd)
Definition: RemoteHandle.h:220
armarx::operator<
bool operator<(const RemoteHandle< PrxTA > &fst, const RemoteHandle< PrxTB > &snd)
Definition: RemoteHandle.h:224
armarx::RemoteHandle::~RemoteHandle
~RemoteHandle()=default
armarx::RemoteHandle
The RemoteHandle class wrapps a ClientSideRemoteHandleControlBlock and can be used just as a Ice prox...
Definition: RemoteHandle.h:45
armarx::operator<=
bool operator<=(const RemoteHandle< PrxTA > &fst, const RemoteHandle< PrxTB > &snd)
Definition: RemoteHandle.h:228
armarx::RemoteHandle::operator<=
friend bool operator<=(const RemoteHandle< PrxTA > &fst, const RemoteHandle< PrxTB > &snd)
Definition: RemoteHandle.h:228
armarx::meta::TypeTemplateTraits::IsInstanceOf
Whether a type T is the instance of a given template Template.
Definition: TemplateMetaProgramming.h:71
armarx::RemoteHandle::operator=
RemoteHandle< PrxType > & operator=(const RemoteHandle< PrxType > &other)=default
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::RemoteHandle::checkedCast
RemoteHandle< TargetPrxType > checkedCast() const
Definition: RemoteHandle.h:171
armarx::operator>
bool operator>(const RemoteHandle< PrxTA > &fst, const RemoteHandle< PrxTB > &snd)
Definition: RemoteHandle.h:232