MIRA
ChannelBuffer.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_CHANNELBUFFER_H_
48 #define _MIRA_CHANNELBUFFER_H_
49 
50 #include <string>
51 #include <list>
52 #include <type_traits>
53 
54 #ifndef Q_MOC_RUN
55 #include <boost/noncopyable.hpp>
56 #include <boost/thread/mutex.hpp>
57 #include <boost/thread/shared_mutex.hpp>
58 #endif
59 
60 #include <error/LoggingCore.h>
61 
62 #include <factory/TypeId.h>
63 
68 
69 #include <utils/Time.h>
70 #include <utils/Stamped.h>
71 #include <platform/Typename.h>
72 
73 #include <math/Eigen.h>
74 
75 #include <fw/FrameworkExports.h>
76 
77 namespace mira {
78 
80 
82 template <typename TargetType>
84 
86 template <typename TargetType>
88 
90 
96 {
99 
102 
105 };
106 
112 {
117 };
118 
120 
134 class MIRA_FRAMEWORK_EXPORT ChannelBufferBase : boost::noncopyable
135 {
136  template <typename TargetType>
138 
139  template <typename TargetType>
140  friend struct ChannelBufferPromoter;
141 
142 public:
143 
145  struct Slot;
146 
150  struct ListItem : boost::noncopyable
151  {
153  // creates empty list
154  next = (Slot*)this;
155  prev = (Slot*)this;
156  }
157 
158  // both will be locked by mutex in ChannelBuffer
161  };
162 
166  struct Slot : public ListItem
167  {
168  boost::shared_mutex lock; // locks the following data
169 
175  boost::mutex serializedValueLock;
176 
181 
187 
194  uint32 flags;
195 
196  const Time& timestamp() const {
197  assert(timestampPtr!=NULL); // pointer is set in ChannelBuffers allocateSlot
198  return *timestampPtr;
199  }
200  };
201 
202 public:
203 
210  mStorageDuration(Duration::seconds(0)),
211  mAutoIncreaseStorageDuration(true),
212  mMinSlots(0),
213  mMaxSlots(100),
214  mSize(0)
215  {
216  }
217 
218  virtual ~ChannelBufferBase();
219 
220 public:
221 
226  {
227  mMinSlots = other.mMinSlots;
228  mMaxSlots = other.mMaxSlots;
229  mStorageDuration = other.mStorageDuration;
230  mAutoIncreaseStorageDuration = other.mAutoIncreaseStorageDuration;
231  }
232 
233 public:
234 
243  void setStorageDuration(const Duration& storageDuration);
244 
245 
253  void setAutoIncreaseStorageDuration(bool increase);
254 
264  void setMinSlots(std::size_t minSlots);
265 
266 
276  void setMaxSlots(std::size_t maxSlots);
277 
278 public:
279 
281  std::size_t getSize() const;
282 
284  std::size_t getMaxSlots() const;
285 
287  std::size_t getMinSlots() const;
288 
290  Duration getStorageDuration() const;
291 
293  bool isAutoIncreasingStorageDuration() const;
294 
295 
296 public:
297 
299  virtual int getTypeId() const = 0;
300 
302  virtual bool isTyped() const = 0;
303 
305  virtual bool isPolymorphic() const = 0;
306 
312  virtual Typename getTypename() const = 0;
313 
317  virtual void setTypename(const Typename& name) = 0;
318 
323  virtual TypeMetaPtr createTypeMeta(Slot* s, MetaTypeDatabase& ioDB) = 0;
324 
328  TypeMetaPtr getTypeMeta() const { return mTypeMeta; }
329 
333  void setTypeMeta(TypeMetaPtr meta) { mTypeMeta = meta; }
334 
342  virtual void fixateType() = 0;
343 
345  virtual Slot* allocateSlot() = 0;
346 
348  virtual void freeSlot(Slot* s) = 0;
349 
353  virtual StampedHeader& getStampedHeader(Slot* s) = 0;
354 
359  virtual const Buffer<uint8>& readSerializedValue(Slot* s) = 0;
360 
367  virtual const Buffer<uint8>& readSerializedValue(Slot* s,
368  uint8 formatVersion, bool orLower) = 0;
369 
375  virtual Buffer<uint8> readSerializedValue(Slot* s,
376  std::list<BinarySerializerCodecPtr>& codecs) = 0;
377 
385  virtual Buffer<uint8> readSerializedValue(Slot* s,
386  std::list<BinarySerializerCodecPtr>& codecs,
387  uint8 formatVersion, bool orLower) = 0;
388 
393  virtual void writeSerializedValue(Slot* s, Buffer<uint8> data) = 0;
394 
402  virtual void readJSON(Slot* s, JSONValue& oValue) = 0;
403 
410  virtual void readJSON(Slot* s, JSONValue& oValue, JSONSerializer& serializer) = 0;
411 
419  virtual void writeJSON(Slot* s, const JSONValue& value) = 0;
420 
427  virtual void writeJSON(Slot* s, JSONDeserializer& value) = 0;
428 
435  virtual void writeXML(Slot* s, const XMLDom::const_iterator& node) = 0;
436 
437 public:
438 
440  // write access
441 
464  Slot* requestWriteSlot();
465 
474  bool finishWriteSlot(Slot* n, bool* dropped=NULL);
475 
480  void discardWriteSlot(Slot* n);
481 
482 
484  // read access
485 
491  Slot* readNewestSlot();
492 
499  Slot* readSlotAtTime(const Time& timestamp, SlotQueryMode mode);
500 
506  void readInterval(const Time& timestamp, std::size_t nrSlots,
507  std::size_t olderSlots, std::size_t newerSlots,
508  IntervalFillMode fillMode, std::list<Slot*>& oSlots);
509 
515  void readInterval(const Time& from, const Time& to, std::list<Slot*>& oSlots);
516 
517 public:
518 
527  template<typename TargetType>
528  ChannelBufferBase* promote();
529 
530 private:
531 
537  Slot* findFirstOlderSlot(const Time& timestamp, Slot* start=NULL);
538 
539 private:
540 
545  void splice(Slot* first, Slot* last, Slot* position) const;
546 
551  void splice(Slot* item, Slot* position) const {
552  splice(item,item,position);
553  }
554 
555  static bool isEmpty(const ListItem& list) { return list.next == &list; }
556  static Slot* begin(ListItem& list) { return list.next; }
557  static Slot* last(ListItem& list) { return list.prev; }
558  static Slot* end(ListItem& list) { return (Slot*)&list; }
559 
561  void deleteSlots(ListItem& list);
562 
563 protected:
564 
566  void clear();
567 
568 private:
569 
574  void resetSlot(Slot* s);
575 
576  // parameters:
577  Duration mStorageDuration;
578  bool mAutoIncreaseStorageDuration;
579  std::size_t mMinSlots;
580  std::size_t mMaxSlots;
581 
582 
583  // mutex that locks all actions when we change our lists
584  mutable boost::mutex mMutex;
585 
586  std::size_t mSize; // current number of slots we are using
587 
588  ListItem mBuffer;
589 
602  ListItem mWaitingOrFree;
603 
607  ListItem mWriting;
608 
609  TypeMetaPtr mTypeMeta;
610 
611 private:
612 
613  std::string dbgDumpList(ListItem& list, bool brief);
614 
615 public:
616 
617  void dbgDump(const std::string& prefix, bool brief=true);
618 
619  static void dbgCheckListIntegrity(const ListItem& list);
620  void dbgCheckIntegrity();
621 };
622 
624 
628 template <typename T>
630 {
631 public:
632 
634 
635 public:
636 
637  virtual ~TypedChannelBufferBase() { this->clear(); }
638 
639 public:
640 
642  struct Slot : public ChannelBufferBase::Slot {
645  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
646  };
647 
648 public:
649 
654  {
655  // cast abstract ChannelBufferBase::Slot to TypedChannelBufferBase<T>::Slot.
656  // this is safe since all slots were created by us in allocateSlot()
657  // as TypedChannelBufferBase<T>::Slots
658  return static_cast<Slot*>(s);
659  }
660 
661 public:
666  virtual int getTypeId() const { return typeId<T>(); }
667  virtual bool isTyped() const { return true; }
668  virtual bool isPolymorphic() const {return false; }
669  virtual Typename getTypename() const { return typeName<T>(); }
670  virtual void setTypename(const Typename& name) {
671  MIRA_THROW(XRuntime, "Cannot set Typename for typed channels");
672  }
674  MetaSerializer ms(ioDB);
675  Slot* slot = castSlot(s);
676  TypeMetaPtr meta = ms.addMeta(slot->data.internalValueRep());
677  return meta;
678  }
679 
680  virtual void fixateType() {} // doesn't do anything
682  Slot* slot = new Slot;
683  slot->timestampPtr = &(static_cast<StampedHeader&>(slot->data).timestamp); // "connect" the timestamp ptr
684  return slot;
685  }
687  // casting s to our slot type ensures that the correct destructor is called
688  Slot* slot = castSlot(s);
689  delete slot;
690  }
691 
692 public:
693 
695  // we can safely cast here, since s is an object of our Slot type
696  Slot* slot = static_cast<Slot*>(s);
697  return stampedHeader(slot->data);
698  }
699 
701  {
702  // lock the serialization mutex to protect serializedValueLock
703  // (we or someone else may write it here)
704  boost::mutex::scoped_lock lock(s->serializedValueLock);
705 
706  // no serialized data yet, so serialize it now (lazy evaluation)
707  if(s->serializedValue.empty()) {
708  // we can safely cast here, since s is an object of our Slot type
709  Slot* slot = static_cast<Slot*>(s);
710 
711  // serialize the data
713  bs.serialize(slot->data.internalValueRep(), false);
714  }
715 
716  return s->serializedValue;
717  }
718 
719 
721  uint8 formatVersion, bool orLower)
722  {
723  uint8 serializerVersion = BinaryBufferSerializer::getSerializerFormatVersion();
724 
725  if (formatVersion == serializerVersion)
726  return readSerializedValue(s);
727 
728  if (formatVersion == 0) {
729  // lock the serialization mutex to protect serializedValueLock
730  // (we or someone else may write it here)
731  boost::mutex::scoped_lock lock(s->serializedValueLock);
732 
733  // no serialized data yet, so serialize it now (lazy evaluation)
734  if(s->serializedValue.empty()) {
735  // we can safely cast here, since s is an object of our Slot type
736  Slot* slot = static_cast<Slot*>(s);
737 
738  // serialize the data
740  bs.serialize(slot->data.internalValueRep(), false);
741  }
742 
743  return s->serializedValue;
744  }
745 
746  MIRA_THROW(XIO, "Requested serialized data of binary format version " << (int)formatVersion
747  << ". Only implemented for versions 0, " << (int)serializerVersion << ".");
748  }
749 
751  std::list<BinarySerializerCodecPtr>& codecs)
752  {
753  Buffer<uint8> serializedValue;
754 
755  // we can safely cast here, since s is an object of our Slot type
756  Slot* slot = static_cast<Slot*>(s);
757 
758  BinaryBufferSerializer bs(&serializedValue);
759 
760  // register codecs
761  foreach(BinarySerializerCodecPtr codec, codecs)
762  bs.registerCodec(codec);
763 
764  // serialize the data
765  bs.serialize(slot->data.internalValueRep(),false);
766 
767  return std::move(serializedValue);
768  }
769 
771  std::list<BinarySerializerCodecPtr>& codecs,
772  uint8 formatVersion, bool orLower)
773  {
774  uint8 serializerVersion = BinaryBufferSerializer::getSerializerFormatVersion();
775 
776  if (formatVersion == serializerVersion)
777  return readSerializedValue(s, codecs);
778 
779  if (formatVersion == 0) {
780  Buffer<uint8> serializedValue;
781 
782  // we can safely cast here, since s is an object of our Slot type
783  Slot* slot = static_cast<Slot*>(s);
784 
785  BinaryBufferSerializerLegacy bs(&serializedValue);
786 
787  // register codecs
788  foreach(BinarySerializerCodecPtr codec, codecs)
789  bs.registerCodec(codec);
790 
791  // serialize the data
792  bs.serialize(slot->data.internalValueRep(),false);
793 
794  return std::move(serializedValue);
795  }
796 
797  MIRA_THROW(XIO, "Requested serialized data of binary format version " << (int)formatVersion
798  << ". Only implemented for versions 0, " << (int)serializerVersion << ".");
799  }
800 
802  {
803  s->serializedValue = std::move(data);
804 
805  // we can safely cast here, since s is an object of our Slot type
806  Slot* slot = static_cast<Slot*>(s);
807 
808  // deserialize into our data
810 
811  {
812  boost::mutex::scoped_lock lock(mCodecsMutex);
813  bs.setCodecs(mCodecs);
814  }
815  bs.deserialize(slot->data.internalValueRep(), false);
816  {
817  boost::mutex::scoped_lock lock(mCodecsMutex);
818  mCodecs = bs.getCodecs();
819  }
820  }
821 
822  virtual void readJSON(ChannelBufferBase::Slot* s, JSONValue& oValue)
823  {
824  // we can safely cast here, since s is an object of our Slot type
825  Slot* slot = static_cast<Slot*>(s);
826 
827  JSONSerializer json;
828  oValue = json.serialize(slot->data.internalValueRep());
829  }
830 
831  virtual void readJSON(ChannelBufferBase::Slot* s, JSONValue& oValue,
832  JSONSerializer& serializer)
833  {
834  // we can safely cast here, since s is an object of our Slot type
835  Slot* slot = static_cast<Slot*>(s);
836 
837  oValue = serializer.serialize(slot->data.internalValueRep());
838  }
839 
840  virtual void writeJSON(ChannelBufferBase::Slot* s, const JSONValue& value)
841  {
842  // we can safely cast here, since s is an object of our Slot type
843  Slot* slot = static_cast<Slot*>(s);
844 
845  JSONDeserializer json(value);
846  json.deserialize(slot->data.internalValueRep());
847  }
848 
849  virtual void writeJSON(ChannelBufferBase::Slot* s, JSONDeserializer& deserializer)
850  {
851  // we can safely cast here, since s is an object of our Slot type
852  Slot* slot = static_cast<Slot*>(s);
853 
854  deserializer.deserialize(slot->data.internalValueRep());
855  }
856 
858  {
859  // we can safely cast here, since s is an object of our Slot type
860  Slot* slot = static_cast<Slot*>(s);
861  XMLDeserializer xml(node);
862  const std::string rootTag = *node;
863  xml.deserializeFromNode(rootTag.c_str(), slot->data.internalValueRep());
864  }
866 
867 private:
868 
870  boost::mutex mCodecsMutex;
871 };
872 
873 
875 
879 template <typename T>
881 {};
882 
884 
889 template <>
891 {
892 public:
893 
895 
896 public:
897 
899  struct Slot : public ChannelBufferBase::Slot {
901  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
902  };
903 
904 public:
905 
910  {
911  // cast abstract ChannelBufferBase::Slot to TypedChannelBufferBase<T>::Slot.
912  // this is safe since all slots were created by us in allocateSlot()
913  // as TypedChannelBufferBase<T>::Slots
914  return static_cast<Slot*>(s);
915  }
916 
917 public:
918 
919  ChannelBuffer() : mTypename("") {}
920 
921  virtual ~ChannelBuffer() { this->clear(); }
922 
923 
924 public:
929  virtual int getTypeId() const { return -1; }
930  virtual bool isTyped() const { return false; }
931  virtual bool isPolymorphic() const {return false; }
932  virtual Typename getTypename() const { return mTypename; }
933  virtual void setTypename(const Typename& name);
935  MIRA_THROW(XRuntime, "Cannot create type meta for untyped channels");
936  // keep compiler happy
937  return TypeMetaPtr();
938  }
939  virtual void fixateType() {} // doesn't do anything
940 
942  Slot* slot = new Slot;
943  slot->timestampPtr = &slot->header.timestamp; // "connect" the timestamp ptr
944  return slot;
945  }
946 
948  // casting s to our slot type ensures that the correct destructor is called
949  Slot* slot = castSlot(s);
950  delete slot;
951  }
952 
953 public:
954 
956  // casting here is not safe, if we actually are a typed buffer,
957  // however, if we are, then the TypedChannelBuffer::getStampedHeader()
958  // of that class would have been called instead of us.
959  Slot* slot = static_cast<Slot*>(s);
960  return slot->header;
961  }
962 
964  {
965  return s->serializedValue;
966  }
967 
969  uint8 formatVersion, bool orLower)
970  {
971  BinaryBufferIstream stream(&(s->serializedValue));
972  uint8 dataVersion = BinaryBufferDeserializer::getDataFormatVersion(stream);
973  if ((!orLower && (dataVersion != formatVersion)) || (dataVersion > formatVersion)) {
974  MIRA_THROW(XIO, "Untyped channel contains serialized data of version " << (int)dataVersion
975  << ", cannot return data of version " << (int)formatVersion << ".");
976  }
977 
978  return readSerializedValue(s);
979  }
980 
981 
983  std::list<BinarySerializerCodecPtr>& codecs)
984  {
985  // we must ignore the codecs
986  return s->serializedValue;
987  }
988 
990  std::list<BinarySerializerCodecPtr>& codecs,
991  uint8 formatVersion, bool orLower)
992  {
993  // we must ignore the codecs
994  return readSerializedValue(s, formatVersion, orLower);
995  }
996 
998  {
999  s->serializedValue = std::move(data);
1000  }
1001 
1002  virtual void readJSON(ChannelBufferBase::Slot* s, JSONValue& oValue);
1003  virtual void readJSON(ChannelBufferBase::Slot* s, JSONValue& oValue, JSONSerializer& serializer);
1004 
1005  virtual void writeJSON(ChannelBufferBase::Slot* s, const JSONValue& value);
1006  virtual void writeJSON(ChannelBufferBase::Slot* s, JSONDeserializer& deserializer);
1007  void writeXML(ChannelBufferBase::Slot* s, const XMLDom::const_iterator& node) override;
1008 
1010 
1011 private:
1012 
1013  Typename mTypename;
1014 };
1015 
1017 
1022 {
1024 
1025 public:
1026 
1029  mMostDerivedClass(NULL),
1030  mFixatedClass(NULL) {}
1031 
1032 public:
1037  virtual bool isPolymorphic() const {return true; }
1038 
1039  virtual int getTypeId() const;
1040 
1041  virtual Typename getTypename() const;
1042 
1043  virtual ChannelBufferBase::Slot* allocateSlot();
1044 
1045  virtual void freeSlot(ChannelBufferBase::Slot* s);
1046 
1047  virtual void fixateType();
1048 
1049  virtual void writeSerializedValue(ChannelBufferBase::Slot* s, Buffer<uint8> data);
1051 
1053  void promote(const Class* promotionClass);
1054 
1055 protected:
1056 
1059 };
1060 
1061 #ifndef MIRA_WINDOWS
1062 // declare template extern to speed up compilation time by several seconds
1063 extern template class TypedChannelBufferBase<Object*>;
1064 #endif
1065 
1067 
1072 template <typename T>
1074 {
1075  static_assert(std::is_base_of<mira::Object,T>::value,
1076  "In channels you can only use pointers of polymorphic "
1077  "classes that are derived from mira::Object. Pointers to "
1078  "other classes cannot be stored in a channel.");
1079 public:
1080  typedef Stamped<T*> ValueType;
1081 
1082 public:
1084  mMostDerivedClass = &T::CLASS();
1085  }
1086 };
1087 
1089 // Channel buffer promotion code
1090 
1097 template <typename U>
1098 struct ChannelBufferPromoterCommon
1099 {
1101  {
1102  assert(!buffer->isTyped() &&
1103  "Can promote untyped channels to typed channels only");
1104 
1105  if(!buffer->getTypename().empty() && buffer->getTypename()!=typeName<U>())
1106  MIRA_THROW(XBadCast, "Invalid promotion from untyped to typed "
1107  "ChannelBuffer. Typename does not match. ('" <<
1108  buffer->getTypename() << "' != '" << typeName<U>() << "'");
1109 
1110  typedef ChannelBufferBase::Slot Slot;
1111 
1112  // create a new typed buffer
1113  ChannelBuffer<U>* typedBuffer = new ChannelBuffer<U>;
1114 
1115  // copy all parameters
1116  typedBuffer->cloneParameters(*buffer);
1117 
1118  // copy all the serialized data from the untyped buffer into the
1119  // typed buffer and deserialize it into the value
1120 
1121  // now lock the whole buffer for reading ...
1122  for(Slot* s=ChannelBufferBase::begin(buffer->mBuffer);
1123  s!=ChannelBufferBase::end(buffer->mBuffer); s=s->next)
1124  s->lock.lock_shared();
1125 
1126  // ... and deserialize the content of each slot into the new buffer
1127  for(Slot* s=ChannelBufferBase::begin(buffer->mBuffer);
1128  s!=ChannelBufferBase::end(buffer->mBuffer); s=s->next)
1129  {
1130  Slot* n = typedBuffer->requestWriteSlot(); // obtain a new slot
1131 
1132  // ... copy the headers
1133  typedBuffer->getStampedHeader(n) = buffer->getStampedHeader(s);
1134 
1135  // ... and write the value into the new slot (this will deserialize the data there)
1136  typedBuffer->writeSerializedValue(n, s->serializedValue);
1137 
1138  typedBuffer->finishWriteSlot(n);
1139  }
1140 
1141  // free the read lock from above ...
1142  for(Slot* s=ChannelBufferBase::begin(buffer->mBuffer);
1143  s!=ChannelBufferBase::end(buffer->mBuffer); s=s->next)
1144  s->lock.unlock_shared();
1145 
1146  return typedBuffer;
1147  }
1148 };
1149 
1151 
1162 template <typename U>
1163 struct ChannelBufferPromoter : public ChannelBufferPromoterCommon<U>
1164 {
1167  {
1168  if(buffer->isTyped())
1169  return buffer; // we are promoted already, do nothing
1170  return Base::promoteUntyped(buffer);
1171  }
1172 };
1173 
1175 
1180 template <typename U>
1182 {
1185  {
1186  // if untyped, promote to typed first
1187  if(!buffer->isTyped())
1188  buffer = Base::promoteUntyped(buffer);
1189 
1190  PolymorphicChannelBuffer* polymorphicBuffer = dynamic_cast<PolymorphicChannelBuffer*>(buffer);
1191 
1192  assert(polymorphicBuffer!=NULL &&
1193  "We should never reach here if the buffer to promote is not "
1194  "polymorphic");
1195 
1196  // get class of U
1197  const Class* classU = &U::CLASS();
1198 
1199  // promote us (this will do error checking and might throw XBadCast)
1200  polymorphicBuffer->promote(classU);
1201 
1202  return polymorphicBuffer;
1203  }
1204 };
1205 
1207 
1208 template<typename U>
1210 {
1211  return ChannelBufferPromoter<U>::promote(this);
1212 }
1213 
1215 
1216 }
1217 
1218 #endif
Serializer for serializing objects in JSON format.
Definition: JSONSerializer.h:93
virtual void writeJSON(ChannelBufferBase::Slot *s, const JSONValue &value)
Writes data in json representation into the slot, using a default-instantiated JSONDeserializer.
Definition: ChannelBuffer.h:840
virtual void freeSlot(ChannelBufferBase::Slot *s)
Called to destroy the slot and free the used memory.
Definition: ChannelBuffer.h:686
Database that stores all meta type information and provides additional functions for accessing the da...
Definition: MetaSerializer.h:509
void deserialize(T &value)
Definition: JSONSerializer.h:427
boost::shared_mutex lock
Definition: ChannelBuffer.h:168
static ChannelBufferBase * promote(ChannelBufferBase *buffer)
Definition: ChannelBuffer.h:1184
Definition: BinarySerializer.h:324
const Time * timestampPtr
Pointer to the actual timestamp in the slot data for easy access.
Definition: ChannelBuffer.h:180
const Class * mFixatedClass
Definition: ChannelBuffer.h:1058
virtual TypeMetaPtr createTypeMeta(ChannelBufferBase::Slot *s, MetaTypeDatabase &ioDB)
Creates meta information for data in slot and stores all meta information in the given database...
Definition: ChannelBuffer.h:934
boost::mutex serializedValueLock
Lock for writing serializedValue in readSerializedData.
Definition: ChannelBuffer.h:175
const Class * mMostDerivedClass
Definition: ChannelBuffer.h:1057
Include file for all eigen related things.
static Slot * castSlot(ChannelBufferBase::Slot *s)
Cast an abstract slot to a typed slot.
Definition: ChannelBuffer.h:909
virtual ~ChannelBuffer()
Definition: ChannelBuffer.h:921
Typed slot derived from ChannelBufferBase::Slot.
Definition: ChannelBuffer.h:642
void promote(const Class *promotionClass)
Promotes the polymorphic type of the channel to the given class type.
The first slot with timestamp < requested will be chosen.
Definition: ChannelBuffer.h:98
static Slot * castSlot(ChannelBufferBase::Slot *s)
Cast an abstract slot to a typed slot.
Definition: ChannelBuffer.h:653
virtual void writeSerializedValue(ChannelBufferBase::Slot *s, Buffer< uint8 > data)
Write the given buffer (that contains binary serialized data WITHOUT the StampedHeader) to the slot...
Definition: ChannelBuffer.h:997
virtual Buffer< uint8 > readSerializedValue(ChannelBufferBase::Slot *s, std::list< BinarySerializerCodecPtr > &codecs, uint8 formatVersion, bool orLower)
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:770
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
std::string Typename
Definition: Typename.h:60
virtual ChannelBufferBase::Slot * allocateSlot()
Creates and returns a new slot.
Definition: ChannelBuffer.h:681
Specialization for polymorphic types (only classes derived from mira::Object are supported!) ...
Definition: ChannelBuffer.h:1073
ValueType header
Definition: ChannelBuffer.h:900
virtual bool isTyped() const
Returns true, if the channel is typed and false, if it is untyped.
Definition: ChannelBuffer.h:930
Time and Duration wrapper class.
Class object which supports some kind of class reflection.
Definition: Class.h:97
virtual Typename getTypename() const
Returns the Typename of the slot values, in contrast to getTypeId() the Typename is portable and uniq...
Definition: ChannelBuffer.h:932
ListItem()
Definition: ChannelBuffer.h:152
Stamped class specialization for polymorphic pointers.
Definition: Stamped.h:238
StampedHeader & stampedHeader(Stamped< T > &data)
Helper to extract the header from stamped data.
Definition: Stamped.h:482
ChannelBufferBase * promote()
Promotes this buffer to the specified target type (if required).
Definition: ChannelBuffer.h:1209
TypeMetaPtr getTypeMeta() const
Returns the meta information for the slots type.
Definition: ChannelBuffer.h:328
Base class for typed channel buffers.
Definition: ChannelBuffer.h:629
virtual void fixateType()
Calling this method will fix the type of the ChannelBuffer.
Definition: ChannelBuffer.h:680
static ChannelBufferBase * promoteUntyped(ChannelBufferBase *buffer)
Definition: ChannelBuffer.h:1100
Get compiler and platform independent typenames.
Framework export macro declaration.
void serialize(const std::string &name, const T &value, const std::string &comment="")
Serializes the specified object value under the given name.
Definition: Serializer.h:204
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:82
void serialize(const T &value, bool enableTypeCheck=true)
Provides a special serialize interface for the BinarySerializer.
Definition: BinarySerializer.h:598
Implements double-linked list item.
Definition: ChannelBuffer.h:150
Const sibling_iterator for iterating over xml nodes that have the same parent (siblings) ...
Definition: XMLDom.h:671
virtual StampedHeader & getStampedHeader(ChannelBufferBase::Slot *s)
Returns a reference to the stamped header information.
Definition: ChannelBuffer.h:955
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:416
Slot * requestWriteSlot()
Returns a slot for writing, where the shared_mutex is already write locked.
ChannelBufferPromoterCommon< U > Base
Definition: ChannelBuffer.h:1183
virtual const Buffer< uint8 > & readSerializedValue(ChannelBufferBase::Slot *s, uint8 formatVersion, bool orLower)
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:720
ChannelBuffer()
Definition: ChannelBuffer.h:1083
A channel buffer for polymorphic types (classes derived from mira::Object)
Definition: ChannelBuffer.h:1021
Deserializer for serializing objects from JSON format.
Definition: JSONSerializer.h:400
virtual int getTypeId() const
Returns typeid of the slot values, the allocator creates.
Definition: ChannelBuffer.h:929
virtual ~TypedChannelBufferBase()
Definition: ChannelBuffer.h:637
void setTypeMeta(TypeMetaPtr meta)
Sets the meta information of the slot&#39;s type.
Definition: ChannelBuffer.h:333
Binary serializer and deserializer.
PolymorphicChannelBuffer()
Constructor.
Definition: ChannelBuffer.h:1028
forward decl.
Definition: ChannelBuffer.h:83
void registerCodec(BinarySerializerCodecPtr codec)
Registers the specified codec instance in this binary serializer.
Definition: BinarySerializer.h:764
virtual Buffer< uint8 > readSerializedValue(ChannelBufferBase::Slot *s, std::list< BinarySerializerCodecPtr > &codecs)
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:982
virtual void writeSerializedValue(ChannelBufferBase::Slot *s, Buffer< uint8 > data)
Write the given buffer (that contains binary serialized data WITHOUT the StampedHeader) to the slot...
Definition: ChannelBuffer.h:801
virtual bool isPolymorphic() const
Returns true if the channel has a polymorphic type.
Definition: ChannelBuffer.h:931
Definition: MetaSerializer.h:586
virtual TypeMetaPtr createTypeMeta(ChannelBufferBase::Slot *s, MetaTypeDatabase &ioDB)
Creates meta information for data in slot and stores all meta information in the given database...
Definition: ChannelBuffer.h:673
virtual StampedHeader & getStampedHeader(ChannelBufferBase::Slot *s)
Returns a reference to the stamped header information.
Definition: ChannelBuffer.h:694
#define MIRA_FRAMEWORK_EXPORT
Definition: FrameworkExports.h:61
Stamped< T * > ValueType
Definition: ChannelBuffer.h:1078
Core class of the logging library.
Use this class to represent time durations.
Definition: Time.h:104
virtual bool isPolymorphic() const
Returns true if the channel has a polymorphic type.
Definition: ChannelBuffer.h:668
The common header for all stamped data.
Definition: Stamped.h:62
const CodecsMap & getCodecs() const
Return the map of currently known codecs.
Definition: BinarySerializer.h:1581
bool empty() const
Checks if the buffer is empty (used size == 0).
Definition: Buffer.h:303
Typed ChannelBuffer.
Definition: ChannelBuffer.h:880
The slot with smallest time difference to the requested will be chosen.
Definition: ChannelBuffer.h:104
virtual StampedHeader & getStampedHeader(Slot *s)=0
Returns a reference to the stamped header information.
ChannelBuffer()
Definition: ChannelBuffer.h:919
virtual const Buffer< uint8 > & readSerializedValue(ChannelBufferBase::Slot *s, uint8 formatVersion, bool orLower)
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:968
Mix in for adding a time stamp, an optional frame id and an optional sequence id to data types like P...
Definition: Stamped.h:149
boost::shared_ptr< TypeMeta > TypeMetaPtr
Definition: MetaSerializer.h:309
virtual int getTypeId() const
Returns typeid of the slot values, the allocator creates.
Definition: ChannelBuffer.h:666
virtual void freeSlot(ChannelBufferBase::Slot *s)
Called to destroy the slot and free the used memory.
Definition: ChannelBuffer.h:947
Mix in for adding a timestamp to data types.
virtual Buffer< uint8 > readSerializedValue(ChannelBufferBase::Slot *s, std::list< BinarySerializerCodecPtr > &codecs, uint8 formatVersion, bool orLower)
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:989
virtual void readJSON(ChannelBufferBase::Slot *s, JSONValue &oValue, JSONSerializer &serializer)
Reads data of that slot as json representation using the provided JSONSerializer. ...
Definition: ChannelBuffer.h:831
Stamped< T > ValueType
Definition: ChannelBuffer.h:633
Serializer for creating meta information out of types using serialization.
void cloneParameters(const ChannelBufferBase &other)
Copies the parameters of other to this.
Definition: ChannelBuffer.h:225
forward decl.
Definition: ChannelBuffer.h:87
virtual bool isPolymorphic() const
Returns true if the channel has a polymorphic type.
Definition: ChannelBuffer.h:1037
ValueType data
the actual data value, that is stored in this channel&#39;s slots
Definition: ChannelBuffer.h:644
Base class that manages the slots of channels by providing read and write access to them...
Definition: ChannelBuffer.h:134
const Time & timestamp() const
Definition: ChannelBuffer.h:196
Container for storing a single data element in the linked list.
Definition: ChannelBuffer.h:166
bool finishWriteSlot(Slot *n, bool *dropped=NULL)
Adds written slot into buffer at the right time position.
void setCodecs(const CodecsMap &codecs)
Set the map of known codecs.
Definition: BinarySerializer.h:1586
virtual void setTypename(const Typename &name)
Sets the Typename of the slot values.
Definition: ChannelBuffer.h:670
virtual void fixateType()
Calling this method will fix the type of the ChannelBuffer.
Definition: ChannelBuffer.h:939
Input stream adapter that can be assigned to any input stream and allows binary input using the >> st...
Definition: BinaryStream.h:523
Slot * next
Definition: ChannelBuffer.h:159
Buffer< uint8 > serializedValue
The data of this slot as serialized data.
Definition: ChannelBuffer.h:186
virtual void readJSON(ChannelBufferBase::Slot *s, JSONValue &oValue)
Reads data of that slot as json representation using a default-instantiated JSONSerializer.
Definition: ChannelBuffer.h:822
virtual const Buffer< uint8 > & readSerializedValue(ChannelBufferBase::Slot *s)
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:963
void deserialize(T &value, bool enableTypeCheck=true, bool recursive=false)
Provides a special deserialize interface for the BinaryDeserializer.
Definition: BinarySerializer.h:1340
StampedHeader ValueType
Definition: ChannelBuffer.h:894
virtual bool isTyped() const
Returns true, if the channel is typed and false, if it is untyped.
Definition: ChannelBuffer.h:667
ChannelBufferPromoterCommon< U > Base
Definition: ChannelBuffer.h:1165
TypeMetaPtr addMeta(const T &v)
Definition: MetaSerializer.h:605
virtual ChannelBufferBase::Slot * allocateSlot()
Creates and returns a new slot.
Definition: ChannelBuffer.h:941
XMLSerializer and XMLDeserializer.
Prefer filling the interval with slots with timestamp > requested.
Definition: ChannelBuffer.h:116
Prefer filling the interval with slots with timestamp < requested.
Definition: ChannelBuffer.h:114
uint32 flags
Used to store internal flags that are especially used by the remote capability of the framework...
Definition: ChannelBuffer.h:194
static ChannelBufferBase * promote(ChannelBufferBase *buffer)
Definition: ChannelBuffer.h:1166
void deserializeFromNode(const char *name, T &value)
In contrast to the deserialize() method this method will use the node that was specified in the const...
Definition: XMLSerializer.h:358
IntervalFillMode
Mode that is used to determine what slots should be added to the interval when not enough slots are a...
Definition: ChannelBuffer.h:111
virtual const Buffer< uint8 > & readSerializedValue(ChannelBufferBase::Slot *s)
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:700
void writeXML(ChannelBufferBase::Slot *s, const XMLDom::const_iterator &node) override
Writes data in xml representation into the slot, using a default-instantiated XMLDeserializer and the...
Definition: ChannelBuffer.h:857
virtual Buffer< uint8 > readSerializedValue(ChannelBufferBase::Slot *s, std::list< BinarySerializerCodecPtr > &codecs)
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:750
Specialization of ChannelBufferPromoter for polymorphic pointer channel buffers.
Definition: ChannelBuffer.h:1181
virtual Typename getTypename() const =0
Returns the Typename of the slot values, in contrast to getTypeId() the Typename is portable and uniq...
SlotQueryMode
Mode that is used to determine the slot obtained from a channel when no slot exists at the exact time...
Definition: ChannelBuffer.h:95
json::Value JSONValue
Imports the json::Value type into mira namespace.
Definition: JSON.h:363
void clear()
Deletes all slots in all lists and therefore clears the whole buffer.
Provides method for generating a unique id for any type.
Slot * prev
Definition: ChannelBuffer.h:160
virtual Typename getTypename() const
Returns the Typename of the slot values, in contrast to getTypeId() the Typename is portable and uniq...
Definition: ChannelBuffer.h:669
ChannelBufferBase()
Constructor.
Definition: ChannelBuffer.h:209
Serializer and Deserializer for JSON format.
virtual void writeJSON(ChannelBufferBase::Slot *s, JSONDeserializer &deserializer)
Writes data from an initialized json deserializer into the slot.
Definition: ChannelBuffer.h:849
virtual bool isTyped() const =0
Returns true, if the channel is typed and false, if it is untyped.
The first slot with timestamp > requested will be chosen.
Definition: ChannelBuffer.h:101
std::map< TypeId, BinarySerializerCodecPtr > CodecsMap
A map of binary serialization codecs.
Definition: BinarySerializer.h:1270
boost::shared_ptr< BinarySerializerCodec > BinarySerializerCodecPtr
Shared pointer of BinarySerializerCodec.
Definition: BinarySerializerCodec.h:64
Deserializer for serializing objects from XML format.
Definition: XMLSerializer.h:313