85 #ifndef r_code_utils_h
86 #define r_code_utils_h
89 #include "../submodules/CoreLibrary/CoreLibrary/base.h"
90 #include "../submodules/CoreLibrary/CoreLibrary/utils.h"
98 size_t operator ()(
P<C> c)
const {
99 return (
size_t)(C *)c;
105 public std::ostream {
108 template<
typename T>
NullOStream& operator <<(T &t) {
117 static Timestamp TimeReference;
118 static std::chrono::microseconds BasePeriod;
119 static float32 FloatTolerance;
120 static std::chrono::microseconds TimeTolerance;
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);
129 static bool Equal(float32 l, float32 r);
130 static bool Synchronous(Timestamp l, Timestamp r);
139 uint64 high = code[index].atom_;
140 return high << 32 | code[index + 1].atom_;
150 code[index].atom_ = (uint64)value >> 32;
151 code[index + 1].atom_ = value & 0x00000000FFFFFFFF;
160 static Timestamp GetTimestamp(
const Atom *iptr);
162 static std::chrono::microseconds GetMicrosecondsSinceEpoch(
const Atom *iptr) {
163 return std::chrono::duration_cast<std::chrono::microseconds>(GetTimestamp(iptr).time_since_epoch());
172 static void SetTimestamp(Atom *iptr, Timestamp timestamp);
181 static void SetTimestampStruct(Code *
object, uint16 index, Timestamp timestamp);
183 static const uint64 MaxTHZ = 0xFFFFFFFF;
185 template<
class O>
static bool HasTimestamp(
const O *
object, uint16 index) {
186 if (object->code_size() <= index)
188 Atom a =
object->code(index);
189 if (a.getDescriptor() != Atom::I_PTR)
191 uint16 t_index = a.asIndex();
192 if (object->code_size() <= t_index + 2)
194 return object->code(t_index).getDescriptor() == Atom::TIMESTAMP;
204 template<
class O>
static Timestamp
GetTimestamp(
const O *
object, uint16 index) {
206 uint16 t_index =
object->code(index).asIndex();
207 return Timestamp(std::chrono::microseconds(GetInt64(&object->code(0), t_index + 1)));
217 template<
class O>
static void SetTimestamp(O *
object, uint16 index, Timestamp timestamp) {
219 uint16 t_index =
object->code(index).asIndex();
220 object->code(t_index) = Atom::Timestamp();
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());
234 static std::string ToString_s_ms_us(Timestamp timestamp, Timestamp time_reference);
243 return std::chrono::microseconds(GetInt64(iptr, 1));
252 iptr[0] = Atom::Duration();
253 SetInt64(iptr, 1, duration.count());
263 static void SetDurationStruct(
Code *
object, uint16 index, std::chrono::microseconds duration);
274 template<
class O>
static std::chrono::microseconds
GetDuration(
const O *
object, uint16 index) {
276 uint16 t_index =
object->code(index).asIndex();
277 return std::chrono::microseconds(GetInt64(&object->code(0), t_index + 1));
287 template<
class O>
static void SetDuration(O *
object, uint16 index, std::chrono::microseconds duration) {
289 uint16 t_index =
object->code(index).asIndex();
290 object->code(t_index) = Atom::Duration();
292 object->code(t_index + 2).atom_ = 0;
293 SetInt64(&object->code(0), t_index + 1, duration.count());
303 static std::string ToString_us(std::chrono::microseconds duration);
305 static std::string GetString(
const Atom *iptr);
306 static void SetString(
Atom *iptr,
const std::string &s);
308 template<
class O>
static std::string GetString(
const O *
object, uint16 index) {
310 uint16 s_index =
object->code(index).asIndex();
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;
320 template<
class O>
static void SetString(O *
object, uint16 index,
const std::string &s) {
322 uint16 s_index =
object->code(index).asIndex();
323 uint8 l = (uint8)s.length();
324 object->code(s_index) = Atom::String(l);
327 for (uint8 i = 0; i < l; ++i) {
333 object->code(++s_index) = st;
339 object->code(++s_index) = st;
342 static int32 GetResilience(Timestamp now, std::chrono::microseconds time_to_live, uint64 upr);
343 static int32 GetResilience(float32 resilience, float32 origin_upr, float32 destination_upr);
345 static std::string RelativeTime(Timestamp t);
353 static bool has_reference(
const Atom* code, uint16 index);
357 const Timestamp Utils_MaxTime(std::chrono::microseconds(9223372036854775807LL));