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>
81 template <
typename TargetType>
85 template <
typename TargetType>
135 template <
typename TargetType>
138 template <
typename TargetType>
196 assert(timestampPtr!=NULL);
197 return *timestampPtr;
209 mStorageDuration(
Duration::seconds(0)),
210 mAutoIncreaseStorageDuration(true),
226 mMinSlots = other.mMinSlots;
227 mMaxSlots = other.mMaxSlots;
228 mStorageDuration = other.mStorageDuration;
229 mAutoIncreaseStorageDuration = other.mAutoIncreaseStorageDuration;
242 void setStorageDuration(
const Duration& storageDuration);
252 void setAutoIncreaseStorageDuration(
bool increase);
263 void setMinSlots(std::size_t minSlots);
275 void setMaxSlots(std::size_t maxSlots);
280 std::size_t getSize()
const;
283 std::size_t getMaxSlots()
const;
286 std::size_t getMinSlots()
const;
289 Duration getStorageDuration()
const;
292 bool isAutoIncreasingStorageDuration()
const;
298 virtual int getTypeId()
const = 0;
301 virtual bool isTyped()
const = 0;
304 virtual bool isPolymorphic()
const = 0;
311 virtual Typename getTypename()
const = 0;
316 virtual void setTypename(
const Typename& name) = 0;
341 virtual void fixateType() = 0;
344 virtual Slot* allocateSlot() = 0;
347 virtual void freeSlot(Slot* s) = 0;
358 virtual const Buffer<uint8>& readSerializedValue(Slot* s) = 0;
367 uint8 formatVersion,
bool orLower) = 0;
375 std::list<BinarySerializerCodecPtr>& codecs) = 0;
385 std::list<BinarySerializerCodecPtr>& codecs,
386 uint8 formatVersion,
bool orLower) = 0;
392 virtual void writeSerializedValue(Slot* s,
Buffer<uint8> data) = 0;
401 virtual void readJSON(Slot* s,
JSONValue& oValue) = 0;
418 virtual void writeJSON(Slot* s,
const JSONValue& value) = 0;
455 Slot* requestWriteSlot();
465 bool finishWriteSlot(Slot* n,
bool* dropped=NULL);
471 void discardWriteSlot(Slot* n);
482 Slot* readNewestSlot();
496 void readInterval(
const Time& timestamp, std::size_t nrSlots,
497 std::size_t olderSlots, std::size_t newerSlots,
504 void readInterval(
const Time& from,
const Time& to, std::list<Slot*>& oSlots);
516 template<
typename TargetType>
526 Slot* findFirstOlderSlot(
const Time& timestamp, Slot* start=NULL);
534 void splice(Slot* first, Slot* last, Slot* position)
const;
540 void splice(Slot* item, Slot* position)
const {
541 splice(item,item,position);
544 static bool isEmpty(
const ListItem& list) {
return list.next == &list; }
545 static Slot* begin(ListItem& list) {
return list.next; }
546 static Slot* last(ListItem& list) {
return list.prev; }
547 static Slot* end(ListItem& list) {
return (Slot*)&list; }
550 void deleteSlots(ListItem& list);
563 void resetSlot(Slot* s);
566 Duration mStorageDuration;
567 bool mAutoIncreaseStorageDuration;
568 std::size_t mMinSlots;
569 std::size_t mMaxSlots;
573 mutable boost::mutex mMutex;
591 ListItem mWaitingOrFree;
602 std::string dbgDumpList(ListItem& list,
bool brief);
606 void dbgDump(
const std::string& prefix,
bool brief=
true);
608 static void dbgCheckListIntegrity(
const ListItem& list);
609 void dbgCheckIntegrity();
617 template <
typename T>
634 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
647 return static_cast<Slot*
>(s);
660 MIRA_THROW(XRuntime,
"Cannot set Typename for typed channels");
702 bs.
serialize(slot->data.internalValueRep(),
false);
710 uint8 formatVersion,
bool orLower)
712 uint8 serializerVersion = BinaryBufferSerializer::getSerializerFormatVersion();
714 if (formatVersion == serializerVersion)
717 if (formatVersion == 0) {
729 bs.
serialize(slot->data.internalValueRep(),
false);
735 MIRA_THROW(XIO,
"Requested serialized data of binary format version " << (
int)formatVersion
736 <<
". Only implemented for versions 0, " << (
int)serializerVersion <<
".");
740 std::list<BinarySerializerCodecPtr>& codecs)
754 bs.
serialize(slot->data.internalValueRep(),
false);
756 return std::move(serializedValue);
760 std::list<BinarySerializerCodecPtr>& codecs,
761 uint8 formatVersion,
bool orLower)
763 uint8 serializerVersion = BinaryBufferSerializer::getSerializerFormatVersion();
765 if (formatVersion == serializerVersion)
768 if (formatVersion == 0) {
781 bs.
serialize(slot->data.internalValueRep(),
false);
783 return std::move(serializedValue);
786 MIRA_THROW(XIO,
"Requested serialized data of binary format version " << (
int)formatVersion
787 <<
". Only implemented for versions 0, " << (
int)serializerVersion <<
".");
801 boost::mutex::scoped_lock lock(mCodecsMutex);
804 bs.
deserialize(slot->data.internalValueRep(),
false);
806 boost::mutex::scoped_lock lock(mCodecsMutex);
817 oValue = json.
serialize(slot->data.internalValueRep());
826 oValue = serializer.
serialize(slot->data.internalValueRep());
843 deserializer.
deserialize(slot->data.internalValueRep());
850 boost::mutex mCodecsMutex;
859 template <
typename T>
881 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
894 return static_cast<Slot*
>(s);
910 virtual bool isTyped()
const {
return false; }
913 virtual void setTypename(
const Typename& name);
915 MIRA_THROW(XRuntime,
"Cannot create type meta for untyped channels");
929 Slot* slot = castSlot(s);
949 uint8 formatVersion,
bool orLower)
952 uint8 dataVersion = BinaryBufferDeserializer::getDataFormatVersion(stream);
953 if ((!orLower && (dataVersion != formatVersion)) || (dataVersion > formatVersion)) {
954 MIRA_THROW(XIO,
"Untyped channel contains serialized data of version " << (
int)dataVersion
955 <<
", cannot return data of version " << (
int)formatVersion <<
".");
958 return readSerializedValue(s);
963 std::list<BinarySerializerCodecPtr>& codecs)
970 std::list<BinarySerializerCodecPtr>& codecs,
971 uint8 formatVersion,
bool orLower)
974 return readSerializedValue(s, formatVersion, orLower);
1007 mMostDerivedClass(NULL),
1008 mFixatedClass(NULL) {}
1017 virtual int getTypeId()
const;
1019 virtual Typename getTypename()
const;
1025 virtual void fixateType();
1031 void promote(
const Class* promotionClass);
1039 #ifndef MIRA_WINDOWS
1050 template <
typename T>
1053 static_assert(std::is_base_of<mira::Object,T>::value,
1054 "In channels you can only use pointers of polymorphic "
1055 "classes that are derived from mira::Object. Pointers to "
1056 "other classes cannot be stored in a channel.");
1062 mMostDerivedClass = &T::CLASS();
1075 template <
typename U>
1076 struct ChannelBufferPromoterCommon
1081 "Can promote untyped channels to typed channels only");
1084 MIRA_THROW(XBadCast,
"Invalid promotion from untyped to typed "
1085 "ChannelBuffer. Typename does not match. ('" <<
1086 buffer->
getTypename() <<
"' != '" << typeName<U>() <<
"'");
1100 for(Slot* s=ChannelBufferBase::begin(buffer->mBuffer);
1101 s!=ChannelBufferBase::end(buffer->mBuffer); s=s->next)
1102 s->lock.lock_shared();
1105 for(Slot* s=ChannelBufferBase::begin(buffer->mBuffer);
1106 s!=ChannelBufferBase::end(buffer->mBuffer); s=s->next)
1120 for(Slot* s=ChannelBufferBase::begin(buffer->mBuffer);
1121 s!=ChannelBufferBase::end(buffer->mBuffer); s=s->next)
1122 s->lock.unlock_shared();
1140 template <
typename U>
1141 struct ChannelBufferPromoter :
public ChannelBufferPromoterCommon<U>
1148 return Base::promoteUntyped(buffer);
1158 template <
typename U>
1166 buffer = Base::promoteUntyped(buffer);
1170 assert(polymorphicBuffer!=NULL &&
1171 "We should never reach here if the buffer to promote is not "
1175 const Class* classU = &U::CLASS();
1178 polymorphicBuffer->
promote(classU);
1180 return polymorphicBuffer;
1186 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:829
virtual void freeSlot(ChannelBufferBase::Slot *s)
Called to destroy the slot and free the used memory.
Definition: ChannelBuffer.h:675
void deserialize(T &value)
Definition: JSONSerializer.h:414
boost::shared_mutex lock
Definition: ChannelBuffer.h:167
Definition: BinarySerializer.h:324
const Time * timestampPtr
Pointer to the actual timestamp in the slot data for easy access.
Definition: ChannelBuffer.h:179
const Class * mFixatedClass
Definition: ChannelBuffer.h:1036
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:914
boost::mutex serializedValueLock
Lock for writing serializedValue in readSerializedData.
Definition: ChannelBuffer.h:174
const Class * mMostDerivedClass
Definition: ChannelBuffer.h:1035
Include file for all eigen related things.
static Slot * castSlot(ChannelBufferBase::Slot *s)
Cast an abstract slot to a typed slot.
Definition: ChannelBuffer.h:889
virtual ~ChannelBuffer()
Definition: ChannelBuffer.h:901
Typed slot derived from ChannelBufferBase::Slot.
Definition: ChannelBuffer.h:631
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:97
static Slot * castSlot(ChannelBufferBase::Slot *s)
Cast an abstract slot to a typed slot.
Definition: ChannelBuffer.h:642
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:977
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:759
std::string Typename
Definition: Typename.h:59
virtual ChannelBufferBase::Slot * allocateSlot()
Creates and returns a new slot.
Definition: ChannelBuffer.h:670
Specialization for polymorphic types (only classes derived from mira::Object are supported!) ...
Definition: ChannelBuffer.h:1051
ValueType header
Definition: ChannelBuffer.h:880
Time and Duration wrapper class.
Class object which supports some kind of class reflection.
Definition: Class.h:97
ListItem()
Definition: ChannelBuffer.h:151
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:1187
Base class for typed channel buffers.
Definition: ChannelBuffer.h:618
virtual void fixateType()
Calling this method will fix the type of the ChannelBuffer.
Definition: ChannelBuffer.h:669
virtual int getTypeId() const
Returns typeid of the slot values, the allocator creates.
Definition: ChannelBuffer.h:909
Get compiler and platform independent typenames.
bool empty() const
Checks if the buffer is empty (used size == 0).
Definition: Buffer.h:303
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:91
void serialize(const T &value, bool enableTypeCheck=true)
Provides a special serialize interface for the BinarySerializer.
Definition: BinarySerializer.h:598
virtual Typename getTypename() const
Returns the Typename of the slot values, in contrast to getTypeId() the Typename is portable and uniq...
Definition: ChannelBuffer.h:912
virtual bool isTyped() const
Returns true, if the channel is typed and false, if it is untyped.
Definition: ChannelBuffer.h:656
TypeMetaPtr getTypeMeta() const
Returns the meta information for the slots type.
Definition: ChannelBuffer.h:327
Implements double-linked list item.
Definition: ChannelBuffer.h:149
virtual StampedHeader & getStampedHeader(ChannelBufferBase::Slot *s)
Returns a reference to the stamped header information.
Definition: ChannelBuffer.h:935
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:421
Slot * requestWriteSlot()
Returns a slot for writing, where the shared_mutex is already write locked.
virtual bool isPolymorphic() const
Returns true if the channel has a polymorphic type.
Definition: ChannelBuffer.h:657
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:709
ChannelBuffer()
Definition: ChannelBuffer.h:1061
A channel buffer for polymorphic types (classes derived from mira::Object)
Definition: ChannelBuffer.h:999
Deserializer for serializing objects from JSON format.
Definition: JSONSerializer.h:387
virtual ~TypedChannelBufferBase()
Definition: ChannelBuffer.h:626
virtual bool isTyped() const
Returns true, if the channel is typed and false, if it is untyped.
Definition: ChannelBuffer.h:910
void setTypeMeta(TypeMetaPtr meta)
Sets the meta information of the slot's type.
Definition: ChannelBuffer.h:332
virtual bool isPolymorphic() const
Returns true if the channel has a polymorphic type.
Definition: ChannelBuffer.h:911
Binary serializer and deserializer.
PolymorphicChannelBuffer()
Constructor.
Definition: ChannelBuffer.h:1006
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:962
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:790
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:662
virtual StampedHeader & getStampedHeader(ChannelBufferBase::Slot *s)
Returns a reference to the stamped header information.
Definition: ChannelBuffer.h:683
#define MIRA_FRAMEWORK_EXPORT
Definition: FrameworkExports.h:61
Stamped< T * > ValueType
Definition: ChannelBuffer.h:1056
Core class of the logging library.
Use this class to represent time durations.
Definition: Time.h:104
Typed ChannelBuffer.
Definition: ChannelBuffer.h:860
The slot with smallest time difference to the requested will be chosen.
Definition: ChannelBuffer.h:103
virtual StampedHeader & getStampedHeader(Slot *s)=0
Returns a reference to the stamped header information.
ChannelBuffer()
Definition: ChannelBuffer.h:899
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:948
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 void freeSlot(ChannelBufferBase::Slot *s)
Called to destroy the slot and free the used memory.
Definition: ChannelBuffer.h:927
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:969
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:820
Stamped< T > ValueType
Definition: ChannelBuffer.h:622
virtual bool isPolymorphic() const
Returns true if the channel has a polymorphic type.
Definition: ChannelBuffer.h:1015
void cloneParameters(const ChannelBufferBase &other)
Copies the parameters of other to this.
Definition: ChannelBuffer.h:224
ValueType data
the actual data value, that is stored in this channel's slots
Definition: ChannelBuffer.h:633
Base class that manages the slots of channels by providing read and write access to them...
Definition: ChannelBuffer.h:133
Container for storing a single data element in the linked list.
Definition: ChannelBuffer.h:165
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:1578
virtual void setTypename(const Typename &name)
Sets the Typename of the slot values.
Definition: ChannelBuffer.h:659
virtual void fixateType()
Calling this method will fix the type of the ChannelBuffer.
Definition: ChannelBuffer.h:919
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:158
Buffer< uint8 > serializedValue
The data of this slot as serialized data.
Definition: ChannelBuffer.h:185
virtual void readJSON(ChannelBufferBase::Slot *s, JSONValue &oValue)
Reads data of that slot as json representation using a default-instantiated JSONSerializer.
Definition: ChannelBuffer.h:811
virtual const Buffer< uint8 > & readSerializedValue(ChannelBufferBase::Slot *s)
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:943
void deserialize(T &value, bool enableTypeCheck=true, bool recursive=false)
Provides a special deserialize interface for the BinaryDeserializer.
Definition: BinarySerializer.h:1332
StampedHeader ValueType
Definition: ChannelBuffer.h:874
const CodecsMap & getCodecs() const
Return the map of currently known codecs.
Definition: BinarySerializer.h:1573
virtual ChannelBufferBase::Slot * allocateSlot()
Creates and returns a new slot.
Definition: ChannelBuffer.h:921
Prefer filling the interval with slots with timestamp > requested.
Definition: ChannelBuffer.h:115
Prefer filling the interval with slots with timestamp < requested.
Definition: ChannelBuffer.h:113
uint32 flags
Used to store internal flags that are especially used by the remote capability of the framework...
Definition: ChannelBuffer.h:193
IntervalFillMode
Mode that is used to determine what slots should be added to the interval when not enough slots are a...
Definition: ChannelBuffer.h:110
virtual const Buffer< uint8 > & readSerializedValue(ChannelBufferBase::Slot *s)
Returns a buffer containing the binary serialized data of the slot.
Definition: ChannelBuffer.h:689
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:739
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:94
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.
virtual int getTypeId() const
Returns typeid of the slot values, the allocator creates.
Definition: ChannelBuffer.h:655
Slot * prev
Definition: ChannelBuffer.h:159
ChannelBufferBase()
Constructor.
Definition: ChannelBuffer.h:208
Serializer and Deserializer for JSON format.
const Time & timestamp() const
Definition: ChannelBuffer.h:195
virtual void writeJSON(ChannelBufferBase::Slot *s, JSONDeserializer &deserializer)
Writes data from an initialized json deserializer into the slot.
Definition: ChannelBuffer.h:838
virtual bool isTyped() const =0
Returns true, if the channel is typed and false, if it is untyped.
virtual Typename getTypename() const
Returns the Typename of the slot values, in contrast to getTypeId() the Typename is portable and uniq...
Definition: ChannelBuffer.h:658
The first slot with timestamp > requested will be chosen.
Definition: ChannelBuffer.h:100
std::map< TypeId, BinarySerializerCodecPtr > CodecsMap
A map of binary serialization codecs.
Definition: BinarySerializer.h:1262
boost::shared_ptr< BinarySerializerCodec > BinarySerializerCodecPtr
Shared pointer of BinarySerializerCodec.
Definition: BinarySerializerCodec.h:64