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>
168 assert(dynamic_cast<Slot*>(iSlot) !=
nullptr);
170 auto typedSlot =
static_cast<Slot*
>(iSlot);
174 iSlot->
lock.unlock_shared();
194 bool owns_lock() {
return locked; }
195 void unlock() { locked =
false; };
211 template<
typename Derived>
222 typedef typename Shared::Slot
Slot;
249 return shared.get()!=
nullptr &&
shared->lock.owns_lock();
255 static std::string emptyString;
256 return shared.get()!=
nullptr ?
shared->channel->getID() : emptyString;
260 return shared.get()!=
nullptr ?
shared->channel->getTypename() :
"";
269 return shared->slot->timestamp();
289 return shared->slot->flags;
299 shared->slot->flags |= moreFlags;
307 MIRA_THROW(XAccessViolation,
"Trying to access ChannelRead / " 308 "ChannelWrite that was not assigned with valid data");
310 if(!
shared->lock.owns_lock())
311 MIRA_THROW(XAccessViolation,
"Trying to access data of ChannelRead " 312 "/ ChannelWrite after losing the lock");
322 shared->channel->getBuffer()->readSerializedValue(
shared->slot);
332 assert(!res.
empty());
338 return shared->channel->getBuffer()->readSerializedValue(
shared->slot, formatVersion, orLower);
344 return shared->channel->getBuffer()->readSerializedValue(
shared->slot, codecs);
348 uint8 formatVersion,
bool orLower) {
350 return shared->channel->getBuffer()->readSerializedValue(
shared->slot, codecs,
351 formatVersion, orLower);
356 shared->channel->getBuffer()->readJSON(
shared->slot, oValue);
361 shared->channel->getBuffer()->readJSON(
shared->slot, oValue, serializer);
368 shared->channel->getBuffer()->writeSerializedValue(
shared->slot, std::move(data));
373 shared->channel->getBuffer()->writeJSON(
shared->slot, value);
378 shared->channel->getBuffer()->writeJSON(
shared->slot, deserializer);
383 shared->channel->getBuffer()->writeXML(
shared->slot, node);
392 template<
typename Derived,
typename T>
422 return static_cast<Slot*
>(this->
shared->slot)->data;
432 return static_cast<Slot*
>(this->
shared->slot)->data;
442 template<
typename Derived>
471 return this->
shared->channel->getBuffer()->getStampedHeader(this->
shared->slot);
481 return this->
shared->channel->getBuffer()->getStampedHeader(this->
shared->slot);
590 uint8 formatVersion,
bool orLower) {
629 if(
shared->lock.owns_lock())
811 if(
shared->lock.owns_lock()) {
822 if(
shared->lock.owns_lock()) {
833 template <typename Derived, typename T, bool Copy = IsCheapToCopy<T>::value>
839 template <
typename Derived,
typename T>
855 typedef boost::shared_lock<boost::shared_mutex>
Lock;
865 typedef boost::unique_lock<boost::shared_mutex>
Lock;
878 typedef boost::shared_lock<boost::shared_mutex>
Lock;
888 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:627
Serializer for serializing objects in JSON format.
Definition: JSONSerializer.h:95
AbstractChannel ChannelType
Definition: ChannelReadWrite.h:875
boost::shared_mutex lock
Definition: ChannelBuffer.h:166
Base::Shared Shared
Definition: ChannelReadWrite.h:400
Base::ValueType ValueType
Definition: ChannelReadWrite.h:403
ChannelReadWriteBase()
Constructs an empty (invalid) ChannelReadWriteBase object.
Definition: ChannelReadWrite.h:408
Base::ChannelTypePtr ChannelTypePtr
Definition: ChannelReadWrite.h:451
Base::Shared Shared
Definition: ChannelReadWrite.h:450
void finish()
Releases the lock explicitly.
Definition: ChannelReadWrite.h:610
boost::shared_lock< boost::shared_mutex > Lock
Definition: ChannelReadWrite.h:855
~ChannelReadWriteShared()
Definition: ChannelReadWrite.h:102
ChannelReadWriteShared< ChannelWrite< T > > Shared
Definition: ChannelReadWrite.h:866
void discard()
Releases the lock explicitly WITHOUT informing the Channel and without signaling the subscribers...
Definition: ChannelReadWrite.h:790
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:336
void readJSON(JSONValue &oValue)
Definition: ChannelReadWrite.h:354
ChannelRead(ChannelTypePtr channel, ChannelBufferBase::Slot *slot)
Is called by Channel to construct a ChannelRead object.
Definition: ChannelReadWrite.h:532
ChannelReadWriteCommonBase< Derived > Base
Definition: ChannelReadWrite.h:447
ChannelBuffer< T >::ValueType ValueType
Definition: ChannelReadWrite.h:864
ChannelBuffer< void >::Slot SlotType
Definition: ChannelReadWrite.h:876
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:693
uint32 getFlags() const
For internal use only.
Definition: ChannelReadWrite.h:287
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Shared::ChannelType ChannelType
Definition: ChannelReadWrite.h:220
Base::ChannelTypePtr ChannelTypePtr
Definition: ChannelReadWrite.h:668
static void discard(Shared *shared)
Definition: ChannelReadWrite.h:633
const ValueType & internalValue() const
Returns a const reference on the data.
Definition: ChannelReadWrite.h:479
ChannelTypePtr channel
pointer to the channel our data/slot belongs to
Definition: ChannelReadWrite.h:179
An object that allows exclusive write access to data of a channel.
Definition: ChannelReadWrite.h:661
const ValueType & operator=(const ValueType &value)
Definition: ChannelReadWrite.h:722
#define MIRA_LOG(level)
Use this macro to log data.
Definition: LoggingCore.h:529
Base::ChannelTypePtr ChannelTypePtr
Definition: ChannelReadWrite.h:511
std::unique_ptr< Slot > slotHolder
we are the unique owner of this slot copy
Definition: ChannelReadWrite.h:184
ChannelBuffer< T >::Slot SlotType
Definition: ChannelReadWrite.h:863
ChannelBuffer< T >::Slot SlotType
Definition: ChannelReadWrite.h:853
ChannelBuffer< T >::ValueType ValueType
Definition: ChannelReadWrite.h:854
Base::Shared Shared
Definition: ChannelReadWrite.h:510
ChannelReadWriteCommonBase(ChannelTypePtr channel, ChannelBufferBase::Slot *slot)
Constructs a valid ChannelReadWriteBase object that is assigned to a channel and slot.
Definition: ChannelReadWrite.h:231
AbstractChannel ChannelType
Definition: ChannelReadWrite.h:885
ChannelBuffer< void >::Slot SlotType
Definition: ChannelReadWrite.h:886
void addFlags(uint32 moreFlags) const
For internal use only.
Definition: ChannelReadWrite.h:297
boost::unique_lock< boost::shared_mutex > Lock
Definition: ChannelReadWrite.h:865
void writeSerializedValue(Buffer< uint8 > data)
Definition: ChannelReadWrite.h:752
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:457
void writeJSON(JSONDeserializer &deserializer)
Definition: ChannelReadWrite.h:760
An object that allows read access to data of a channel.
Definition: ChannelReadWrite.h:503
void readJSON(JSONValue &oValue, JSONSerializer &serializer)
Definition: ChannelReadWrite.h:359
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:78
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:776
Base::Shared Shared
Definition: ChannelReadWrite.h:667
void writeJSON(const JSONValue &value)
Definition: ChannelReadWrite.h:371
ChannelBuffer< void >::ValueType ValueType
Definition: ChannelReadWrite.h:887
const ValueType & internalValue() const
Returns a const reference on the data.
Definition: ChannelReadWrite.h:430
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:763
Base::Slot Slot
Definition: ChannelReadWrite.h:402
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:418
ChannelReadWriteCommonBase< Derived > Base
Definition: ChannelReadWrite.h:397
const Buffer< uint8 > & readSerializedValue()
Definition: ChannelReadWrite.h:577
ValueType * operator->()
Returns a pointer on the data.
Definition: ChannelReadWrite.h:747
ValueType & internalValue()
Returns a reference on the data.
Definition: ChannelReadWrite.h:469
bool isValid() const
Returns true, if data was assigned to the ChannelRead or ChannelWrite and if this data is locked...
Definition: ChannelReadWrite.h:247
Definition: ChannelReadWrite.h:74
boost::shared_ptr< Shared > SharedPtr
Definition: ChannelReadWrite.h:217
ChannelWrite()
Default constructor that constructs a ChannelWrite object that is invalid at the beginning and contai...
Definition: ChannelReadWrite.h:683
Deserializer for serializing objects from JSON format.
Definition: JSONSerializer.h:406
ChannelReadWriteTraits< DerivedContainer >::Lock Lock
Definition: ChannelReadWrite.h:90
void writeJSON(JSONDeserializer &deserializer)
Definition: ChannelReadWrite.h:376
void writeXML(const XMLDom::const_iterator &node)
Definition: ChannelReadWrite.h:381
Shared::Slot Slot
Definition: ChannelReadWrite.h:222
Definition: AbstractChannel.h:70
const ValueType & operator*() const
Returns a const reference on the data.
Definition: ChannelReadWrite.h:551
ChannelRead()
Default constructor that constructs a ChannelRead object that is invalid at the beginning and contain...
Definition: ChannelReadWrite.h:525
ChannelReadWriteBase< ChannelWrite< T >, T > Base
Definition: ChannelReadWrite.h:664
bool empty() const
Checks if the buffer is empty (used size == 0).
Definition: Buffer.h:299
ValueType & operator*()
Returns a reference on the data.
Definition: ChannelReadWrite.h:714
TypeMetaPtr getTypeMeta() const
Definition: ChannelReadWrite.h:263
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:276
void writeXML(const XMLDom::const_iterator &node)
Definition: ChannelReadWrite.h:764
ChannelReadWriteBase< ChannelRead< T >, T > Base
Definition: ChannelReadWrite.h:507
ChannelType * ChannelTypePtr
Definition: ChannelReadWrite.h:159
static Time now()
Returns the current utc based time.
Definition: Time.h:481
const std::string & getChannelID()
Definition: ChannelReadWrite.h:254
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:221
boost::shared_ptr< TypeMeta > TypeMetaPtr
Definition: MetaSerializer.h:309
ChannelBufferBase::Slot * slot
Definition: ChannelReadWrite.h:187
Definition: ChannelReadWrite.h:212
Buffer< uint8 > readSerializedValue(std::list< BinarySerializerCodecPtr > &codecs, uint8 formatVersion, bool orLower)
Definition: ChannelReadWrite.h:347
const Buffer< uint8 > & readSerializedValue()
Definition: ChannelReadWrite.h:319
ChannelReadWriteTraits< DerivedContainer >::ValueType ValueType
Definition: ChannelReadWrite.h:157
void writeSerializedValue(Buffer< uint8 > data)
Definition: ChannelReadWrite.h:366
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:305
ConcreteChannel< T > ChannelType
Definition: ChannelReadWrite.h:862
Container for storing a single data element in the linked list.
Definition: ChannelBuffer.h:164
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:572
ChannelReadCopy< Derived > Shared
Definition: ChannelReadWrite.h:842
static void discard(Shared *shared)
Definition: ChannelReadWrite.h:819
#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:228
std::string getTypename() const
Definition: ChannelReadWrite.h:259
ChannelTypePtr channel
pointer to the channel our data/slot belongs to
Definition: ChannelReadWrite.h:130
ChannelReadWriteShared< ChannelWrite< void > > Shared
Definition: ChannelReadWrite.h:889
ConcreteChannel< T > ChannelType
Definition: ChannelReadWrite.h:852
Base::ValueType ValueType
Definition: ChannelReadWrite.h:452
boost::shared_lock< boost::shared_mutex > Lock
Definition: ChannelReadWrite.h:878
void readJSON(JSONValue &oValue)
Definition: ChannelReadWrite.h:595
Shared::ValueType ValueType
Definition: ChannelReadWrite.h:223
SharedPtr shared
Definition: ChannelReadWrite.h:387
Base::ValueType ValueType
Definition: ChannelReadWrite.h:512
boost::unique_lock< boost::shared_mutex > Lock
Definition: ChannelReadWrite.h:888
void writeJSON(const JSONValue &value)
Definition: ChannelReadWrite.h:756
Definition: ChannelReadWrite.h:834
ValueType & internalValue()
Returns a reference on the data.
Definition: ChannelReadWrite.h:420
Buffer< uint8 > readSerializedValue(std::list< BinarySerializerCodecPtr > &codecs)
Definition: ChannelReadWrite.h:585
ChannelReadWriteBase(ChannelTypePtr channel, ChannelBufferBase::Slot *slot)
Constructs a valid ChannelReadWriteBase object that is assigned to a channel and slot.
Definition: ChannelReadWrite.h:411
void readJSON(JSONValue &oValue, JSONSerializer &serializer)
Definition: ChannelReadWrite.h:599
Base::ChannelTypePtr ChannelTypePtr
Definition: ChannelReadWrite.h:401
ChannelBuffer< void >::ValueType ValueType
Definition: ChannelReadWrite.h:877
ChannelReadWriteShared< ChannelRead< void > > Shared
Definition: ChannelReadWrite.h:879
Definition: LoggingCore.h:75
json::Value JSONValue
Imports the json::Value type into mira namespace.
Definition: JSON.h:361
Base class for all framework channels.
ChannelReadWriteTraits< DerivedContainer >::ValueType ValueType
Definition: ChannelReadWrite.h:92
Definition: ChannelReadWrite.h:393
ChannelReadWriteBase(ChannelTypePtr channel, ChannelBufferBase::Slot *slot)
Constructs a valid ChannelReadWriteBase object that is assigned to a channel and slot.
Definition: ChannelReadWrite.h:460
Base::ValueType ValueType
Definition: ChannelReadWrite.h:669
Buffer< uint8 > readSerializedValue(std::list< BinarySerializerCodecPtr > &codecs, uint8 formatVersion, bool orLower)
Definition: ChannelReadWrite.h:589
MockLock lock
Definition: ChannelReadWrite.h:201
Channel< T > getChannel()
Returns a write-only channel proxy object of the underlying channel.
ChannelReadWriteShared< Derived > Shared
Definition: ChannelReadWrite.h:836
Buffer< uint8 > readSerializedValue(std::list< BinarySerializerCodecPtr > &codecs)
Same as above, but allows to specify codecs for serialization.
Definition: ChannelReadWrite.h:342
ChannelReadWriteTraits< Derived >::Shared Shared
Definition: ChannelReadWrite.h:216
ChannelReadSelectCopy< ChannelRead< T >, T >::Shared Shared
Definition: ChannelReadWrite.h:856
const Time & getTimestamp() const
Definition: ChannelReadWrite.h:267
static void finish(Shared *shared)
Definition: ChannelReadWrite.h:808
ChannelReadWriteTraits< DerivedContainer >::ChannelType ChannelType
Definition: ChannelReadWrite.h:89
const Buffer< uint8 > & readSerializedValue(uint8 formatVersion, bool orLower)
Definition: ChannelReadWrite.h:581