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> 80 template<
typename TargetType>
84 template<
typename TargetType>
134 template<
typename TargetType>
137 template<
typename TargetType>
201 virtual ~Slot() =
default;
220 mMinSlots = other.mMinSlots;
221 mMaxSlots = other.mMaxSlots;
222 mStorageDuration = other.mStorageDuration;
223 mAutoIncreaseStorageDuration = other.mAutoIncreaseStorageDuration;
292 virtual bool isTyped()
const = 0;
380 uint8 formatVersion,
bool orLower) = 0;
499 void readInterval(
const Time& timestamp, std::size_t nrSlots, std::size_t olderSlots,
500 std::size_t newerSlots,
IntervalFillMode fillMode, std::list<Slot*>& oSlots,
501 bool keepStorageDuration);
509 void readInterval(
const Time& from,
const Time& to, std::list<Slot*>& oSlots,
bool keepStorageDuration);
520 template<
typename TargetType>
529 Slot* findFirstOlderSlot(
const Time& timestamp, Slot* start =
nullptr);
536 void splice(Slot* first, Slot* last, Slot* position)
const;
542 void splice(Slot* item, Slot* position)
const 544 splice(item, item, position);
547 static bool isEmpty(
const ListItem& list)
549 return list.next == &list;
551 static Slot* begin(ListItem& list)
555 static Slot* last(ListItem& list)
559 static Slot* end(ListItem& list)
565 void deleteSlots(ListItem& list);
577 void increaseStorageDuration(
const Time& requested);
583 void resetSlot(Slot* s);
587 bool mAutoIncreaseStorageDuration =
true;
588 std::size_t mMinSlots = 0;
589 std::size_t mMaxSlots = 100;
592 mutable boost::mutex mMutex;
594 std::size_t mSize = 0;
610 ListItem mWaitingOrFree;
620 std::string dbgDumpList(ListItem& list,
bool brief);
623 void dbgDump(
const std::string& prefix,
bool brief =
true);
652 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
654 template<bool IsCheap = IsCheapToCopy<T>::value>
657 static_assert(IsCheap,
658 "TypedChannelBufferBase<T>::Slot::clone() must not be called " 659 "with IsCheapToCopy<T>=false");
660 auto slot = std::make_unique<Slot>();
666 slot->flags = this->
flags;
667 slot->data = this->
data;
681 return static_cast<Slot*
>(s);
703 return typeName<T>();
707 MIRA_THROW(XRuntime,
"Cannot set Typename for typed channels");
753 bs.
serialize(slot->data.internalValueRep(),
false);
760 bool orLower)
override 762 uint8 serializerVersion = BinaryBufferSerializer::getSerializerFormatVersion();
764 if (formatVersion == serializerVersion)
767 if (formatVersion == 0) {
779 bs.
serialize(slot->data.internalValueRep(),
false);
786 "Requested serialized data of binary format version " 787 << (
int)formatVersion <<
". Only implemented for versions 0, " 788 << (
int)serializerVersion <<
".");
792 std::list<BinarySerializerCodecPtr>& codecs)
override 806 bs.
serialize(slot->data.internalValueRep(),
false);
808 return std::move(serializedValue);
812 uint8 formatVersion,
bool orLower)
override 814 uint8 serializerVersion = BinaryBufferSerializer::getSerializerFormatVersion();
816 if (formatVersion == serializerVersion)
819 if (formatVersion == 0) {
832 bs.
serialize(slot->data.internalValueRep(),
false);
834 return std::move(serializedValue);
838 "Requested serialized data of binary format version " 839 << (
int)formatVersion <<
". Only implemented for versions 0, " 840 << (
int)serializerVersion <<
".");
854 boost::mutex::scoped_lock lock(mCodecsMutex);
857 bs.
deserialize(slot->data.internalValueRep(),
false);
859 boost::mutex::scoped_lock lock(mCodecsMutex);
870 oValue = json.
serialize(slot->data.internalValueRep());
878 oValue = serializer.
serialize(slot->data.internalValueRep());
895 deserializer.
deserialize(slot->data.internalValueRep());
903 const std::string rootTag = *node;
910 boost::mutex mCodecsMutex;
939 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
948 return static_cast<Slot*
>(s);
983 MIRA_THROW(XRuntime,
"Cannot create type meta for untyped channels");
1009 Slot* slot =
static_cast<Slot*
>(s);
1010 return slot->header;
1019 bool orLower)
override 1022 uint8 dataVersion = BinaryBufferDeserializer::getDataFormatVersion(stream);
1023 if ((!orLower && (dataVersion != formatVersion)) || (dataVersion > formatVersion)) {
1025 "Untyped channel contains serialized data of version " 1026 << (
int)dataVersion <<
", cannot return data of version " << (
int)formatVersion
1034 std::list<BinarySerializerCodecPtr>& codecs)
override 1041 uint8 formatVersion,
bool orLower)
override 1118 template<
typename T>
1121 static_assert(std::is_base_of<mira::Object, T>::value,
1122 "In channels you can only use pointers of polymorphic " 1123 "classes that are derived from mira::Object. Pointers to " 1124 "other classes cannot be stored in a channel.");
1132 mMostDerivedClass = &T::CLASS();
1145 template<
typename U>
1146 struct ChannelBufferPromoterCommon
1150 assert(!buffer->
isTyped() &&
"Can promote untyped channels to typed channels only");
1154 "Invalid promotion from untyped to typed " 1155 "ChannelBuffer. Typename does not match. ('" 1156 << buffer->
getTypename() <<
"' != '" << typeName<U>() <<
"'");
1170 for (Slot* s = ChannelBufferBase::begin(buffer->mBuffer);
1171 s != ChannelBufferBase::end(buffer->mBuffer); s = s->next)
1172 s->lock.lock_shared();
1175 for (Slot* s = ChannelBufferBase::begin(buffer->mBuffer);
1176 s != ChannelBufferBase::end(buffer->mBuffer); s = s->next) {
1189 for (Slot* s = ChannelBufferBase::begin(buffer->mBuffer);
1190 s != ChannelBufferBase::end(buffer->mBuffer); s = s->next)
1191 s->lock.unlock_shared();
1209 template<
typename U>
1210 struct ChannelBufferPromoter :
public ChannelBufferPromoterCommon<U>
1217 return Base::promoteUntyped(buffer);
1227 template<
typename U>
1235 buffer = Base::promoteUntyped(buffer);
1239 assert(polymorphicBuffer !=
nullptr 1240 &&
"We should never reach here if the buffer to promote is not " 1244 const Class* classU = &U::CLASS();
1247 polymorphicBuffer->
promote(classU);
1249 return polymorphicBuffer;
1255 template<
typename U>
Serializer for serializing objects in JSON format.
Definition: JSONSerializer.h:95
void fixateType() override
Calling this method will fix the type of the ChannelBuffer.
Typename getTypename() const override
Returns the Typename of the slot values, in contrast to getTypeId() the Typename is portable and uniq...
Definition: ChannelBuffer.h:701
void fixateType() override
Calling this method will fix the type of the ChannelBuffer.
Definition: ChannelBuffer.h:717
void deserialize(T &value)
Definition: JSONSerializer.h:436
void setStorageDuration(const Duration &storageDuration)
Sets the desired storage duration (the history).
boost::shared_mutex lock
Definition: ChannelBuffer.h:166
const Buffer< uint8 > & readSerializedValue(ChannelBufferBase::Slot *s, uint8 formatVersion, bool orLower) override
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:1018
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:1106
Buffer< uint8 > readSerializedValue(ChannelBufferBase::Slot *s, std::list< BinarySerializerCodecPtr > &codecs) override
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:791
bool isPolymorphic() const override
Returns true if the channel has a polymorphic type.
Definition: ChannelBuffer.h:1083
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:1105
bool isAutoIncreasingStorageDuration() const
Returns whether the storage duration is automatically increased.
Include file for all eigen related things.
ChannelBufferBase::Slot * allocateSlot() override
Creates and returns a new slot.
Definition: ChannelBuffer.h:718
virtual ~ChannelBuffer()
Definition: ChannelBuffer.h:954
Typed slot derived from ChannelBufferBase::Slot.
Definition: ChannelBuffer.h:648
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
ChannelBufferBase::Slot * allocateSlot() override
Creates and returns a new slot.
ChannelBufferBase::Slot * allocateSlot() override
Creates and returns a new slot.
Definition: ChannelBuffer.h:989
Slot * castSlot(ChannelBufferBase::Slot *s)
Cast an abstract slot to a typed slot.
Definition: ChannelBuffer.h:946
void writeSerializedValue(ChannelBufferBase::Slot *s, Buffer< uint8 > data) override
Write the given buffer (that contains binary serialized data WITHOUT the StampedHeader) to the slot...
Definition: ChannelBuffer.h:1047
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...
int getTypeId() const override
Returns typeid of the slot values, the allocator creates.
Definition: ChannelBuffer.h:689
std::string Typename
Definition: Typename.h:60
void writeJSON(ChannelBufferBase::Slot *s, const JSONValue &value) override
Writes data in json representation into the slot, using a default-instantiated JSONDeserializer.
Definition: ChannelBuffer.h:881
Specialization for polymorphic types (only classes derived from mira::Object are supported!) ...
Definition: ChannelBuffer.h:1119
ValueType header
Definition: ChannelBuffer.h:938
Time and Duration wrapper class.
Class object which supports some kind of class reflection.
Definition: Class.h:97
void freeSlot(ChannelBufferBase::Slot *s) override
Called to destroy the slot and free the used memory.
Definition: ChannelBuffer.h:725
virtual void setTypename(const Typename &name)=0
Sets the Typename of the slot values.
ListItem()
Definition: ChannelBuffer.h:149
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:1256
TypeMetaPtr getTypeMeta() const
Returns the meta information for the slots type.
Definition: ChannelBuffer.h:318
Base class for typed channel buffers.
Definition: ChannelBuffer.h:635
void setMinSlots(std::size_t minSlots)
Sets the desired min.
static void dbgCheckListIntegrity(const ListItem &list)
Get compiler and platform independent typenames.
bool isPolymorphic() const override
Returns true if the channel has a polymorphic type.
Definition: ChannelBuffer.h:972
void readJSON(ChannelBufferBase::Slot *s, JSONValue &oValue, JSONSerializer &serializer) override
Reads data of that slot as json representation using the provided JSONSerializer. ...
Definition: ChannelBuffer.h:873
virtual Slot * allocateSlot()=0
Creates and returns a new slot.
const Buffer< uint8 > & readSerializedValue(ChannelBufferBase::Slot *s) override
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:1013
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
Slot * readSlotAtTime(const Time ×tamp, SlotQueryMode mode, bool keepStorageDuration)
Returns a slot at the given time according to mode.
Implements double-linked list item.
Definition: ChannelBuffer.h:147
Const sibling_iterator for iterating over xml nodes that have the same parent (siblings) ...
Definition: XMLDom.h:763
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.
bool isPolymorphic() const override
Returns true if the channel has a polymorphic type.
Definition: ChannelBuffer.h:697
ChannelBuffer()
Definition: ChannelBuffer.h:1130
A channel buffer for polymorphic types (classes derived from mira::Object)
Definition: ChannelBuffer.h:1070
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 ~TypedChannelBufferBase()
Definition: ChannelBuffer.h:641
void setTypeMeta(TypeMetaPtr meta)
Sets the meta information of the slot's type.
Definition: ChannelBuffer.h:326
Binary serializer and deserializer.
PolymorphicChannelBuffer()
Constructor.
Definition: ChannelBuffer.h:1076
void registerCodec(BinarySerializerCodecPtr codec)
Registers the specified codec instance in this binary serializer.
Definition: BinarySerializer.h:756
int getTypeId() const override
Returns typeid of the slot values, the allocator creates.
virtual const Buffer< uint8 > & readSerializedValue(Slot *s)=0
Returns a buffer containing the binary serialized data of the slot.
Buffer< uint8 > readSerializedValue(ChannelBufferBase::Slot *s, std::list< BinarySerializerCodecPtr > &codecs, uint8 formatVersion, bool orLower) override
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:1040
Slot * castSlot(ChannelBufferBase::Slot *s)
Cast an abstract slot to a typed slot.
Definition: ChannelBuffer.h:676
EIGEN_MAKE_ALIGNED_OPERATOR_NEW std::unique_ptr< Slot > clone()
Definition: ChannelBuffer.h:655
Stamped< T * > ValueType
Definition: ChannelBuffer.h:1124
sec_type seconds() const
Returns normalized number of seconds (0..59)
Definition: Time.h:280
Typename getTypename() const override
Returns the Typename of the slot values, in contrast to getTypeId() the Typename is portable and uniq...
Definition: ChannelBuffer.h:976
Core class of the logging library.
Use this class to represent time durations.
Definition: Time.h:106
const CodecsMap & getCodecs() const
Return the map of currently known codecs.
Definition: BinarySerializer.h:1570
void setTypename(const Typename &name) override
Sets the Typename of the slot values.
Definition: ChannelBuffer.h:705
void writeSerializedValue(ChannelBufferBase::Slot *s, Buffer< uint8 > data) override
Write the given buffer (that contains binary serialized data WITHOUT the StampedHeader) to the slot...
Definition: ChannelBuffer.h:843
bool empty() const
Checks if the buffer is empty (used size == 0).
Definition: Buffer.h:299
StampedHeader & getStampedHeader(ChannelBufferBase::Slot *s) override
Returns a reference to the stamped header information.
Definition: ChannelBuffer.h:1004
void freeSlot(ChannelBufferBase::Slot *s) override
Called to destroy the slot and free the used memory.
Typed ChannelBuffer.
Definition: ChannelBuffer.h:919
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.
void freeSlot(ChannelBufferBase::Slot *s) override
Called to destroy the slot and free the used memory.
Definition: ChannelBuffer.h:996
int getTypeId() const override
Returns typeid of the slot values, the allocator creates.
Definition: ChannelBuffer.h:964
virtual StampedHeader & getStampedHeader(Slot *s)=0
Returns a reference to the stamped header information.
ChannelBuffer()
Definition: ChannelBuffer.h:952
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
Mix in for adding a timestamp to data types.
Stamped< T > ValueType
Definition: ChannelBuffer.h:638
ChannelBufferBase()=default
Constructor.
void cloneParameters(const ChannelBufferBase &other)
Copies the parameters of other to this.
Definition: ChannelBuffer.h:218
virtual bool isPolymorphic() const =0
Returns true if the channel has a polymorphic type.
void dbgDump(const std::string &prefix, bool brief=true)
void writeSerializedValue(ChannelBufferBase::Slot *s, Buffer< uint8 > data) override
Write the given buffer (that contains binary serialized data WITHOUT the StampedHeader) to the slot...
void setMaxSlots(std::size_t maxSlots)
Sets the max.
virtual ~ChannelBufferBase()
Buffer< uint8 > readSerializedValue(ChannelBufferBase::Slot *s, std::list< BinarySerializerCodecPtr > &codecs) override
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:1033
ValueType data
the actual data value, that is stored in this channel's slots
Definition: ChannelBuffer.h:651
Buffer< uint8 > readSerializedValue(ChannelBufferBase::Slot *s, std::list< BinarySerializerCodecPtr > &codecs, uint8 formatVersion, bool orLower) override
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:811
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.
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...
bool isTyped() const override
Returns true, if the channel is typed and false, if it is untyped.
Definition: ChannelBuffer.h:693
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.
Buffer< uint8 > serializedValue
The data of this slot as serialized data.
Definition: ChannelBuffer.h:184
const Buffer< uint8 > & readSerializedValue(ChannelBufferBase::Slot *s) override
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:740
const Buffer< uint8 > & readSerializedValue(ChannelBufferBase::Slot *s, uint8 formatVersion, bool orLower) override
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:759
bool finishWriteSlot(Slot *n, bool *dropped=nullptr)
Adds written slot into buffer at the right time position.
StampedHeader & getStampedHeader(ChannelBufferBase::Slot *s) override
Returns a reference to the stamped header information.
Definition: ChannelBuffer.h:733
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:932
void writeJSON(ChannelBufferBase::Slot *s, JSONDeserializer &deserializer) override
Writes data from an initialized json deserializer into the slot.
Definition: ChannelBuffer.h:890
Typename getTypename() const override
Returns the Typename of the slot values, in contrast to getTypeId() the Typename is portable and uniq...
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 fixateType() override
Calling this method will fix the type of the ChannelBuffer.
Definition: ChannelBuffer.h:987
void setAutoIncreaseStorageDuration(bool increase)
Sets whether the storage duration is automatically increased on read accesses.
void readJSON(ChannelBufferBase::Slot *s, JSONValue &oValue) override
Reads data of that slot as json representation using a default-instantiated JSONSerializer.
Definition: ChannelBuffer.h:864
uint32 flags
Used to store internal flags that are especially used by the remote capability of the framework...
Definition: ChannelBuffer.h:192
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
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:898
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
TypeMetaPtr createTypeMeta(ChannelBufferBase::Slot *s, MetaTypeDatabase &ioDB) override
Creates meta information for data in slot and stores all meta information in the given database...
Definition: ChannelBuffer.h:981
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
TypeMetaPtr createTypeMeta(ChannelBufferBase::Slot *s, MetaTypeDatabase &ioDB) override
Creates meta information for data in slot and stores all meta information in the given database...
Definition: ChannelBuffer.h:709
void readInterval(const Time ×tamp, std::size_t nrSlots, std::size_t olderSlots, std::size_t newerSlots, IntervalFillMode fillMode, std::list< Slot *> &oSlots, bool keepStorageDuration)
Reads an interval of slots.
bool isTyped() const override
Returns true, if the channel is typed and false, if it is untyped.
Definition: ChannelBuffer.h:968
virtual void writeJSON(Slot *s, const JSONValue &value)=0
Writes data in json representation into the slot, using a default-instantiated JSONDeserializer.
Serializer and Deserializer for JSON format.
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