CMS 3D CMS Logo

get_underlying_safe.h
Go to the documentation of this file.
1 #ifndef FWCore_Utilities_get_underlying_safe_h
2 #define FWCore_Utilities_get_underlying_safe_h
3 
4 /*
5  Description:
6 
7  The function get_underlying(), provided by propagate_const, should not be called directly
8  by users because it may cast away constness.
9  This header provides helper function(s) get_underlying_safe() to users of propagate_const<T>.
10  The get_underlying_safe() functions avoid this issue.
11 
12  If called with a non-const ref argument, get_underlying_safe() simply calls get_underlying().
13  If called with a const ref argument, get_underlying_safe() returns a pointer to const,
14  which preserves constness, but requires copying the pointer.
15 
16  For non-copyable pointers, such as std::unique_ptr, it is not possible to preserve constness.
17  so get_underlying_safe() will fail to compile if called with a const ref to a unique_ptr.
18  This is intentional.
19 
20  This header can be expanded to support other smart pointers as needed.
21 */
22 
23 //
24 // Original Author: Bill Tanenbaum
25 //
26 
27 // system include files
28 #include <memory>
29 
30 // user include files
31 
33 
34 // forward declarations
35 
36 namespace edm {
37 
38  // for std::shared_ptr
39  template <typename T>
40  std::shared_ptr<T>& get_underlying_safe(propagate_const<std::shared_ptr<T>>& iP) {
41  return get_underlying(iP);
42  }
43  template <typename T>
44  std::shared_ptr<T const> get_underlying_safe(propagate_const<std::shared_ptr<T>> const& iP) {
45  std::shared_ptr<T const> copy = get_underlying(iP);
46  return copy;
47  }
48 
49  // for bare pointer
50  template <typename T>
52  return get_underlying(iP);
53  }
54  template <typename T>
56  T const* copy = get_underlying(iP);
57  return copy;
58  }
59 
60  // for std::unique_ptr
61  template <typename T>
62  std::unique_ptr<T>& get_underlying_safe(propagate_const<std::unique_ptr<T>>& iP) {
63  return get_underlying(iP);
64  }
65  // This template below will deliberately not compile.
66  template <typename T>
67  std::unique_ptr<T const> get_underlying_safe(propagate_const<std::unique_ptr<T>> const& iP) {
68  std::unique_ptr<T const> copy = get_underlying(iP);
69  return copy;
70  }
71 
72 } // namespace edm
73 
74 #endif
def copy(args, dbName)
T & get_underlying(propagate_const< T > &)
std::shared_ptr< T > & get_underlying_safe(propagate_const< std::shared_ptr< T >> &iP)
HLT enums.
long double T