knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
TripleContainer.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/semweb/TripleContainer.h"
7 #include "knowrob/integration/python/utils.h"
8 
9 using namespace knowrob;
10 
11 ProxyTripleContainer::ProxyTripleContainer(const std::vector<TriplePtr> *triples)
12  : triples_(triples) {
13 }
14 
15 ProxyTripleContainer::ProxyTripleContainer(const std::vector<TriplePtr> &triples)
16  : triples_(&triplesData_), triplesData_(triples) {
17  // take the ownership of the triples
18  for (uint32_t i = 0; i < triples.size(); ++i) {
19  if (triples[i].owned) {
20  triplesData_[i].owned = true;
21  triples[i].owned = false;
22  }
23  }
24 }
25 
27  return [it = triples_->begin(), end = triples_->end()]()
28  mutable -> const TriplePtr * {
29  if (it == end) {
30  return nullptr;
31  }
32  auto &ptr = *it;
33  ++it;
34  return &ptr;
35  };
36 }
37 
39  : data_(batchSize), batchSize_(batchSize), actualSize_(0) {
40 }
41 
42 void TripleViewBatch::add(const TriplePtr &triple) {
43  if (actualSize_ < batchSize_) {
44  auto &entry = data_[actualSize_++];
45  if (entry.owned && entry.ptr) {
46  delete entry.ptr;
47  }
48  if (triple.owned) {
49  entry.ptr = triple.ptr;
50  entry.owned = true;
51  triple.owned = false;
52  } else {
53  entry.ptr = new TripleCopy(*triple.ptr);
54  entry.owned = true;
55  }
56  }
57 }
58 
60  return [this, i = std::size_t(0)]() mutable -> const TriplePtr * {
61  if (i < actualSize_) return &data_[i++];
62  return nullptr;
63  };
64 }
65 
67  return [this, i = std::size_t(0)]() mutable -> TriplePtr * {
68  if (i < actualSize_) return &data_[i++];
69  return nullptr;
70  };
71 }
72 
73 namespace knowrob::py {
74  template<>
76  using namespace boost::python;
77  class_<TripleContainer, std::shared_ptr<TripleContainer>, boost::noncopyable>
78  ("TripleContainer", no_init)
79  .def("__iter__",
80  boost::python::iterator<TripleContainer, boost::python::return_value_policy<boost::python::copy_const_reference>>{});
81  }
82 }
std::function< TriplePtr *()> MutableGenerator
ProxyTripleContainer(const std::vector< TriplePtr > *triples)
const std::vector< TriplePtr > * triples_
const std::vector< TriplePtr > triplesData_
ConstGenerator cgenerator() const override
static iterator end()
std::function< const TriplePtr *()> ConstGenerator
void add(const TriplePtr &triple)
std::vector< TriplePtr > data_
MutableGenerator generator() override
TripleViewBatch(uint32_t batchSize)
ConstGenerator cgenerator() const override
void createType< TripleContainer >()
TripleTemplate< std::string > TripleCopy
Definition: Triple.h:577
Triple * ptr
Definition: Triple.h:590