xref: /aosp_15_r20/external/libchrome/base/scoped_generic.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright 2014 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_SCOPED_GENERIC_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_SCOPED_GENERIC_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <stdlib.h>
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #include <algorithm>
11*635a8641SAndroid Build Coastguard Worker 
12*635a8641SAndroid Build Coastguard Worker #include "base/compiler_specific.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
14*635a8641SAndroid Build Coastguard Worker 
15*635a8641SAndroid Build Coastguard Worker namespace base {
16*635a8641SAndroid Build Coastguard Worker 
17*635a8641SAndroid Build Coastguard Worker // This class acts like unique_ptr with a custom deleter (although is slightly
18*635a8641SAndroid Build Coastguard Worker // less fancy in some of the more escoteric respects) except that it keeps a
19*635a8641SAndroid Build Coastguard Worker // copy of the object rather than a pointer, and we require that the contained
20*635a8641SAndroid Build Coastguard Worker // object has some kind of "invalid" value.
21*635a8641SAndroid Build Coastguard Worker //
22*635a8641SAndroid Build Coastguard Worker // Defining a scoper based on this class allows you to get a scoper for
23*635a8641SAndroid Build Coastguard Worker // non-pointer types without having to write custom code for set, reset, and
24*635a8641SAndroid Build Coastguard Worker // move, etc. and get almost identical semantics that people are used to from
25*635a8641SAndroid Build Coastguard Worker // unique_ptr.
26*635a8641SAndroid Build Coastguard Worker //
27*635a8641SAndroid Build Coastguard Worker // It is intended that you will typedef this class with an appropriate deleter
28*635a8641SAndroid Build Coastguard Worker // to implement clean up tasks for objects that act like pointers from a
29*635a8641SAndroid Build Coastguard Worker // resource management standpoint but aren't, such as file descriptors and
30*635a8641SAndroid Build Coastguard Worker // various types of operating system handles. Using unique_ptr for these
31*635a8641SAndroid Build Coastguard Worker // things requires that you keep a pointer to the handle valid for the lifetime
32*635a8641SAndroid Build Coastguard Worker // of the scoper (which is easy to mess up).
33*635a8641SAndroid Build Coastguard Worker //
34*635a8641SAndroid Build Coastguard Worker // For an object to be able to be put into a ScopedGeneric, it must support
35*635a8641SAndroid Build Coastguard Worker // standard copyable semantics and have a specific "invalid" value. The traits
36*635a8641SAndroid Build Coastguard Worker // must define a free function and also the invalid value to assign for
37*635a8641SAndroid Build Coastguard Worker // default-constructed and released objects.
38*635a8641SAndroid Build Coastguard Worker //
39*635a8641SAndroid Build Coastguard Worker //   struct FooScopedTraits {
40*635a8641SAndroid Build Coastguard Worker //     // It's assumed that this is a fast inline function with little-to-no
41*635a8641SAndroid Build Coastguard Worker //     // penalty for duplicate calls. This must be a static function even
42*635a8641SAndroid Build Coastguard Worker //     // for stateful traits.
43*635a8641SAndroid Build Coastguard Worker //     static int InvalidValue() {
44*635a8641SAndroid Build Coastguard Worker //       return 0;
45*635a8641SAndroid Build Coastguard Worker //     }
46*635a8641SAndroid Build Coastguard Worker //
47*635a8641SAndroid Build Coastguard Worker //     // This free function will not be called if f == InvalidValue()!
48*635a8641SAndroid Build Coastguard Worker //     static void Free(int f) {
49*635a8641SAndroid Build Coastguard Worker //       ::FreeFoo(f);
50*635a8641SAndroid Build Coastguard Worker //     }
51*635a8641SAndroid Build Coastguard Worker //   };
52*635a8641SAndroid Build Coastguard Worker //
53*635a8641SAndroid Build Coastguard Worker //   typedef ScopedGeneric<int, FooScopedTraits> ScopedFoo;
54*635a8641SAndroid Build Coastguard Worker template<typename T, typename Traits>
55*635a8641SAndroid Build Coastguard Worker class ScopedGeneric {
56*635a8641SAndroid Build Coastguard Worker  private:
57*635a8641SAndroid Build Coastguard Worker   // This must be first since it's used inline below.
58*635a8641SAndroid Build Coastguard Worker   //
59*635a8641SAndroid Build Coastguard Worker   // Use the empty base class optimization to allow us to have a D
60*635a8641SAndroid Build Coastguard Worker   // member, while avoiding any space overhead for it when D is an
61*635a8641SAndroid Build Coastguard Worker   // empty class.  See e.g. http://www.cantrip.org/emptyopt.html for a good
62*635a8641SAndroid Build Coastguard Worker   // discussion of this technique.
63*635a8641SAndroid Build Coastguard Worker   struct Data : public Traits {
DataData64*635a8641SAndroid Build Coastguard Worker     explicit Data(const T& in) : generic(in) {}
DataData65*635a8641SAndroid Build Coastguard Worker     Data(const T& in, const Traits& other) : Traits(other), generic(in) {}
66*635a8641SAndroid Build Coastguard Worker     T generic;
67*635a8641SAndroid Build Coastguard Worker   };
68*635a8641SAndroid Build Coastguard Worker 
69*635a8641SAndroid Build Coastguard Worker  public:
70*635a8641SAndroid Build Coastguard Worker   typedef T element_type;
71*635a8641SAndroid Build Coastguard Worker   typedef Traits traits_type;
72*635a8641SAndroid Build Coastguard Worker 
ScopedGeneric()73*635a8641SAndroid Build Coastguard Worker   ScopedGeneric() : data_(traits_type::InvalidValue()) {}
74*635a8641SAndroid Build Coastguard Worker 
75*635a8641SAndroid Build Coastguard Worker   // Constructor. Takes responsibility for freeing the resource associated with
76*635a8641SAndroid Build Coastguard Worker   // the object T.
ScopedGeneric(const element_type & value)77*635a8641SAndroid Build Coastguard Worker   explicit ScopedGeneric(const element_type& value) : data_(value) {}
78*635a8641SAndroid Build Coastguard Worker 
79*635a8641SAndroid Build Coastguard Worker   // Constructor. Allows initialization of a stateful traits object.
ScopedGeneric(const element_type & value,const traits_type & traits)80*635a8641SAndroid Build Coastguard Worker   ScopedGeneric(const element_type& value, const traits_type& traits)
81*635a8641SAndroid Build Coastguard Worker       : data_(value, traits) {
82*635a8641SAndroid Build Coastguard Worker   }
83*635a8641SAndroid Build Coastguard Worker 
84*635a8641SAndroid Build Coastguard Worker   // Move constructor. Allows initialization from a ScopedGeneric rvalue.
ScopedGeneric(ScopedGeneric<T,Traits> && rvalue)85*635a8641SAndroid Build Coastguard Worker   ScopedGeneric(ScopedGeneric<T, Traits>&& rvalue)
86*635a8641SAndroid Build Coastguard Worker       : data_(rvalue.release(), rvalue.get_traits()) {
87*635a8641SAndroid Build Coastguard Worker   }
88*635a8641SAndroid Build Coastguard Worker 
~ScopedGeneric()89*635a8641SAndroid Build Coastguard Worker   ~ScopedGeneric() {
90*635a8641SAndroid Build Coastguard Worker     FreeIfNecessary();
91*635a8641SAndroid Build Coastguard Worker   }
92*635a8641SAndroid Build Coastguard Worker 
93*635a8641SAndroid Build Coastguard Worker   // operator=. Allows assignment from a ScopedGeneric rvalue.
94*635a8641SAndroid Build Coastguard Worker   ScopedGeneric& operator=(ScopedGeneric<T, Traits>&& rvalue) {
95*635a8641SAndroid Build Coastguard Worker     reset(rvalue.release());
96*635a8641SAndroid Build Coastguard Worker     return *this;
97*635a8641SAndroid Build Coastguard Worker   }
98*635a8641SAndroid Build Coastguard Worker 
99*635a8641SAndroid Build Coastguard Worker   // Frees the currently owned object, if any. Then takes ownership of a new
100*635a8641SAndroid Build Coastguard Worker   // object, if given. Self-resets are not allowd as on unique_ptr. See
101*635a8641SAndroid Build Coastguard Worker   // http://crbug.com/162971
102*635a8641SAndroid Build Coastguard Worker   void reset(const element_type& value = traits_type::InvalidValue()) {
103*635a8641SAndroid Build Coastguard Worker     if (data_.generic != traits_type::InvalidValue() && data_.generic == value)
104*635a8641SAndroid Build Coastguard Worker       abort();
105*635a8641SAndroid Build Coastguard Worker     FreeIfNecessary();
106*635a8641SAndroid Build Coastguard Worker     data_.generic = value;
107*635a8641SAndroid Build Coastguard Worker   }
108*635a8641SAndroid Build Coastguard Worker 
swap(ScopedGeneric & other)109*635a8641SAndroid Build Coastguard Worker   void swap(ScopedGeneric& other) {
110*635a8641SAndroid Build Coastguard Worker     // Standard swap idiom: 'using std::swap' ensures that std::swap is
111*635a8641SAndroid Build Coastguard Worker     // present in the overload set, but we call swap unqualified so that
112*635a8641SAndroid Build Coastguard Worker     // any more-specific overloads can be used, if available.
113*635a8641SAndroid Build Coastguard Worker     using std::swap;
114*635a8641SAndroid Build Coastguard Worker     swap(static_cast<Traits&>(data_), static_cast<Traits&>(other.data_));
115*635a8641SAndroid Build Coastguard Worker     swap(data_.generic, other.data_.generic);
116*635a8641SAndroid Build Coastguard Worker   }
117*635a8641SAndroid Build Coastguard Worker 
118*635a8641SAndroid Build Coastguard Worker   // Release the object. The return value is the current object held by this
119*635a8641SAndroid Build Coastguard Worker   // object. After this operation, this object will hold a null value, and
120*635a8641SAndroid Build Coastguard Worker   // will not own the object any more.
release()121*635a8641SAndroid Build Coastguard Worker   element_type release() WARN_UNUSED_RESULT {
122*635a8641SAndroid Build Coastguard Worker     element_type old_generic = data_.generic;
123*635a8641SAndroid Build Coastguard Worker     data_.generic = traits_type::InvalidValue();
124*635a8641SAndroid Build Coastguard Worker     return old_generic;
125*635a8641SAndroid Build Coastguard Worker   }
126*635a8641SAndroid Build Coastguard Worker 
127*635a8641SAndroid Build Coastguard Worker   // Returns a raw pointer to the object storage, to allow the scoper to be used
128*635a8641SAndroid Build Coastguard Worker   // to receive and manage out-parameter values. Implies reset().
receive()129*635a8641SAndroid Build Coastguard Worker   element_type* receive() WARN_UNUSED_RESULT {
130*635a8641SAndroid Build Coastguard Worker     reset();
131*635a8641SAndroid Build Coastguard Worker     return &data_.generic;
132*635a8641SAndroid Build Coastguard Worker   }
133*635a8641SAndroid Build Coastguard Worker 
get()134*635a8641SAndroid Build Coastguard Worker   const element_type& get() const { return data_.generic; }
135*635a8641SAndroid Build Coastguard Worker 
136*635a8641SAndroid Build Coastguard Worker   // Returns true if this object doesn't hold the special null value for the
137*635a8641SAndroid Build Coastguard Worker   // associated data type.
is_valid()138*635a8641SAndroid Build Coastguard Worker   bool is_valid() const { return data_.generic != traits_type::InvalidValue(); }
139*635a8641SAndroid Build Coastguard Worker 
140*635a8641SAndroid Build Coastguard Worker   bool operator==(const element_type& value) const {
141*635a8641SAndroid Build Coastguard Worker     return data_.generic == value;
142*635a8641SAndroid Build Coastguard Worker   }
143*635a8641SAndroid Build Coastguard Worker   bool operator!=(const element_type& value) const {
144*635a8641SAndroid Build Coastguard Worker     return data_.generic != value;
145*635a8641SAndroid Build Coastguard Worker   }
146*635a8641SAndroid Build Coastguard Worker 
get_traits()147*635a8641SAndroid Build Coastguard Worker   Traits& get_traits() { return data_; }
get_traits()148*635a8641SAndroid Build Coastguard Worker   const Traits& get_traits() const { return data_; }
149*635a8641SAndroid Build Coastguard Worker 
150*635a8641SAndroid Build Coastguard Worker  private:
FreeIfNecessary()151*635a8641SAndroid Build Coastguard Worker   void FreeIfNecessary() {
152*635a8641SAndroid Build Coastguard Worker     if (data_.generic != traits_type::InvalidValue()) {
153*635a8641SAndroid Build Coastguard Worker       data_.Free(data_.generic);
154*635a8641SAndroid Build Coastguard Worker       data_.generic = traits_type::InvalidValue();
155*635a8641SAndroid Build Coastguard Worker     }
156*635a8641SAndroid Build Coastguard Worker   }
157*635a8641SAndroid Build Coastguard Worker 
158*635a8641SAndroid Build Coastguard Worker   // Forbid comparison. If U != T, it totally doesn't make sense, and if U ==
159*635a8641SAndroid Build Coastguard Worker   // T, it still doesn't make sense because you should never have the same
160*635a8641SAndroid Build Coastguard Worker   // object owned by two different ScopedGenerics.
161*635a8641SAndroid Build Coastguard Worker   template <typename T2, typename Traits2> bool operator==(
162*635a8641SAndroid Build Coastguard Worker       const ScopedGeneric<T2, Traits2>& p2) const;
163*635a8641SAndroid Build Coastguard Worker   template <typename T2, typename Traits2> bool operator!=(
164*635a8641SAndroid Build Coastguard Worker       const ScopedGeneric<T2, Traits2>& p2) const;
165*635a8641SAndroid Build Coastguard Worker 
166*635a8641SAndroid Build Coastguard Worker   Data data_;
167*635a8641SAndroid Build Coastguard Worker 
168*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(ScopedGeneric);
169*635a8641SAndroid Build Coastguard Worker };
170*635a8641SAndroid Build Coastguard Worker 
171*635a8641SAndroid Build Coastguard Worker template<class T, class Traits>
swap(const ScopedGeneric<T,Traits> & a,const ScopedGeneric<T,Traits> & b)172*635a8641SAndroid Build Coastguard Worker void swap(const ScopedGeneric<T, Traits>& a,
173*635a8641SAndroid Build Coastguard Worker           const ScopedGeneric<T, Traits>& b) {
174*635a8641SAndroid Build Coastguard Worker   a.swap(b);
175*635a8641SAndroid Build Coastguard Worker }
176*635a8641SAndroid Build Coastguard Worker 
177*635a8641SAndroid Build Coastguard Worker template<class T, class Traits>
178*635a8641SAndroid Build Coastguard Worker bool operator==(const T& value, const ScopedGeneric<T, Traits>& scoped) {
179*635a8641SAndroid Build Coastguard Worker   return value == scoped.get();
180*635a8641SAndroid Build Coastguard Worker }
181*635a8641SAndroid Build Coastguard Worker 
182*635a8641SAndroid Build Coastguard Worker template<class T, class Traits>
183*635a8641SAndroid Build Coastguard Worker bool operator!=(const T& value, const ScopedGeneric<T, Traits>& scoped) {
184*635a8641SAndroid Build Coastguard Worker   return value != scoped.get();
185*635a8641SAndroid Build Coastguard Worker }
186*635a8641SAndroid Build Coastguard Worker 
187*635a8641SAndroid Build Coastguard Worker }  // namespace base
188*635a8641SAndroid Build Coastguard Worker 
189*635a8641SAndroid Build Coastguard Worker #endif  // BASE_SCOPED_GENERIC_H_
190