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