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> std::shared_ptr<T>& get_underlying_safe(propagate_const<std::shared_ptr<T>>& iP) {return get_underlying(iP);}
40  template<typename T> std::shared_ptr<T const> get_underlying_safe(propagate_const<std::shared_ptr<T>> const& iP ) {std::shared_ptr<T const> copy = get_underlying(iP); return copy;}
41 
42  // for bare pointer
43  template<typename T> T*& get_underlying_safe(propagate_const<T*>& iP) {return get_underlying(iP);}
44  template<typename T> T const* get_underlying_safe(propagate_const<T*> const& iP) {T const* copy = get_underlying(iP); return copy;}
45 
46  // for std::unique_ptr
47  template<typename T> std::unique_ptr<T>& get_underlying_safe(propagate_const<std::unique_ptr<T>>& iP) {return get_underlying(iP);}
48  // This template below will deliberately not compile.
49  template<typename T> std::unique_ptr<T const> get_underlying_safe(propagate_const<std::unique_ptr<T>> const& iP) {std::unique_ptr<T const> copy = get_underlying(iP); return copy;}
50 
51 }
52 
53 #endif
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