PdCom  5.0
Process data communication client
Subscription.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * vim:tw=78
3  *
4  * Copyright (C) 2021 Richard Hacker (lerichi at gmx dot net),
5  * Florian Pose (fp at igh dot de),
6  * Bjarne von Horn (vh at igh dot de).
7  *
8  * This file is part of the PdCom library.
9  *
10  * The PdCom library is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or (at your
13  * option) any later version.
14  *
15  * The PdCom library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with the PdCom library. If not, see <http://www.gnu.org/licenses/>.
22  *
23  *****************************************************************************/
24 
27 #ifndef PDCOM5_SUBSCRIPTION_H
28 #define PDCOM5_SUBSCRIPTION_H
29 
30 #include "Exception.h"
31 #include "Variable.h"
32 #include "details.h"
33 #include "visibility.h"
34 
35 #include <chrono>
36 #include <functional>
37 #include <memory>
38 #include <string>
39 
40 namespace PdCom {
41 namespace impl {
42 class Subscription;
43 }
44 
45 class Process;
46 class Variable;
47 class Subscriber;
48 
55 struct PDCOM5_PUBLIC Selector
56 {
57  using CopyFunctionType = std::function<
58  void(void * /* destination */, const void * /* src */)>;
59 
60  virtual ~Selector() = default;
65  virtual std::unique_ptr<Selector> clone() const;
70  virtual std::size_t getRequiredSize(const Variable &variable) const;
76  virtual CopyFunctionType getCopyFunction(const Variable &variable) const;
77 };
78 
93 class PDCOM5_PUBLIC Subscription
94 {
95  public:
96  struct PDCOM5_PUBLIC invalid_subscription : Exception
97  {
98  invalid_subscription() : Exception("invalid subscription") {}
99  };
100 
101  enum class State {
102  Invalid = 0,
103  Pending,
104  Active,
105  };
106 
107  enum class SubscriberState {
109  FullyConstructed,
111  UnderConstruction,
112  };
113 
115  Subscription() = default;
116  Subscription(Subscription &&) noexcept;
117  Subscription &operator=(Subscription &&) noexcept;
118 
125  Subscription(
126  Subscriber &subscriber,
127  const Variable &variable,
128  const Selector &selector = {});
129 
137  Subscription(
138  Subscriber &subscriber,
139  Process &process,
140  const std::string &path,
141  const Selector &selector = {});
142 
147  void poll();
154  const void *getData() const;
162  template <typename T>
163  typename std::enable_if<!std::is_arithmetic<T>::value, void>::type
164  getValue(T &dest) const
165  {
166  details::copyData(
167  &dest[0],
169  getData(), getVariable().getTypeInfo().type,
170  std::min<size_t>(
171  dest.size(),
172  getVariable().getSizeInfo().totalElements()));
173  }
180  template <typename T>
181  typename std::enable_if<std::is_arithmetic<T>::value, void>::type
182  getValue(T &dest) const
183  {
184  details::copyData(
185  &dest, details::TypeInfoTraits<T>::type_info.type, getData(),
186  getVariable().getTypeInfo().type, 1);
187  }
188 
194  Variable getVariable() const noexcept;
195 
201  void print(std::ostream &os, char delimiter) const;
202 
206  bool empty() const noexcept { return !(pimpl); }
210  State getState() const noexcept { return state_; }
211 
212  private:
213  friend impl::Subscription;
214  struct ImplDeleter
215  {
216  void operator()(impl::Subscription *) const;
217  };
218  std::unique_ptr<impl::Subscription, ImplDeleter> pimpl;
219  State state_ = State::Invalid;
220 };
221 
222 } // namespace PdCom
223 
224 
225 #endif // PDCOM5_SUBSCRIPTION_H
Definition: Exception.h:33
Selector base class for creating views on multidimensional data.
Definition: Subscription.h:55
Definition: details.h:40
PdCom Variable interface.
Definition: Variable.h:56
Definition: Subscription.h:96
PdCom Subscription interface.
Definition: Subscription.h:93
std::enable_if< std::is_arithmetic< T >::value, void >::type getValue(T &dest) const
Copy the values into a custom buffer.
Definition: Subscription.h:182
State getState() const noexcept
Get the current state.
Definition: Subscription.h:210
Definition: Subscriber.h:91
SubscriberState
Definition: Subscription.h:107
library version string as "major.minor.patch"
Definition: details.h:37
std::enable_if<!std::is_arithmetic< T >::value, void >::type getValue(T &dest) const
Copy the values into a custom buffer.
Definition: Subscription.h:164