MIRA
ChannelReadInterval.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_CHANNELREADINTERVAL_H_
48 #define _MIRA_CHANNELREADINTERVAL_H_
49 
50 #ifndef Q_MOC_RUN
51 #include <boost/shared_ptr.hpp>
52 #endif
53 
54 #include <fw/ChannelReadWrite.h>
55 
56 namespace mira {
57 
59 
71 template<typename T>
73 {
75  typedef ChannelType* ChannelTypePtr;
76 
77  typedef typename ChannelType::Buffer Buffer;
78  typedef typename ChannelType::ValueType ValueType;
79  typedef typename ChannelType::Slot Slot;
80 
81 public:
82  typedef std::list<ChannelRead<T> > IntervalList;
83 
86  {
87  public:
88 
89  // some STL conform typedefs that are needed for iterators
90  typedef ptrdiff_t difference_type;
91  typedef std::bidirectional_iterator_tag iterator_category;
92  typedef ValueType value_type;
93  typedef const ValueType* pointer;
94  typedef const ValueType& reference;
95 
96  private:
97 
98  friend class ChannelReadInterval;
99  const_iterator(const typename IntervalList::const_iterator& i) : mIterator(i) {}
100 
101  public:
102 
103  const_iterator(const const_iterator& other) : mIterator(other.mIterator) {}
104 
105  public:
106 
108  operator ChannelRead<T>() const { return *mIterator; }
109 
110 #if (__cplusplus >= 200710)
111  explicit operator typename IntervalList::const_iterator() const { return mIterator; }
113 #else
114  operator typename IntervalList::const_iterator() const { return mIterator; }
116 #endif
117 
118  public:
119 
120  reference operator*() const { return (*(*mIterator)); }
121  pointer operator->() const { return &(*(*mIterator)); }
122 
124  ++mIterator;
125  return *this;
126  }
127 
129  iterator tmp = *this;
130  ++mIterator;
131  return tmp;
132  }
133 
135  --mIterator;
136  return *this;
137  }
138 
140  iterator tmp = *this;
141  --mIterator;
142  return tmp;
143  }
144 
145  public:
146 
147  bool operator==(const const_iterator& other) const {
148  return mIterator==other.mIterator;
149  }
150  bool operator!=(const const_iterator& other) const {
151  return mIterator!=other.mIterator;
152  }
153 
154  private:
155 
156  typename IntervalList::const_iterator mIterator;
157  };
158 
160 
161 public:
162 
163  const_iterator begin() const { return const_iterator(mInterval.begin()); }
164  const_iterator end() const { return const_iterator(mInterval.end()); }
165 
166 public:
167 
168  std::size_t size() const { return mInterval.size(); }
169  bool empty() const { return mInterval.empty(); }
170 
171 public:
172 
174  ChannelReadInterval() : mChannel(NULL) {}
175 
178  std::list<ChannelBufferBase::Slot*>& slotList) :
179  mChannel(channel)
180  {
181  // add all slots of the interval to our own interval list
182  // create one ChannelRead object per slot within the interval
183  for(auto it=slotList.begin(); it!=slotList.end(); ++it)
184  {
185  Slot* s = Buffer::castSlot(*it);
186 
187  // since the underlying channel buffer stores the slots starting
188  // with the newest and ending with the oldest, we are moving
189  // backward in time, however in our interval list we want to start
190  // with the oldest element and end with the newest, so push_front
191  // to revert the order.
192  mInterval.push_front(ChannelRead<T>(channel, s));
193  }
194  }
195 
196 public:
197 
198  void finish() {
199  mInterval.clear();
200  mChannel = NULL;
201  }
202 
203  const std::string& getChannelID() {
204  static std::string emptyString;
205  return mChannel!=NULL ? mChannel->getID() : emptyString;
206  }
207 
208  std::string getTypename() const {
209  return mChannel!=NULL ? mChannel->getTypename() : "";
210  }
211 
213  return mChannel!=NULL ? mChannel->getTypeMeta() : TypeMetaPtr();
214  }
215 
220 
221 private:
222 
223  void checkValid() const {
224  if(mChannel==NULL)
225  MIRA_THROW(XAccessViolation, "Trying to access ChannelReadInterval"
226  " that was not assigned with valid data");
227  }
228 
229  ChannelTypePtr mChannel;
230  IntervalList mInterval;
231 };
232 
234 
235 } // namespace
236 
237 #endif
An object that allows read access to a whole interval of channel data.
Definition: ChannelReadInterval.h:72
bool empty() const
Definition: ChannelReadInterval.h:169
Channel< T > getChannel()
Returns a read-only channel proxy object of the underlying channel.
const_iterator operator--(int)
Definition: ChannelReadInterval.h:139
ChannelReadInterval()
Constructs an empty interval.
Definition: ChannelReadInterval.h:174
An exception that occurs whenever a channel has no data.
Definition: Channel.h:88
reference operator*() const
Definition: ChannelReadInterval.h:120
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
std::list< ChannelRead< T > > IntervalList
Definition: ChannelReadInterval.h:82
const std::string & getChannelID()
Definition: ChannelReadInterval.h:203
const ValueType * pointer
Definition: ChannelReadInterval.h:93
pointer operator->() const
Definition: ChannelReadInterval.h:121
Definition: ChannelReadWrite.h:65
An object that allows read access to data of a channel.
Definition: ChannelReadWrite.h:440
const ValueType & reference
Definition: ChannelReadInterval.h:94
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:82
const_iterator end() const
Definition: ChannelReadInterval.h:164
const_iterator begin() const
Definition: ChannelReadInterval.h:163
Classes for automatic locking/unlocking when reading and writing to channels.
const_iterator & operator--()
Definition: ChannelReadInterval.h:134
const_iterator(const const_iterator &other)
Definition: ChannelReadInterval.h:103
void finish()
Definition: ChannelReadInterval.h:198
std::bidirectional_iterator_tag iterator_category
Definition: ChannelReadInterval.h:91
Const iterator for iterating over the interval.
Definition: ChannelReadInterval.h:85
bool operator==(const const_iterator &other) const
Definition: ChannelReadInterval.h:147
ValueType value_type
Definition: ChannelReadInterval.h:92
boost::shared_ptr< TypeMeta > TypeMetaPtr
Definition: MetaSerializer.h:309
const_iterator operator++(int)
Definition: ChannelReadInterval.h:128
const_iterator iterator
Definition: ChannelReadInterval.h:159
bool operator!=(const const_iterator &other) const
Definition: ChannelReadInterval.h:150
ptrdiff_t difference_type
Definition: ChannelReadInterval.h:90
TypeMetaPtr getTypeMeta() const
Definition: ChannelReadInterval.h:212
std::size_t size() const
Definition: ChannelReadInterval.h:168
const_iterator & operator++()
Definition: ChannelReadInterval.h:123
ChannelReadInterval(ChannelTypePtr channel, std::list< ChannelBufferBase::Slot *> &slotList)
Constructs an interval on channel with given range.
Definition: ChannelReadInterval.h:177
std::string getTypename() const
Definition: ChannelReadInterval.h:208