NameStackRegistry.h
Go to the documentation of this file.
1 #ifndef fhiclcpp_types_detail_NameStackRegistry_h
2 #define fhiclcpp_types_detail_NameStackRegistry_h
3 
4 // ======================================================================
5 // 'NameStackRegistry' exists solely because C++ does not have the
6 // reflection facilities required to return the members of a struct or
7 // class, which is needed to walk through the members of a table. For
8 // example, consider the following struct and associated Table<S>
9 // object:
10 //
11 // struct S {
12 // Atom<int> num{Name("num")};
13 // Sequence<int> list{Name("list")};
14 // };
15 // Table<S> t{Name("t")};
16 //
17 // The keys for this type of configuration are:
18 //
19 // t
20 // t.num
21 // t.list
22 //
23 // C++ , however, does not have the ability to do this. The
24 // registration system below, however, does do this by taking the Name
25 // values above ("t", "num" and "list"), and emplacing them into a
26 // container that is assembled when each fhiclcpp parameter is
27 // constructed: first 't', then 'n', then 'l'.
28 //
29 // All fhiclcpp parameters register their names/keys via the
30 // ParameterBase base class, which, itself, has a member of type
31 // ParameterMetadata that stores the name/key.
32 //
33 // If the C++ reflection facilities improve to the level that a
34 // struct's or class's members can be returned (either at
35 // compile-time, or run-time), this registry should be removed.
36 // ======================================================================
37 
39 
40 #include <string>
41 #include <vector>
42 
43 namespace fhicl {
44 
46  public:
47  static std::string full_key(std::string const& key);
48 
49  static void
51  {
52  instance_().names_.pop_back();
53  }
54 
55  static void
57  {
58  instance_().names_.clear();
59  }
60 
61  static bool
63  {
64  return instance_().names_.empty();
65  }
66 
67  static std::string
69  {
70  return instance_().names_.back();
71  }
72 
73  private:
74  std::vector<std::string> names_{};
75 
76  static NameStackRegistry&
78  {
79  // The use of the registry is restricted to the construction of
80  // fhiclcpp types. As construction happens on only one thread,
81  // it is sufficient for each thread to have its own copy.
82  // Although a thread-local static would be appropriate here, not
83  // all implementations adequately support thread-local variables
84  // for the use case here. We thus use a custom-built per-thread
85  // cache.
87  return registry.slot_for_current_thread();
88  }
89  };
90 }
91 
92 #endif /* fhiclcpp_types_detail_NameStackRegistry_h */
93 
94 // Local variables:
95 // mode: c++
96 // End:
std::string string
Definition: nybbler.cc:12
def key(type, name=None)
Definition: graph.py:13
static std::string current()
static NameStackRegistry & instance_()
static std::string full_key(std::string const &key)
std::vector< std::string > names_