RTI Routing Service Version 7.7.0
PortIterator.hpp
1/*
2 * (c) Copyright, Real-Time Innovations, 2017.
3 * All rights reserved.
4 *
5 * No duplications, whole or partial, manual or electronic, may be made
6 * without express written permission. Any such copies, or
7 * revisions thereof, must display this notice unaltered.
8 * This code contains trade secrets of Real-Time Innovations, Inc.
9 */
10
11#ifndef RTI_ROUTING_PROCESSOR_PORTS_ITERATOR_HPP_
12#define RTI_ROUTING_PROCESSOR_PORTS_ITERATOR_HPP_
13
14#include <dds/core/Value.hpp>
15#include <dds/core/SafeEnumeration.hpp>
16#include <rti/core/NativeValueType.hpp>
17
18
19#include <dds/core/xtypes/DynamicData.hpp>
20#include <dds/sub/SampleInfo.hpp>
21#include <rti/routing/processor/Input.hpp>
22#include <rti/routing/processor/Output.hpp>
23
24namespace rti { namespace routing { namespace processor {
25
26template <typename PORT, typename DataRep, typename InfoRep> struct TypedPortType;
27
28template <typename DataRep, typename InfoRep>
29struct TypedPortType<Input, DataRep, InfoRep>
30{
31 typedef TypedInput<DataRep, InfoRep> type;
32 typedef RTI_RoutingServiceInput native_type;
33
34 static native_type * begin(RTI_RoutingServiceRoute *native_route)
35 {
36 return RTI_RoutingServiceRoute_get_first_input(native_route);
37 }
38
39 static native_type * next(
40 RTI_RoutingServiceRoute *native_route,
41 native_type *native)
42 {
43 return RTI_RoutingServiceRoute_get_next_input(native_route, native);
44 }
45
46 static Input* from_native(native_type *native)
47 {
48 return static_cast<Input*>(RTI_RoutingServiceInput_get_user_data(
49 native));
50 }
51
52};
53
54template <typename DataRep, typename InfoRep>
55struct TypedPortType<Output, DataRep, InfoRep>
56{
57 typedef TypedOutput<DataRep, InfoRep> type;
58 typedef RTI_RoutingServiceOutput native_type;
59
60 static native_type * begin(RTI_RoutingServiceRoute *native_route)
61 {
62 return RTI_RoutingServiceRoute_get_first_output(native_route);
63 }
64
65 static native_type * next(
66 RTI_RoutingServiceRoute *native_route,
67 native_type *native)
68 {
69 return RTI_RoutingServiceRoute_get_next_output(native_route, native);
70 }
71
72 static Output* from_native(native_type *native)
73 {
74 return static_cast<Output*>(RTI_RoutingServiceOutput_get_user_data(
75 native));
76 }
77};
78
79template <typename PORT,
80 typename DataRep,
81 typename InfoRep = dds::sub::SampleInfo>
82class PortIterator {
83public:
84 // iterator traits
85 typedef std::random_access_iterator_tag iterator_category;
86 typedef typename TypedPortType<PORT, DataRep, InfoRep>::type value_type;
87 typedef value_type reference;
88 typedef value_type pointer;
89 typedef std::ptrdiff_t difference_type;
90
91
92 explicit PortIterator(
93 RTI_RoutingServiceRoute *native_route)
94 : native_route_(native_route)
95 {
96 native_ = TypedPortType<PORT, DataRep, InfoRep>::begin(native_route);
97 }
98
99 explicit PortIterator()
100 : native_route_(NULL), native_(NULL)
101 {
102 }
103
104 PortIterator (const PortIterator<PORT, DataRep, InfoRep>& other)
105 : native_route_(other.native_route_),
106 native_(other.native_)
107 {}
108
109 PortIterator & operator = (const PortIterator<PORT, DataRep, InfoRep>& other)
110 {
111 native_route_ = other.native_route_;
112 native_ = other.native_;
113
114 return *this;
115 }
116
117 value_type operator*()
118 {
119 return TypedPortType<PORT, DataRep, InfoRep>::from_native(native_);
120 }
121
122 value_type operator->()
123 {
124 return *(*this);
125 }
126
127 PortIterator & operator++()
128 {
129 native_ = TypedPortType<PORT, DataRep, InfoRep>::next(
130 native_route_,
131 native_);
132
133 return *this;
134 }
135
136 PortIterator operator++(int)
137 {
138 PortIterator temp(*this);
139 ++(*this);
140 return temp;
141 }
142
143
144 friend bool operator<(
145 const PortIterator & s1,
146 const PortIterator & s2)
147 {
148 return s1.index() < s2.index();
149 }
150
151 friend bool operator>(
152 const PortIterator & s1,
153 const PortIterator & s2)
154 {
155 return s1.index() > s2.index();
156 }
157
158 friend bool operator<=(
159 const PortIterator & s1,
160 const PortIterator & s2)
161 {
162 return s1.index() <= s2.index();
163 }
164
165 friend bool operator>=(
166 const PortIterator & s1,
167 const PortIterator & s2)
168 {
169 return s1.index() >= s2.index();
170 }
171
172 friend bool operator==(
173 const PortIterator & s1,
174 const PortIterator & s2)
175 {
176 return (s1.index() == s2.index());
177 }
178
179 friend bool operator!=(
180 const PortIterator & s1,
181 const PortIterator & s2)
182 {
183 return !(s1 == s2);
184 }
185
186private:
187 int32_t index() const
188 {
189 return (native_ == NULL)
190 ? RTI_INT32_MAX
191 : TypedPortType<PORT, DataRep, InfoRep>::from_native(native_)->index();
192 }
193
194 RTI_RoutingServiceRoute *native_route_;
195 typename TypedPortType<PORT, DataRep, InfoRep>::native_type *native_;
196
197};
198
199
200} } }
201
202#endif // RTI_ROUTING_PROCESSOR_PORTS_ITERATOR_HPP_