knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
TokenQueue.cpp
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 #include <knowrob/queries/TokenQueue.h>
7 #include "knowrob/integration/python/utils.h"
8 
9 using namespace knowrob;
10 
12  : TokenStream() {}
13 
15  if (isOpened()) {
17  }
18 }
19 
21  {
22  std::lock_guard<std::mutex> lock(queue_mutex_);
23  queue_.push(tok);
24  }
25  queue_CV_.notify_one();
26 }
27 
28 void TokenQueue::push(const TokenPtr &tok) {
29  pushToQueue(tok);
30 }
31 
33  std::unique_lock<std::mutex> lock(queue_mutex_);
34  queue_CV_.wait(lock, [&] { return !queue_.empty(); });
35  return queue_.front();
36 }
37 
39  std::lock_guard<std::mutex> lock(queue_mutex_);
40  queue_.pop();
41 }
42 
44  TokenPtr x = front();
45  pop();
46  return x;
47 }
48 
49 namespace knowrob::py {
50  template<>
52  using namespace boost::python;
53  class_<TokenQueue, std::shared_ptr<TokenQueue>, bases<TokenStream>, boost::noncopyable>
54  ("TokenQueue", init<>())
55  .def("front", with<no_gil>(&TokenQueue::front, return_value_policy<reference_existing_object>()))
56  .def("pop", &TokenQueue::pop)
57  .def("pop_front", with<no_gil>(&TokenQueue::pop_front))
58  .def("empty", &TokenQueue::empty)
59  .def("size", &TokenQueue::size);
60  }
61 }
std::queue< TokenPtr > queue_
Definition: TokenQueue.h:53
bool empty() const
Definition: TokenQueue.h:45
std::mutex queue_mutex_
Definition: TokenQueue.h:55
TokenPtr & front()
Definition: TokenQueue.cpp:32
void push(const TokenPtr &tok) override
Definition: TokenQueue.cpp:28
TokenPtr pop_front()
Definition: TokenQueue.cpp:43
auto size() const
Definition: TokenQueue.h:50
std::condition_variable queue_CV_
Definition: TokenQueue.h:54
~TokenQueue() override
Definition: TokenQueue.cpp:14
void pushToQueue(const TokenPtr &tok)
Definition: TokenQueue.cpp:20
bool isOpened() const
Definition: TokenStream.cpp:44
void createType< TokenQueue >()
Definition: TokenQueue.cpp:51
std::shared_ptr< const Token > TokenPtr
Definition: Token.h:74