-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathTObjectWrapper.h
More file actions
80 lines (69 loc) · 2.03 KB
/
TObjectWrapper.h
File metadata and controls
80 lines (69 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#ifndef O2_TOBJECTWRAPPER_H
#define O2_TOBJECTWRAPPER_H
#include <TClass.h>
#include <TObject.h>
#include <cxxabi.h>
#include <iosfwd>
#include <memory>
#include <stdexcept>
#include <typeinfo>
namespace o2
{
// anonymous namespace to prevent usage outside of this file
namespace
{
/// utility function to demangle cxx type names
std::string demangle(const char* name)
{
int status = -4; // some arbitrary value to eliminate the compiler warning
std::unique_ptr<char, void (*)(void*)> res{abi::__cxa_demangle(name, nullptr, nullptr, &status), std::free};
return (status == 0) ? res.get() : name;
}
} // end anonymous namespace
/// A wrapper class to easily promote any type to a TObject
/// does not take ownership of wrapped object and should not be used
/// in tight loops since construction expensive
template <typename T>
class TObjectWrapper : public TObject
{
public:
TObjectWrapper(T* obj) : mObj(obj), TObject()
{
// make sure that a dictionary for this wrapper exists
auto& t = typeid(*this);
std::string message("Need dicionary for type ");
auto typestring = demangle(t.name());
message.append(typestring);
message.append("\n");
auto hasdict = TClass::HasDictionarySelection(typestring.c_str());
if (!hasdict) {
throw std::runtime_error(message);
}
}
TObjectWrapper() : TObjectWrapper(nullptr)
{
}
void setObj(T* obj)
{
mObj = obj;
}
T* getObj() const
{
return mObj;
}
~TObjectWrapper() override = default;
private:
T* mObj;
ClassDefOverride(TObjectWrapper, 1);
};
} // namespace o2
#endif