knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::TokenStream::Channel Class Reference

#include <TokenStream.h>

Public Member Functions

 Channel (const std::shared_ptr< TokenStream > &stream)
 
 ~Channel ()
 
 Channel (const Channel &)=delete
 
void push (const TokenPtr &tok)
 
void close ()
 
bool isOpened () const
 
bool hasValidIterator () const
 
void invalidateIterator ()
 
uint32_t id () const
 
 Channel (const std::shared_ptr< TokenStream > &stream)
 
 ~Channel ()
 
 Channel (const Channel &)=delete
 
void push (const TokenPtr &tok)
 
void close ()
 
bool isOpened () const
 
bool hasValidIterator () const
 
void invalidateIterator ()
 
uint32_t id () const
 

Static Public Member Functions

static std::shared_ptr< Channelcreate (const std::shared_ptr< TokenStream > &stream)
 
static std::shared_ptr< Channelcreate (const std::shared_ptr< TokenStream > &stream)
 

Protected Attributes

std::shared_ptr< TokenStreamstream_
 
std::list< std::shared_ptr< Channel > >::iterator iterator_
 
std::atomic< bool > isOpened_
 
std::atomic< bool > hasValidIterator_
 
std::shared_mutex mutex_
 

Friends

class TokenStream
 

Detailed Description

An input channel of a stream.

Definition at line 51 of file TokenStream.h.

Constructor & Destructor Documentation

◆ Channel() [1/4]

TokenStream::Channel::Channel ( const std::shared_ptr< TokenStream > &  stream)
explicit
Parameters
streamthe stream associated to this channel.

Definition at line 78 of file TokenStream.cpp.

79  : stream_(stream),
80  isOpened_(true),
81  hasValidIterator_(true) {
82 }
std::atomic< bool > isOpened_
Definition: TokenStream.h:110
std::atomic< bool > hasValidIterator_
Definition: TokenStream.h:111
std::shared_ptr< TokenStream > stream_
Definition: TokenStream.h:106

◆ ~Channel() [1/2]

TokenStream::Channel::~Channel ( )

Definition at line 84 of file TokenStream.cpp.

84  {
85  close();
86 }

◆ Channel() [2/4]

knowrob::TokenStream::Channel::Channel ( const Channel )
delete

Cannot be copy-assigned.

◆ Channel() [3/4]

knowrob::TokenStream::Channel::Channel ( const std::shared_ptr< TokenStream > &  stream)
explicit
Parameters
streamthe stream associated to this channel.

◆ ~Channel() [2/2]

knowrob::TokenStream::Channel::~Channel ( )

◆ Channel() [4/4]

knowrob::TokenStream::Channel::Channel ( const Channel )
delete

Cannot be copy-assigned.

Member Function Documentation

◆ close() [1/2]

void TokenStream::Channel::close ( )

Close the channel.

Definition at line 104 of file TokenStream.cpp.

104  {
105  // prevent channels from being closed while other channel operations are in progress.
106  // also avoid close being called multiple times at the same time.
107  std::lock_guard<std::shared_mutex> lock(mutex_);
108  if (isOpened()) {
109  isOpened_ = false;
110  if (stream_->isOpened()) {
111  stream_->push(*this, EndOfEvaluation::get());
112  stream_ = {};
113  }
114  }
115 }
std::shared_mutex mutex_
Definition: TokenStream.h:112

◆ close() [2/2]

void knowrob::TokenStream::Channel::close ( )

Close the channel.

◆ create() [1/2]

std::shared_ptr< TokenStream::Channel > TokenStream::Channel::create ( const std::shared_ptr< TokenStream > &  stream)
static

Create a new stream channel. Note that this will generate an error in case the stream is closed already.

Returns
a new stream channel

Definition at line 88 of file TokenStream.cpp.

89  {
90  // prevent the stream from being closed and to modify the channels list,
91  // as we are about to add a new channel to the list.
92  std::lock_guard<std::mutex> lock1(stream->channel_mutex_);
93  if (stream->isOpened()) {
94  auto channel = std::make_shared<TokenStream::Channel>(stream);
95  stream->channels_.push_back(channel);
96  channel->iterator_ = stream->channels_.end();
97  --channel->iterator_;
98  return channel;
99  } else {
100  throw QueryError("cannot create a channel of a closed stream");
101  }
102 }

◆ create() [2/2]

static std::shared_ptr<Channel> knowrob::TokenStream::Channel::create ( const std::shared_ptr< TokenStream > &  stream)
static

Create a new stream channel. Note that this will generate an error in case the stream is closed already.

Returns
a new stream channel

◆ hasValidIterator() [1/2]

bool knowrob::TokenStream::Channel::hasValidIterator ( ) const
inline
Returns
true if the iterator is valid

Definition at line 92 of file TokenStream.h.

92 { return hasValidIterator_; }

◆ hasValidIterator() [2/2]

bool knowrob::TokenStream::Channel::hasValidIterator ( ) const
inline
Returns
true if the iterator is valid

Definition at line 92 of file TokenStream.h.

92 { return hasValidIterator_; }

◆ id() [1/2]

uint32_t TokenStream::Channel::id ( ) const
Returns
the id of this channel

Definition at line 117 of file TokenStream.cpp.

117  {
118  return reinterpret_cast<std::uintptr_t>(this);
119 }

◆ id() [2/2]

uint32_t knowrob::TokenStream::Channel::id ( ) const
Returns
the id of this channel

◆ invalidateIterator() [1/2]

void knowrob::TokenStream::Channel::invalidateIterator ( )
inline

Invalidate the iterator of this channel

Definition at line 97 of file TokenStream.h.

97 { hasValidIterator_ = false; }

◆ invalidateIterator() [2/2]

void knowrob::TokenStream::Channel::invalidateIterator ( )
inline

Invalidate the iterator of this channel

Definition at line 97 of file TokenStream.h.

97 { hasValidIterator_ = false; }

◆ isOpened() [1/2]

bool TokenStream::Channel::isOpened ( ) const
Returns
true if opened.

Definition at line 136 of file TokenStream.cpp.

136  {
137  return isOpened_;
138 }

◆ isOpened() [2/2]

bool knowrob::TokenStream::Channel::isOpened ( ) const
Returns
true if opened.

◆ push() [1/2]

void TokenStream::Channel::push ( const TokenPtr tok)

Push a QueryResult into this channel.

Parameters
toka QueryResult pointer.

Definition at line 121 of file TokenStream.cpp.

121  {
122  // prevent channels from being closed while push operations are in progress
123  // note: this is a shared lock, i.e., multiple push operations can be performed in parallel.
124  std::shared_lock<std::shared_mutex> lock(mutex_);
125  if (isOpened()) {
126  stream_->push(*this, tok);
127  if (tok->indicatesEndOfEvaluation()) {
128  isOpened_ = false;
129  stream_ = {};
130  }
131  } else if (!tok->indicatesEndOfEvaluation()) {
132  KB_WARN("message pushed to closed stream {}", reinterpret_cast<std::uintptr_t>(this));
133  }
134 }
#define KB_WARN
Definition: Logger.h:27

◆ push() [2/2]

void knowrob::TokenStream::Channel::push ( const TokenPtr tok)

Push a QueryResult into this channel.

Parameters
toka QueryResult pointer.

Friends And Related Function Documentation

◆ TokenStream

TokenStream
friend

Definition at line 114 of file TokenStream.h.

Member Data Documentation

◆ hasValidIterator_

std::atomic< bool > knowrob::TokenStream::Channel::hasValidIterator_
protected

Definition at line 111 of file TokenStream.h.

◆ isOpened_

std::atomic< bool > knowrob::TokenStream::Channel::isOpened_
protected

Definition at line 110 of file TokenStream.h.

◆ iterator_

std::list< std::shared_ptr< Channel > >::iterator knowrob::TokenStream::Channel::iterator_
protected

Definition at line 108 of file TokenStream.h.

◆ mutex_

std::shared_mutex knowrob::TokenStream::Channel::mutex_
protected

Definition at line 112 of file TokenStream.h.

◆ stream_

std::shared_ptr< TokenStream > knowrob::TokenStream::Channel::stream_
protected

Definition at line 106 of file TokenStream.h.


The documentation for this class was generated from the following files: