Ignition Common

API Reference

3.0.0
detail/PluginPtr.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 
19 #ifndef IGNITION_COMMON_DETAIL_PLUGINPTR_HH_
20 #define IGNITION_COMMON_DETAIL_PLUGINPTR_HH_
21 #include <memory>
22 #include <utility>
23 
26 
27 namespace ignition
28 {
29  namespace common
30  {
32  template <typename PluginType>
34  : dataPtr(new PluginType)
35  {
36  // Do nothing
37  }
38 
40  template <typename PluginType>
42  const TemplatePluginPtr &_other)
43  : dataPtr(new PluginType)
44  {
45  this->dataPtr->PrivateCopyPluginInstance(*_other.dataPtr);
46  }
47 
49  template <typename PluginType>
50  // the following is a false positive with cppcheck 1.82 fixed in 1.83
51  // cppcheck-suppress syntaxError
52  template <typename OtherPluginType>
55  : dataPtr(new PluginType)
56  {
58  "The requested PluginPtr cast would discard const qualifiers");
59  this->dataPtr->PrivateCopyPluginInstance(*_other.dataPtr);
60  }
61 
63  template <typename PluginType>
65  const TemplatePluginPtr &_other)
66  {
67  this->dataPtr->PrivateCopyPluginInstance(*_other.dataPtr);
68  return *this;
69  }
70 
72  template <typename PluginType>
73  template <typename OtherPluginType>
76  {
78  "The requested PluginPtr cast would discard const qualifiers");
79  this->dataPtr->PrivateCopyPluginInstance(*_other.dataPtr);
80  return *this;
81  }
82 
84  template <typename PluginType>
86  TemplatePluginPtr &&_other)
87  : dataPtr(std::move(_other.dataPtr))
88  {
89  // Do nothing
90  }
91 
93  template <typename PluginType>
95  TemplatePluginPtr &&_other)
96  {
97  this->dataPtr = std::move(_other.dataPtr);
98  return *this;
99  }
100 
102  template <typename PluginType>
105  {
106  this->Clear();
107  return *this;
108  }
109 
111  template <typename PluginType>
113  {
114  return dataPtr.get();
115  }
116 
118  template <typename PluginType>
120  {
121  return (*dataPtr);
122  }
123 
125  #define DETAIL_IGN_COMMON_PLUGINPTR_IMPLEMENT_OPERATOR(op)\
126  template <typename PluginType>\
127  bool TemplatePluginPtr<PluginType>::operator op (\
128  const TemplatePluginPtr &_other) const\
129  {\
130  return (this->dataPtr->PrivateGetInstancePtr() op \
131  _other.dataPtr->PrivateGetInstancePtr() );\
132  }
133 
140 
142  template <typename PluginType>
143  std::size_t TemplatePluginPtr<PluginType>::Hash() const
144  {
146  this->dataPtr->PrivateGetInstancePtr());
147  }
148 
150  template <typename PluginType>
152  {
153  return (nullptr == this->dataPtr->PrivateGetInstancePtr());
154  }
155 
157  template <typename PluginType>
159  {
160  return !this->IsEmpty();
161  }
162 
164  template <typename PluginType>
166  {
167  this->dataPtr->PrivateSetPluginInstance(nullptr);
168  }
169 
171  template <typename PluginType>
173  : dataPtr(new PluginType)
174  {
175  dataPtr->PrivateSetPluginInstance(info);
176  }
177  }
178 }
179 
180 // Note that opening up namespace std is legal here because we are specializing
181 // a templated structure from the STL, which is permitted (and even encouraged).
182 namespace std
183 {
187  template <typename PluginType>
188  struct hash<ignition::common::TemplatePluginPtr<PluginType>>
189  {
190  size_t operator()(
192  {
193  return ptr.Hash();
194  }
195  };
196 }
197 
198 #endif
This class manages the lifecycle of a plugin instance. It can receive a plugin instance from the Plug...
Definition: PluginPtr.hh:53
size_t operator()(const TemplatePluginPtr< PluginType > &ptr) const
Definition: detail/PluginPtr.hh:190
PluginType & operator*() const
Get a reference to the wrapper for the plugin instance that is being managed by this PluginPtr...
Definition: detail/PluginPtr.hh:119
std::size_t Hash() const
Produces a hash for the plugin instance that this PluginPtr is holding. This function allows PluginPt...
Definition: detail/PluginPtr.hh:143
TemplatePluginPtr & operator=(const TemplatePluginPtr &_other)
Copy assignment operator. This PluginPtr will now point at the same plugin instance as _other...
Definition: detail/PluginPtr.hh:64
STL namespace.
detail::ConstCompatible< To, From > ConstCompatible
Contains a static constexpr field named value which will be true if the type From has a const-quality...
Definition: TemplateHelpers.hh:47
TemplatePluginPtr()
Default constructor. Creates a PluginPtr object that does not point to any plugin instance...
Definition: detail/PluginPtr.hh:33
PluginType * operator->() const
Access the wrapper for the plugin instance and call one of its member functions.
Definition: detail/PluginPtr.hh:112
bool IsEmpty() const
Check if this PluginPtr is holding a plugin instance.
Definition: detail/PluginPtr.hh:151
T move(T... args)
T get(T... args)
Holds info required to construct a plugin.
Definition: PluginInfo.hh:38
void Clear()
Clears the Plugin instance from this PluginPtr. IsEmpty() will return true after this is used...
Definition: detail/PluginPtr.hh:165
#define DETAIL_IGN_COMMON_PLUGINPTR_IMPLEMENT_OPERATOR(op)
Definition: detail/PluginPtr.hh:125
Forward declarations for the common classes.