Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TGlobal.h
Go to the documentation of this file.
1 // @(#)root/meta:$Id$
2 // Author: Rene Brun 13/11/95
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TGlobal
13 #define ROOT_TGlobal
14 
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // TGlobal //
19 // //
20 // Global variables class (global variables are obtained from CINT). //
21 // //
22 //////////////////////////////////////////////////////////////////////////
23 
24 #include "TDictionary.h"
25 
26 #include <functional>
27 
28 class TGlobal : public TDictionary {
29 
30 private:
31  DataMemberInfo_t *fInfo; //!pointer to CINT data member info
32 
33 public:
34  TGlobal(DataMemberInfo_t *info = nullptr);
35  TGlobal (const TGlobal &);
36  TGlobal &operator=(const TGlobal &);
37 
38  virtual ~TGlobal();
39  virtual Int_t GetArrayDim() const;
40  virtual DeclId_t GetDeclId() const;
41  virtual Int_t GetMaxIndex(Int_t dim) const;
42  virtual void *GetAddress() const;
43  virtual const char *GetTypeName() const;
44  virtual const char *GetFullTypeName() const;
45  virtual Bool_t IsValid();
46  virtual Long_t Property() const;
47  virtual bool Update(DataMemberInfo_t *info);
48 
49  ClassDef(TGlobal,2) //Global variable class
50 };
51 
52 // Class to map the "funcky" globals and be able to add them to the list of globals.
53 class TGlobalMappedFunction : public TGlobal {
54 public:
55  typedef void *(*GlobalFunc_t)();
56  typedef std::function<void *()> GlobalFunctor_t;
57 
58  TGlobalMappedFunction(const char *name, const char *type, GlobalFunc_t funcPtr);
59 
60  virtual ~TGlobalMappedFunction() = default;
61  Int_t GetArrayDim() const override { return 0; }
62  DeclId_t GetDeclId() const override { return (DeclId_t)(fFuncPtr); } // Used as DeclId because of uniqueness
63  Int_t GetMaxIndex(Int_t /*dim*/) const override { return -1; }
64  void *GetAddress() const override { return !fFunctor ? (*fFuncPtr)() : fFunctor(); }
65  const char *GetTypeName() const override { return GetTitle(); }
66  const char *GetFullTypeName() const override { return GetTitle(); }
67  Long_t Property() const override { return 0; }
68  bool Update(DataMemberInfo_t * /*info*/) override { return false; }
69 
70  static void Add(TGlobalMappedFunction *gmf);
71 
72  template <typename GlobFunc>
73  static void MakeFunctor(const char *name, const char *type, GlobFunc &func)
74  {
75  auto glob = new TGlobalMappedFunction(name, type, (GlobalFunc_t)((void *)&func));
76  glob->fFunctor = [&func] {
77  auto &res = func();
78  return (void *)(&res);
79  };
80  Add(glob);
81  }
82 
83  template <typename GlobFunc>
84  static void MakeFunctor(const char *name, const char *type, GlobFunc &func, GlobalFunctor_t functor)
85  {
86  auto glob = new TGlobalMappedFunction(name, type, (GlobalFunc_t)((void *)&func));
87  glob->fFunctor = functor;
88  Add(glob);
89  }
90 
91 private:
92  GlobalFunc_t fFuncPtr{nullptr}; // Function to call to get the address
93 
94  GlobalFunctor_t fFunctor; // functor which correctly returns pointer
95 
96  TGlobalMappedFunction &operator=(const TGlobal &) = delete;
97  // Some of the special ones are created before the list is create e.g gFile
98  // We need to buffer them.
99  static TList &GetEarlyRegisteredGlobals();
100 
101  friend class TROOT;
102 };
103 
104 #endif