47 #ifndef _MIRA_CHANNELBUFFER_H_ 48 #define _MIRA_CHANNELBUFFER_H_ 52 #include <type_traits> 55 #include <boost/noncopyable.hpp> 56 #include <boost/thread/mutex.hpp> 57 #include <boost/thread/shared_mutex.hpp> 82 template <
typename TargetType>
86 template <
typename TargetType>
136 template <
typename TargetType>
139 template <
typename TargetType>
197 assert(timestampPtr!=NULL);
198 return *timestampPtr;
201 virtual ~
Slot() =
default;
202 virtual std::unique_ptr<Slot> clone() = 0;
213 mStorageDuration(
Duration::seconds(0)),
214 mAutoIncreaseStorageDuration(true),
230 mMinSlots = other.mMinSlots;
231 mMaxSlots = other.mMaxSlots;
232 mStorageDuration = other.mStorageDuration;
233 mAutoIncreaseStorageDuration = other.mAutoIncreaseStorageDuration;
246 void setStorageDuration(
const Duration& storageDuration);
256 void setAutoIncreaseStorageDuration(
bool increase);
267 void setMinSlots(std::size_t minSlots);
279 void setMaxSlots(std::size_t maxSlots);
284 std::size_t getSize()
const;
287 std::size_t getMaxSlots()
const;
290 std::size_t getMinSlots()
const;
293 Duration getStorageDuration()
const;
296 bool isAutoIncreasingStorageDuration()
const;
302 virtual int getTypeId()
const = 0;
305 virtual bool isTyped()
const = 0;
308 virtual bool isPolymorphic()
const = 0;
315 virtual Typename getTypename()
const = 0;
320 virtual void setTypename(
const Typename& name) = 0;
345 virtual void fixateType() = 0;
348 virtual Slot* allocateSlot() = 0;
351 virtual void freeSlot(Slot* s) = 0;
362 virtual const Buffer<uint8>& readSerializedValue(Slot* s) = 0;
371 uint8 formatVersion,
bool orLower) = 0;
379 std::list<BinarySerializerCodecPtr>& codecs) = 0;
389 std::list<BinarySerializerCodecPtr>& codecs,
390 uint8 formatVersion,
bool orLower) = 0;
396 virtual void writeSerializedValue(Slot* s,
Buffer<uint8> data) = 0;
405 virtual void readJSON(Slot* s,
JSONValue& oValue) = 0;
422 virtual void writeJSON(Slot* s,
const JSONValue& value) = 0;
467 Slot* requestWriteSlot();
477 bool finishWriteSlot(Slot* n,
bool* dropped=NULL);
483 void discardWriteSlot(Slot* n);
494 Slot* readNewestSlot();
509 void readInterval(
const Time& timestamp, std::size_t nrSlots,
510 std::size_t olderSlots, std::size_t newerSlots,
518 void readInterval(
const Time& from,
const Time& to, std::list<Slot*>& oSlots);
530 template<
typename TargetType>
540 Slot* findFirstOlderSlot(
const Time& timestamp, Slot* start=NULL);
548 void splice(Slot* first, Slot* last, Slot* position)
const;
554 void splice(Slot* item, Slot* position)
const {
555 splice(item,item,position);
558 static bool isEmpty(
const ListItem& list) {
return list.next == &list; }
559 static Slot* begin(ListItem& list) {
return list.next; }
560 static Slot* last(ListItem& list) {
return list.prev; }
561 static Slot* end(ListItem& list) {
return (Slot*)&list; }
564 void deleteSlots(ListItem& list);
577 void resetSlot(Slot* s);
580 Duration mStorageDuration;
581 bool mAutoIncreaseStorageDuration;
582 std::size_t mMinSlots;
583 std::size_t mMaxSlots;
587 mutable boost::mutex mMutex;
605 ListItem mWaitingOrFree;
616 std::string dbgDumpList(ListItem& list,
bool brief);
620 void dbgDump(
const std::string& prefix,
bool brief=
true);
622 static void dbgCheckListIntegrity(
const ListItem& list);
623 void dbgCheckIntegrity();
631 template <
typename T>
648 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
650 std::unique_ptr<ChannelBufferBase::Slot>
clone()
override {
651 auto slot = std::make_unique<Slot>();
654 slot->flags = this->
flags;
655 slot->data = this->
data;
670 return static_cast<Slot*
>(s);
683 MIRA_THROW(XRuntime,
"Cannot set Typename for typed channels");
725 bs.
serialize(slot->data.internalValueRep(),
false);
733 uint8 formatVersion,
bool orLower)
735 uint8 serializerVersion = BinaryBufferSerializer::getSerializerFormatVersion();
737 if (formatVersion == serializerVersion)
740 if (formatVersion == 0) {
752 bs.
serialize(slot->data.internalValueRep(),
false);
758 MIRA_THROW(XIO,
"Requested serialized data of binary format version " << (
int)formatVersion
759 <<
". Only implemented for versions 0, " << (
int)serializerVersion <<
".");
763 std::list<BinarySerializerCodecPtr>& codecs)
777 bs.
serialize(slot->data.internalValueRep(),
false);
779 return std::move(serializedValue);
783 std::list<BinarySerializerCodecPtr>& codecs,
784 uint8 formatVersion,
bool orLower)
786 uint8 serializerVersion = BinaryBufferSerializer::getSerializerFormatVersion();
788 if (formatVersion == serializerVersion)
791 if (formatVersion == 0) {
804 bs.
serialize(slot->data.internalValueRep(),
false);
806 return std::move(serializedValue);
809 MIRA_THROW(XIO,
"Requested serialized data of binary format version " << (
int)formatVersion
810 <<
". Only implemented for versions 0, " << (
int)serializerVersion <<
".");
824 boost::mutex::scoped_lock lock(mCodecsMutex);
827 bs.
deserialize(slot->data.internalValueRep(),
false);
829 boost::mutex::scoped_lock lock(mCodecsMutex);
840 oValue = json.
serialize(slot->data.internalValueRep());
849 oValue = serializer.
serialize(slot->data.internalValueRep());
866 deserializer.
deserialize(slot->data.internalValueRep());
874 const std::string rootTag = *node;
882 boost::mutex mCodecsMutex;
891 template <
typename T>
913 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
915 std::unique_ptr<ChannelBufferBase::Slot>
clone()
override {
916 auto slot = std::make_unique<Slot>();
917 slot->serializedValue = this->serializedValue;
918 slot->timestampPtr = &slot->header.timestamp;
919 slot->flags = this->flags;
920 slot->header = this->header;
932 return static_cast<Slot*
>(s);
948 virtual bool isTyped()
const {
return false; }
951 virtual void setTypename(
const Typename& name);
953 MIRA_THROW(XRuntime,
"Cannot create type meta for untyped channels");
967 Slot* slot = castSlot(s);
987 uint8 formatVersion,
bool orLower)
990 uint8 dataVersion = BinaryBufferDeserializer::getDataFormatVersion(stream);
991 if ((!orLower && (dataVersion != formatVersion)) || (dataVersion > formatVersion)) {
992 MIRA_THROW(XIO,
"Untyped channel contains serialized data of version " << (
int)dataVersion
993 <<
", cannot return data of version " << (
int)formatVersion <<
".");
996 return readSerializedValue(s);
1001 std::list<BinarySerializerCodecPtr>& codecs)
1008 std::list<BinarySerializerCodecPtr>& codecs,
1009 uint8 formatVersion,
bool orLower)
1012 return readSerializedValue(s, formatVersion, orLower);
1047 mMostDerivedClass(NULL),
1048 mFixatedClass(NULL) {}
1057 virtual int getTypeId()
const;
1059 virtual Typename getTypename()
const;
1065 virtual void fixateType();
1071 void promote(
const Class* promotionClass);
1079 #ifndef MIRA_WINDOWS 1090 template <
typename T>
1093 static_assert(std::is_base_of<mira::Object,T>::value,
1094 "In channels you can only use pointers of polymorphic " 1095 "classes that are derived from mira::Object. Pointers to " 1096 "other classes cannot be stored in a channel.");
1102 mMostDerivedClass = &T::CLASS();
1115 template <
typename U>
1116 struct ChannelBufferPromoterCommon
1121 "Can promote untyped channels to typed channels only");
1124 MIRA_THROW(XBadCast,
"Invalid promotion from untyped to typed " 1125 "ChannelBuffer. Typename does not match. ('" <<
1126 buffer->
getTypename() <<
"' != '" << typeName<U>() <<
"'");
1140 for(Slot* s=ChannelBufferBase::begin(buffer->mBuffer);
1141 s!=ChannelBufferBase::end(buffer->mBuffer); s=s->next)
1142 s->lock.lock_shared();
1145 for(Slot* s=ChannelBufferBase::begin(buffer->mBuffer);
1146 s!=ChannelBufferBase::end(buffer->mBuffer); s=s->next)
1160 for(Slot* s=ChannelBufferBase::begin(buffer->mBuffer);
1161 s!=ChannelBufferBase::end(buffer->mBuffer); s=s->next)
1162 s->lock.unlock_shared();
1180 template <
typename U>
1181 struct ChannelBufferPromoter :
public ChannelBufferPromoterCommon<U>
1188 return Base::promoteUntyped(buffer);
1198 template <
typename U>
1206 buffer = Base::promoteUntyped(buffer);
1210 assert(polymorphicBuffer!=NULL &&
1211 "We should never reach here if the buffer to promote is not " 1215 const Class* classU = &U::CLASS();
1218 polymorphicBuffer->
promote(classU);
1220 return polymorphicBuffer;
1226 template<
typename U>
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:852
virtual void freeSlot(ChannelBufferBase::Slot *s)
Called to destroy the slot and free the used memory.
Definition: ChannelBuffer.h:698
void deserialize(T &value)
Definition: JSONSerializer.h:427
boost::shared_mutex lock
Definition: ChannelBuffer.h:168
EIGEN_MAKE_ALIGNED_OPERATOR_NEW std::unique_ptr< ChannelBufferBase::Slot > clone() override
Definition: ChannelBuffer.h:650
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:1076
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:952
boost::mutex serializedValueLock
Lock for writing serializedValue in readSerializedData.
Definition: ChannelBuffer.h:175
const Class * mMostDerivedClass
Definition: ChannelBuffer.h:1075
Include file for all eigen related things.
virtual ~ChannelBuffer()
Definition: ChannelBuffer.h:939
Typed slot derived from ChannelBufferBase::Slot.
Definition: ChannelBuffer.h:645
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
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:1015
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:782
Slot * castSlot(ChannelBufferBase::Slot *s)
Cast an abstract slot to a typed slot.
Definition: ChannelBuffer.h:930
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:693
Specialization for polymorphic types (only classes derived from mira::Object are supported!) ...
Definition: ChannelBuffer.h:1091
ValueType header
Definition: ChannelBuffer.h:912
virtual bool isTyped() const
Returns true, if the channel is typed and false, if it is untyped.
Definition: ChannelBuffer.h:948
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:950
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:1227
TypeMetaPtr getTypeMeta() const
Returns the meta information for the slots type.
Definition: ChannelBuffer.h:331
Base class for typed channel buffers.
Definition: ChannelBuffer.h:632
virtual void fixateType()
Calling this method will fix the type of the ChannelBuffer.
Definition: ChannelBuffer.h:692
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:973
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.
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:732
ChannelBuffer()
Definition: ChannelBuffer.h:1101
A channel buffer for polymorphic types (classes derived from mira::Object)
Definition: ChannelBuffer.h:1039
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:947
virtual ~TypedChannelBufferBase()
Definition: ChannelBuffer.h:640
void setTypeMeta(TypeMetaPtr meta)
Sets the meta information of the slot's type.
Definition: ChannelBuffer.h:336
Binary serializer and deserializer.
PolymorphicChannelBuffer()
Constructor.
Definition: ChannelBuffer.h:1046
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:1000
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:813
virtual bool isPolymorphic() const
Returns true if the channel has a polymorphic type.
Definition: ChannelBuffer.h:949
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:685
Slot * castSlot(ChannelBufferBase::Slot *s)
Cast an abstract slot to a typed slot.
Definition: ChannelBuffer.h:665
virtual StampedHeader & getStampedHeader(ChannelBufferBase::Slot *s)
Returns a reference to the stamped header information.
Definition: ChannelBuffer.h:706
#define MIRA_FRAMEWORK_EXPORT
Definition: FrameworkExports.h:61
Stamped< T * > ValueType
Definition: ChannelBuffer.h:1096
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:680
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:892
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:937
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:986
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:678
virtual void freeSlot(ChannelBufferBase::Slot *s)
Called to destroy the slot and free the used memory.
Definition: ChannelBuffer.h:965
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:1007
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:843
Stamped< T > ValueType
Definition: ChannelBuffer.h:636
void cloneParameters(const ChannelBufferBase &other)
Copies the parameters of other to this.
Definition: ChannelBuffer.h:228
virtual bool isPolymorphic() const
Returns true if the channel has a polymorphic type.
Definition: ChannelBuffer.h:1055
ValueType data
the actual data value, that is stored in this channel's slots
Definition: ChannelBuffer.h:647
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:682
virtual void fixateType()
Calling this method will fix the type of the ChannelBuffer.
Definition: ChannelBuffer.h:957
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:834
virtual const Buffer< uint8 > & readSerializedValue(ChannelBufferBase::Slot *s)
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:981
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:906
virtual bool isTyped() const
Returns true, if the channel is typed and false, if it is untyped.
Definition: ChannelBuffer.h:679
virtual ChannelBufferBase::Slot * allocateSlot()
Creates and returns a new slot.
Definition: ChannelBuffer.h:959
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
EIGEN_MAKE_ALIGNED_OPERATOR_NEW std::unique_ptr< ChannelBufferBase::Slot > clone() override
Definition: ChannelBuffer.h:915
uint32 flags
Used to store internal flags that are especially used by the remote capability of the framework...
Definition: ChannelBuffer.h:194
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:712
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:869
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:762
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:681
ChannelBufferBase()
Constructor.
Definition: ChannelBuffer.h:212
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:861
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