48 #ifndef _MIRA_CHANNELLOCKEDREADWRITE_H_ 49 #define _MIRA_CHANNELLOCKEDREADWRITE_H_ 52 #include <boost/shared_ptr.hpp> 53 #include <boost/thread/shared_mutex.hpp> 54 #include <boost/thread/locks.hpp> 73 template <
typename ConcreteChannelReadWrite>
85 template<
typename DerivedContainer>
107 #if __cplusplus >= 201703L 108 if(std::uncaught_exceptions() == 0)
110 if(!std::uncaught_exception())
113 DerivedContainer::finish(
this);
117 catch(std::exception& ex) {
121 MIRA_LOG(
ERROR) <<
"Unknown error in ChannelRead/Write::finish()";
124 DerivedContainer::discard(
this);
150 template<
typename DerivedContainer>
165 iSlot->
lock.unlock_shared();
185 bool owns_lock() {
return locked; }
186 void unlock() { locked =
false; };
202 template<
typename Derived>
213 typedef typename Shared::Slot
Slot;
240 return shared.get()!=
nullptr &&
shared->lock.owns_lock();
246 static std::string emptyString;
247 return shared.get()!=
nullptr ?
shared->channel->getID() : emptyString;
251 return shared.get()!=
nullptr ?
shared->channel->getTypename() :
"";
260 return shared->slot->timestamp();
280 return shared->slot->flags;
290 shared->slot->flags |= moreFlags;
298 MIRA_THROW(XAccessViolation,
"Trying to access ChannelRead / " 299 "ChannelWrite that was not assigned with valid data");
301 if(!
shared->lock.owns_lock())
302 MIRA_THROW(XAccessViolation,
"Trying to access data of ChannelRead " 303 "/ ChannelWrite after losing the lock");
313 shared->channel->getBuffer()->readSerializedValue(
shared->slot);
323 assert(!res.
empty());
329 return shared->channel->getBuffer()->readSerializedValue(
shared->slot, formatVersion, orLower);
335 return shared->channel->getBuffer()->readSerializedValue(
shared->slot, codecs);
339 uint8 formatVersion,
bool orLower) {
341 return shared->channel->getBuffer()->readSerializedValue(
shared->slot, codecs,
342 formatVersion, orLower);
347 shared->channel->getBuffer()->readJSON(
shared->slot, oValue);
352 shared->channel->getBuffer()->readJSON(
shared->slot, oValue, serializer);
359 shared->channel->getBuffer()->writeSerializedValue(
shared->slot, std::move(data));
364 shared->channel->getBuffer()->writeJSON(
shared->slot, value);
369 shared->channel->getBuffer()->writeJSON(
shared->slot, deserializer);
374 shared->channel->getBuffer()->writeXML(
shared->slot, node);
383 template<
typename Derived,
typename T>
413 return static_cast<Slot*
>(this->
shared->slot)->data;
423 return static_cast<Slot*
>(this->
shared->slot)->data;
433 template<
typename Derived>
462 return this->
shared->channel->getBuffer()->getStampedHeader(this->
shared->slot);
472 return this->
shared->channel->getBuffer()->getStampedHeader(this->
shared->slot);
581 uint8 formatVersion,
bool orLower) {
620 if(
shared->lock.owns_lock())
800 if(
shared->lock.owns_lock()) {
811 if(
shared->lock.owns_lock()) {
822 template <typename Derived, typename T, bool Copy = IsCheapToCopy<T>::value>
828 template <
typename Derived,
typename T>
844 typedef boost::shared_lock<boost::shared_mutex>
Lock;
854 typedef boost::unique_lock<boost::shared_mutex>
Lock;
867 typedef boost::shared_lock<boost::shared_mutex>
Lock;
877 typedef boost::unique_lock<boost::shared_mutex>
Lock;
ChannelReadWriteShared(ChannelTypePtr iChannel, ChannelBufferBase::Slot *iSlot)
Definition: ChannelReadWrite.h:99
static void finish(Shared *shared)
Definition: ChannelReadWrite.h:618
Serializer for serializing objects in JSON format.
Definition: JSONSerializer.h:93
AbstractChannel ChannelType
Definition: ChannelReadWrite.h:864
boost::shared_mutex lock
Definition: ChannelBuffer.h:168
Base::Shared Shared
Definition: ChannelReadWrite.h:391
Base::ValueType ValueType
Definition: ChannelReadWrite.h:394
ChannelReadWriteBase()
Constructs an empty (invalid) ChannelReadWriteBase object.
Definition: ChannelReadWrite.h:399
Base::ChannelTypePtr ChannelTypePtr
Definition: ChannelReadWrite.h:442
Base::Shared Shared
Definition: ChannelReadWrite.h:441
void finish()
Releases the lock explicitly.
Definition: ChannelReadWrite.h:601
boost::shared_lock< boost::shared_mutex > Lock
Definition: ChannelReadWrite.h:844
~ChannelReadWriteShared()
Definition: ChannelReadWrite.h:102
ChannelReadWriteShared< ChannelWrite< T > > Shared
Definition: ChannelReadWrite.h:855
void discard()
Releases the lock explicitly WITHOUT informing the Channel and without signaling the subscribers...
Definition: ChannelReadWrite.h:779
Definition: SyncTimedRead.h:62
An exception that occurs whenever a channel has no data.
Definition: Channel.h:88
const Buffer< uint8 > & readSerializedValue(uint8 formatVersion, bool orLower)
Definition: ChannelReadWrite.h:327
void readJSON(JSONValue &oValue)
Definition: ChannelReadWrite.h:345
ChannelRead(ChannelTypePtr channel, ChannelBufferBase::Slot *slot)
Is called by Channel to construct a ChannelRead object.
Definition: ChannelReadWrite.h:523
ChannelReadWriteCommonBase< Derived > Base
Definition: ChannelReadWrite.h:438
ChannelBuffer< T >::ValueType ValueType
Definition: ChannelReadWrite.h:853
ChannelBuffer< void >::Slot SlotType
Definition: ChannelReadWrite.h:865
ChannelBufferBase::Slot * slot
the slot in that channel we are pointing on (this pointer is valid unless the channel's buffer is des...
Definition: ChannelReadWrite.h:136
ChannelWrite(ChannelTypePtr channel, ChannelBufferBase::Slot *slot)
Is called by Channel to construct a valid ChannelWrite object with the corresponding data...
Definition: ChannelReadWrite.h:682
uint32 getFlags() const
For internal use only.
Definition: ChannelReadWrite.h:278
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Shared::ChannelType ChannelType
Definition: ChannelReadWrite.h:211
Base::ChannelTypePtr ChannelTypePtr
Definition: ChannelReadWrite.h:659
static void discard(Shared *shared)
Definition: ChannelReadWrite.h:624
const ValueType & internalValue() const
Returns a const reference on the data.
Definition: ChannelReadWrite.h:470
ChannelTypePtr channel
pointer to the channel our data/slot belongs to
Definition: ChannelReadWrite.h:170
An object that allows exclusive write access to data of a channel.
Definition: ChannelReadWrite.h:652
const ValueType & operator=(const ValueType &value)
Definition: ChannelReadWrite.h:711
#define MIRA_LOG(level)
Use this macro to log data.
Definition: LoggingCore.h:529
Base::ChannelTypePtr ChannelTypePtr
Definition: ChannelReadWrite.h:502
ChannelBuffer< T >::Slot SlotType
Definition: ChannelReadWrite.h:852
ChannelBuffer< T >::Slot SlotType
Definition: ChannelReadWrite.h:842
ChannelBuffer< T >::ValueType ValueType
Definition: ChannelReadWrite.h:843
Base::Shared Shared
Definition: ChannelReadWrite.h:501
ChannelReadWriteCommonBase(ChannelTypePtr channel, ChannelBufferBase::Slot *slot)
Constructs a valid ChannelReadWriteBase object that is assigned to a channel and slot.
Definition: ChannelReadWrite.h:222
AbstractChannel ChannelType
Definition: ChannelReadWrite.h:874
ChannelBuffer< void >::Slot SlotType
Definition: ChannelReadWrite.h:875
void addFlags(uint32 moreFlags) const
For internal use only.
Definition: ChannelReadWrite.h:288
boost::unique_lock< boost::shared_mutex > Lock
Definition: ChannelReadWrite.h:854
void writeSerializedValue(Buffer< uint8 > data)
Definition: ChannelReadWrite.h:741
A special base class creating and keeping a copy of the channel slot instead of managing access and l...
Definition: ChannelReadWrite.h:151
Definition: ChannelReadWrite.h:67
ChannelReadWriteBase()
Constructs an empty (invalid) ChannelReadWriteBase object.
Definition: ChannelReadWrite.h:448
void writeJSON(JSONDeserializer &deserializer)
Definition: ChannelReadWrite.h:749
An object that allows read access to data of a channel.
Definition: ChannelReadWrite.h:494
void readJSON(JSONValue &oValue, JSONSerializer &serializer)
Definition: ChannelReadWrite.h:350
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:82
Internally used by ChannelReadWriteBase! It contains the information about the channel and slot that ...
Definition: ChannelReadWrite.h:86
void finish()
Releases the lock explicitly and informs the Channel to signal all Subscribers that new data is avail...
Definition: ChannelReadWrite.h:765
Base::Shared Shared
Definition: ChannelReadWrite.h:658
void writeJSON(const JSONValue &value)
Definition: ChannelReadWrite.h:362
ChannelBuffer< void >::ValueType ValueType
Definition: ChannelReadWrite.h:876
const ValueType & internalValue() const
Returns a const reference on the data.
Definition: ChannelReadWrite.h:421
ChannelReadWriteTraits< DerivedContainer >::SlotType Slot
Definition: ChannelReadWrite.h:156
Const sibling_iterator for iterating over xml nodes that have the same parent (siblings) ...
Definition: XMLDom.h:671
Base::Slot Slot
Definition: ChannelReadWrite.h:393
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:418
ChannelReadWriteCommonBase< Derived > Base
Definition: ChannelReadWrite.h:388
std::unique_ptr< ChannelBufferBase::Slot > slotHolder
we are the unique owner of this slot copy
Definition: ChannelReadWrite.h:175
const Buffer< uint8 > & readSerializedValue()
Definition: ChannelReadWrite.h:568
ValueType * operator->()
Returns a pointer on the data.
Definition: ChannelReadWrite.h:736
ValueType & internalValue()
Returns a reference on the data.
Definition: ChannelReadWrite.h:460
bool isValid() const
Returns true, if data was assigned to the ChannelRead or ChannelWrite and if this data is locked...
Definition: ChannelReadWrite.h:238
Definition: ChannelReadWrite.h:74
boost::shared_ptr< Shared > SharedPtr
Definition: ChannelReadWrite.h:208
ChannelWrite()
Default constructor that constructs a ChannelWrite object that is invalid at the beginning and contai...
Definition: ChannelReadWrite.h:674
Deserializer for serializing objects from JSON format.
Definition: JSONSerializer.h:400
ChannelReadWriteTraits< DerivedContainer >::Lock Lock
Definition: ChannelReadWrite.h:90
void writeJSON(JSONDeserializer &deserializer)
Definition: ChannelReadWrite.h:367
void writeXML(const XMLDom::const_iterator &node)
Definition: ChannelReadWrite.h:372
Shared::Slot Slot
Definition: ChannelReadWrite.h:213
Definition: AbstractChannel.h:70
const ValueType & operator*() const
Returns a const reference on the data.
Definition: ChannelReadWrite.h:542
ChannelRead()
Default constructor that constructs a ChannelRead object that is invalid at the beginning and contain...
Definition: ChannelReadWrite.h:516
ChannelReadWriteBase< ChannelWrite< T >, T > Base
Definition: ChannelReadWrite.h:655
bool empty() const
Checks if the buffer is empty (used size == 0).
Definition: Buffer.h:303
ValueType & operator*()
Returns a reference on the data.
Definition: ChannelReadWrite.h:703
TypeMetaPtr getTypeMeta() const
Definition: ChannelReadWrite.h:254
ChannelReadWriteTraits< DerivedContainer >::ChannelType ChannelType
Definition: ChannelReadWrite.h:155
Type trait to define if a class is cheap to copy.
int getUseCount() const
Returns the number of shared instances for this data, may be useful for debugging purposes...
Definition: ChannelReadWrite.h:267
void writeXML(const XMLDom::const_iterator &node)
Definition: ChannelReadWrite.h:753
ChannelReadWriteBase< ChannelRead< T >, T > Base
Definition: ChannelReadWrite.h:498
ChannelType * ChannelTypePtr
Definition: ChannelReadWrite.h:159
const std::string & getChannelID()
Definition: ChannelReadWrite.h:245
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
Shared::ChannelTypePtr ChannelTypePtr
Definition: ChannelReadWrite.h:212
boost::shared_ptr< TypeMeta > TypeMetaPtr
Definition: MetaSerializer.h:309
ChannelBufferBase::Slot * slot
Definition: ChannelReadWrite.h:178
Definition: ChannelReadWrite.h:203
Buffer< uint8 > readSerializedValue(std::list< BinarySerializerCodecPtr > &codecs, uint8 formatVersion, bool orLower)
Definition: ChannelReadWrite.h:338
const Buffer< uint8 > & readSerializedValue()
Definition: ChannelReadWrite.h:310
ChannelReadWriteTraits< DerivedContainer >::ValueType ValueType
Definition: ChannelReadWrite.h:157
void writeSerializedValue(Buffer< uint8 > data)
Definition: ChannelReadWrite.h:357
ChannelType * ChannelTypePtr
Definition: ChannelReadWrite.h:94
ChannelReadWriteTraits< DerivedContainer >::SlotType Slot
Definition: ChannelReadWrite.h:91
void checkValid() const
checks if we are still locked, if not it throws a XAccessViolation
Definition: ChannelReadWrite.h:296
ConcreteChannel< T > ChannelType
Definition: ChannelReadWrite.h:851
Container for storing a single data element in the linked list.
Definition: ChannelBuffer.h:166
Lock lock
a shared lock for that data in the channel's buffer
Definition: ChannelReadWrite.h:139
ChannelReadCopy(ChannelTypePtr iChannel, ChannelBufferBase::Slot *iSlot)
Definition: ChannelReadWrite.h:162
const ValueType * operator->() const
Returns a const pointer on the data.
Definition: ChannelReadWrite.h:563
ChannelReadCopy< Derived > Shared
Definition: ChannelReadWrite.h:831
static void discard(Shared *shared)
Definition: ChannelReadWrite.h:808
#define MIRA_LOG_EXCEPTION(level, ex)
Log the specified exception, including all information that the exception object carries.
Definition: LoggingAux.h:107
Channel< T > getChannel()
Returns a read-only channel proxy object of the underlying channel.
ChannelReadWriteCommonBase()
Constructs an empty (invalid) ChannelReadWriteBase object.
Definition: ChannelReadWrite.h:219
std::string getTypename() const
Definition: ChannelReadWrite.h:250
ChannelTypePtr channel
pointer to the channel our data/slot belongs to
Definition: ChannelReadWrite.h:130
static Time now() static Time eternity()
Returns the current utc based time.
Definition: Time.h:481
ChannelReadWriteShared< ChannelWrite< void > > Shared
Definition: ChannelReadWrite.h:878
ConcreteChannel< T > ChannelType
Definition: ChannelReadWrite.h:841
Base::ValueType ValueType
Definition: ChannelReadWrite.h:443
boost::shared_lock< boost::shared_mutex > Lock
Definition: ChannelReadWrite.h:867
void readJSON(JSONValue &oValue)
Definition: ChannelReadWrite.h:586
Shared::ValueType ValueType
Definition: ChannelReadWrite.h:214
SharedPtr shared
Definition: ChannelReadWrite.h:378
Base::ValueType ValueType
Definition: ChannelReadWrite.h:503
boost::unique_lock< boost::shared_mutex > Lock
Definition: ChannelReadWrite.h:877
void writeJSON(const JSONValue &value)
Definition: ChannelReadWrite.h:745
Definition: ChannelReadWrite.h:823
ValueType & internalValue()
Returns a reference on the data.
Definition: ChannelReadWrite.h:411
Buffer< uint8 > readSerializedValue(std::list< BinarySerializerCodecPtr > &codecs)
Definition: ChannelReadWrite.h:576
ChannelReadWriteBase(ChannelTypePtr channel, ChannelBufferBase::Slot *slot)
Constructs a valid ChannelReadWriteBase object that is assigned to a channel and slot.
Definition: ChannelReadWrite.h:402
void readJSON(JSONValue &oValue, JSONSerializer &serializer)
Definition: ChannelReadWrite.h:590
Base::ChannelTypePtr ChannelTypePtr
Definition: ChannelReadWrite.h:392
ChannelBuffer< void >::ValueType ValueType
Definition: ChannelReadWrite.h:866
ChannelReadWriteShared< ChannelRead< void > > Shared
Definition: ChannelReadWrite.h:868
Definition: LoggingCore.h:75
json::Value JSONValue
Imports the json::Value type into mira namespace.
Definition: JSON.h:363
Base class for all framework channels.
ChannelReadWriteTraits< DerivedContainer >::ValueType ValueType
Definition: ChannelReadWrite.h:92
Definition: ChannelReadWrite.h:384
ChannelReadWriteBase(ChannelTypePtr channel, ChannelBufferBase::Slot *slot)
Constructs a valid ChannelReadWriteBase object that is assigned to a channel and slot.
Definition: ChannelReadWrite.h:451
Base::ValueType ValueType
Definition: ChannelReadWrite.h:660
Buffer< uint8 > readSerializedValue(std::list< BinarySerializerCodecPtr > &codecs, uint8 formatVersion, bool orLower)
Definition: ChannelReadWrite.h:580
MockLock lock
Definition: ChannelReadWrite.h:192
Channel< T > getChannel()
Returns a write-only channel proxy object of the underlying channel.
ChannelReadWriteShared< Derived > Shared
Definition: ChannelReadWrite.h:825
Buffer< uint8 > readSerializedValue(std::list< BinarySerializerCodecPtr > &codecs)
Same as above, but allows to specify codecs for serialization.
Definition: ChannelReadWrite.h:333
ChannelReadWriteTraits< Derived >::Shared Shared
Definition: ChannelReadWrite.h:207
ChannelReadSelectCopy< ChannelRead< T >, T >::Shared Shared
Definition: ChannelReadWrite.h:845
const Time & getTimestamp() const
Definition: ChannelReadWrite.h:258
static void finish(Shared *shared)
Definition: ChannelReadWrite.h:797
ChannelReadWriteTraits< DerivedContainer >::ChannelType ChannelType
Definition: ChannelReadWrite.h:89
const Buffer< uint8 > & readSerializedValue(uint8 formatVersion, bool orLower)
Definition: ChannelReadWrite.h:572