OpenSplice ISO C++ 2 DCPS  v6.x
ISO C++ 2 OpenSplice Data Distribution Service Data-Centric Publish-Subscribe API
find.hpp
Go to the documentation of this file.
1 /*
2  * Vortex OpenSplice
3  *
4  * This software and documentation are Copyright 2006 to 2021 ADLINK
5  * Technology Limited, its affiliated companies and licensors. All rights
6  * reserved.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 #ifndef OSPL_DDS_SUB_DETAIL_FIND_HPP_
22 #define OSPL_DDS_SUB_DETAIL_FIND_HPP_
23 
28 // Implementation
29 #include <string>
30 #include <vector>
31 
32 #include <dds/sub/DataReader.hpp>
33 #include <dds/sub/Subscriber.hpp>
36 
37 #include <org/opensplice/sub/SubscriberDelegate.hpp>
38 #include <org/opensplice/sub/BuiltinSubscriberDelegate.hpp>
39 #include <org/opensplice/sub/AnyDataReaderDelegate.hpp>
40 
46 namespace dds
47 {
48 namespace sub
49 {
50 
51 
52 
53 namespace detail
54 {
55 
56 
57 /*********************************************************************
58  * To be able to properly copy found readers into the given iterators
59  * (which are related to typed DataReaders or AnyDataReaders), we have
60  * to specialize the find() template functions.
61  * But template functions specialization is not supported by C++. So,
62  * we have to use these helper classes to get the specialization.
63  *********************************************************************/
64 
65 typedef std::vector<org::opensplice::sub::AnyDataReaderDelegate::ref_type> base_readers_vector;
66 typedef std::vector<org::opensplice::sub::AnyDataReaderDelegate::ref_type>::iterator base_readers_iterator;
67 
68 /*
69  * Copy helper class for typed readers.
70  */
71 template <typename READER, typename ITERATOR>
72 class ReadersCopySpecialization
73 {
74 public:
75  static
76  bool copy(base_readers_iterator base_iter, ITERATOR typed_iter) {
77  bool copied = false;
78  try {
79  /* Cast base reader to typed delegate: */
80  typename READER::DELEGATE_REF_T reader_typed =
81  OSPL_CXX11_STD_MODULE::dynamic_pointer_cast<typename READER::DELEGATE_T>(*base_iter);
82  READER dr(reader_typed);
83  if(dr != dds::core::null)
84  {
85  *typed_iter = dr;
86  copied = true;
87  }
88  } catch (...) {
89  /* Ignore, because subscriber can have returned readers that are of
90  * different types. */
91  }
92  return copied;
93  }
94 };
95 
96 /*
97  * Copy helper class for any readers.
98  */
99 template <typename ITERATOR>
100 class ReadersCopySpecialization<dds::sub::AnyDataReader, ITERATOR>
101 {
102 public:
103  static
104  bool copy(base_readers_iterator base_iter, ITERATOR any_iter) {
105  *any_iter = (*base_iter)->wrapper_to_any();
106  return true;
107  }
108 };
109 
110 
111 /*
112  * Copy helper class for list of readers.
113  */
114 template <typename READER, typename ITERATOR>
115 class ReadersCopy
116 {
117 public:
118  static
119  uint32_t copy(base_readers_vector base_readers,
120  ITERATOR begin,
121  uint32_t max_size)
122  {
123  uint32_t size = 0;
124  base_readers_iterator iter;
125  for (iter = base_readers.begin(); (size < max_size) && (iter != base_readers.end()); ++iter) {
126  if (ReadersCopySpecialization<READER, ITERATOR>::copy(iter, begin)) {
127  begin++;
128  size++;
129  }
130  }
131  return size;
132  }
133 
134  static
135  uint32_t copy(base_readers_vector base_readers,
136  ITERATOR begin)
137  {
138  uint32_t size = 0;
139  base_readers_iterator iter;
140  for (iter = base_readers.begin(); iter != base_readers.end(); ++iter) {
141  if (ReadersCopySpecialization<READER, ITERATOR>::copy(iter, begin)) {
142  begin++;
143  size++;
144  }
145  }
146  return size;
147  }
148 };
149 
150 } /* namespace detail */
151 
152 
153 
154 template <typename READER, typename FwdIterator>
155 uint32_t
156 find(const dds::sub::Subscriber& sub,
157  const std::string &topic_name,
158  FwdIterator begin, uint32_t max_size)
159 {
160  uint32_t size = 0;
161  ISOCPP_REPORT_STACK_DDS_BEGIN(sub);
162  if (max_size > 0) {
163  detail::base_readers_vector base_readers;
164  base_readers = sub.delegate()->find_datareaders(topic_name);
165  size = detail::ReadersCopy<READER, FwdIterator>::copy(base_readers, begin, max_size);
166  }
167  return size;
168 }
169 
170 template <typename READER, typename BinIterator>
171 uint32_t
172 find(const dds::sub::Subscriber& sub,
173  const std::string &topic_name,
174  BinIterator begin)
175 {
176  ISOCPP_REPORT_STACK_DDS_BEGIN(sub);
177  uint32_t size = 0;
178  detail::base_readers_vector base_readers;
179  base_readers = sub.delegate()->find_datareaders(topic_name);
180  size = detail::ReadersCopy<READER, BinIterator>::copy(base_readers, begin);
181  return size;
182 }
183 
184 template <typename READER, typename T, typename FwdIterator>
185 uint32_t
186 find(const dds::sub::Subscriber& sub,
187  const dds::topic::TopicDescription& topic_description,
188  FwdIterator begin, uint32_t max_size)
189 {
190  ISOCPP_REPORT_STACK_DDS_BEGIN(sub);
191  return find<READER, T, FwdIterator>(sub, topic_description.name(), begin, max_size);
192 }
193 
194 template <typename READER, typename T, typename BinIterator>
195 uint32_t
196 find(const dds::sub::Subscriber& sub,
197  const dds::topic::TopicDescription& topic_description,
198  BinIterator begin)
199 {
200  ISOCPP_REPORT_STACK_DDS_BEGIN(sub);
201  return find<READER, T, BinIterator>(sub, topic_description.name(), begin);
202 }
203 
204 template <typename READER, typename FwdIterator>
205 uint32_t
206 find(const dds::sub::Subscriber& sub,
207  const dds::sub::status::DataState& rs,
208  FwdIterator begin, uint32_t max_size)
209 {
210  ISOCPP_REPORT_STACK_DDS_BEGIN(sub);
211  uint32_t size = 0;
212  if (max_size > 0) {
213  detail::base_readers_vector base_readers;
214  base_readers = sub.delegate()->get_datareaders(rs);
215  size = detail::ReadersCopy<READER, FwdIterator>::copy(base_readers, begin, max_size);
216  }
217  return size;
218 }
219 
220 template <typename READER, typename BinIterator>
221 uint32_t
222 find(const dds::sub::Subscriber& sub,
223  const dds::sub::status::DataState& rs,
224  BinIterator begin)
225 {
226  ISOCPP_REPORT_STACK_DDS_BEGIN(sub);
227  uint32_t size = 0;
228  detail::base_readers_vector base_readers;
229  base_readers = sub.delegate()->get_datareaders(rs);
230  size = detail::ReadersCopy<READER, BinIterator>::copy(base_readers, begin);
231  return size;
232 }
233 
234 }
235 }
236 
239 #endif /* OSPL_DDS_SUB_DETAIL_FIND_HPP_ */
240 
A Subscriber is the object responsible for the actual reception of the data resulting from its subscr...
Definition: Subscriber.hpp:53
This class is the base for Topic, ContentFilteredTopic and MultiTopic.
Class to hold sample DataState information.
Definition: DataState.hpp:371
Definition: array.hpp:23
uint32_t find(const dds::sub::Subscriber &sub, const std::string &topic_name, FwdIterator begin, uint32_t max_size)
const std::string & name() const