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;
210 mStorageDuration(
Duration::seconds(0)),
211 mAutoIncreaseStorageDuration(true),
227 mMinSlots = other.mMinSlots;
228 mMaxSlots = other.mMaxSlots;
229 mStorageDuration = other.mStorageDuration;
230 mAutoIncreaseStorageDuration = other.mAutoIncreaseStorageDuration;
243 void setStorageDuration(
const Duration& storageDuration);
253 void setAutoIncreaseStorageDuration(
bool increase);
264 void setMinSlots(std::size_t minSlots);
276 void setMaxSlots(std::size_t maxSlots);
281 std::size_t getSize()
const;
284 std::size_t getMaxSlots()
const;
287 std::size_t getMinSlots()
const;
290 Duration getStorageDuration()
const;
293 bool isAutoIncreasingStorageDuration()
const;
299 virtual int getTypeId()
const = 0;
302 virtual bool isTyped()
const = 0;
305 virtual bool isPolymorphic()
const = 0;
312 virtual Typename getTypename()
const = 0;
317 virtual void setTypename(
const Typename& name) = 0;
342 virtual void fixateType() = 0;
345 virtual Slot* allocateSlot() = 0;
348 virtual void freeSlot(Slot* s) = 0;
359 virtual const Buffer<uint8>& readSerializedValue(Slot* s) = 0;
368 uint8 formatVersion,
bool orLower) = 0;
376 std::list<BinarySerializerCodecPtr>& codecs) = 0;
386 std::list<BinarySerializerCodecPtr>& codecs,
387 uint8 formatVersion,
bool orLower) = 0;
393 virtual void writeSerializedValue(Slot* s,
Buffer<uint8> data) = 0;
402 virtual void readJSON(Slot* s,
JSONValue& oValue) = 0;
419 virtual void writeJSON(Slot* s,
const JSONValue& value) = 0;
464 Slot* requestWriteSlot();
474 bool finishWriteSlot(Slot* n,
bool* dropped=NULL);
480 void discardWriteSlot(Slot* n);
491 Slot* readNewestSlot();
506 void readInterval(
const Time& timestamp, std::size_t nrSlots,
507 std::size_t olderSlots, std::size_t newerSlots,
515 void readInterval(
const Time& from,
const Time& to, std::list<Slot*>& oSlots);
527 template<
typename TargetType>
537 Slot* findFirstOlderSlot(
const Time& timestamp, Slot* start=NULL);
545 void splice(Slot* first, Slot* last, Slot* position)
const;
551 void splice(Slot* item, Slot* position)
const {
552 splice(item,item,position);
555 static bool isEmpty(
const ListItem& list) {
return list.next == &list; }
556 static Slot* begin(ListItem& list) {
return list.next; }
557 static Slot* last(ListItem& list) {
return list.prev; }
558 static Slot* end(ListItem& list) {
return (Slot*)&list; }
561 void deleteSlots(ListItem& list);
574 void resetSlot(Slot* s);
577 Duration mStorageDuration;
578 bool mAutoIncreaseStorageDuration;
579 std::size_t mMinSlots;
580 std::size_t mMaxSlots;
584 mutable boost::mutex mMutex;
602 ListItem mWaitingOrFree;
613 std::string dbgDumpList(ListItem& list,
bool brief);
617 void dbgDump(
const std::string& prefix,
bool brief=
true);
619 static void dbgCheckListIntegrity(
const ListItem& list);
620 void dbgCheckIntegrity();
628 template <
typename T>
645 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
658 return static_cast<Slot*
>(s);
671 MIRA_THROW(XRuntime,
"Cannot set Typename for typed channels");
713 bs.
serialize(slot->data.internalValueRep(),
false);
721 uint8 formatVersion,
bool orLower)
723 uint8 serializerVersion = BinaryBufferSerializer::getSerializerFormatVersion();
725 if (formatVersion == serializerVersion)
728 if (formatVersion == 0) {
740 bs.
serialize(slot->data.internalValueRep(),
false);
746 MIRA_THROW(XIO,
"Requested serialized data of binary format version " << (
int)formatVersion
747 <<
". Only implemented for versions 0, " << (
int)serializerVersion <<
".");
751 std::list<BinarySerializerCodecPtr>& codecs)
765 bs.
serialize(slot->data.internalValueRep(),
false);
767 return std::move(serializedValue);
771 std::list<BinarySerializerCodecPtr>& codecs,
772 uint8 formatVersion,
bool orLower)
774 uint8 serializerVersion = BinaryBufferSerializer::getSerializerFormatVersion();
776 if (formatVersion == serializerVersion)
779 if (formatVersion == 0) {
792 bs.
serialize(slot->data.internalValueRep(),
false);
794 return std::move(serializedValue);
797 MIRA_THROW(XIO,
"Requested serialized data of binary format version " << (
int)formatVersion
798 <<
". Only implemented for versions 0, " << (
int)serializerVersion <<
".");
812 boost::mutex::scoped_lock lock(mCodecsMutex);
815 bs.
deserialize(slot->data.internalValueRep(),
false);
817 boost::mutex::scoped_lock lock(mCodecsMutex);
828 oValue = json.
serialize(slot->data.internalValueRep());
837 oValue = serializer.
serialize(slot->data.internalValueRep());
854 deserializer.
deserialize(slot->data.internalValueRep());
862 const std::string rootTag = *node;
870 boost::mutex mCodecsMutex;
879 template <
typename T>
901 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
914 return static_cast<Slot*
>(s);
930 virtual bool isTyped()
const {
return false; }
933 virtual void setTypename(
const Typename& name);
935 MIRA_THROW(XRuntime,
"Cannot create type meta for untyped channels");
949 Slot* slot = castSlot(s);
969 uint8 formatVersion,
bool orLower)
972 uint8 dataVersion = BinaryBufferDeserializer::getDataFormatVersion(stream);
973 if ((!orLower && (dataVersion != formatVersion)) || (dataVersion > formatVersion)) {
974 MIRA_THROW(XIO,
"Untyped channel contains serialized data of version " << (
int)dataVersion
975 <<
", cannot return data of version " << (
int)formatVersion <<
".");
978 return readSerializedValue(s);
983 std::list<BinarySerializerCodecPtr>& codecs)
990 std::list<BinarySerializerCodecPtr>& codecs,
991 uint8 formatVersion,
bool orLower)
994 return readSerializedValue(s, formatVersion, orLower);
1029 mMostDerivedClass(NULL),
1030 mFixatedClass(NULL) {}
1039 virtual int getTypeId()
const;
1041 virtual Typename getTypename()
const;
1047 virtual void fixateType();
1053 void promote(
const Class* promotionClass);
1061 #ifndef MIRA_WINDOWS 1072 template <
typename T>
1075 static_assert(std::is_base_of<mira::Object,T>::value,
1076 "In channels you can only use pointers of polymorphic " 1077 "classes that are derived from mira::Object. Pointers to " 1078 "other classes cannot be stored in a channel.");
1084 mMostDerivedClass = &T::CLASS();
1097 template <
typename U>
1098 struct ChannelBufferPromoterCommon
1103 "Can promote untyped channels to typed channels only");
1106 MIRA_THROW(XBadCast,
"Invalid promotion from untyped to typed " 1107 "ChannelBuffer. Typename does not match. ('" <<
1108 buffer->
getTypename() <<
"' != '" << typeName<U>() <<
"'");
1122 for(Slot* s=ChannelBufferBase::begin(buffer->mBuffer);
1123 s!=ChannelBufferBase::end(buffer->mBuffer); s=s->next)
1124 s->lock.lock_shared();
1127 for(Slot* s=ChannelBufferBase::begin(buffer->mBuffer);
1128 s!=ChannelBufferBase::end(buffer->mBuffer); s=s->next)
1142 for(Slot* s=ChannelBufferBase::begin(buffer->mBuffer);
1143 s!=ChannelBufferBase::end(buffer->mBuffer); s=s->next)
1144 s->lock.unlock_shared();
1162 template <
typename U>
1163 struct ChannelBufferPromoter :
public ChannelBufferPromoterCommon<U>
1170 return Base::promoteUntyped(buffer);
1180 template <
typename U>
1188 buffer = Base::promoteUntyped(buffer);
1192 assert(polymorphicBuffer!=NULL &&
1193 "We should never reach here if the buffer to promote is not " 1197 const Class* classU = &U::CLASS();
1200 polymorphicBuffer->
promote(classU);
1202 return polymorphicBuffer;
1208 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:840
virtual void freeSlot(ChannelBufferBase::Slot *s)
Called to destroy the slot and free the used memory.
Definition: ChannelBuffer.h:686
void deserialize(T &value)
Definition: JSONSerializer.h:427
boost::shared_mutex lock
Definition: ChannelBuffer.h:168
Definition: BinarySerializer.h:324
const Time * timestampPtr
Pointer to the actual timestamp in the slot data for easy access.
Definition: ChannelBuffer.h:180
const Class * mFixatedClass
Definition: ChannelBuffer.h:1058
virtual TypeMetaPtr createTypeMeta(ChannelBufferBase::Slot *s, MetaTypeDatabase &ioDB)
Creates meta information for data in slot and stores all meta information in the given database...
Definition: ChannelBuffer.h:934
boost::mutex serializedValueLock
Lock for writing serializedValue in readSerializedData.
Definition: ChannelBuffer.h:175
const Class * mMostDerivedClass
Definition: ChannelBuffer.h:1057
Include file for all eigen related things.
static Slot * castSlot(ChannelBufferBase::Slot *s)
Cast an abstract slot to a typed slot.
Definition: ChannelBuffer.h:909
virtual ~ChannelBuffer()
Definition: ChannelBuffer.h:921
Typed slot derived from ChannelBufferBase::Slot.
Definition: ChannelBuffer.h:642
void promote(const Class *promotionClass)
Promotes the polymorphic type of the channel to the given class type.
The first slot with timestamp < requested will be chosen.
Definition: ChannelBuffer.h:98
static Slot * castSlot(ChannelBufferBase::Slot *s)
Cast an abstract slot to a typed slot.
Definition: ChannelBuffer.h:653
virtual void writeSerializedValue(ChannelBufferBase::Slot *s, Buffer< uint8 > data)
Write the given buffer (that contains binary serialized data WITHOUT the StampedHeader) to the slot...
Definition: ChannelBuffer.h:997
virtual Buffer< uint8 > readSerializedValue(ChannelBufferBase::Slot *s, std::list< BinarySerializerCodecPtr > &codecs, uint8 formatVersion, bool orLower)
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:770
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
std::string Typename
Definition: Typename.h:60
virtual ChannelBufferBase::Slot * allocateSlot()
Creates and returns a new slot.
Definition: ChannelBuffer.h:681
Specialization for polymorphic types (only classes derived from mira::Object are supported!) ...
Definition: ChannelBuffer.h:1073
ValueType header
Definition: ChannelBuffer.h:900
virtual bool isTyped() const
Returns true, if the channel is typed and false, if it is untyped.
Definition: ChannelBuffer.h:930
Time and Duration wrapper class.
Class object which supports some kind of class reflection.
Definition: Class.h:97
virtual Typename getTypename() const
Returns the Typename of the slot values, in contrast to getTypeId() the Typename is portable and uniq...
Definition: ChannelBuffer.h:932
ListItem()
Definition: ChannelBuffer.h:152
Stamped class specialization for polymorphic pointers.
Definition: Stamped.h:238
StampedHeader & stampedHeader(Stamped< T > &data)
Helper to extract the header from stamped data.
Definition: Stamped.h:482
ChannelBufferBase * promote()
Promotes this buffer to the specified target type (if required).
Definition: ChannelBuffer.h:1209
TypeMetaPtr getTypeMeta() const
Returns the meta information for the slots type.
Definition: ChannelBuffer.h:328
Base class for typed channel buffers.
Definition: ChannelBuffer.h:629
virtual void fixateType()
Calling this method will fix the type of the ChannelBuffer.
Definition: ChannelBuffer.h:680
Get compiler and platform independent typenames.
Framework export macro declaration.
void serialize(const std::string &name, const T &value, const std::string &comment="")
Serializes the specified object value under the given name.
Definition: Serializer.h:204
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:82
void serialize(const T &value, bool enableTypeCheck=true)
Provides a special serialize interface for the BinarySerializer.
Definition: BinarySerializer.h:598
Implements double-linked list item.
Definition: ChannelBuffer.h:150
Const sibling_iterator for iterating over xml nodes that have the same parent (siblings) ...
Definition: XMLDom.h:671
virtual StampedHeader & getStampedHeader(ChannelBufferBase::Slot *s)
Returns a reference to the stamped header information.
Definition: ChannelBuffer.h:955
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:416
Slot * requestWriteSlot()
Returns a slot for writing, where the shared_mutex is already write locked.
virtual const Buffer< uint8 > & readSerializedValue(ChannelBufferBase::Slot *s, uint8 formatVersion, bool orLower)
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:720
ChannelBuffer()
Definition: ChannelBuffer.h:1083
A channel buffer for polymorphic types (classes derived from mira::Object)
Definition: ChannelBuffer.h:1021
Deserializer for serializing objects from JSON format.
Definition: JSONSerializer.h:400
virtual int getTypeId() const
Returns typeid of the slot values, the allocator creates.
Definition: ChannelBuffer.h:929
virtual ~TypedChannelBufferBase()
Definition: ChannelBuffer.h:637
void setTypeMeta(TypeMetaPtr meta)
Sets the meta information of the slot's type.
Definition: ChannelBuffer.h:333
Binary serializer and deserializer.
PolymorphicChannelBuffer()
Constructor.
Definition: ChannelBuffer.h:1028
void registerCodec(BinarySerializerCodecPtr codec)
Registers the specified codec instance in this binary serializer.
Definition: BinarySerializer.h:764
virtual Buffer< uint8 > readSerializedValue(ChannelBufferBase::Slot *s, std::list< BinarySerializerCodecPtr > &codecs)
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:982
virtual void writeSerializedValue(ChannelBufferBase::Slot *s, Buffer< uint8 > data)
Write the given buffer (that contains binary serialized data WITHOUT the StampedHeader) to the slot...
Definition: ChannelBuffer.h:801
virtual bool isPolymorphic() const
Returns true if the channel has a polymorphic type.
Definition: ChannelBuffer.h:931
virtual TypeMetaPtr createTypeMeta(ChannelBufferBase::Slot *s, MetaTypeDatabase &ioDB)
Creates meta information for data in slot and stores all meta information in the given database...
Definition: ChannelBuffer.h:673
virtual StampedHeader & getStampedHeader(ChannelBufferBase::Slot *s)
Returns a reference to the stamped header information.
Definition: ChannelBuffer.h:694
#define MIRA_FRAMEWORK_EXPORT
Definition: FrameworkExports.h:61
Stamped< T * > ValueType
Definition: ChannelBuffer.h:1078
Core class of the logging library.
Use this class to represent time durations.
Definition: Time.h:104
virtual bool isPolymorphic() const
Returns true if the channel has a polymorphic type.
Definition: ChannelBuffer.h:668
const CodecsMap & getCodecs() const
Return the map of currently known codecs.
Definition: BinarySerializer.h:1581
bool empty() const
Checks if the buffer is empty (used size == 0).
Definition: Buffer.h:303
Typed ChannelBuffer.
Definition: ChannelBuffer.h:880
The slot with smallest time difference to the requested will be chosen.
Definition: ChannelBuffer.h:104
virtual StampedHeader & getStampedHeader(Slot *s)=0
Returns a reference to the stamped header information.
ChannelBuffer()
Definition: ChannelBuffer.h:919
virtual const Buffer< uint8 > & readSerializedValue(ChannelBufferBase::Slot *s, uint8 formatVersion, bool orLower)
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:968
Mix in for adding a time stamp, an optional frame id and an optional sequence id to data types like P...
Definition: Stamped.h:149
boost::shared_ptr< TypeMeta > TypeMetaPtr
Definition: MetaSerializer.h:309
virtual int getTypeId() const
Returns typeid of the slot values, the allocator creates.
Definition: ChannelBuffer.h:666
virtual void freeSlot(ChannelBufferBase::Slot *s)
Called to destroy the slot and free the used memory.
Definition: ChannelBuffer.h:947
Mix in for adding a timestamp to data types.
virtual Buffer< uint8 > readSerializedValue(ChannelBufferBase::Slot *s, std::list< BinarySerializerCodecPtr > &codecs, uint8 formatVersion, bool orLower)
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:989
virtual void readJSON(ChannelBufferBase::Slot *s, JSONValue &oValue, JSONSerializer &serializer)
Reads data of that slot as json representation using the provided JSONSerializer. ...
Definition: ChannelBuffer.h:831
Stamped< T > ValueType
Definition: ChannelBuffer.h:633
void cloneParameters(const ChannelBufferBase &other)
Copies the parameters of other to this.
Definition: ChannelBuffer.h:225
virtual bool isPolymorphic() const
Returns true if the channel has a polymorphic type.
Definition: ChannelBuffer.h:1037
ValueType data
the actual data value, that is stored in this channel's slots
Definition: ChannelBuffer.h:644
Base class that manages the slots of channels by providing read and write access to them...
Definition: ChannelBuffer.h:134
const Time & timestamp() const
Definition: ChannelBuffer.h:196
Container for storing a single data element in the linked list.
Definition: ChannelBuffer.h:166
bool finishWriteSlot(Slot *n, bool *dropped=NULL)
Adds written slot into buffer at the right time position.
void setCodecs(const CodecsMap &codecs)
Set the map of known codecs.
Definition: BinarySerializer.h:1586
virtual void setTypename(const Typename &name)
Sets the Typename of the slot values.
Definition: ChannelBuffer.h:670
virtual void fixateType()
Calling this method will fix the type of the ChannelBuffer.
Definition: ChannelBuffer.h:939
Input stream adapter that can be assigned to any input stream and allows binary input using the >> st...
Definition: BinaryStream.h:523
Slot * next
Definition: ChannelBuffer.h:159
Buffer< uint8 > serializedValue
The data of this slot as serialized data.
Definition: ChannelBuffer.h:186
virtual void readJSON(ChannelBufferBase::Slot *s, JSONValue &oValue)
Reads data of that slot as json representation using a default-instantiated JSONSerializer.
Definition: ChannelBuffer.h:822
virtual const Buffer< uint8 > & readSerializedValue(ChannelBufferBase::Slot *s)
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:963
void deserialize(T &value, bool enableTypeCheck=true, bool recursive=false)
Provides a special deserialize interface for the BinaryDeserializer.
Definition: BinarySerializer.h:1340
StampedHeader ValueType
Definition: ChannelBuffer.h:894
virtual bool isTyped() const
Returns true, if the channel is typed and false, if it is untyped.
Definition: ChannelBuffer.h:667
virtual ChannelBufferBase::Slot * allocateSlot()
Creates and returns a new slot.
Definition: ChannelBuffer.h:941
XMLSerializer and XMLDeserializer.
Prefer filling the interval with slots with timestamp > requested.
Definition: ChannelBuffer.h:116
Prefer filling the interval with slots with timestamp < requested.
Definition: ChannelBuffer.h:114
uint32 flags
Used to store internal flags that are especially used by the remote capability of the framework...
Definition: ChannelBuffer.h:194
void deserializeFromNode(const char *name, T &value)
In contrast to the deserialize() method this method will use the node that was specified in the const...
Definition: XMLSerializer.h:358
IntervalFillMode
Mode that is used to determine what slots should be added to the interval when not enough slots are a...
Definition: ChannelBuffer.h:111
virtual const Buffer< uint8 > & readSerializedValue(ChannelBufferBase::Slot *s)
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:700
void writeXML(ChannelBufferBase::Slot *s, const XMLDom::const_iterator &node) override
Writes data in xml representation into the slot, using a default-instantiated XMLDeserializer and the...
Definition: ChannelBuffer.h:857
virtual Buffer< uint8 > readSerializedValue(ChannelBufferBase::Slot *s, std::list< BinarySerializerCodecPtr > &codecs)
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:750
virtual Typename getTypename() const =0
Returns the Typename of the slot values, in contrast to getTypeId() the Typename is portable and uniq...
SlotQueryMode
Mode that is used to determine the slot obtained from a channel when no slot exists at the exact time...
Definition: ChannelBuffer.h:95
json::Value JSONValue
Imports the json::Value type into mira namespace.
Definition: JSON.h:363
void clear()
Deletes all slots in all lists and therefore clears the whole buffer.
Provides method for generating a unique id for any type.
Slot * prev
Definition: ChannelBuffer.h:160
virtual Typename getTypename() const
Returns the Typename of the slot values, in contrast to getTypeId() the Typename is portable and uniq...
Definition: ChannelBuffer.h:669
ChannelBufferBase()
Constructor.
Definition: ChannelBuffer.h:209
Serializer and Deserializer for JSON format.
virtual void writeJSON(ChannelBufferBase::Slot *s, JSONDeserializer &deserializer)
Writes data from an initialized json deserializer into the slot.
Definition: ChannelBuffer.h:849
virtual bool isTyped() const =0
Returns true, if the channel is typed and false, if it is untyped.
The first slot with timestamp > requested will be chosen.
Definition: ChannelBuffer.h:101
std::map< TypeId, BinarySerializerCodecPtr > CodecsMap
A map of binary serialization codecs.
Definition: BinarySerializer.h:1270
boost::shared_ptr< BinarySerializerCodec > BinarySerializerCodecPtr
Shared pointer of BinarySerializerCodec.
Definition: BinarySerializerCodec.h:64
Deserializer for serializing objects from XML format.
Definition: XMLSerializer.h:313