DataStorm  0.1
Data Distribution Service
Types.h
Go to the documentation of this file.
1 // **********************************************************************
2 //
3 // Copyright (c) 2018-present ZeroC, Inc. All rights reserved.
4 //
5 // **********************************************************************
6 
7 #pragma once
8 
9 #include <DataStorm/Config.h>
10 
11 #include <Ice/Communicator.h>
12 #include <Ice/Optional.h>
13 
14 namespace DataStorm
15 {
16 
20 enum struct DiscardPolicy
21 {
23  None,
24 
31  SendTime,
32 
37  Priority
38 };
39 
44 enum struct ClearHistoryPolicy
45 {
47  OnAdd,
48 
50  OnRemove,
51 
53  OnAll,
54 
57 
59  Never
60 };
61 
68 class Config
69 {
70 public:
71 
82  Config(Ice::optional<int> sampleCount = Ice::nullopt,
83  Ice::optional<int> sampleLifetime = Ice::nullopt,
84  Ice::optional<ClearHistoryPolicy> clearHistory = Ice::nullopt) noexcept :
85  sampleCount(std::move(sampleCount)),
86  sampleLifetime(std::move(sampleLifetime)),
87  clearHistory(std::move(clearHistory))
88  {
89  }
90 
96  Ice::optional<int> sampleCount;
97 
104  Ice::optional<int> sampleLifetime;
105 
111  Ice::optional<ClearHistoryPolicy> clearHistory;
112 };
113 
122 class ReaderConfig : public Config
123 {
124 public:
125 
137  ReaderConfig(Ice::optional<int> sampleCount = Ice::nullopt,
138  Ice::optional<int> sampleLifetime = Ice::nullopt,
139  Ice::optional<ClearHistoryPolicy> clearHistory = Ice::nullopt,
140  Ice::optional<DiscardPolicy> discardPolicy = Ice::nullopt) noexcept :
141  Config(std::move(sampleCount), std::move(sampleLifetime), std::move(clearHistory)),
142  discardPolicy(std::move(discardPolicy))
143  {
144  }
145 
150  Ice::optional<DiscardPolicy> discardPolicy;
151 };
152 
161 class WriterConfig : public Config
162 {
163 public:
164 
176  WriterConfig(Ice::optional<int> sampleCount = Ice::nullopt,
177  Ice::optional<int> sampleLifetime = Ice::nullopt,
178  Ice::optional<ClearHistoryPolicy> clearHistory = Ice::nullopt,
179  Ice::optional<int> priority = Ice::nullopt) noexcept :
180  Config(std::move(sampleCount), std::move(sampleLifetime), std::move(clearHistory)),
181  priority(std::move(priority))
182  {
183  }
184 
189  Ice::optional<int> priority;
190 };
191 
195 enum struct CallbackReason
196 {
198  Connect,
199 
201  Disconnect
202 };
203 
213 template<typename T, typename Enabler=void>
214 struct Encoder
215 {
227  static std::vector<unsigned char>
228  encode(const std::shared_ptr<Ice::Communicator>& communicator, const T& value) noexcept;
229 };
230 
240 template<typename T, typename Enabler=void>
241 struct Decoder
242 {
254  static T
255  decode(const std::shared_ptr<Ice::Communicator>& communicator, const std::vector<unsigned char>& value) noexcept;
256 };
257 
266 template<typename T, typename Enabler=void>
267 struct Cloner
268 {
277  static T clone(const T& value) noexcept
278  {
279  return value;
280  }
281 };
282 
286 template<typename T>
287 struct Encoder<T, typename std::enable_if<std::is_base_of<::Ice::Value, T>::value>::type>
288 {
289  static std::vector<unsigned char>
290  encode(const std::shared_ptr<Ice::Communicator>& communicator, const T& value) noexcept
291  {
292  return Encoder<std::shared_ptr<T>>::encode(communicator, std::make_shared<T>(value));
293  }
294 };
295 
299 template<typename T>
300 struct Decoder<T, typename std::enable_if<std::is_base_of<::Ice::Value, T>::value>::type>
301 {
302  static T
303  decode(const std::shared_ptr<Ice::Communicator>& communicator, const std::vector<unsigned char>& data) noexcept
304  {
305  return *Decoder<std::shared_ptr<T>>::decode(communicator, data);
306  }
307 };
308 
312 template<typename T>
313 struct Cloner<std::shared_ptr<T>, typename std::enable_if<std::is_base_of<::Ice::Value, T>::value>::type>
314 {
315  static
316  std::shared_ptr<T> clone(const std::shared_ptr<T>& value) noexcept
317  {
318  return value->ice_clone();
319  }
320 };
321 
325 template<typename T, typename E> std::vector<unsigned char>
326 Encoder<T, E>::encode(const std::shared_ptr<Ice::Communicator>& communicator, const T& value) noexcept
327 {
328  std::vector<unsigned char> v;
329  Ice::OutputStream stream(communicator);
330  stream.write(value);
331  stream.finished(v);
332  return v;
333 }
334 
338 template<typename T, typename E> T
339 Decoder<T, E>::decode(const std::shared_ptr<Ice::Communicator>& communicator,
340  const std::vector<unsigned char>& value) noexcept
341 {
342  T v;
343  if(value.empty())
344  {
345  v = T();
346  }
347  else
348  {
349  Ice::InputStream(communicator, value).read(v);
350  }
351  return v;
352 }
353 
354 }
DiscardPolicy
The discard policy specifies how samples are discarded by readers upon receival.
Definition: Types.h:20
Clear the sample history when a new sample is received.
The Cloner template provides a method to clone user types.
Definition: Types.h:267
The callback is called because of a disconnection.
static T decode(const std::shared_ptr< Ice::Communicator > &communicator, const std::vector< unsigned char > &value) noexcept
Decode a value.
Definition: Types.h:339
Samples are discarded based on the writer priority.
Clear the sample history when a new sample which is not a partial update is received.
Ice::optional< DiscardPolicy > discardPolicy
Specifies if and how samples are discarded after being received by a reader.
Definition: Types.h:150
Clear the sample history when a Remove sample is received.
Samples are discared based on the sample timestamp.
static T clone(const T &value) noexcept
Clone the given value.
Definition: Types.h:277
The Decoder template provides a method to decode user types.
Definition: Types.h:241
Clear the sample history when a Add sample is received.
Ice::optional< int > sampleCount
The sampleCount configuration specifies how many samples are kept by the reader or writer in its samp...
Definition: Types.h:96
The WriterConfig class specifies configuration options specific to writers.
Definition: Types.h:161
ClearHistoryPolicy
The clear history policy specifies when the history is cleared.
Definition: Types.h:44
Never clear the sample history.
Ice::optional< int > priority
Specifies the writer priority.
Definition: Types.h:189
The Encoder template provides a method to encode decode user types.
Definition: Types.h:214
CallbackReason
The callback action enumurator specifies the reason why a callback is called.
Definition: Types.h:195
Definition: CtrlCHandler.h:13
WriterConfig(Ice::optional< int > sampleCount=Ice::nullopt, Ice::optional< int > sampleLifetime=Ice::nullopt, Ice::optional< ClearHistoryPolicy > clearHistory=Ice::nullopt, Ice::optional< int > priority=Ice::nullopt) noexcept
Construct a WriterConfig object.
Definition: Types.h:176
ReaderConfig(Ice::optional< int > sampleCount=Ice::nullopt, Ice::optional< int > sampleLifetime=Ice::nullopt, Ice::optional< ClearHistoryPolicy > clearHistory=Ice::nullopt, Ice::optional< DiscardPolicy > discardPolicy=Ice::nullopt) noexcept
Construct a ReaderConfig object.
Definition: Types.h:137
The configuration base class holds configuration options common to readers and writers.
Definition: Types.h:68
Samples are never discarded.
The ReaderConfig class specifies configuration options specific to readers.
Definition: Types.h:122
Ice::optional< int > sampleLifetime
The sampleLifetime configuration specifies samples to keep in the writer or reader history based on t...
Definition: Types.h:104
static std::vector< unsigned char > encode(const std::shared_ptr< Ice::Communicator > &communicator, const T &value) noexcept
Encode the given value.
Definition: Types.h:326
Config(Ice::optional< int > sampleCount=Ice::nullopt, Ice::optional< int > sampleLifetime=Ice::nullopt, Ice::optional< ClearHistoryPolicy > clearHistory=Ice::nullopt) noexcept
Construct a Config object.
Definition: Types.h:82
Ice::optional< ClearHistoryPolicy > clearHistory
The clear history policy specifies when samples are removed from the sample history.
Definition: Types.h:111
The callback is called because of connection.