RAJA
RAJA provides a collection of platform portability abstractions for C++ HPC applications.
Registry.hpp
Go to the documentation of this file.
1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
2 // Copyright (c) Lawrence Livermore National Security, LLC and other
3 // RAJA Project Developers. See top-level LICENSE and COPYRIGHT
4 // files for dates and other details. No copyright assignment is required
5 // to contribute to RAJA.
6 //
7 // SPDX-License-Identifier: (BSD-3-Clause)
8 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
9 
10 #ifndef RAJA_Registry_HPP
11 #define RAJA_Registry_HPP
12 
13 #include <memory>
14 
15 namespace RAJA
16 {
17 namespace util
18 {
19 
20 template<typename T>
22 {
23  std::string Name, Desc;
24  std::shared_ptr<T> object;
25 
26 public:
27  RegistryEntry(const std::string& N,
28  const std::string& D,
29  std::shared_ptr<T> (*C)())
30  : Name(N),
31  Desc(D),
32  object(C())
33  {}
34 
35  const std::string& getName() const { return Name; }
36 
37  const std::string& getDesc() const { return Desc; }
38 
39  T* get() const { return object.get(); }
40 };
41 
45 template<typename T>
46 class Registry
47 {
48 public:
49  using type = T;
51 
52  class node;
53  class iterator;
54 
55 private:
56  Registry() = delete;
57 
58  friend class node;
59  static node *Head, *Tail;
60 
61 public:
64  class node
65  {
66  friend class iterator;
67  friend Registry<T>;
68 
69  node* Next;
70  const entry& Val;
71 
72  public:
73  node(const entry& V) : Next(nullptr), Val(V) {}
74  };
75 
83  static RAJASHAREDDLL_API void add_node(node* N);
84 
87  class iterator
88  {
89  const node* Cur;
90 
91  public:
92  explicit iterator(const node* N) : Cur(N) {}
93 
94  bool operator==(const iterator& That) const { return Cur == That.Cur; }
95 
96  bool operator!=(const iterator& That) const { return Cur != That.Cur; }
97 
99  {
100  Cur = Cur->Next;
101  return *this;
102  }
103 
104  const entry& operator*() const { return Cur->Val; }
105 
106  const entry* operator->() const { return &Cur->Val; }
107  };
108 
109  // begin is not defined here in order to avoid usage of an undefined static
110  // data member, instead it's instantiated by RAJA_INSTANTIATE_REGISTRY.
111  static RAJASHAREDDLL_API iterator begin();
112 
113  static iterator end() { return iterator(nullptr); }
114 
116  template<typename V>
117  class add
118  {
119  entry Entry;
120  node Node;
121 
122  static std::shared_ptr<T> CtorFn() { return std::make_shared<V>(); }
123 
124  public:
125  add(const std::string& Name, const std::string& Desc)
126  : Entry(Name, Desc, CtorFn),
127  Node(Entry)
128  {
129  add_node(&Node);
130  }
131  };
132 };
133 
134 } // namespace util
135 } // namespace RAJA
136 
137 #define RAJA_INSTANTIATE_REGISTRY(REGISTRY_CLASS) \
138  namespace RAJA \
139  { \
140  namespace util \
141  { \
142  template<typename T> \
143  typename Registry<T>::node* Registry<T>::Head = nullptr; \
144  template<typename T> \
145  typename Registry<T>::node* Registry<T>::Tail = nullptr; \
146  template<typename T> \
147  void Registry<T>::add_node(typename Registry<T>::node* N) \
148  { \
149  if (Tail) \
150  Tail->Next = N; \
151  else \
152  Head = N; \
153  Tail = N; \
154  } \
155  template<typename T> \
156  typename Registry<T>::iterator Registry<T>::begin() \
157  { \
158  return iterator(Head); \
159  } \
160  template REGISTRY_CLASS::node* Registry<REGISTRY_CLASS::type>::Head; \
161  template REGISTRY_CLASS::node* Registry<REGISTRY_CLASS::type>::Tail; \
162  template void Registry<REGISTRY_CLASS::type>::add_node( \
163  REGISTRY_CLASS::node*); \
164  template REGISTRY_CLASS::iterator Registry<REGISTRY_CLASS::type>::begin(); \
165  } \
166  }
167 
168 #endif
Definition: Registry.hpp:22
T * get() const
Definition: Registry.hpp:39
const std::string & getName() const
Definition: Registry.hpp:35
RegistryEntry(const std::string &N, const std::string &D, std::shared_ptr< T >(*C)())
Definition: Registry.hpp:27
const std::string & getDesc() const
Definition: Registry.hpp:37
A static registration template.
Definition: Registry.hpp:118
add(const std::string &Name, const std::string &Desc)
Definition: Registry.hpp:125
Definition: Registry.hpp:88
const entry & operator*() const
Definition: Registry.hpp:104
bool operator!=(const iterator &That) const
Definition: Registry.hpp:96
iterator(const node *N)
Definition: Registry.hpp:92
const entry * operator->() const
Definition: Registry.hpp:106
bool operator==(const iterator &That) const
Definition: Registry.hpp:94
iterator & operator++()
Definition: Registry.hpp:98
Definition: Registry.hpp:65
node(const entry &V)
Definition: Registry.hpp:73
Definition: Registry.hpp:47
T type
Definition: Registry.hpp:49
static RAJASHAREDDLL_API iterator begin()
static RAJASHAREDDLL_API void add_node(node *N)
static iterator end()
Definition: Registry.hpp:113
Definition: AlignedRangeIndexSetBuilders.cpp:35
auto Name(const char *n)
Definition: kernel_name.hpp:31