Logo ROOT  
Reference Guide
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 static const UniqueId nid{nullval};
67 return nid;
68 }
69
70 static constexpr Value_t nullval = 0UL; ///< The value of the nullid.
71
72private:
73 UniqueId(Value_t val) : _val{val} {}
74
75 Value_t _val; ///< Numerical value of the ID.
76
77 static std::atomic<Value_t> counter; ///< The static object counter to get the next ID value.
78};
79
80template <class Class>
81std::atomic<typename UniqueId<Class>::Value_t> UniqueId<Class>::counter{UniqueId<Class>::nullval};
82
83/// A helper function to replace pointer comparisons with UniqueId comparisons.
84/// With pointer comparisons, we can also have `nullptr`. In the UniqueId case,
85/// this translates to the `nullid`.
86template <class Class>
88{
89 return ptr ? ptr->uniqueId() : UniqueId<Class>::nullid();
90}
91
92} // namespace RooFit
93
94#endif
void Class()
Definition: Class.C:29
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition: Common.h:18
UniqueId< Class > const & getUniqueId(Class const *ptr)
A helper function to replace pointer comparisons with UniqueId comparisons.
Definition: UniqueId.h:87
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:77
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:73
Value_t _val
Numerical value of the ID.
Definition: UniqueId.h:75
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:70
constexpr Value_t value() const
Return numerical value of ID.
Definition: UniqueId.h:59