hobbes
a language, embedded compiler, and runtime for efficient dynamic expression evaluation, data storage and analysis
ptr.H
Go to the documentation of this file.
1 
2 #ifndef HOBBES_UTIL_PTR_HPP_INCLUDED
3 #define HOBBES_UTIL_PTR_HPP_INCLUDED
4 
5 #include <hobbes/util/hash.H>
6 #include <memory>
7 #include <vector>
8 #include <unordered_map>
9 #include <map>
10 #include <iostream>
11 #include <atomic>
12 #include <mutex>
13 
14 namespace hobbes {
15 
16 template <typename T, typename ... Args>
18  public:
19  typedef T object_type;
20 
21  const std::shared_ptr<T>& get(const std::function<T*(Args...)>& mk, const Args&... args) {
22  std::lock_guard<std::mutex> lock(mutex);
23  auto k = std::tuple<Args...>(args...);
24  const auto& r = this->values[k];
25  if (r) return r;
26 
27  this->values[k] = std::shared_ptr<T>(mk(args...));
28  return this->values[k];
29  }
30 
31  size_t compact() {
32  size_t c = 0;
33  std::lock_guard<std::mutex> lock(mutex);
34  for (typename Values::iterator v = this->values.begin(); v != this->values.end();) {
35  if (v->second.use_count() == 1) {
36  this->values.erase(v++);
37  ++c;
38  } else {
39  ++v;
40  }
41  }
42  return c;
43  }
44  private:
45  std::mutex mutex;
46  typedef std::unordered_map<std::tuple<Args...>, std::shared_ptr<T>, genHash<std::tuple<Args...>>> Values;
48  };
49 
50 template <size_t i, typename ... UniqueRefcMaps>
51  inline typename std::enable_if<i == sizeof...(UniqueRefcMaps), size_t>::type compactAll(std::tuple<UniqueRefcMaps...>&) {
52  return 0;
53  }
54 
55 template <size_t i, typename ... UniqueRefcMaps>
56  inline typename std::enable_if<i < sizeof...(UniqueRefcMaps), size_t>::type compactAll(std::tuple<UniqueRefcMaps...>& ms) {
57  return std::get<i>(ms).compact() + compactAll<i+1, UniqueRefcMaps...>(ms);
58  }
59 
60 template <size_t i, typename T, typename ... Ts>
61  struct TypeIndex {
62  static const size_t value = i;
63  };
64 
65 template <size_t i, typename T, typename ... Ts>
66  struct TypeIndex<i, T, T, Ts...> {
67  static const size_t value = i;
68  };
69 
70 template <size_t i, typename T, typename U, typename ... Ts>
71  struct TypeIndex<i, T, U, Ts...> {
72  static const size_t value = TypeIndex<i+1, T, Ts...>::value;
73  };
74 
75 template <typename ... UniqueRefcMaps>
76  class unique_refc_maps {
77  private:
78  typedef std::tuple<UniqueRefcMaps...> RefcMaps;
79  RefcMaps refcMaps;
80  public:
81  template <typename T>
82  typename std::tuple_element<TypeIndex<0, T, UniqueRefcMaps...>::value, RefcMaps>::type& at() {
83  return std::get<TypeIndex<0, T, UniqueRefcMaps...>::value>(this->refcMaps);
84  }
85 
86  size_t compact() {
87  size_t tc = 0;
88  while (true) {
89  size_t c = compactAll<0>(this->refcMaps);
90  tc += c;
91  if (c == 0) break;
92  }
93  return tc;
94  }
95  };
96 
97 template <typename T>
98  T align(T x, T m) {
99  if (m == 0 || (x % m) == 0) {
100  return x;
101  } else {
102  return (1 + (x / m)) * m;
103  }
104  }
105 
106 template <typename T, typename S>
107  const T* is(const S* s) {
108  if (s && s->case_id() == T::type_case_id) {
109  return (const T*)s;
110  } else {
111  return 0;
112  }
113  }
114 
115 template <typename T, typename S>
116  T* isM(S* s) {
117  if (s && s->case_id() == T::type_case_id) {
118  return (T*)s;
119  } else {
120  return 0;
121  }
122  }
123 
124 template <typename T, typename S>
125  T* is(const std::shared_ptr<S>& s) {
126  return isM<T, S>(s.get());
127  }
128 
129 }
130 
131 #endif
T align(T x, T m)
Definition: ptr.H:98
std::mutex mutex
Definition: ptr.H:45
Definition: hash.H:17
Definition: ptr.H:17
const T * is(const S *s)
Definition: ptr.H:107
size_t compact()
Definition: ptr.H:31
Definition: boot.H:7
std::enable_if< i< sizeof...(UniqueRefcMaps), size_t >::type compactAll(std::tuple< UniqueRefcMaps... > &ms) { return std::get< i >ms).compact()+compactAll< i+1, UniqueRefcMaps... >ms);}template< size_t i, typename T, typename ... Ts > struct TypeIndex { static const size_t value=i;};template< size_t i, typename T, typename ... Ts > struct TypeIndex< i, T, T, Ts... > { static const size_t value=i;};template< size_t i, typename T, typename U, typename ... Ts > struct TypeIndex< i, T, U, Ts... > { static const size_t value=TypeIndex< i+1, T, Ts... >::value;};template< typename ... UniqueRefcMaps > class unique_refc_maps { private:typedef std::tuple< UniqueRefcMaps... > RefcMaps;RefcMaps refcMaps;public:template< typename T > typename std::tuple_element< TypeIndex< 0, T, UniqueRefcMaps... >::value, RefcMaps >::type &at() { return std::get< TypeIndex< 0, T, UniqueRefcMaps... >::value >this-> refcMaps
Definition: ptr.H:83
Values values
Definition: ptr.H:47
T * get(variant< Ctors... > &v)
Definition: variant.H:156
std::unordered_map< std::tuple< Args... >, std::shared_ptr< T >, genHash< std::tuple< Args... > > > Values
Definition: ptr.H:46
T * isM(S *s)
Definition: ptr.H:116
size_t r(const reader::MetaData &md, size_t o, T *t)
Definition: storage.H:1730
T object_type
Definition: ptr.H:19
std::enable_if< i==sizeof...(UniqueRefcMaps), size_t >::type compactAll(std::tuple< UniqueRefcMaps... > &)
Definition: ptr.H:51
constexpr char at(size_t i, const char(&s)[N])
Definition: storage.H:1456
std::map< std::string, llvm::Value * > Args
Definition: dfa.C:1276
MonoTypePtr tuple(const MonoTypes &mtys=MonoTypes())
Definition: type.H:1068
LexicalAnnotation m(const YYLTYPE &p)
Definition: hexpr.parse.C:127