Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
UniqueId.h
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Jonas Rembser, CERN, Jun 2021
5 *
6 * Copyright (c) 2021, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
13#ifndef roofit_roofitcore_RooFit_UniqueId_h
14#define roofit_roofitcore_RooFit_UniqueId_h
15
16#include <atomic>
17
18namespace RooFit {
19
20/// A UniqueId can be added as a class member to enhance any class with a
21/// unique identifier for each instantiated object.
22///
23/// Example:
24/// ~~~{.cpp}
25/// class MyClass {
26///
27/// public:
28/// /// Return unique ID by reference.
29/// /// Please always use the name `uniqueId` for the getter.
30/// UniqueId<MyClass> const& uniqueId() const { return _uniqueId; }
31///
32/// private:
33/// const UniqueId<MyClass> _uniqueId; //! should be non-persistent
34///
35/// };
36/// ~~~
37
38template <class Class>
39struct UniqueId {
40public:
41 using Value_t = unsigned long;
42
43 /// Create a new UniqueId with the next value from the static counter.
45
46 // Disable all sorts of copying and moving to ensure uniqueness.
47 UniqueId(const UniqueId &) = delete;
48 UniqueId &operator=(const UniqueId &) = delete;
49 UniqueId(UniqueId &&) = delete;
51
52 operator Value_t() const { return _val; }
53
54 /// Return numerical value of ID.
55 /// Use only if necessary, as the UniqueId type information is lost and
56 /// copying/moving is not prohibited for the value type.
57 /// Please don't turn this into a cast operator, as a function with an
58 /// explicit name is easier to track in the codebase.
59 constexpr Value_t value() const { return _val; }
60
61 bool operator==(UniqueId const &other) const { return _val == other._val; }
62 bool operator<(UniqueId const &other) const { return _val < other._val; }
63
64 /// Get an ID that is less than the ID of any object (similar to nullptr).
65 static UniqueId const &nullid()
66 {
67 static const UniqueId nid{nullval};
68 return nid;
69 }
70
71 static constexpr Value_t nullval = 0UL; ///< The value of the nullid.
72
73private:
74 UniqueId(Value_t val) : _val{val} {}
75
76 Value_t _val; ///< Numerical value of the ID.
77
78 static std::atomic<Value_t> counter; ///< The static object counter to get the next ID value.
79};
80
81template <class Class>
82std::atomic<typename UniqueId<Class>::Value_t> UniqueId<Class>::counter{UniqueId<Class>::nullval};
83
84/// A helper function to replace pointer comparisons with UniqueId comparisons.
85/// With pointer comparisons, we can also have `nullptr`. In the UniqueId case,
86/// this translates to the `nullid`.
87template <class Class,
88 class UniqueId_t = std::remove_reference_t<decltype(std::declval<std::remove_pointer_t<Class>>().uniqueId())>>
89UniqueId_t const &getUniqueId(Class const *ptr)
90{
91 return ptr ? ptr->uniqueId() : UniqueId_t::nullid();
92}
93
94} // namespace RooFit
95
96#endif
static TClass * Class()
Definition Class.C:29
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26
UniqueId_t const & getUniqueId(Class const *ptr)
A helper function to replace pointer comparisons with UniqueId comparisons.
Definition UniqueId.h:89
A UniqueId can be added as a class member to enhance any class with a unique identifier for each inst...
Definition UniqueId.h:39
bool operator==(UniqueId const &other) const
Definition UniqueId.h:61
UniqueId & operator=(UniqueId &&)=delete
static std::atomic< Value_t > counter
The static object counter to get the next ID value.
Definition UniqueId.h:78
UniqueId(const UniqueId &)=delete
UniqueId()
Create a new UniqueId with the next value from the static counter.
Definition UniqueId.h:44
unsigned long Value_t
Definition UniqueId.h:41
UniqueId(Value_t val)
Definition UniqueId.h:74
Value_t _val
Numerical value of the ID.
Definition UniqueId.h:76
UniqueId(UniqueId &&)=delete
static UniqueId const & nullid()
Get an ID that is less than the ID of any object (similar to nullptr).
Definition UniqueId.h:65
UniqueId & operator=(const UniqueId &)=delete
bool operator<(UniqueId const &other) const
Definition UniqueId.h:62
static constexpr Value_t nullval
The value of the nullid.
Definition UniqueId.h:71
constexpr Value_t value() const
Return numerical value of ID.
Definition UniqueId.h:59