CoreDX DDS C++ Reference Manual
sample.hh
Go to the documentation of this file.
1 /*****************************************************************
2  *
3  * file: sample.hh
4  * desc: This file provides the C++ Sample declarations.
5  *
6  *****************************************************************
7  *
8  * Copyright(C) 2006-2016 Twin Oaks Computing, Inc
9  * All rights reserved. Castle Rock, CO 80108
10  *
11  *****************************************************************
12  *
13  * This software has been provided pursuant to a License Agreement
14  * containing restrictions on its use. This software contains
15  * valuable trade secrets and proprietary information of
16  * Twin Oaks Computing, Inc and is protected by law. It may not be
17  * copied or distributed in any form or medium, disclosed to third
18  * parties, reverse engineered or used in any manner not provided
19  * for in said License Agreement except with the prior written
20  * authorization from Twin Oaks Computing, Inc.
21  *
22  *****************************************************************/
23 
24 #ifndef DDS_SAMPLE_HH
25 #define DDS_SAMPLE_HH
26 
27 #include <string>
28 #include <stdexcept>
29 #include <memory>
30 #include <iterator>
31 
32 #include <stdio.h>
33 #include <dds/dds.hh>
34 
44 namespace DDS {
45 
46  namespace details {
47 
48  template <bool, typename T, typename U>
49  struct if_ {
50  typedef T type;
51  };
52 
53  template <typename T, typename U>
54  struct if_ <false, T, U> {
55  typedef U type;
56  };
57 
58  } // namespace details
59 
60  template<typename T, bool> class SampleIterator;
61  template<typename T> class Sample;
62  template<typename T> class SampleRef;
63  template<typename T> class WriteSample;
64  template<typename T> class WriteSampleRef;
65  template<class T> class LoanedSamples;
66  template<class T> class SharedSamples;
67 
68  namespace rpc {
69  template <typename TReq, typename TRep> class Requester;
70  template <typename TReq, typename TRep> class Replier;
71  }
72 
73  template <typename T>
74  struct dds_type_traits
75  {
76  typedef typename T::Seq Seq;
77  typedef const typename T::Seq ConstSeq;
78  typedef typename T::DataReader * DataReader;
79  typedef typename T::DataWriter * DataWriter;
80 
81  typedef DDS::SampleRef<T> SampleRefType;
82  typedef DDS::SampleRef<T> SampleIteratorValueType;
83  typedef DDS::SampleRef<const T> ConstSampleIteratorValueType;
84  typedef DDS::LoanedSamples<T> LoanedSamplesType;
85  typedef DDS::SharedSamples<T> SharedSamplesType;
88  };
89 
90  template <>
91  struct dds_type_traits<DDS::SampleInfo>
92  {
93  typedef DDS::SampleInfoSeq Seq;
94  typedef const DDS::SampleInfoSeq ConstSeq;
95  };
96 
97  struct dds_entity_traits; // defined in vendor_dependent.h
98 
99  // -------------------------------------------------------------------------
100  // -------------------------------------------------------------------------
108  template <typename T>
109  class Sample
110  {
111  public:
112  typedef T Data;
113  typedef T& DataReference;
114  typedef const T& ConstDataReference;
115  typedef typename DDS::dds_type_traits<T>::Seq Seq;
116  typedef typename DDS::dds_type_traits<T>::DataReader DataReader;
117  typedef typename DDS::dds_type_traits<T>::DataWriter DataWriter;
118 
121  Sample() :_data(NULL),_info(NULL) {}
124  Sample(const Sample<T>& other);
127  Sample(SampleRef<T> sample_ref);
130  Sample(const T &data, const DDS::SampleInfo& info);
133  ~Sample();
134 
138  DDS::SampleIdentity_t identity() const;
143  DDS::SampleIdentity_t related_identity() const;
146  void swap(Sample & other);
149  T & data();
152  DDS::SampleInfo & info();
155  const T & data() const;
158  const DDS::SampleInfo & info() const;
159 
162  void set_data(const T &t);
165  void set_data(const T *t);
168  void set_info(const DDS::SampleInfo &i);
171  void set_info(const DDS::SampleInfo *i);
172 
175  Sample & operator = (Sample<T> sample);
178  Sample & operator = (SampleRef<T> sample_ref);
181  operator DataReference ();
184  operator ConstDataReference () const;
185 
186  friend class DDS::DataReader;
187  friend class DDS::DataWriter;
188  template <typename TReq, typename TRep> friend class DDS::rpc::Requester;
189  template <typename TReq, typename TRep> friend class DDS::rpc::Replier;
190 
191  protected:
192  T * _data;
193  DDS::SampleInfo * _info;
194  DDS::SampleIdentity_t _identity;
195  DDS::SampleIdentity_t _related_identity;
196  };
197 
198  // -------------------------------------------------------------------------
199  // -------------------------------------------------------------------------
210  template <typename T>
211  class SampleRef
212  {
213  public:
214  typedef T Data;
215  typedef T& DataReference;
216  typedef const T& ConstDataReference;
217  typedef typename DDS::dds_type_traits<T>::Seq Seq;
218  typedef typename DDS::dds_type_traits<T>::DataReader DataReader;
219  typedef typename DDS::dds_type_traits<T>::DataWriter DataWriter;
220 
221  public:
224  SampleRef();
227  SampleRef(T * data, DDS::SampleInfo * info);
230  SampleRef(T & data, DDS::SampleInfo & info);
233  SampleRef(Sample<T> & sample);
234 
237  SampleRef & operator = (Sample<T> & sample);
240  T & data() const;
243  DDS::SampleInfo & info() const;
246  operator T & () const;
247 
248  // this might be more useful, right? [ is the spec wrong ? ]
249  // T * operator ->();
250  // const T * operator ->() const;
251 
252  SampleRef<T> * operator ->();
253  const SampleRef<T> * operator ->() const;
254 
257  void set_data(T * data);
260  void set_data(T & data);
263  void set_info(DDS::SampleInfo * info);
266  void set_info(DDS::SampleInfo & info);
269  bool is_nil_data() const;
272  bool is_nil_info() const;
275  void swap(SampleRef & other) throw();
279  DDS::SampleIdentity_t identity() const;
284  DDS::SampleIdentity_t related_identity() const;
285 
286  friend class DDS::DataReader;
287  friend class DDS::DataWriter;
288  template <typename TReq, typename TRep> friend class DDS::rpc::Requester;
289  template <typename TReq, typename TRep> friend class DDS::rpc::Replier;
290  protected:
291  T * _data;
292  DDS::SampleInfo * _info;
293  DDS::SampleIdentity_t _identity;
294  DDS::SampleIdentity_t _related_identity;
295  };
296 
297  // -------------------------------------------------------------------------
298  // -------------------------------------------------------------------------
305  template <typename T, bool IsConst>
306  class SampleIterator
307  {
308  public:
309  typedef std::random_access_iterator_tag iterator_category;
310 
313  typedef typename details::if_<IsConst,
316 
317  typedef value_type reference;
318  typedef value_type pointer;
321  typedef std::ptrdiff_t difference_type;
322 
323  typedef typename details::if_<IsConst,
324  typename dds_type_traits<T>::ConstSeq,
325  typename dds_type_traits<T>::Seq>::type Seq;
326 
327  typedef typename details::if_<IsConst,
328  typename dds_type_traits<DDS::SampleInfo>::ConstSeq,
329  typename dds_type_traits<DDS::SampleInfo>::Seq>::type InfoSeq;
330 
333  SampleIterator();
336  explicit SampleIterator(Seq & seq,
337  InfoSeq & info_seq,
338  int position = 0);
344  SampleIterator & operator = (const SampleIterator<T, false> & si);
348  value_type operator * () const;
352  value_type operator ->() const;
356  value_type operator [] (difference_type offset) const;
359  SampleIterator & operator ++ ();
362  SampleIterator operator ++ (int);
365  SampleIterator & operator -- ();
368  SampleIterator operator -- (int);
371  SampleIterator & operator -= (difference_type i);
374  SampleIterator & operator += (difference_type i);
377  SampleIterator operator - (difference_type i) const;
380  SampleIterator operator + (difference_type i) const;
383  difference_type operator - (const SampleIterator & s2) const;
386  bool operator < (const SampleIterator & s2) const;
389  bool operator > (const SampleIterator & s2) const;
392  bool operator <= (const SampleIterator & s2) const;
395  bool operator >= (const SampleIterator & s2) const;
398  bool operator == (const SampleIterator & s2) const;
401  bool operator != (const SampleIterator & s2) const;
404  bool is_end() const;
405 
406  protected:
407  uint32_t _current;
408  Seq * _sample_seq;
409  InfoSeq * _info_seq;
410  };
411 
412  // -------------------------------------------------------------------------
413  // -------------------------------------------------------------------------
414  template <typename T>
415  LoanedSamples<T> move(LoanedSamples<T> & ls) throw();
416 
417  // -------------------------------------------------------------------------
418  // -------------------------------------------------------------------------
426  // This class implements the move-constructor idiom as in DDS-PSM-Cxx
427  template <typename T>
428  class LoanedSamples
429  {
430  public:
431  typedef typename dds_type_traits<T>::Seq Seq;
432  typedef typename dds_type_traits<T>::DataReader DataReader;
433 
440 
449  typedef std::ptrdiff_t difference_type;
450 
451  public:
454  LoanedSamples();
457  ~LoanedSamples() throw();
458 
466  static LoanedSamples<T> move_construct_from_loans(DataReader reader,
467  Seq & data_seq,
468  DDS::SampleInfoSeq & info_seq);
474  void release(DataReader & reader_ptr,
475  Seq & data_seq,
476  DDS::SampleInfoSeq & info_seq);
477 
478 #ifdef TOC_MOVE_SEMANTICS_AVAILABLE
479  LoanedSamples(const LoanedSamples &) = delete;
480  LoanedSamples(LoanedSamples &&) noexcept;
481  LoanedSamples & operator = (const LoanedSamples &) = delete;
482  LoanedSamples & operator = (LoanedSamples &&) noexcept;
483 #else
484  struct LoanMemento
485  {
486  DataReader _reader;
487  Seq _data_seq;
488  DDS::SampleInfoSeq _info_seq;
489  };
490 
491  LoanedSamples(LoanedSamples &); // copy constructor (to force VS to enable RVO)
492 
496  LoanedSamples(LoanMemento loan_memento) throw();
499  LoanedSamples & operator = (LoanMemento loan_memento) throw();
500  operator LoanMemento () throw();
503  friend LoanedSamples move<> (LoanedSamples &) throw();
504 #endif
505 
507  Seq & data_seq();
510  DDS::SampleInfoSeq & info_seq();
511 
514  const Seq & data_seq() const;
517  const DDS::SampleInfoSeq & info_seq() const;
518 
522  value_type operator [] (size_t index);
526  const_value_type operator [] (size_t index) const;
529  int length() const;
532  int size() const { return this->length(); }
537  void return_loan();
540  void swap(LoanedSamples & that) throw();
547  SharedSamples<T> to_shared() throw();
550  iterator begin();
553  iterator end();
556  const_iterator begin() const;
559  const_iterator end() const;
560 
561  private:
562  DataReader _reader;
563  Seq _data_seq;
564  DDS::SampleInfoSeq _info_seq;
565 
566 #ifndef TOC_MOVE_SEMANTICS_AVAILABLE
567  LoanedSamples & operator = (LoanedSamples &);
568 #endif
569 
570  };
571 
572  // -------------------------------------------------------------------------
573  // -------------------------------------------------------------------------
581  template <typename T>
582  class SharedSamples
583  {
584  public:
585  typedef typename dds_type_traits<T>::Seq Seq;
586  typedef typename dds_type_traits<T>::DataReader DataReader;
587 
594 
603  typedef std::ptrdiff_t difference_type;
604 
605  public:
608  SharedSamples();
611  SharedSamples(const SharedSamples &);
614  ~SharedSamples() throw();
617  SharedSamples & operator = (const SharedSamples &);
620  Seq & data_seq();
623  DDS::SampleInfoSeq & info_seq();
626  const Seq & data_seq() const;
629  const DDS::SampleInfoSeq & info_seq() const;
633  value_type operator [] (size_t index);
637  const_value_type operator [] (size_t index) const;
640  int length() const;
643  void swap(SharedSamples & that) throw();
646  iterator begin();
649  iterator end();
652  const_iterator begin() const;
655  const_iterator end() const;
656 
657  private:
658  Seq * _data_seq;
659  DDS::SampleInfoSeq * _info_seq;
660  };
661 
662  // -------------------------------------------------------------------------
663  // -------------------------------------------------------------------------
668  template <typename T>
669  class WriteSample
670  {
671 
672  public:
675  WriteSample();
678  WriteSample(const WriteSample<T>& other);
681  WriteSample(const T & data);
684  WriteSample(const WriteSampleRef<T> & wsref);
687  ~WriteSample();
688 
691  WriteSample & operator = (const WriteSampleRef<T> & wsref);
694  WriteSample & operator = (const WriteSample<T> & ws);
695 
698  const T & data() const; // this can't allocate new 'T'
701  T & data();
704  void set_data(const T & data);
707  void set_identity(const DDS::SampleIdentity_t & id);
710  void swap(WriteSample & other) throw();
713  DDS::SampleIdentity_t identity() const;
714 
715  friend class DDS::DataReader;
716  friend class DDS::DataWriter;
717  template <typename TReq, typename TRep> friend class DDS::rpc::Requester;
718  template <typename TReq, typename TRep> friend class DDS::rpc::Replier;
719  protected:
720  T * _data;
721  SampleIdentity_t _identity;
722  };
723 
724  // -------------------------------------------------------------------------
725  // -------------------------------------------------------------------------
730  template <typename T>
731  class WriteSampleRef
732  {
733  public:
736  WriteSampleRef();
739  WriteSampleRef(T & data); // was "const data", but that doesn't seem like much of a 'Ref'
745  WriteSampleRef(const WriteSampleRef<T> & wsref);
748  WriteSampleRef & operator = (WriteSample<T> & ws);
751  WriteSampleRef & operator = (const WriteSampleRef<T> & wsref);
754  const T & data() const;
757  T & data();
760  void set_data(T & data); // was const T & data... but doesn't seem like a 'Ref'
763  void set_identity(const DDS::SampleIdentity_t & id);
766  void swap(WriteSampleRef & other) throw();
769  DDS::SampleIdentity_t identity() const;
770 
771  friend class DDS::DataReader;
772  friend class DDS::DataWriter;
773  template <typename TReq, typename TRep> friend class DDS::rpc::Requester;
774  template <typename TReq, typename TRep> friend class DDS::rpc::Replier;
775  protected:
776  T * _data;
777  SampleIdentity_t _identity;
778  };
779 
780 
781  // -------------------------------------------------------------------------
782  // -------------------------------------------------------------------------
783 
784 #ifdef TOC_MOVE_SEMANTICS_AVAILABLE
785  template <typename T>
786  LoanedSamples<T> move(LoanedSamples<T> & ls) noexcept
787  {
788  return std::move(ls);
789  }
790 #else
791  template <typename T>
793  {
794  return LoanedSamples<T>(typename LoanedSamples<T>::LoanMemento(ls));
795  }
796 #endif
797 
798  template <typename T>
799  typename LoanedSamples<T>::iterator begin(LoanedSamples<T> & ls)
800  {
801  return ls.begin();
802  }
803 
804  template <typename T>
805  typename LoanedSamples<T>::const_iterator begin(const LoanedSamples<T> & ls)
806  {
807  return ls.begin();
808  }
809 
810  template <typename T>
812  {
813  return ls.end();
814  }
815 
816  template <typename T>
817  typename LoanedSamples<T>::const_iterator end(const LoanedSamples<T> & ls)
818  {
819  return ls.end();
820  }
821 
822  template <typename T>
823  void swap(LoanedSamples<T> &ls1, LoanedSamples<T> &ls2) throw()
824  {
825  ls1.swap(ls2);
826  }
827 
828  template <typename T>
829  typename SharedSamples<T>::iterator begin(SharedSamples<T> & ss)
830  {
831  return ss.begin();
832  }
833 
834  template <typename T>
835  typename SharedSamples<T>::const_iterator begin(const SharedSamples<T> & ss)
836  {
837  return ss.begin();
838  }
839 
840  template <typename T>
842  {
843  return ss.end();
844  }
845 
846  template <typename T>
847  typename SharedSamples<T>::const_iterator end(const SharedSamples<T> & ss)
848  {
849  return ss.end();
850  }
851 
852  template <typename T>
853  void swap(SharedSamples<T> &ss1, SharedSamples<T> &ss2) throw()
854  {
855  ss1.swap(ss2);
856  }
857 
858 
859 
860 } // namespace dds
861 
862 
863 #include <dds/sample_tmpl.hh>
864 #include <dds/sampleref_tmpl.hh>
865 #include <dds/sampleiterator_tmpl.hh>
866 #include <dds/loanedsamples_tmpl.hh>
867 #include <dds/sharedsamples_tmpl.hh>
868 #include <dds/writesample_tmpl.hh>
869 #include <dds/writesampleref_tmpl.hh>
870 
871 
872 #endif // DDS_SAMPLE_HH
873 
SampleIterator< T, true > const_iterator
Definition: sample.hh:593
The DataWriter entity provides an interface for the application to publish (write) data...
Definition: dds.hh:2179
A Requester sends requests and receives replies.
Definition: request_reply.hh:54
SampleIterator< T, false > iterator
Definition: sample.hh:590
dds_type_traits< T >::ConstSampleIteratorValueType const_value_type
Definition: sample.hh:600
A replier receives requests and send replies.
Definition: request_reply.hh:57
Holds a collection of sample data and related meta-data.
Definition: sample.hh:65
iterator begin()
Obtain a SampleIterator object that refers to the beginning of the data sequence. ...
Holds a reference to sample data intended for transmission.
Definition: sample.hh:64
Sample()
Default constructor. Empty data.
Definition: sample.hh:121
Sample holds the data and related meta information.
Definition: sample.hh:61
void swap(LoanedSamples &that)
Swap contents and responsibility between this and &#39;that&#39;.
SampleIterator< T, true > const_iterator
Definition: sample.hh:439
details::if_< IsConst, typename dds_type_traits< T >::ConstSampleIteratorValueType, typename dds_type_traits< T >::SampleIteratorValueType >::type value_type
Definition: sample.hh:315
SampleRef holds a reference to data and related meta information.
Definition: sample.hh:62
The DataReader entity allows the application to subscribe to and read data.
Definition: dds.hh:2466
std::ptrdiff_t difference_type
Definition: sample.hh:449
SampleIterator< T, false > iterator
Definition: sample.hh:436
Provides the DDS infrastructure.
Definition: dds_builtin_basic.hh:28
Holds sample data intended for transmission.
Definition: sample.hh:63
dds_type_traits< T >::SampleIteratorValueType value_type
Definition: sample.hh:597
iterator end()
Obtain a SampleIterator object that refers to the end of the data sequence.
Definition: dds_builtin_basic.hh:239
dds_type_traits< T >::ConstSampleIteratorValueType const_value_type
Definition: sample.hh:446
std::ptrdiff_t difference_type
Definition: sample.hh:321
int size() const
Access the length of the contained data sequence.
Definition: sample.hh:532
iterator end()
Obtain an iterator that refers to the end of the contained sequences.
dds_type_traits< T >::SampleIteratorValueType value_type
Definition: sample.hh:443
Provides a means to iterate over a collection of Samples.
Definition: sample.hh:60
std::ptrdiff_t difference_type
Definition: sample.hh:603
iterator begin()
Obtain an iterator that refers to the beginning of the contained sequences.
Holds a reference to a collection of sample data and related meta-data.
Definition: sample.hh:66
void swap(SharedSamples &that)
Swap the contents of this with &#39;that&#39;.

© 2009-2017 Twin Oaks Computing, Inc
Castle Rock, CO 80108
All rights reserved.