TermImpl.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2017, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarX
19  * @author Mirko Waechter( mirko.waechter at kit dot edu)
20  * @date 2017
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "TermImpl.h"
26 
27 template class ::IceInternal::Handle<::armarx::TermImpl>;
28 
29 namespace armarx
30 {
31  TermImpl::~TermImpl()
32  {
33  // assure all childs are removed properly (required because of loop)
34  removeChildren();
35  }
36 
37  void TermImpl::addChild(const TermImplBasePtr& child, const Ice::Current& c)
38  {
39  child->setParent(this);
40 
41  childs.push_back(child);
42  }
43 
44  TermImplSequence TermImpl::getChilds(const Ice::Current& c)
45  {
46  return childs;
47  }
48 
49  TermImplBasePtr TermImpl::getParent(const Ice::Current& c)
50  {
51  return parent;
52  }
53 
54  bool TermImpl::getValue(const Ice::Current& c) const
55  {
56  return value;
57  }
58 
59  StringVariantBaseMap TermImpl::getDatafields(const Ice::Current&) const
60  {
61  return datafieldValues;
62  }
63 
64  TermType TermImpl::getType(const Ice::Current& c) const
65  {
66  return type;
67  }
68 
69  void TermImpl::update(const Ice::Current& c)
70  {
71  if (parent)
72  {
73  parent->update();
74  }
75  }
76 
77  void TermImpl::updateWithData(const Ice::Current& c)
78  {
79  if (parent)
80  {
81  parent->updateWithData();
82  }
83  }
84 
85  void TermImpl::removeChildren()
86  {
87  __incRef(); // prevent self deletion
88  TermImplSequence::iterator iterChilds = childs.begin();
89 
90  while (iterChilds != childs.end())
91  {
92  if (*iterChilds)
93  {
94  (*iterChilds)->resetParent();
95  }
96 
97  childs.erase(iterChilds);
98  iterChilds = childs.begin();
99  }
100 
101  __decRef();
102  this->childs.clear();
103  }
104 
105  void TermImpl::setParent(const TermImplBasePtr& parent, const Ice::Current& c)
106  {
107  this->parent = parent;
108  }
109 
110  void TermImpl::resetParent(const Ice::Current& c)
111  {
112  parent = nullptr;
113  }
114 
115  int TermImpl::atomicDecAndTestValue(volatile int* counter, int value)
116  {
117  unsigned char c;
118  __asm__ __volatile__(
119  "lock ; decl %0; sete %1"
120  :"=m"(*counter), "=qm"(c)
121  :"m"(*counter) : "memory");
122  return c != value;
123  }
124 
125  void TermImpl::__incRef()
126  {
127  Shared::__incRef();
128  }
129 
130  void TermImpl::__decRef()
131  {
132  // int numberChilds = int(getChilds().size());
133  int numberChilds = int(childs.size());
134 #if defined(_WIN32)
135  assert(InterlockedExchangeAdd(&_ref, 0) > 0);
136 
137  if (InterlockedDecrement(&_ref) == numberChilds && !_noDelete)
138  {
139  _noDelete = true;
140  delete this;
141  }
142 
143 #elif defined(ICE_HAS_GCC_BUILTINS)
144  int c = __sync_fetch_and_sub(&_ref, 1);
145  assert(c > 0);
146 
147  if ((c == numberChilds + 1) && !_noDelete)
148  {
149  _noDelete = true;
150  delete this;
151  }
152 
153 #elif defined(ICE_HAS_ATOMIC_FUNCTIONS)
154  assert(IceUtilInternal::atomicExchangeAdd(&_ref, 0) > 0);
155 
156  if (IceUtilInternal::atomicDecAndTestValue(&_ref, numberChilds) && !_noDelete)
157  {
158  _noDelete = true;
159  delete this;
160  }
161 #elif ICE_INT_VERSION >= 30603
162  int c = _ref.fetch_sub(1);
163  assert(c > 0);
164 
165  if ((c == numberChilds + 1) && !__hasFlag(NoDelete))
166  {
167  __setNoDelete(true);
168  delete this;
169  }
170 #else
171  _mutex.lock();
172  bool doDelete = false;
173  assert(_ref > 0);
174 
175  if (--_ref == numberChilds)
176  {
177  doDelete = !_noDelete;
178  _noDelete = true;
179  }
180 
181  _mutex.unlock();
182 
183  if (doDelete)
184  {
185  delete this;
186  }
187 #endif
188  }
189 }
armarx::StringVariantBaseMap
std::map< std::string, VariantBasePtr > StringVariantBaseMap
Definition: ManagedIceObject.h:111
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
TermImpl.h
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:67
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28