replicode
utils.h
Go to the documentation of this file.
1 // utils.h
2 //
3 // Author: Eric Nivel
4 //
5 // BSD license:
6 // Copyright (c) 2010, Eric Nivel
7 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are met:
10 //
11 // - Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // - Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 // - Neither the name of Eric Nivel nor the
17 // names of their contributors may be used to endorse or promote products
18 // derived from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
24 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef r_code_utils_h
32 #define r_code_utils_h
33 
34 #include "atom.h"
35 #include "CoreLibrary/base.h"
36 #include <iostream>
37 
38 using namespace core;
39 
40 namespace r_code {
41 
42 // For use in STL containers.
43 template<class C> class PHash {
44 public:
45  size_t operator()(P<C> c) const {
46  return (size_t)(C *)c;
47  }
48 };
49 
50 // Debugging facility.
52  public std::ostream {
53 public:
54  NullOStream(): std::ostream(NULL) {}
55  template<typename T> NullOStream& operator <<(T &t) {
56  return *this;
57  }
58 };
59 
60 class Code;
61 
63 private:
64  static uint64_t TimeReference; // starting time.
66  static double FloatTolerance;
68 public:
69  static uint64_t GetTimeReference();
70  static uint64_t GetBasePeriod();
71  static uint64_t GetFloatTolerance();
72  static uint64_t GetTimeTolerance();
73  static void SetReferenceValues(uint64_t base_period, double float_tolerance, double time_tolerance);
74  static void SetTimeReference(uint64_t time_reference);
75 
76  static bool Equal(double l, double r);
77  static bool Synchronous(uint64_t l, uint64_t r);
78 
79  static uint64_t GetTimestamp(const Atom *iptr);
80  static void SetTimestamp(Atom *iptr, uint64_t t);
81  static void SetTimestamp(Code *object, uint32_t index, uint64_t t); // allocates atoms.
82 
83  static const uint64_t MaxTime = 0xFFFFFFFFFFFFFFFF;
84  static const uint64_t MaxTHZ = 0xFFFFFFFF;
85 
86  template<class O> static uint64_t GetTimestamp(const O *object, uint16_t index)
87  {
88  uint16_t t_index = object->code(index).asIndex();
89  uint64_t high = object->code(t_index + 1).atom;
90  return high << 32 | object->code(t_index + 2).atom;
91  }
92 
93  template<class O> static void SetTimestamp(O *object, uint16_t index, uint64_t t)
94  {
95  uint16_t t_index = object->code(index).asIndex();
96  object->code(t_index) = Atom::Timestamp();
97  object->code(t_index + 1).atom = t >> 32;
98  object->code(t_index + 2).atom = t & 0x00000000FFFFFFFF;
99  }
100 
101  static std::string GetString(const Atom *iptr);
102  static void SetString(Atom *iptr, const std::string &s);
103 
104  template<class O> static std::string GetString(const O *object, uint16_t index) {
105 
106  uint16_t s_index = object->code(index).asIndex();
107  char buffer[255];
108  uint8_t char_count = (object->code(s_index).atom & 0x000000FF);
109  buffer[char_count] = 0;
110  for (int i = 0; i < char_count; i += 4) {
111  uint64_t val = object->code(s_index + 1 + i / 4);
112  buffer[i] = (val & 0x000000ff);
113  buffer[i + 1] = (val & 0x0000ff00) >> 8;
114  buffer[i + 2] = (val & 0x00ff0000) >> 16;
115  buffer[i + 3] = (val & 0xff000000) >> 24;
116  }
117  return std::string(buffer);
118  }
119 
120  template<class O> static void SetString(O *object, uint16_t index, const std::string &s) {
121 
122  uint16_t s_index = object->code(index).asIndex();
123  uint8_t l = (uint8_t)s.length();
124  object->code(s_index) = Atom::String(l);
125  uint64_t _st = 0;
126  int8_t shift = 0;
127  for (uint8_t i = 0; i < l; ++i) {
128 
129  _st |= s[i] << shift;
130  shift += 8;
131  if (shift == 32) {
132 
133  object->code(++s_index) = _st;
134  _st = 0;
135  shift = 0;
136  }
137  }
138  if (l % 4)
139  object->code(++s_index) = _st;
140  }
141 
142  static int64_t GetResilience(uint64_t now, uint64_t time_to_live, uint64_t upr); // ttl: us, upr: us.
143  static int64_t GetGroupResilience(double resilience, double origin_upr, double destination_upr); // express the res in destination group, given the res in origin group.
144 
145  static std::string RelativeTime(uint64_t t);
146 };
147 }
148 
149 
150 #endif
Definition: utils.h:51
std::ostream & operator<<(std::ostream &os, RepliStruct *structure)
Definition: replistruct.cpp:718
static void SetTimestamp(O *object, uint16_t index, uint64_t t)
Definition: utils.h:93
bool now(const Context &context, uint16_t &index)
Definition: operator.cpp:55
static double FloatTolerance
Definition: utils.h:66
static uint64_t TimeTolerance
Definition: utils.h:67
Definition: base.cpp:35
#define dll_export
Definition: dll.h:44
size_t operator()(P< C > c) const
Definition: utils.h:45
static uint64_t BasePeriod
Definition: utils.h:65
Definition: atom.cpp:36
Definition: base.h:47
static void SetString(O *object, uint16_t index, const std::string &s)
Definition: utils.h:120
Definition: utils.h:43
static uint64_t GetTimestamp(const O *object, uint16_t index)
Definition: utils.h:86
Definition: atom.h:45
static std::string GetString(const O *object, uint16_t index)
Definition: utils.h:104
Definition: utils.h:62
static uint64_t TimeReference
Definition: utils.h:64
NullOStream()
Definition: utils.h:54
Definition: object.h:172