propagate_const.h
Go to the documentation of this file.
1 #ifndef cetlib_propagate_const_h
2 #define cetlib_propagate_const_h
3 // -*- C++ -*-
4 //
5 // Package: FWCore/Utilities
6 // Class : propagate_const
7 //
8 /**\class propagate_const propagate_const.h
9  "FWCore/Utilities/interface/propagate_const.h"
10 
11  Description: propagate const to pointer like objects. Based on C++ experimental
12  std::propagate_const.
13 
14  Usage:
15  <usage>
16 
17 */
18 //
19 // Original Author: Chris Jones
20 // Created: Fri, 18 Dec 2015 14:56:12 GMT
21 //
22 
23 // system include files
24 #include <type_traits>
25 #include <utility>
26 
27 // user include files
28 
29 // forward declarations
30 
31 namespace cet {
32 
33  template <typename T>
35 
36  template <typename T>
38  template <typename T>
39  T const& get_underlying(propagate_const<T> const&);
40 
41  template <typename T>
42  class propagate_const {
43 
44  public:
45  friend T& get_underlying<T>(propagate_const<T>&);
46  friend T const& get_underlying<T>(propagate_const<T> const&);
47 
48  using element_type =
49  typename std::remove_reference<decltype(*std::declval<T&>())>::type;
50 
51  propagate_const() = default;
52 
54  propagate_const(propagate_const<T> const&) = delete;
55  template <typename U>
56  propagate_const(U&& iValue) : m_value(std::forward<U>(iValue))
57  {}
58 
61 
62  template <typename U>
64  operator=(U&& iValue)
65  {
66  m_value = std::forward<U>(iValue);
67  return *this;
68  }
69 
70  // ---------- const member functions ---------------------
71  element_type const*
72  get() const
73  {
74  return &(*m_value);
75  }
76  element_type const* operator->() const { return this->get(); }
77  element_type const& operator*() const { return *m_value; }
78 
79  operator element_type const*() const { return this->get(); }
80 
81  // ---------- member functions ---------------------------
83  get()
84  {
85  return &(*m_value);
86  }
87  element_type* operator->() { return this->get(); }
88  element_type& operator*() { return *m_value; }
89 
90  operator element_type*() { return this->get(); }
91 
92  private:
93  // ---------- member data --------------------------------
95  };
96 
97  template <typename T>
98  T&
100  {
101  return iP.m_value;
102  }
103  template <typename T>
104  T const&
106  {
107  return iP.m_value;
108  }
109 }
110 
111 #endif /* cetlib_propagate_const_h */
112 
113 // Local Variables:
114 // mode: c++
115 // End:
element_type const & operator*() const
STL namespace.
propagate_const & operator=(U &&iValue)
propagate_const(U &&iValue)
propagate_const()=default
propagate_const< T > & operator=(propagate_const &&)=default
element_type * operator->()
element_type const * operator->() const
element_type & operator*()
typename std::remove_reference< decltype(*std::declval< T & >())>::type element_type
T & get_underlying(propagate_const< T > &)
static QCString type
Definition: declinfo.cpp:672
friend T & get_underlying(propagate_const< T > &)