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 namespace mira {
76 
78 
80 template <typename TargetType>
82 
84 template <typename TargetType>
86 
88 
94 {
97 
100 
103 };
104 
110 {
115 };
116 
118 
132 class ChannelBufferBase : boost::noncopyable
133 {
134  template <typename TargetType>
136 
137  template <typename TargetType>
138  friend struct ChannelBufferPromoter;
139 
140 public:
141 
143  struct Slot;
144 
148  struct ListItem : boost::noncopyable
149  {
151  // creates empty list
152  next = (Slot*)this;
153  prev = (Slot*)this;
154  }
155 
156  // both will be locked by mutex in ChannelBuffer
159  };
160 
164  struct Slot : public ListItem
165  {
166  boost::shared_mutex lock; // locks the following data
167 
173  boost::mutex serializedValueLock;
174 
179 
185 
193 
194  const Time& timestamp() const {
195  assert(timestampPtr!=NULL); // pointer is set in ChannelBuffers allocateSlot
196  return *timestampPtr;
197  }
198 
199  // required to allow dynamic_cast for subclasses
200  virtual ~Slot() = default;
201  };
202 
203 public:
204 
211  mStorageDuration(Duration::seconds(0)),
212  mAutoIncreaseStorageDuration(true),
213  mMinSlots(0),
214  mMaxSlots(100),
215  mSize(0)
216  {
217  }
218 
219  virtual ~ChannelBufferBase();
220 
221 public:
222 
227  {
228  mMinSlots = other.mMinSlots;
229  mMaxSlots = other.mMaxSlots;
230  mStorageDuration = other.mStorageDuration;
231  mAutoIncreaseStorageDuration = other.mAutoIncreaseStorageDuration;
232  }
233 
234 public:
235 
244  void setStorageDuration(const Duration& storageDuration);
245 
246 
254  void setAutoIncreaseStorageDuration(bool increase);
255 
265  void setMinSlots(std::size_t minSlots);
266 
267 
277  void setMaxSlots(std::size_t maxSlots);
278 
279 public:
280 
282  std::size_t getSize() const;
283 
285  std::size_t getMaxSlots() const;
286 
288  std::size_t getMinSlots() const;
289 
292 
294  bool isAutoIncreasingStorageDuration() const;
295 
296 
297 public:
298 
300  virtual int getTypeId() const = 0;
301 
303  virtual bool isTyped() const = 0;
304 
306  virtual bool isPolymorphic() const = 0;
307 
313  virtual Typename getTypename() const = 0;
314 
318  virtual void setTypename(const Typename& name) = 0;
319 
324  virtual TypeMetaPtr createTypeMeta(Slot* s, MetaTypeDatabase& ioDB) = 0;
325 
329  TypeMetaPtr getTypeMeta() const { return mTypeMeta; }
330 
334  void setTypeMeta(TypeMetaPtr meta) { mTypeMeta = meta; }
335 
343  virtual void fixateType() = 0;
344 
346  virtual Slot* allocateSlot() = 0;
347 
349  virtual void freeSlot(Slot* s) = 0;
350 
354  virtual StampedHeader& getStampedHeader(Slot* s) = 0;
355 
360  virtual const Buffer<uint8>& readSerializedValue(Slot* s) = 0;
361 
368  virtual const Buffer<uint8>& readSerializedValue(Slot* s,
369  uint8 formatVersion, bool orLower) = 0;
370 
376  virtual Buffer<uint8> readSerializedValue(Slot* s,
377  std::list<BinarySerializerCodecPtr>& codecs) = 0;
378 
386  virtual Buffer<uint8> readSerializedValue(Slot* s,
387  std::list<BinarySerializerCodecPtr>& codecs,
388  uint8 formatVersion, bool orLower) = 0;
389 
394  virtual void writeSerializedValue(Slot* s, Buffer<uint8> data) = 0;
395 
403  virtual void readJSON(Slot* s, JSONValue& oValue) = 0;
404 
411  virtual void readJSON(Slot* s, JSONValue& oValue, JSONSerializer& serializer) = 0;
412 
420  virtual void writeJSON(Slot* s, const JSONValue& value) = 0;
421 
428  virtual void writeJSON(Slot* s, JSONDeserializer& value) = 0;
429 
436  virtual void writeXML(Slot* s, const XMLDom::const_iterator& node) = 0;
437 
438 public:
439 
441  // write access
442 
465  Slot* requestWriteSlot();
466 
475  bool finishWriteSlot(Slot* n, bool* dropped=NULL);
476 
481  void discardWriteSlot(Slot* n);
482 
483 
485  // read access
486 
492  Slot* readNewestSlot();
493 
500  Slot* readSlotAtTime(const Time& timestamp, SlotQueryMode mode);
501 
507  void readInterval(const Time& timestamp, std::size_t nrSlots,
508  std::size_t olderSlots, std::size_t newerSlots,
509  IntervalFillMode fillMode, std::list<Slot*>& oSlots);
510 
516  void readInterval(const Time& from, const Time& to, std::list<Slot*>& oSlots);
517 
518 public:
519 
528  template<typename TargetType>
530 
531 private:
532 
538  Slot* findFirstOlderSlot(const Time& timestamp, Slot* start=NULL);
539 
540 private:
541 
546  void splice(Slot* first, Slot* last, Slot* position) const;
547 
552  void splice(Slot* item, Slot* position) const {
553  splice(item,item,position);
554  }
555 
556  static bool isEmpty(const ListItem& list) { return list.next == &list; }
557  static Slot* begin(ListItem& list) { return list.next; }
558  static Slot* last(ListItem& list) { return list.prev; }
559  static Slot* end(ListItem& list) { return (Slot*)&list; }
560 
562  void deleteSlots(ListItem& list);
563 
564 protected:
565 
567  void clear();
568 
569 private:
570 
575  void resetSlot(Slot* s);
576 
577  // parameters:
578  Duration mStorageDuration;
579  bool mAutoIncreaseStorageDuration;
580  std::size_t mMinSlots;
581  std::size_t mMaxSlots;
582 
583 
584  // mutex that locks all actions when we change our lists
585  mutable boost::mutex mMutex;
586 
587  std::size_t mSize; // current number of slots we are using
588 
589  ListItem mBuffer;
590 
603  ListItem mWaitingOrFree;
604 
608  ListItem mWriting;
609 
610  TypeMetaPtr mTypeMeta;
611 
612 private:
613 
614  std::string dbgDumpList(ListItem& list, bool brief);
615 
616 public:
617 
618  void dbgDump(const std::string& prefix, bool brief=true);
619 
620  static void dbgCheckListIntegrity(const ListItem& list);
621  void dbgCheckIntegrity();
622 };
623 
625 
629 template <typename T>
631 {
632 public:
633 
635 
636 public:
637 
638  virtual ~TypedChannelBufferBase() { this->clear(); }
639 
640 public:
641 
643  struct Slot : public ChannelBufferBase::Slot {
646  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
647 
648  template <bool IsCheap = IsCheapToCopy<T>::value>
649  std::unique_ptr<Slot> clone() {
650  static_assert(IsCheap, "TypedChannelBufferBase<T>::Slot::clone() must not be called "
651  "with IsCheapToCopy<T>=false");
652  auto slot = std::make_unique<Slot>();
653  {
654  boost::mutex::scoped_lock lock(this->serializedValueLock);
655  slot->serializedValue = this->serializedValue;
656  }
657  slot->timestampPtr = &(static_cast<StampedHeader&>(slot->data).timestamp);
658  slot->flags = this->flags;
659  slot->data = this->data;
660  return slot;
661  }
662  };
663 
664 protected:
665 
670  {
671  // cast abstract ChannelBufferBase::Slot to TypedChannelBufferBase<T>::Slot.
672  // this is safe since all slots were created by us in allocateSlot()
673  // as TypedChannelBufferBase<T>::Slots
674  return static_cast<Slot*>(s);
675  }
676 
677 public:
682  virtual int getTypeId() const { return typeId<T>(); }
683  virtual bool isTyped() const { return true; }
684  virtual bool isPolymorphic() const {return false; }
685  virtual Typename getTypename() const { return typeName<T>(); }
686  virtual void setTypename(const Typename& name) {
687  MIRA_THROW(XRuntime, "Cannot set Typename for typed channels");
688  }
690  MetaSerializer ms(ioDB);
691  Slot* slot = castSlot(s);
692  TypeMetaPtr meta = ms.addMeta(slot->data.internalValueRep());
693  return meta;
694  }
695 
696  virtual void fixateType() {} // doesn't do anything
698  Slot* slot = new Slot;
699  slot->timestampPtr = &(static_cast<StampedHeader&>(slot->data).timestamp); // "connect" the timestamp ptr
700  return slot;
701  }
703  // casting s to our slot type ensures that the correct destructor is called
704  Slot* slot = castSlot(s);
705  delete slot;
706  }
707 
708 public:
709 
711  // we can safely cast here, since s is an object of our Slot type
712  Slot* slot = static_cast<Slot*>(s);
713  return stampedHeader(slot->data);
714  }
715 
717  {
718  // lock the serialization mutex to protect serializedValueLock
719  // (we or someone else may write it here)
720  boost::mutex::scoped_lock lock(s->serializedValueLock);
721 
722  // no serialized data yet, so serialize it now (lazy evaluation)
723  if(s->serializedValue.empty()) {
724  // we can safely cast here, since s is an object of our Slot type
725  Slot* slot = static_cast<Slot*>(s);
726 
727  // serialize the data
729  bs.serialize(slot->data.internalValueRep(), false);
730  }
731 
732  return s->serializedValue;
733  }
734 
735 
737  uint8 formatVersion, bool orLower)
738  {
739  uint8 serializerVersion = BinaryBufferSerializer::getSerializerFormatVersion();
740 
741  if (formatVersion == serializerVersion)
742  return readSerializedValue(s);
743 
744  if (formatVersion == 0) {
745  // lock the serialization mutex to protect serializedValueLock
746  // (we or someone else may write it here)
747  boost::mutex::scoped_lock lock(s->serializedValueLock);
748 
749  // no serialized data yet, so serialize it now (lazy evaluation)
750  if(s->serializedValue.empty()) {
751  // we can safely cast here, since s is an object of our Slot type
752  Slot* slot = static_cast<Slot*>(s);
753 
754  // serialize the data
756  bs.serialize(slot->data.internalValueRep(), false);
757  }
758 
759  return s->serializedValue;
760  }
761 
762  MIRA_THROW(XIO, "Requested serialized data of binary format version " << (int)formatVersion
763  << ". Only implemented for versions 0, " << (int)serializerVersion << ".");
764  }
765 
767  std::list<BinarySerializerCodecPtr>& codecs)
768  {
769  Buffer<uint8> serializedValue;
770 
771  // we can safely cast here, since s is an object of our Slot type
772  Slot* slot = static_cast<Slot*>(s);
773 
774  BinaryBufferSerializer bs(&serializedValue);
775 
776  // register codecs
777  foreach(BinarySerializerCodecPtr codec, codecs)
778  bs.registerCodec(codec);
779 
780  // serialize the data
781  bs.serialize(slot->data.internalValueRep(),false);
782 
783  return std::move(serializedValue);
784  }
785 
787  std::list<BinarySerializerCodecPtr>& codecs,
788  uint8 formatVersion, bool orLower)
789  {
790  uint8 serializerVersion = BinaryBufferSerializer::getSerializerFormatVersion();
791 
792  if (formatVersion == serializerVersion)
793  return readSerializedValue(s, codecs);
794 
795  if (formatVersion == 0) {
796  Buffer<uint8> serializedValue;
797 
798  // we can safely cast here, since s is an object of our Slot type
799  Slot* slot = static_cast<Slot*>(s);
800 
801  BinaryBufferSerializerLegacy bs(&serializedValue);
802 
803  // register codecs
804  foreach(BinarySerializerCodecPtr codec, codecs)
805  bs.registerCodec(codec);
806 
807  // serialize the data
808  bs.serialize(slot->data.internalValueRep(),false);
809 
810  return std::move(serializedValue);
811  }
812 
813  MIRA_THROW(XIO, "Requested serialized data of binary format version " << (int)formatVersion
814  << ". Only implemented for versions 0, " << (int)serializerVersion << ".");
815  }
816 
818  {
819  s->serializedValue = std::move(data);
820 
821  // we can safely cast here, since s is an object of our Slot type
822  Slot* slot = static_cast<Slot*>(s);
823 
824  // deserialize into our data
826 
827  {
828  boost::mutex::scoped_lock lock(mCodecsMutex);
829  bs.setCodecs(mCodecs);
830  }
831  bs.deserialize(slot->data.internalValueRep(), false);
832  {
833  boost::mutex::scoped_lock lock(mCodecsMutex);
834  mCodecs = bs.getCodecs();
835  }
836  }
837 
838  virtual void readJSON(ChannelBufferBase::Slot* s, JSONValue& oValue)
839  {
840  // we can safely cast here, since s is an object of our Slot type
841  Slot* slot = static_cast<Slot*>(s);
842 
843  JSONSerializer json;
844  oValue = json.serialize(slot->data.internalValueRep());
845  }
846 
847  virtual void readJSON(ChannelBufferBase::Slot* s, JSONValue& oValue,
848  JSONSerializer& serializer)
849  {
850  // we can safely cast here, since s is an object of our Slot type
851  Slot* slot = static_cast<Slot*>(s);
852 
853  oValue = serializer.serialize(slot->data.internalValueRep());
854  }
855 
856  virtual void writeJSON(ChannelBufferBase::Slot* s, const JSONValue& value)
857  {
858  // we can safely cast here, since s is an object of our Slot type
859  Slot* slot = static_cast<Slot*>(s);
860 
861  JSONDeserializer json(value);
862  json.deserialize(slot->data.internalValueRep());
863  }
864 
865  virtual void writeJSON(ChannelBufferBase::Slot* s, JSONDeserializer& deserializer)
866  {
867  // we can safely cast here, since s is an object of our Slot type
868  Slot* slot = static_cast<Slot*>(s);
869 
870  deserializer.deserialize(slot->data.internalValueRep());
871  }
872 
874  {
875  // we can safely cast here, since s is an object of our Slot type
876  Slot* slot = static_cast<Slot*>(s);
877  XMLDeserializer xml(node);
878  const std::string rootTag = *node;
879  xml.deserializeFromNode(rootTag.c_str(), slot->data.internalValueRep());
880  }
882 
883 private:
884 
886  boost::mutex mCodecsMutex;
887 };
888 
889 
891 
895 template <typename T>
897 {};
898 
900 
905 template <>
906 class ChannelBuffer<void> : public ChannelBufferBase
907 {
908 public:
909 
911 
912 public:
913 
915  struct Slot : public ChannelBufferBase::Slot {
917  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
918  };
919 
920 protected:
921 
926  {
927  return static_cast<Slot*>(s);
928  }
929 
930 public:
931 
932  ChannelBuffer() : mTypename("") {}
933 
934  virtual ~ChannelBuffer() { this->clear(); }
935 
936 
937 public:
942  virtual int getTypeId() const { return -1; }
943  virtual bool isTyped() const { return false; }
944  virtual bool isPolymorphic() const {return false; }
945  virtual Typename getTypename() const { return mTypename; }
946  virtual void setTypename(const Typename& name);
948  MIRA_THROW(XRuntime, "Cannot create type meta for untyped channels");
949  // keep compiler happy
950  return TypeMetaPtr();
951  }
952  virtual void fixateType() {} // doesn't do anything
953 
955  Slot* slot = new Slot;
956  slot->timestampPtr = &slot->header.timestamp; // "connect" the timestamp ptr
957  return slot;
958  }
959 
961  // casting s to our slot type ensures that the correct destructor is called
962  Slot* slot = castSlot(s);
963  delete slot;
964  }
965 
966 public:
967 
969  // casting here is not safe, if we actually are a typed buffer,
970  // however, if we are, then the TypedChannelBuffer::getStampedHeader()
971  // of that class would have been called instead of us.
972  Slot* slot = static_cast<Slot*>(s);
973  return slot->header;
974  }
975 
977  {
978  return s->serializedValue;
979  }
980 
982  uint8 formatVersion, bool orLower)
983  {
984  BinaryBufferIstream stream(&(s->serializedValue));
985  uint8 dataVersion = BinaryBufferDeserializer::getDataFormatVersion(stream);
986  if ((!orLower && (dataVersion != formatVersion)) || (dataVersion > formatVersion)) {
987  MIRA_THROW(XIO, "Untyped channel contains serialized data of version " << (int)dataVersion
988  << ", cannot return data of version " << (int)formatVersion << ".");
989  }
990 
991  return readSerializedValue(s);
992  }
993 
994 
996  std::list<BinarySerializerCodecPtr>& codecs)
997  {
998  // we must ignore the codecs
999  return s->serializedValue;
1000  }
1001 
1003  std::list<BinarySerializerCodecPtr>& codecs,
1004  uint8 formatVersion, bool orLower)
1005  {
1006  // we must ignore the codecs
1007  return readSerializedValue(s, formatVersion, orLower);
1008  }
1009 
1011  {
1012  s->serializedValue = std::move(data);
1013  }
1014 
1015  virtual void readJSON(ChannelBufferBase::Slot* s, JSONValue& oValue);
1016  virtual void readJSON(ChannelBufferBase::Slot* s, JSONValue& oValue, JSONSerializer& serializer);
1017 
1018  virtual void writeJSON(ChannelBufferBase::Slot* s, const JSONValue& value);
1019  virtual void writeJSON(ChannelBufferBase::Slot* s, JSONDeserializer& deserializer);
1020  void writeXML(ChannelBufferBase::Slot* s, const XMLDom::const_iterator& node) override;
1021 
1023 
1024 private:
1025 
1026  Typename mTypename;
1027 };
1028 
1030 
1035 {
1037 
1038 public:
1039 
1042  mMostDerivedClass(NULL),
1043  mFixatedClass(NULL) {}
1044 
1045 public:
1050  virtual bool isPolymorphic() const {return true; }
1051 
1052  virtual int getTypeId() const;
1053 
1054  virtual Typename getTypename() const;
1055 
1057 
1058  virtual void freeSlot(ChannelBufferBase::Slot* s);
1059 
1060  virtual void fixateType();
1061 
1064 
1066  void promote(const Class* promotionClass);
1067 
1068 protected:
1069 
1072 };
1073 
1074 // declare template extern to speed up compilation time by several seconds
1075 extern template class TypedChannelBufferBase<Object*>;
1076 
1078 
1083 template <typename T>
1085 {
1086  static_assert(std::is_base_of<mira::Object,T>::value,
1087  "In channels you can only use pointers of polymorphic "
1088  "classes that are derived from mira::Object. Pointers to "
1089  "other classes cannot be stored in a channel.");
1090 public:
1091  typedef Stamped<T*> ValueType;
1092 
1093 public:
1095  mMostDerivedClass = &T::CLASS();
1096  }
1097 };
1098 
1100 // Channel buffer promotion code
1101 
1108 template <typename U>
1109 struct ChannelBufferPromoterCommon
1110 {
1112  {
1113  assert(!buffer->isTyped() &&
1114  "Can promote untyped channels to typed channels only");
1115 
1116  if(!buffer->getTypename().empty() && buffer->getTypename()!=typeName<U>())
1117  MIRA_THROW(XBadCast, "Invalid promotion from untyped to typed "
1118  "ChannelBuffer. Typename does not match. ('" <<
1119  buffer->getTypename() << "' != '" << typeName<U>() << "'");
1120 
1121  typedef ChannelBufferBase::Slot Slot;
1122 
1123  // create a new typed buffer
1124  ChannelBuffer<U>* typedBuffer = new ChannelBuffer<U>;
1125 
1126  // copy all parameters
1127  typedBuffer->cloneParameters(*buffer);
1128 
1129  // copy all the serialized data from the untyped buffer into the
1130  // typed buffer and deserialize it into the value
1131 
1132  // now lock the whole buffer for reading ...
1133  for(Slot* s=ChannelBufferBase::begin(buffer->mBuffer);
1134  s!=ChannelBufferBase::end(buffer->mBuffer); s=s->next)
1135  s->lock.lock_shared();
1136 
1137  // ... and deserialize the content of each slot into the new buffer
1138  for(Slot* s=ChannelBufferBase::begin(buffer->mBuffer);
1139  s!=ChannelBufferBase::end(buffer->mBuffer); s=s->next)
1140  {
1141  Slot* n = typedBuffer->requestWriteSlot(); // obtain a new slot
1142 
1143  // ... copy the headers
1144  typedBuffer->getStampedHeader(n) = buffer->getStampedHeader(s);
1145 
1146  // ... and write the value into the new slot (this will deserialize the data there)
1147  typedBuffer->writeSerializedValue(n, s->serializedValue);
1148 
1149  typedBuffer->finishWriteSlot(n);
1150  }
1151 
1152  // free the read lock from above ...
1153  for(Slot* s=ChannelBufferBase::begin(buffer->mBuffer);
1154  s!=ChannelBufferBase::end(buffer->mBuffer); s=s->next)
1155  s->lock.unlock_shared();
1156 
1157  return typedBuffer;
1158  }
1159 };
1160 
1162 
1173 template <typename U>
1174 struct ChannelBufferPromoter : public ChannelBufferPromoterCommon<U>
1175 {
1178  {
1179  if(buffer->isTyped())
1180  return buffer; // we are promoted already, do nothing
1181  return Base::promoteUntyped(buffer);
1182  }
1183 };
1184 
1186 
1191 template <typename U>
1193 {
1196  {
1197  // if untyped, promote to typed first
1198  if(!buffer->isTyped())
1199  buffer = Base::promoteUntyped(buffer);
1200 
1201  PolymorphicChannelBuffer* polymorphicBuffer = dynamic_cast<PolymorphicChannelBuffer*>(buffer);
1202 
1203  assert(polymorphicBuffer!=NULL &&
1204  "We should never reach here if the buffer to promote is not "
1205  "polymorphic");
1206 
1207  // get class of U
1208  const Class* classU = &U::CLASS();
1209 
1210  // promote us (this will do error checking and might throw XBadCast)
1211  polymorphicBuffer->promote(classU);
1212 
1213  return polymorphicBuffer;
1214  }
1215 };
1216 
1218 
1219 template<typename U>
1221 {
1222  return ChannelBufferPromoter<U>::promote(this);
1223 }
1224 
1226 
1227 }
1228 
1229 #endif
Serializer for serializing objects in JSON format.
Definition: JSONSerializer.h:95
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:856
Slot * readSlotAtTime(const Time &timestamp, SlotQueryMode mode)
Returns a slot at the given time according to mode.
virtual void freeSlot(ChannelBufferBase::Slot *s)
Called to destroy the slot and free the used memory.
Definition: ChannelBuffer.h:702
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:436
virtual ChannelBufferBase::Slot * allocateSlot()
Creates and returns a new slot.
void setStorageDuration(const Duration &storageDuration)
Sets the desired storage duration (the history).
boost::shared_mutex lock
Definition: ChannelBuffer.h:166
static ChannelBufferBase * promote(ChannelBufferBase *buffer)
Definition: ChannelBuffer.h:1195
Definition: BinarySerializer.h:316
const Time * timestampPtr
Pointer to the actual timestamp in the slot data for easy access.
Definition: ChannelBuffer.h:178
const Class * mFixatedClass
Definition: ChannelBuffer.h:1071
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:947
virtual void readJSON(Slot *s, JSONValue &oValue)=0
Reads data of that slot as json representation using a default-instantiated JSONSerializer.
boost::mutex serializedValueLock
Lock for writing serializedValue in readSerializedData.
Definition: ChannelBuffer.h:173
const Class * mMostDerivedClass
Definition: ChannelBuffer.h:1070
bool isAutoIncreasingStorageDuration() const
Returns whether the storage duration is automatically increased.
Include file for all eigen related things.
virtual ~ChannelBuffer()
Definition: ChannelBuffer.h:934
Typed slot derived from ChannelBufferBase::Slot.
Definition: ChannelBuffer.h:643
void promote(const Class *promotionClass)
Promotes the polymorphic type of the channel to the given class type.
virtual void freeSlot(Slot *s)=0
Called to destroy the slot and free the used memory.
The first slot with timestamp < requested will be chosen.
Definition: ChannelBuffer.h:96
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:1010
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:786
Slot * castSlot(ChannelBufferBase::Slot *s)
Cast an abstract slot to a typed slot.
Definition: ChannelBuffer.h:925
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
virtual TypeMetaPtr createTypeMeta(Slot *s, MetaTypeDatabase &ioDB)=0
Creates meta information for data in slot and stores all meta information in the given database...
std::string Typename
Definition: Typename.h:60
virtual ChannelBufferBase::Slot * allocateSlot()
Creates and returns a new slot.
Definition: ChannelBuffer.h:697
Specialization for polymorphic types (only classes derived from mira::Object are supported!) ...
Definition: ChannelBuffer.h:1084
ValueType header
Definition: ChannelBuffer.h:916
virtual bool isTyped() const
Returns true, if the channel is typed and false, if it is untyped.
Definition: ChannelBuffer.h:943
Time and Duration wrapper class.
Class object which supports some kind of class reflection.
Definition: Class.h:97
virtual void freeSlot(ChannelBufferBase::Slot *s)
Called to destroy the slot and free the used memory.
virtual Typename getTypename() const
Returns the Typename of the slot values, in contrast to getTypeId() the Typename is portable and uniq...
Definition: ChannelBuffer.h:945
uint32_t uint32
Definition: Types.h:64
virtual void setTypename(const Typename &name)=0
Sets the Typename of the slot values.
ListItem()
Definition: ChannelBuffer.h:150
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:1220
TypeMetaPtr getTypeMeta() const
Returns the meta information for the slots type.
Definition: ChannelBuffer.h:329
Base class for typed channel buffers.
Definition: ChannelBuffer.h:630
void setMinSlots(std::size_t minSlots)
Sets the desired min.
virtual void fixateType()
Calling this method will fix the type of the ChannelBuffer.
Definition: ChannelBuffer.h:696
static ChannelBufferBase * promoteUntyped(ChannelBufferBase *buffer)
Definition: ChannelBuffer.h:1111
static void dbgCheckListIntegrity(const ListItem &list)
Get compiler and platform independent typenames.
virtual Slot * allocateSlot()=0
Creates and returns a new slot.
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:78
void serialize(const T &value, bool enableTypeCheck=true)
Provides a special serialize interface for the BinarySerializer.
Definition: BinarySerializer.h:590
Implements double-linked list item.
Definition: ChannelBuffer.h:148
Const sibling_iterator for iterating over xml nodes that have the same parent (siblings) ...
Definition: XMLDom.h:763
virtual StampedHeader & getStampedHeader(ChannelBufferBase::Slot *s)
Returns a reference to the stamped header information.
Definition: ChannelBuffer.h:968
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:418
Slot * requestWriteSlot()
Returns a slot for writing, where the shared_mutex is already write locked.
ChannelBufferPromoterCommon< U > Base
Definition: ChannelBuffer.h:1194
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:736
ChannelBuffer()
Definition: ChannelBuffer.h:1094
virtual int getTypeId() const
Returns typeid of the slot values, the allocator creates.
A channel buffer for polymorphic types (classes derived from mira::Object)
Definition: ChannelBuffer.h:1034
Deserializer for serializing objects from JSON format.
Definition: JSONSerializer.h:406
virtual void writeXML(Slot *s, const XMLDom::const_iterator &node)=0
Writes data in xml representation into the slot, using a default-instantiated XMLDeserializer and the...
virtual int getTypeId() const
Returns typeid of the slot values, the allocator creates.
Definition: ChannelBuffer.h:942
virtual ~TypedChannelBufferBase()
Definition: ChannelBuffer.h:638
void setTypeMeta(TypeMetaPtr meta)
Sets the meta information of the slot&#39;s type.
Definition: ChannelBuffer.h:334
Binary serializer and deserializer.
PolymorphicChannelBuffer()
Constructor.
Definition: ChannelBuffer.h:1041
forward decl.
Definition: ChannelBuffer.h:81
void registerCodec(BinarySerializerCodecPtr codec)
Registers the specified codec instance in this binary serializer.
Definition: BinarySerializer.h:756
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:995
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:817
virtual bool isPolymorphic() const
Returns true if the channel has a polymorphic type.
Definition: ChannelBuffer.h:944
virtual const Buffer< uint8 > & readSerializedValue(Slot *s)=0
Returns a buffer containing the binary serialized data of the slot.
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:689
Slot * castSlot(ChannelBufferBase::Slot *s)
Cast an abstract slot to a typed slot.
Definition: ChannelBuffer.h:669
virtual StampedHeader & getStampedHeader(ChannelBufferBase::Slot *s)
Returns a reference to the stamped header information.
Definition: ChannelBuffer.h:710
EIGEN_MAKE_ALIGNED_OPERATOR_NEW std::unique_ptr< Slot > clone()
Definition: ChannelBuffer.h:649
Stamped< T * > ValueType
Definition: ChannelBuffer.h:1089
Core class of the logging library.
Use this class to represent time durations.
Definition: Time.h:106
virtual bool isPolymorphic() const
Returns true if the channel has a polymorphic type.
Definition: ChannelBuffer.h:684
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:1570
uint8_t uint8
Definition: Types.h:62
bool empty() const
Checks if the buffer is empty (used size == 0).
Definition: Buffer.h:299
Typed ChannelBuffer.
Definition: ChannelBuffer.h:896
The slot with smallest time difference to the requested will be chosen.
Definition: ChannelBuffer.h:102
virtual void fixateType()=0
Calling this method will fix the type of the ChannelBuffer.
virtual StampedHeader & getStampedHeader(Slot *s)=0
Returns a reference to the stamped header information.
ChannelBuffer()
Definition: ChannelBuffer.h:932
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:981
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:682
virtual void freeSlot(ChannelBufferBase::Slot *s)
Called to destroy the slot and free the used memory.
Definition: ChannelBuffer.h:960
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:1002
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:847
Stamped< T > ValueType
Definition: ChannelBuffer.h:634
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:226
forward decl.
Definition: ChannelBuffer.h:85
virtual bool isPolymorphic() const =0
Returns true if the channel has a polymorphic type.
void dbgDump(const std::string &prefix, bool brief=true)
void setMaxSlots(std::size_t maxSlots)
Sets the max.
virtual bool isPolymorphic() const
Returns true if the channel has a polymorphic type.
Definition: ChannelBuffer.h:1050
ValueType data
the actual data value, that is stored in this channel&#39;s slots
Definition: ChannelBuffer.h:645
Base class that manages the slots of channels by providing read and write access to them...
Definition: ChannelBuffer.h:132
const Time & timestamp() const
Definition: ChannelBuffer.h:194
Container for storing a single data element in the linked list.
Definition: ChannelBuffer.h:164
std::size_t getMaxSlots() const
Returns the size of the max number of slots.
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:1575
Slot * readNewestSlot()
Returns the newest(latest) slot for reading, where the shared_mutex is already read locked...
virtual void setTypename(const Typename &name)
Sets the Typename of the slot values.
Definition: ChannelBuffer.h:686
virtual void fixateType()
Calling this method will fix the type of the ChannelBuffer.
Definition: ChannelBuffer.h:952
Input stream adapter that can be assigned to any input stream and allows binary input using the >> st...
Definition: BinaryStream.h:525
Slot * next
Definition: ChannelBuffer.h:157
std::size_t getSize() const
Returns the size of the buffer.
virtual Typename getTypename() const
Returns the Typename of the slot values, in contrast to getTypeId() the Typename is portable and uniq...
Buffer< uint8 > serializedValue
The data of this slot as serialized data.
Definition: ChannelBuffer.h:184
virtual void readJSON(ChannelBufferBase::Slot *s, JSONValue &oValue)
Reads data of that slot as json representation using a default-instantiated JSONSerializer.
Definition: ChannelBuffer.h:838
virtual void fixateType()
Calling this method will fix the type of the ChannelBuffer.
virtual const Buffer< uint8 > & readSerializedValue(ChannelBufferBase::Slot *s)
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:976
void deserialize(T &value, bool enableTypeCheck=true, bool recursive=false)
Provides a special deserialize interface for the BinaryDeserializer.
Definition: BinarySerializer.h:1329
StampedHeader ValueType
Definition: ChannelBuffer.h:910
virtual void writeSerializedValue(ChannelBufferBase::Slot *s, Buffer< uint8 > data)
Write the given buffer (that contains binary serialized data WITHOUT the StampedHeader) to the slot...
virtual bool isTyped() const
Returns true, if the channel is typed and false, if it is untyped.
Definition: ChannelBuffer.h:683
ChannelBufferPromoterCommon< U > Base
Definition: ChannelBuffer.h:1176
TypeMetaPtr addMeta(const T &v)
Definition: MetaSerializer.h:605
virtual ChannelBufferBase::Slot * allocateSlot()
Creates and returns a new slot.
Definition: ChannelBuffer.h:954
XMLSerializer and XMLDeserializer.
std::size_t getMinSlots() const
Returns the size of the min number of slots.
virtual void writeSerializedValue(Slot *s, Buffer< uint8 > data)=0
Write the given buffer (that contains binary serialized data WITHOUT the StampedHeader) to the slot...
Prefer filling the interval with slots with timestamp > requested.
Definition: ChannelBuffer.h:114
void discardWriteSlot(Slot *n)
Discards written slot.
Prefer filling the interval with slots with timestamp < requested.
Definition: ChannelBuffer.h:112
void setAutoIncreaseStorageDuration(bool increase)
Sets whether the storage duration is automatically increased on read accesses.
uint32 flags
Used to store internal flags that are especially used by the remote capability of the framework...
Definition: ChannelBuffer.h:192
static ChannelBufferBase * promote(ChannelBufferBase *buffer)
Definition: ChannelBuffer.h:1177
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:362
IntervalFillMode
Mode that is used to determine what slots should be added to the interval when not enough slots are a...
Definition: ChannelBuffer.h:109
virtual const Buffer< uint8 > & readSerializedValue(ChannelBufferBase::Slot *s)
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:716
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:873
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:766
Specialization of ChannelBufferPromoter for polymorphic pointer channel buffers.
Definition: ChannelBuffer.h:1192
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:93
json::Value JSONValue
Imports the json::Value type into mira namespace.
Definition: JSON.h:361
void clear()
Deletes all slots in all lists and therefore clears the whole buffer.
Provides method for generating a unique id for any type.
Duration getStorageDuration() const
Returns the storage duration.
Slot * prev
Definition: ChannelBuffer.h:158
virtual Typename getTypename() const
Returns the Typename of the slot values, in contrast to getTypeId() the Typename is portable and uniq...
Definition: ChannelBuffer.h:685
virtual void writeJSON(Slot *s, const JSONValue &value)=0
Writes data in json representation into the slot, using a default-instantiated JSONDeserializer.
void readInterval(const Time &timestamp, std::size_t nrSlots, std::size_t olderSlots, std::size_t newerSlots, IntervalFillMode fillMode, std::list< Slot *> &oSlots)
Reads an interval of slots.
ChannelBufferBase()
Constructor.
Definition: ChannelBuffer.h:210
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:865
virtual int getTypeId() const =0
Returns typeid of the slot values, the allocator creates.
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:99
std::map< TypeId, BinarySerializerCodecPtr > CodecsMap
A map of binary serialization codecs.
Definition: BinarySerializer.h:1259
boost::shared_ptr< BinarySerializerCodec > BinarySerializerCodecPtr
Shared pointer of BinarySerializerCodec.
Definition: BinarySerializerCodec.h:64
Deserializer for serializing objects from XML format.
Definition: XMLSerializer.h:314