knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
TokenStream.h
Go to the documentation of this file.
1 /*
2  * This file is part of KnowRob, please consult
3  * https://github.com/knowrob/knowrob for license details.
4  */
5 
6 #ifndef KNOWROB_TOKEN_STREAM_H_
7 #define KNOWROB_TOKEN_STREAM_H_
8 
9 #include <memory>
10 #include <mutex>
11 #include <shared_mutex>
12 #include <atomic>
13 #include <list>
14 #include <knowrob/queries/Token.h>
15 #include <knowrob/queries/EndOfEvaluation.h>
16 
17 namespace knowrob {
22  class TokenStream {
23  public:
25 
26  virtual ~TokenStream();
27 
31  TokenStream(const TokenStream &) = delete;
32 
41  virtual void close();
42 
46  bool isOpened() const;
47 
51  class Channel {
52  public:
56  explicit Channel(const std::shared_ptr<TokenStream> &stream);
57 
59 
63  Channel(const Channel &) = delete;
64 
71  static std::shared_ptr<Channel> create(const std::shared_ptr<TokenStream> &stream);
72 
77  void push(const TokenPtr &tok);
78 
82  void close();
83 
87  bool isOpened() const;
88 
92  bool hasValidIterator() const { return hasValidIterator_; }
93 
98 
102  uint32_t id() const;
103 
104  protected:
105  // the stream of this channel
106  std::shared_ptr<TokenStream> stream_;
107  // iterator of this channel withing the stream
108  std::list<std::shared_ptr<Channel>>::iterator iterator_;
109  // flag indicating whether channel is open (i.e., no EOS received so far)
110  std::atomic<bool> isOpened_;
111  std::atomic<bool> hasValidIterator_;
112  std::shared_mutex mutex_;
113 
114  friend class TokenStream;
115  };
116 
117  protected:
118  std::list<std::shared_ptr<Channel>> channels_;
119  std::atomic<bool> isOpened_;
120  std::mutex channel_mutex_;
121 
122  virtual void push(Channel &channel, const TokenPtr &tok);
123 
124  virtual void push(const TokenPtr &tok) = 0;
125  };
126 }
127 
128 #endif //KNOWROB_TOKEN_STREAM_H_
void push(const TokenPtr &tok)
static std::shared_ptr< Channel > create(const std::shared_ptr< TokenStream > &stream)
std::shared_mutex mutex_
Definition: TokenStream.h:112
std::list< std::shared_ptr< Channel > >::iterator iterator_
Definition: TokenStream.h:108
Channel(const std::shared_ptr< TokenStream > &stream)
std::atomic< bool > isOpened_
Definition: TokenStream.h:110
std::atomic< bool > hasValidIterator_
Definition: TokenStream.h:111
Channel(const Channel &)=delete
std::shared_ptr< TokenStream > stream_
Definition: TokenStream.h:106
std::atomic< bool > isOpened_
Definition: TokenStream.h:119
virtual void push(const TokenPtr &tok)=0
virtual void close()
std::mutex channel_mutex_
Definition: TokenStream.h:120
TokenStream(const TokenStream &)=delete
bool isOpened() const
virtual void push(Channel &channel, const TokenPtr &tok)
std::list< std::shared_ptr< Channel > > channels_
Definition: TokenStream.h:118
std::shared_ptr< const Token > TokenPtr
Definition: Token.h:74