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  virtual ~Slot() = default;
200  virtual std::unique_ptr<Slot> clone() = 0;
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  std::unique_ptr<ChannelBufferBase::Slot> clone() override {
649  auto slot = std::make_unique<Slot>();
650  slot->serializedValue = this->serializedValue;
651  slot->timestampPtr = &(static_cast<StampedHeader&>(slot->data).timestamp);
652  slot->flags = this->flags;
653  slot->data = this->data;
654  return slot;
655  }
656  };
657 
658 protected:
659 
664  {
665  // cast abstract ChannelBufferBase::Slot to TypedChannelBufferBase<T>::Slot.
666  // this is safe since all slots were created by us in allocateSlot()
667  // as TypedChannelBufferBase<T>::Slots
668  return static_cast<Slot*>(s);
669  }
670 
671 public:
676  virtual int getTypeId() const { return typeId<T>(); }
677  virtual bool isTyped() const { return true; }
678  virtual bool isPolymorphic() const {return false; }
679  virtual Typename getTypename() const { return typeName<T>(); }
680  virtual void setTypename(const Typename& name) {
681  MIRA_THROW(XRuntime, "Cannot set Typename for typed channels");
682  }
684  MetaSerializer ms(ioDB);
685  Slot* slot = castSlot(s);
686  TypeMetaPtr meta = ms.addMeta(slot->data.internalValueRep());
687  return meta;
688  }
689 
690  virtual void fixateType() {} // doesn't do anything
692  Slot* slot = new Slot;
693  slot->timestampPtr = &(static_cast<StampedHeader&>(slot->data).timestamp); // "connect" the timestamp ptr
694  return slot;
695  }
697  // casting s to our slot type ensures that the correct destructor is called
698  Slot* slot = castSlot(s);
699  delete slot;
700  }
701 
702 public:
703 
705  // we can safely cast here, since s is an object of our Slot type
706  Slot* slot = static_cast<Slot*>(s);
707  return stampedHeader(slot->data);
708  }
709 
711  {
712  // lock the serialization mutex to protect serializedValueLock
713  // (we or someone else may write it here)
714  boost::mutex::scoped_lock lock(s->serializedValueLock);
715 
716  // no serialized data yet, so serialize it now (lazy evaluation)
717  if(s->serializedValue.empty()) {
718  // we can safely cast here, since s is an object of our Slot type
719  Slot* slot = static_cast<Slot*>(s);
720 
721  // serialize the data
723  bs.serialize(slot->data.internalValueRep(), false);
724  }
725 
726  return s->serializedValue;
727  }
728 
729 
731  uint8 formatVersion, bool orLower)
732  {
733  uint8 serializerVersion = BinaryBufferSerializer::getSerializerFormatVersion();
734 
735  if (formatVersion == serializerVersion)
736  return readSerializedValue(s);
737 
738  if (formatVersion == 0) {
739  // lock the serialization mutex to protect serializedValueLock
740  // (we or someone else may write it here)
741  boost::mutex::scoped_lock lock(s->serializedValueLock);
742 
743  // no serialized data yet, so serialize it now (lazy evaluation)
744  if(s->serializedValue.empty()) {
745  // we can safely cast here, since s is an object of our Slot type
746  Slot* slot = static_cast<Slot*>(s);
747 
748  // serialize the data
750  bs.serialize(slot->data.internalValueRep(), false);
751  }
752 
753  return s->serializedValue;
754  }
755 
756  MIRA_THROW(XIO, "Requested serialized data of binary format version " << (int)formatVersion
757  << ". Only implemented for versions 0, " << (int)serializerVersion << ".");
758  }
759 
761  std::list<BinarySerializerCodecPtr>& codecs)
762  {
763  Buffer<uint8> serializedValue;
764 
765  // we can safely cast here, since s is an object of our Slot type
766  Slot* slot = static_cast<Slot*>(s);
767 
768  BinaryBufferSerializer bs(&serializedValue);
769 
770  // register codecs
771  foreach(BinarySerializerCodecPtr codec, codecs)
772  bs.registerCodec(codec);
773 
774  // serialize the data
775  bs.serialize(slot->data.internalValueRep(),false);
776 
777  return std::move(serializedValue);
778  }
779 
781  std::list<BinarySerializerCodecPtr>& codecs,
782  uint8 formatVersion, bool orLower)
783  {
784  uint8 serializerVersion = BinaryBufferSerializer::getSerializerFormatVersion();
785 
786  if (formatVersion == serializerVersion)
787  return readSerializedValue(s, codecs);
788 
789  if (formatVersion == 0) {
790  Buffer<uint8> serializedValue;
791 
792  // we can safely cast here, since s is an object of our Slot type
793  Slot* slot = static_cast<Slot*>(s);
794 
795  BinaryBufferSerializerLegacy bs(&serializedValue);
796 
797  // register codecs
798  foreach(BinarySerializerCodecPtr codec, codecs)
799  bs.registerCodec(codec);
800 
801  // serialize the data
802  bs.serialize(slot->data.internalValueRep(),false);
803 
804  return std::move(serializedValue);
805  }
806 
807  MIRA_THROW(XIO, "Requested serialized data of binary format version " << (int)formatVersion
808  << ". Only implemented for versions 0, " << (int)serializerVersion << ".");
809  }
810 
812  {
813  s->serializedValue = std::move(data);
814 
815  // we can safely cast here, since s is an object of our Slot type
816  Slot* slot = static_cast<Slot*>(s);
817 
818  // deserialize into our data
820 
821  {
822  boost::mutex::scoped_lock lock(mCodecsMutex);
823  bs.setCodecs(mCodecs);
824  }
825  bs.deserialize(slot->data.internalValueRep(), false);
826  {
827  boost::mutex::scoped_lock lock(mCodecsMutex);
828  mCodecs = bs.getCodecs();
829  }
830  }
831 
832  virtual void readJSON(ChannelBufferBase::Slot* s, JSONValue& oValue)
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  JSONSerializer json;
838  oValue = json.serialize(slot->data.internalValueRep());
839  }
840 
841  virtual void readJSON(ChannelBufferBase::Slot* s, JSONValue& oValue,
842  JSONSerializer& serializer)
843  {
844  // we can safely cast here, since s is an object of our Slot type
845  Slot* slot = static_cast<Slot*>(s);
846 
847  oValue = serializer.serialize(slot->data.internalValueRep());
848  }
849 
850  virtual void writeJSON(ChannelBufferBase::Slot* s, const JSONValue& value)
851  {
852  // we can safely cast here, since s is an object of our Slot type
853  Slot* slot = static_cast<Slot*>(s);
854 
855  JSONDeserializer json(value);
856  json.deserialize(slot->data.internalValueRep());
857  }
858 
859  virtual void writeJSON(ChannelBufferBase::Slot* s, JSONDeserializer& deserializer)
860  {
861  // we can safely cast here, since s is an object of our Slot type
862  Slot* slot = static_cast<Slot*>(s);
863 
864  deserializer.deserialize(slot->data.internalValueRep());
865  }
866 
868  {
869  // we can safely cast here, since s is an object of our Slot type
870  Slot* slot = static_cast<Slot*>(s);
871  XMLDeserializer xml(node);
872  const std::string rootTag = *node;
873  xml.deserializeFromNode(rootTag.c_str(), slot->data.internalValueRep());
874  }
876 
877 private:
878 
880  boost::mutex mCodecsMutex;
881 };
882 
883 
885 
889 template <typename T>
891 {};
892 
894 
899 template <>
900 class ChannelBuffer<void> : public ChannelBufferBase
901 {
902 public:
903 
905 
906 public:
907 
909  struct Slot : public ChannelBufferBase::Slot {
911  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
912 
913  std::unique_ptr<ChannelBufferBase::Slot> clone() override {
914  auto slot = std::make_unique<Slot>();
915  slot->serializedValue = this->serializedValue;
916  slot->timestampPtr = &slot->header.timestamp;
917  slot->flags = this->flags;
918  slot->header = this->header;
919  return slot;
920  }
921  };
922 
923 protected:
924 
929  {
930  return static_cast<Slot*>(s);
931  }
932 
933 public:
934 
935  ChannelBuffer() : mTypename("") {}
936 
937  virtual ~ChannelBuffer() { this->clear(); }
938 
939 
940 public:
945  virtual int getTypeId() const { return -1; }
946  virtual bool isTyped() const { return false; }
947  virtual bool isPolymorphic() const {return false; }
948  virtual Typename getTypename() const { return mTypename; }
949  virtual void setTypename(const Typename& name);
951  MIRA_THROW(XRuntime, "Cannot create type meta for untyped channels");
952  // keep compiler happy
953  return TypeMetaPtr();
954  }
955  virtual void fixateType() {} // doesn't do anything
956 
958  Slot* slot = new Slot;
959  slot->timestampPtr = &slot->header.timestamp; // "connect" the timestamp ptr
960  return slot;
961  }
962 
964  // casting s to our slot type ensures that the correct destructor is called
965  Slot* slot = castSlot(s);
966  delete slot;
967  }
968 
969 public:
970 
972  // casting here is not safe, if we actually are a typed buffer,
973  // however, if we are, then the TypedChannelBuffer::getStampedHeader()
974  // of that class would have been called instead of us.
975  Slot* slot = static_cast<Slot*>(s);
976  return slot->header;
977  }
978 
980  {
981  return s->serializedValue;
982  }
983 
985  uint8 formatVersion, bool orLower)
986  {
987  BinaryBufferIstream stream(&(s->serializedValue));
988  uint8 dataVersion = BinaryBufferDeserializer::getDataFormatVersion(stream);
989  if ((!orLower && (dataVersion != formatVersion)) || (dataVersion > formatVersion)) {
990  MIRA_THROW(XIO, "Untyped channel contains serialized data of version " << (int)dataVersion
991  << ", cannot return data of version " << (int)formatVersion << ".");
992  }
993 
994  return readSerializedValue(s);
995  }
996 
997 
999  std::list<BinarySerializerCodecPtr>& codecs)
1000  {
1001  // we must ignore the codecs
1002  return s->serializedValue;
1003  }
1004 
1006  std::list<BinarySerializerCodecPtr>& codecs,
1007  uint8 formatVersion, bool orLower)
1008  {
1009  // we must ignore the codecs
1010  return readSerializedValue(s, formatVersion, orLower);
1011  }
1012 
1014  {
1015  s->serializedValue = std::move(data);
1016  }
1017 
1018  virtual void readJSON(ChannelBufferBase::Slot* s, JSONValue& oValue);
1019  virtual void readJSON(ChannelBufferBase::Slot* s, JSONValue& oValue, JSONSerializer& serializer);
1020 
1021  virtual void writeJSON(ChannelBufferBase::Slot* s, const JSONValue& value);
1022  virtual void writeJSON(ChannelBufferBase::Slot* s, JSONDeserializer& deserializer);
1023  void writeXML(ChannelBufferBase::Slot* s, const XMLDom::const_iterator& node) override;
1024 
1026 
1027 private:
1028 
1029  Typename mTypename;
1030 };
1031 
1033 
1038 {
1040 
1041 public:
1042 
1045  mMostDerivedClass(NULL),
1046  mFixatedClass(NULL) {}
1047 
1048 public:
1053  virtual bool isPolymorphic() const {return true; }
1054 
1055  virtual int getTypeId() const;
1056 
1057  virtual Typename getTypename() const;
1058 
1060 
1061  virtual void freeSlot(ChannelBufferBase::Slot* s);
1062 
1063  virtual void fixateType();
1064 
1067 
1069  void promote(const Class* promotionClass);
1070 
1071 protected:
1072 
1075 };
1076 
1077 // declare template extern to speed up compilation time by several seconds
1078 extern template class TypedChannelBufferBase<Object*>;
1079 
1081 
1086 template <typename T>
1088 {
1089  static_assert(std::is_base_of<mira::Object,T>::value,
1090  "In channels you can only use pointers of polymorphic "
1091  "classes that are derived from mira::Object. Pointers to "
1092  "other classes cannot be stored in a channel.");
1093 public:
1094  typedef Stamped<T*> ValueType;
1095 
1096 public:
1098  mMostDerivedClass = &T::CLASS();
1099  }
1100 };
1101 
1103 // Channel buffer promotion code
1104 
1111 template <typename U>
1112 struct ChannelBufferPromoterCommon
1113 {
1115  {
1116  assert(!buffer->isTyped() &&
1117  "Can promote untyped channels to typed channels only");
1118 
1119  if(!buffer->getTypename().empty() && buffer->getTypename()!=typeName<U>())
1120  MIRA_THROW(XBadCast, "Invalid promotion from untyped to typed "
1121  "ChannelBuffer. Typename does not match. ('" <<
1122  buffer->getTypename() << "' != '" << typeName<U>() << "'");
1123 
1124  typedef ChannelBufferBase::Slot Slot;
1125 
1126  // create a new typed buffer
1127  ChannelBuffer<U>* typedBuffer = new ChannelBuffer<U>;
1128 
1129  // copy all parameters
1130  typedBuffer->cloneParameters(*buffer);
1131 
1132  // copy all the serialized data from the untyped buffer into the
1133  // typed buffer and deserialize it into the value
1134 
1135  // now lock the whole buffer for reading ...
1136  for(Slot* s=ChannelBufferBase::begin(buffer->mBuffer);
1137  s!=ChannelBufferBase::end(buffer->mBuffer); s=s->next)
1138  s->lock.lock_shared();
1139 
1140  // ... and deserialize the content of each slot into the new buffer
1141  for(Slot* s=ChannelBufferBase::begin(buffer->mBuffer);
1142  s!=ChannelBufferBase::end(buffer->mBuffer); s=s->next)
1143  {
1144  Slot* n = typedBuffer->requestWriteSlot(); // obtain a new slot
1145 
1146  // ... copy the headers
1147  typedBuffer->getStampedHeader(n) = buffer->getStampedHeader(s);
1148 
1149  // ... and write the value into the new slot (this will deserialize the data there)
1150  typedBuffer->writeSerializedValue(n, s->serializedValue);
1151 
1152  typedBuffer->finishWriteSlot(n);
1153  }
1154 
1155  // free the read lock from above ...
1156  for(Slot* s=ChannelBufferBase::begin(buffer->mBuffer);
1157  s!=ChannelBufferBase::end(buffer->mBuffer); s=s->next)
1158  s->lock.unlock_shared();
1159 
1160  return typedBuffer;
1161  }
1162 };
1163 
1165 
1176 template <typename U>
1177 struct ChannelBufferPromoter : public ChannelBufferPromoterCommon<U>
1178 {
1181  {
1182  if(buffer->isTyped())
1183  return buffer; // we are promoted already, do nothing
1184  return Base::promoteUntyped(buffer);
1185  }
1186 };
1187 
1189 
1194 template <typename U>
1196 {
1199  {
1200  // if untyped, promote to typed first
1201  if(!buffer->isTyped())
1202  buffer = Base::promoteUntyped(buffer);
1203 
1204  PolymorphicChannelBuffer* polymorphicBuffer = dynamic_cast<PolymorphicChannelBuffer*>(buffer);
1205 
1206  assert(polymorphicBuffer!=NULL &&
1207  "We should never reach here if the buffer to promote is not "
1208  "polymorphic");
1209 
1210  // get class of U
1211  const Class* classU = &U::CLASS();
1212 
1213  // promote us (this will do error checking and might throw XBadCast)
1214  polymorphicBuffer->promote(classU);
1215 
1216  return polymorphicBuffer;
1217  }
1218 };
1219 
1221 
1222 template<typename U>
1224 {
1225  return ChannelBufferPromoter<U>::promote(this);
1226 }
1227 
1229 
1230 }
1231 
1232 #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:850
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:696
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:428
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:1198
EIGEN_MAKE_ALIGNED_OPERATOR_NEW std::unique_ptr< ChannelBufferBase::Slot > clone() override
Definition: ChannelBuffer.h:648
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:1074
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:950
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:1073
bool isAutoIncreasingStorageDuration() const
Returns whether the storage duration is automatically increased.
Include file for all eigen related things.
virtual ~ChannelBuffer()
Definition: ChannelBuffer.h:937
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:1013
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:780
Slot * castSlot(ChannelBufferBase::Slot *s)
Cast an abstract slot to a typed slot.
Definition: ChannelBuffer.h:928
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:691
Specialization for polymorphic types (only classes derived from mira::Object are supported!) ...
Definition: ChannelBuffer.h:1087
ValueType header
Definition: ChannelBuffer.h:910
virtual bool isTyped() const
Returns true, if the channel is typed and false, if it is untyped.
Definition: ChannelBuffer.h:946
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:948
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:1223
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:690
static ChannelBufferBase * promoteUntyped(ChannelBufferBase *buffer)
Definition: ChannelBuffer.h:1114
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:971
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:1197
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:730
ChannelBuffer()
Definition: ChannelBuffer.h:1097
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:1037
Deserializer for serializing objects from JSON format.
Definition: JSONSerializer.h:401
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:945
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:1044
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:998
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:811
virtual bool isPolymorphic() const
Returns true if the channel has a polymorphic type.
Definition: ChannelBuffer.h:947
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:683
Slot * castSlot(ChannelBufferBase::Slot *s)
Cast an abstract slot to a typed slot.
Definition: ChannelBuffer.h:663
virtual StampedHeader & getStampedHeader(ChannelBufferBase::Slot *s)
Returns a reference to the stamped header information.
Definition: ChannelBuffer.h:704
Stamped< T * > ValueType
Definition: ChannelBuffer.h:1092
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:678
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:890
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:935
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:984
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:676
virtual void freeSlot(ChannelBufferBase::Slot *s)
Called to destroy the slot and free the used memory.
Definition: ChannelBuffer.h:963
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:1005
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:841
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:1053
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:680
virtual void fixateType()
Calling this method will fix the type of the ChannelBuffer.
Definition: ChannelBuffer.h:955
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:832
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:979
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:904
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:677
ChannelBufferPromoterCommon< U > Base
Definition: ChannelBuffer.h:1179
TypeMetaPtr addMeta(const T &v)
Definition: MetaSerializer.h:605
virtual ChannelBufferBase::Slot * allocateSlot()
Creates and returns a new slot.
Definition: ChannelBuffer.h:957
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.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW std::unique_ptr< ChannelBufferBase::Slot > clone() override
Definition: ChannelBuffer.h:913
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:1180
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:710
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:867
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:760
Specialization of ChannelBufferPromoter for polymorphic pointer channel buffers.
Definition: ChannelBuffer.h:1195
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
virtual std::unique_ptr< Slot > clone()=0
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:679
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:859
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