/home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/stable/src/ds/include/RAT/DS/RootFactory.hh Source File

Ratpac-two: /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/stable/src/ds/include/RAT/DS/RootFactory.hh Source File
Ratpac-two
RootFactory.hh
1 #ifndef RAT_DS_ROOTFACTORY_HH
2 #define RAT_DS_ROOTFACTORY_HH
3 
4 #include <TClass.h>
5 
6 #include <stdexcept>
7 #include <string>
8 
9 #include "RAT/DS/Root.hh"
10 
11 namespace RAT::DS {
12 class RootFactory {
13  public:
14  static void SetClassName(const std::string& name) { ClassName() = name; }
15 
16  static Root* Create() {
17  const std::string& name = ClassName();
18  if (name.empty() || name == "RAT::DS::Root") return new Root(); // default: base type
19 
20  TClass* cl = TClass::GetClass(name.c_str());
21  if (!cl) throw std::runtime_error("RootFactory: no dictionary for " + name);
22  if (!cl->InheritsFrom(Root::Class())) throw std::runtime_error("RootFactory: " + name + " is not a RAT::DS::Root");
23 
24  void* obj = cl->New(); // uses the I/O default ctor
25  if (!obj) throw std::runtime_error("RootFactory: New() failed for " + name);
26 
27  // NOT static_cast<Root*>(obj) — see below
28  return static_cast<Root*>(cl->DynamicCast(Root::Class(), obj, kTRUE));
29  }
30 
31  static const std::string& GetClassName() { return ClassName(); }
32 
33  private:
34  static std::string& ClassName() {
35  static std::string n;
36  return n;
37  }
38 };
39 } // namespace RAT::DS
40 
41 #endif
Definition: RootFactory.hh:12
Definition: Root.hh:37