AERA
object.tpl.cpp
1 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 //_/_/
3 //_/_/ AERA
4 //_/_/ Autocatalytic Endogenous Reflective Architecture
5 //_/_/
6 //_/_/ Copyright (c) 2018-2025 Jeff Thompson
7 //_/_/ Copyright (c) 2018-2025 Kristinn R. Thorisson
8 //_/_/ Copyright (c) 2018-2025 Icelandic Institute for Intelligent Machines
9 //_/_/ http://www.iiim.is
10 //_/_/
11 //_/_/ Copyright (c) 2010-2012 Eric Nivel
12 //_/_/ Center for Analysis and Design of Intelligent Agents
13 //_/_/ Reykjavik University, Menntavegur 1, 102 Reykjavik, Iceland
14 //_/_/ http://cadia.ru.is
15 //_/_/
16 //_/_/ Part of this software was developed by Eric Nivel
17 //_/_/ in the HUMANOBS EU research project, which included
18 //_/_/ the following parties:
19 //_/_/
20 //_/_/ Autonomous Systems Laboratory
21 //_/_/ Technical University of Madrid, Spain
22 //_/_/ http://www.aslab.org/
23 //_/_/
24 //_/_/ Communicative Machines
25 //_/_/ Edinburgh, United Kingdom
26 //_/_/ http://www.cmlabs.com/
27 //_/_/
28 //_/_/ Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
29 //_/_/ University of Lugano and SUPSI, Switzerland
30 //_/_/ http://www.idsia.ch/
31 //_/_/
32 //_/_/ Institute of Cognitive Sciences and Technologies
33 //_/_/ Consiglio Nazionale delle Ricerche, Italy
34 //_/_/ http://www.istc.cnr.it/
35 //_/_/
36 //_/_/ Dipartimento di Ingegneria Informatica
37 //_/_/ University of Palermo, Italy
38 //_/_/ http://diid.unipa.it/roboticslab/
39 //_/_/
40 //_/_/
41 //_/_/ --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
42 //_/_/
43 //_/_/ Redistribution and use in source and binary forms, with or without
44 //_/_/ modification, is permitted provided that the following conditions
45 //_/_/ are met:
46 //_/_/ - Redistributions of source code must retain the above copyright
47 //_/_/ and collaboration notice, this list of conditions and the
48 //_/_/ following disclaimer.
49 //_/_/ - Redistributions in binary form must reproduce the above copyright
50 //_/_/ notice, this list of conditions and the following disclaimer
51 //_/_/ in the documentation and/or other materials provided with
52 //_/_/ the distribution.
53 //_/_/
54 //_/_/ - Neither the name of its copyright holders nor the names of its
55 //_/_/ contributors may be used to endorse or promote products
56 //_/_/ derived from this software without specific prior
57 //_/_/ written permission.
58 //_/_/
59 //_/_/ - CADIA Clause: The license granted in and to the software
60 //_/_/ under this agreement is a limited-use license.
61 //_/_/ The software may not be used in furtherance of:
62 //_/_/ (i) intentionally causing bodily injury or severe emotional
63 //_/_/ distress to any person;
64 //_/_/ (ii) invading the personal privacy or violating the human
65 //_/_/ rights of any person; or
66 //_/_/ (iii) committing or preparing for any act of war.
67 //_/_/
68 //_/_/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
69 //_/_/ CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
70 //_/_/ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
71 //_/_/ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
72 //_/_/ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
73 //_/_/ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
74 //_/_/ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
75 //_/_/ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
76 //_/_/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
77 //_/_/ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
78 //_/_/ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
79 //_/_/ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
80 //_/_/ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
81 //_/_/ OF SUCH DAMAGE.
82 //_/_/
83 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
84 
85 namespace r_exec {
86 
87 template<class C, class U> Object<C, U>::Object() : C(), hash_value_(0), invalidated_(0) {
88 }
89 
90 template<class C, class U> Object<C, U>::Object(r_code::Mem* /* mem */) : C(), hash_value_(0), invalidated_(0) {
91 
92  C::set_oid(UNDEFINED_OID);
93 }
94 
95 template<class C, class U> Object<C, U>::~Object() {
96 
97  invalidate();
98 }
99 
100 template<class C, class U> bool Object<C, U>::is_invalidated() {
101 
102  return invalidated_ == 1;
103 }
104 
105 template<class C, class U> bool Object<C, U>::invalidate() {
106 
107  if (invalidated_)
108  return true;
109  invalidated_ = 1;
110 
111  acq_views();
112  C::views_.clear();
113  rel_views();
114 
115  if (C::code(0).getDescriptor() == Atom::MARKER) {
116 
117  for (uint16 i = 0; i < C::references_size(); ++i)
118  C::get_reference(i)->remove_marker(this);
119  }
120 
121  if (C::is_registered())
122  r_code::Mem::Get()->delete_object(this);
123 
124  return false;
125 }
126 
127 template<class C, class U> void Object<C, U>::compute_hash_value() {
128 
129  hash_value_ = C::code(0).asOpcode() << 20; // 12 bits for the opcode.
130  hash_value_ |= (C::code_size() & 0x00000FFF) << 8; // 12 bits for the code size.
131  hash_value_ |= C::references_size() & 0x000000FF; // 8 bits for the reference set size.
132 }
133 
134 template<class C, class U> float32 Object<C, U>::get_psln_thr() {
135 
136  psln_thrCS_.enter();
137  float32 r = C::code(C::code(0).getAtomCount()).asFloat(); // psln is always the last member of an object.
138  psln_thrCS_.leave();
139  return r;
140 }
141 
142 template<class C, class U> void Object<C, U>::mod(uint16 member_index, float32 value) {
143 
144  if (member_index != C::code_size() - 1)
145  return;
146  float32 v = C::code(member_index).asFloat() + value;
147  if (v < 0)
148  v = 0;
149  else if (v > 1)
150  v = 1;
151 
152  psln_thrCS_.enter();
153  C::code(member_index) = Atom::Float(v);
154  psln_thrCS_.leave();
155 }
156 
157 template<class C, class U> void Object<C, U>::set(uint16 member_index, float32 value) {
158 
159  if (member_index != C::code_size() - 1)
160  return;
161 
162  psln_thrCS_.enter();
163  C::code(member_index) = Atom::Float(value);
164  psln_thrCS_.leave();
165 }
166 
167 template<class C, class U> r_code::_View *Object<C, U>::get_view(r_code::Code *group, bool lock) {
168 
169  if (lock)
170  acq_views();
171 
172  r_code::_View probe;
173  probe.references_[0] = group;
174 
175  std::unordered_set<r_code::_View *, r_code::_View::Hash, r_code::_View::Equal>::const_iterator v = C::views_.find(&probe);
176  if (v != C::views_.end()) {
177 
178  if (lock)
179  rel_views();
180  return (r_exec::View *)*v;
181  }
182 
183  if (lock)
184  rel_views();
185  return NULL;
186 }
187 }
r_code::_View
Definition: r_code/object.h:164
r_code::Code
Definition: r_code/object.h:224
r_exec::View
Definition: view.h:102
r_code::Mem
Definition: r_code/object.h:420