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
27template class ::IceInternal::Handle<::armarx::TermImpl>;
28
29namespace armarx
30{
32 {
33 // assure all childs are removed properly (required because of loop)
35 }
36
37 void
38 TermImpl::addChild(const TermImplBasePtr& child, const Ice::Current& c)
39 {
40 child->setParent(this);
41
42 childs.push_back(child);
43 }
44
45 TermImplSequence
46 TermImpl::getChilds(const Ice::Current& c)
47 {
48 return childs;
49 }
50
51 TermImplBasePtr
52 TermImpl::getParent(const Ice::Current& c)
53 {
54 return parent;
55 }
56
57 bool
58 TermImpl::getValue(const Ice::Current& c) const
59 {
60 return value;
61 }
62
64 TermImpl::getDatafields(const Ice::Current&) const
65 {
66 return datafieldValues;
67 }
68
69 TermType
70 TermImpl::getType(const Ice::Current& c) const
71 {
72 return type;
73 }
74
75 void
76 TermImpl::update(const Ice::Current& c)
77 {
78 if (parent)
79 {
80 parent->update();
81 }
82 }
83
84 void
85 TermImpl::updateWithData(const Ice::Current& c)
86 {
87 if (parent)
88 {
89 parent->updateWithData();
90 }
91 }
92
93 void
95 {
96 __incRef(); // prevent self deletion
97 TermImplSequence::iterator iterChilds = childs.begin();
98
99 while (iterChilds != childs.end())
100 {
101 if (*iterChilds)
102 {
103 (*iterChilds)->resetParent();
104 }
105
106 childs.erase(iterChilds);
107 iterChilds = childs.begin();
108 }
109
110 __decRef();
111 this->childs.clear();
112 }
113
114 void
115 TermImpl::setParent(const TermImplBasePtr& parent, const Ice::Current& c)
116 {
117 this->parent = parent;
118 }
119
120 void
121 TermImpl::resetParent(const Ice::Current& c)
122 {
123 parent = nullptr;
124 }
125
126 int
127 TermImpl::atomicDecAndTestValue(volatile int* counter, int value)
128 {
129 unsigned char c;
130 __asm__ __volatile__("lock ; decl %0; sete %1"
131 : "=m"(*counter), "=qm"(c)
132 : "m"(*counter)
133 : "memory");
134 return c != value;
135 }
136
137 void
139 {
140 Shared::__incRef();
141 }
142
143 void
145 {
146 // int numberChilds = int(getChilds().size());
147 int numberChilds = int(childs.size());
148#if defined(_WIN32)
149 assert(InterlockedExchangeAdd(&_ref, 0) > 0);
150
151 if (InterlockedDecrement(&_ref) == numberChilds && !_noDelete)
152 {
153 _noDelete = true;
154 delete this;
155 }
156
157#elif defined(ICE_HAS_GCC_BUILTINS)
158 int c = __sync_fetch_and_sub(&_ref, 1);
159 assert(c > 0);
160
161 if ((c == numberChilds + 1) && !_noDelete)
162 {
163 _noDelete = true;
164 delete this;
165 }
166
167#elif defined(ICE_HAS_ATOMIC_FUNCTIONS)
168 assert(IceUtilInternal::atomicExchangeAdd(&_ref, 0) > 0);
169
170 if (IceUtilInternal::atomicDecAndTestValue(&_ref, numberChilds) && !_noDelete)
171 {
172 _noDelete = true;
173 delete this;
174 }
175#elif ICE_INT_VERSION >= 30603
176 int c = _ref.fetch_sub(1);
177 assert(c > 0);
178
179 if ((c == numberChilds + 1) && !__hasFlag(NoDelete))
180 {
181 __setNoDelete(true);
182 delete this;
183 }
184#else
185 _mutex.lock();
186 bool doDelete = false;
187 assert(_ref > 0);
188
189 if (--_ref == numberChilds)
190 {
191 doDelete = !_noDelete;
192 _noDelete = true;
193 }
194
195 _mutex.unlock();
196
197 if (doDelete)
198 {
199 delete this;
200 }
201#endif
202 }
203} // namespace armarx
constexpr T c
void update(const Ice::Current &c=Ice::emptyCurrent) override
Updates the parent in the expression tree.
Definition TermImpl.cpp:76
void updateWithData(const Ice::Current &c=Ice::emptyCurrent) override
Definition TermImpl.cpp:85
StringVariantBaseMap getDatafields(const Ice::Current &) const override
Definition TermImpl.cpp:64
static int atomicDecAndTestValue(volatile int *counter, int value)
atomicDecAndTestValue - decrement and test @v: pointer of type AtomicCounter @value: value to test fo...
Definition TermImpl.cpp:127
bool getValue(const Ice::Current &c=Ice::emptyCurrent) const override
retrieve current value of term.
Definition TermImpl.cpp:58
~TermImpl() override
Destructor of TermImpl.
Definition TermImpl.cpp:31
void addChild(const TermImplBasePtr &child, const Ice::Current &c=Ice::emptyCurrent) override
Add child to term.
Definition TermImpl.cpp:38
void __incRef() override
Overwritten version of reference counting from GCShared.
Definition TermImpl.cpp:138
void resetParent(const Ice::Current &c=Ice::emptyCurrent) override
Reset the parent of this term.
Definition TermImpl.cpp:121
TermType getType(const Ice::Current &c=Ice::emptyCurrent) const override
Retrieve type of term.
Definition TermImpl.cpp:70
TermImplSequence getChilds(const Ice::Current &c=Ice::emptyCurrent) override
retrieve childs of this term in the expression tree.
Definition TermImpl.cpp:46
TermImplBasePtr getParent(const Ice::Current &c=Ice::emptyCurrent) override
retrieve parent of this term in the expression tree.
Definition TermImpl.cpp:52
void removeChildren()
Definition TermImpl.cpp:94
void __decRef() override
Overwritten version of reference counting from GCShared.
Definition TermImpl.cpp:144
void setParent(const TermImplBasePtr &parent, const Ice::Current &c=Ice::emptyCurrent) override
Sets the parent for this term in the expression tree.
Definition TermImpl.cpp:115
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::map< std::string, VariantBasePtr > StringVariantBaseMap