AERA
r_code/utils.h
1 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 //_/_/
3 //_/_/ AERA
4 //_/_/ Autocatalytic Endogenous Reflective Architecture
5 //_/_/
6 //_/_/ Copyright (c) 2018-2023 Jeff Thompson
7 //_/_/ Copyright (c) 2018-2023 Kristinn R. Thorisson
8 //_/_/ Copyright (c) 2018-2023 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 #ifndef r_code_utils_h
86 #define r_code_utils_h
87 
88 #include "atom.h"
89 #include "../submodules/CoreLibrary/CoreLibrary/base.h"
90 #include "../submodules/CoreLibrary/CoreLibrary/utils.h"
91 
92 
93 namespace r_code {
94 
95 // For use in STL containers.
96 template<class C> class PHash {
97 public:
98  size_t operator ()(P<C> c) const {
99  return (size_t)(C *)c;
100  }
101 };
102 
103 // Debugging facility.
104 class NullOStream :
105  public std::ostream {
106 public:
107  NullOStream() : std::ostream(NULL) {}
108  template<typename T> NullOStream& operator <<(T &t) {
109  return *this;
110  }
111 };
112 
113 class Code;
114 
115 class dll_export Utils {
116 private:
117  static Timestamp TimeReference; // starting time.
118  static std::chrono::microseconds BasePeriod;
119  static float32 FloatTolerance;
120  static std::chrono::microseconds TimeTolerance;
121 public:
122  static Timestamp GetTimeReference();
123  static std::chrono::microseconds GetBasePeriod();
124  static uint32 GetFloatTolerance();
125  static std::chrono::microseconds GetTimeTolerance();
126  static void SetReferenceValues(std::chrono::microseconds base_period, float32 float_tolerance, std::chrono::microseconds time_tolerance);
127  static void SetTimeReference(Timestamp time_reference);
128 
129  static bool Equal(float32 l, float32 r);
130  static bool Synchronous(Timestamp l, Timestamp r);
131 
138  static int64 GetInt64(const Atom *code, size_t index) {
139  uint64 high = code[index].atom_;
140  return high << 32 | code[index + 1].atom_;
141  }
142 
149  static void SetInt64(Atom *code, size_t index, int64 value) {
150  code[index].atom_ = (uint64)value >> 32;
151  code[index + 1].atom_ = value & 0x00000000FFFFFFFF;
152  }
153 
160  static Timestamp GetTimestamp(const Atom *iptr);
161 
162  static std::chrono::microseconds GetMicrosecondsSinceEpoch(const Atom *iptr) {
163  return std::chrono::duration_cast<std::chrono::microseconds>(GetTimestamp(iptr).time_since_epoch());
164  }
165 
172  static void SetTimestamp(Atom *iptr, Timestamp timestamp);
173 
181  static void SetTimestampStruct(Code *object, uint16 index, Timestamp timestamp); // Expands object->code to allocate atoms.
182 
183  static const uint64 MaxTHZ = 0xFFFFFFFF;
184 
185  template<class O> static bool HasTimestamp(const O *object, uint16 index) {
186  if (object->code_size() <= index)
187  return false;
188  Atom a = object->code(index);
189  if (a.getDescriptor() != Atom::I_PTR)
190  return false;
191  uint16 t_index = a.asIndex();
192  if (object->code_size() <= t_index + 2)
193  return false;
194  return object->code(t_index).getDescriptor() == Atom::TIMESTAMP;
195  }
196 
204  template<class O> static Timestamp GetTimestamp(const O *object, uint16 index) {
205 
206  uint16 t_index = object->code(index).asIndex();
207  return Timestamp(std::chrono::microseconds(GetInt64(&object->code(0), t_index + 1)));
208  }
209 
217  template<class O> static void SetTimestamp(O *object, uint16 index, Timestamp timestamp) {
218 
219  uint16 t_index = object->code(index).asIndex();
220  object->code(t_index) = Atom::Timestamp();
221  // This will resize the code array if needed.
222  object->code(t_index + 2).atom_ = 0;
223  SetInt64(&object->code(0), t_index + 1, std::chrono::duration_cast<std::chrono::microseconds>(timestamp.time_since_epoch()).count());
224  }
225 
234  static std::string ToString_s_ms_us(Timestamp timestamp, Timestamp time_reference);
235 
242  static std::chrono::microseconds GetDuration(const Atom *iptr) {
243  return std::chrono::microseconds(GetInt64(iptr, 1));
244  }
245 
251  static void SetDuration(Atom *iptr, std::chrono::microseconds duration) {
252  iptr[0] = Atom::Duration();
253  SetInt64(iptr, 1, duration.count());
254  }
255 
263  static void SetDurationStruct(Code *object, uint16 index, std::chrono::microseconds duration);
264 
274  template<class O> static std::chrono::microseconds GetDuration(const O *object, uint16 index) {
275 
276  uint16 t_index = object->code(index).asIndex();
277  return std::chrono::microseconds(GetInt64(&object->code(0), t_index + 1));
278  }
279 
287  template<class O> static void SetDuration(O *object, uint16 index, std::chrono::microseconds duration) {
288 
289  uint16 t_index = object->code(index).asIndex();
290  object->code(t_index) = Atom::Duration();
291  // This will resize the code array if needed.
292  object->code(t_index + 2).atom_ = 0;
293  SetInt64(&object->code(0), t_index + 1, duration.count());
294  }
295 
303  static std::string ToString_us(std::chrono::microseconds duration);
304 
305  static std::string GetString(const Atom *iptr);
306  static void SetString(Atom *iptr, const std::string &s);
307 
308  template<class O> static std::string GetString(const O *object, uint16 index) {
309 
310  uint16 s_index = object->code(index).asIndex();
311  std::string s;
312  char buffer[255];
313  uint8 char_count = (object->code(s_index).atom_ & 0x000000FF);
314  memcpy(buffer, &object->code(s_index + 1), char_count);
315  buffer[char_count] = 0;
316  s += buffer;
317  return s;
318  }
319 
320  template<class O> static void SetString(O *object, uint16 index, const std::string &s) {
321 
322  uint16 s_index = object->code(index).asIndex();
323  uint8 l = (uint8)s.length();
324  object->code(s_index) = Atom::String(l);
325  uint32 st = 0;
326  int8 shift = 0;
327  for (uint8 i = 0; i < l; ++i) {
328 
329  st |= s[i] << shift;
330  shift += 8;
331  if (shift == 32) {
332 
333  object->code(++s_index) = st;
334  st = 0;
335  shift = 0;
336  }
337  }
338  if (l % 4)
339  object->code(++s_index) = st;
340  }
341 
342  static int32 GetResilience(Timestamp now, std::chrono::microseconds time_to_live, uint64 upr); // ttl: us, upr: us.
343  static int32 GetResilience(float32 resilience, float32 origin_upr, float32 destination_upr); // express the res in destination group, given the res in origin group.
344 
345  static std::string RelativeTime(Timestamp t);
346 
353  static bool has_reference(const Atom* code, uint16 index);
354 };
355 
356 // This is Timestamp::max() duration_cast to microseconds and used to initialize a Timestamp.
357 const Timestamp Utils_MaxTime(std::chrono::microseconds(9223372036854775807LL));
358 
359 }
360 
361 
362 #endif
r_code::Utils::SetDuration
static void SetDuration(O *object, uint16 index, std::chrono::microseconds duration)
Definition: r_code/utils.h:287
r_code::Utils::SetDuration
static void SetDuration(Atom *iptr, std::chrono::microseconds duration)
Definition: r_code/utils.h:251
r_code::Utils::GetDuration
static std::chrono::microseconds GetDuration(const O *object, uint16 index)
Definition: r_code/utils.h:274
r_code::Utils
Definition: r_code/utils.h:115
r_code::Utils::GetDuration
static std::chrono::microseconds GetDuration(const Atom *iptr)
Definition: r_code/utils.h:242
r_code::Atom
Definition: atom.h:104
r_code::NullOStream
Definition: r_code/utils.h:105
r_code::Utils::SetInt64
static void SetInt64(Atom *code, size_t index, int64 value)
Definition: r_code/utils.h:149
r_code::Utils::GetInt64
static int64 GetInt64(const Atom *code, size_t index)
Definition: r_code/utils.h:138
r_code::Utils::SetTimestamp
static void SetTimestamp(O *object, uint16 index, Timestamp timestamp)
Definition: r_code/utils.h:217
core::P
Definition: base.h:102
r_code::Code
Definition: r_code/object.h:224
r_code::Utils::GetTimestamp
static Timestamp GetTimestamp(const O *object, uint16 index)
Definition: r_code/utils.h:204
r_code::PHash
Definition: r_code/utils.h:96