summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-03-12 19:19:24 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-03-12 19:19:24 +0000
commit821261bc506fe4a04a0ae0ce8eb6488cca74e52d (patch)
tree0448a068b0d1dc2c2550a0430b4dba0069620741 /base
parent1c54f8f9dba0c6a483d33442a613c581d6412d94 (diff)
downloadchromium_src-821261bc506fe4a04a0ae0ce8eb6488cca74e52d.zip
chromium_src-821261bc506fe4a04a0ae0ce8eb6488cca74e52d.tar.gz
chromium_src-821261bc506fe4a04a0ae0ce8eb6488cca74e52d.tar.bz2
Add ScopedGeneric.
This is intended to be used in a typedef for declaring various types of scoped resources, like ScopedFD or ScopedGDIObject. It attempts to be like scoped_ptr but without pointer semantics. Currently the scoped objects are either custom one-offs that don't support move semantics and have subtly varying behavior, or they use a pointer like ScopedFD which is a memory stomp waiting to happen (since you must keep the int* alive longer than the scoper so it can be dereferenced and closed). R=ajwong@chromium.org, viettrungluu@chromium.org, ajwong Review URL: https://codereview.chromium.org/189613002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@256596 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/BUILD.gn1
-rw-r--r--base/base.gyp1
-rw-r--r--base/base.gypi1
-rw-r--r--base/scoped_generic.h174
-rw-r--r--base/scoped_generic_unittest.cc153
5 files changed, 330 insertions, 0 deletions
diff --git a/base/BUILD.gn b/base/BUILD.gn
index 28fe453..d676f05 100644
--- a/base/BUILD.gn
+++ b/base/BUILD.gn
@@ -471,6 +471,7 @@ component("base") {
"safe_numerics.h",
"safe_strerror_posix.cc",
"safe_strerror_posix.h",
+ "scoped_generic.h",
"scoped_native_library.cc",
"scoped_native_library.h",
"sequence_checker.h",
diff --git a/base/base.gyp b/base/base.gyp
index 1816a7f..fcad933 100644
--- a/base/base.gyp
+++ b/base/base.gyp
@@ -582,6 +582,7 @@
'rand_util_unittest.cc',
'numerics/safe_numerics_unittest.cc',
'scoped_clear_errno_unittest.cc',
+ 'scoped_generic_unittest.cc',
'scoped_native_library_unittest.cc',
'scoped_observer.h',
'security_unittest.cc',
diff --git a/base/base.gypi b/base/base.gypi
index 0861dac..fcc3251 100644
--- a/base/base.gypi
+++ b/base/base.gypi
@@ -502,6 +502,7 @@
'numerics/safe_math_impl.h',
'safe_strerror_posix.cc',
'safe_strerror_posix.h',
+ 'scoped_generic.h',
'scoped_native_library.cc',
'scoped_native_library.h',
'sequence_checker.h',
diff --git a/base/scoped_generic.h b/base/scoped_generic.h
new file mode 100644
index 0000000..3879b20
--- /dev/null
+++ b/base/scoped_generic.h
@@ -0,0 +1,174 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_SCOPED_GENERIC_H_
+#define BASE_SCOPED_GENERIC_H_
+
+#include <stdlib.h>
+
+#include "base/compiler_specific.h"
+#include "base/move.h"
+
+namespace base {
+
+// This class acts like ScopedPtr with a custom deleter (although is slightly
+// less fancy in some of the more escoteric respects) except that it keeps a
+// copy of the object rather than a pointer, and we require that the contained
+// object has some kind of "invalid" value.
+//
+// Defining a scoper based on this class allows you to get a scoper for
+// non-pointer types without having to write custom code for set, reset, and
+// move, etc. and get almost identical semantics that people are used to from
+// scoped_ptr.
+//
+// It is intended that you will typedef this class with an appropriate deleter
+// to implement clean up tasks for objects that act like pointers from a
+// resource management standpoint but aren't, such as file descriptors and
+// various types of operating system handles. Using scoped_ptr for these
+// things requires that you keep a pointer to the handle valid for the lifetime
+// of the scoper (which is easy to mess up).
+//
+// For an object to be able to be put into a ScopedGeneric, it must support
+// standard copyable semantics and have a specific "invalid" value. The traits
+// must define a free function and also the invalid value to assign for
+// default-constructed and released objects.
+//
+// struct FooScopedTraits {
+// // It's assumed that this is a fast inline function with little-to-no
+// // penalty for duplicate calls. This must be a static function even
+// // for stateful traits.
+// static int InvalidValue() {
+// return 0;
+// }
+//
+// // This free function will not be called if f == InvalidValue()!
+// static void Free(int f) {
+// ::FreeFoo(f);
+// }
+// };
+//
+// typedef ScopedGeneric<int, FooScopedTraits> ScopedFoo;
+template<typename T, typename Traits>
+class ScopedGeneric {
+ MOVE_ONLY_TYPE_FOR_CPP_03(ScopedGeneric, RValue)
+
+ private:
+ // This must be first since it's used inline below.
+ //
+ // Use the empty base class optimization to allow us to have a D
+ // member, while avoiding any space overhead for it when D is an
+ // empty class. See e.g. http://www.cantrip.org/emptyopt.html for a good
+ // discussion of this technique.
+ struct Data : public Traits {
+ explicit Data(const T& in) : generic(in) {}
+ Data(const T& in, const Traits& other) : Traits(other), generic(in) {}
+ T generic;
+ };
+
+ public:
+ typedef T element_type;
+ typedef Traits traits_type;
+
+ ScopedGeneric() : data_(traits_type::InvalidValue()) {}
+
+ // Constructor. Takes responsibility for freeing the resource associated with
+ // the object T.
+ explicit ScopedGeneric(const element_type& value) : data_(value) {}
+
+ // Constructor. Allows initialization of a stateful traits object.
+ ScopedGeneric(const element_type& value, const traits_type& traits)
+ : data_(value, traits) {
+ }
+
+ // Move constructor for C++03 move emulation.
+ ScopedGeneric(RValue rvalue)
+ : data_(rvalue.object->release(), rvalue.object->get_traits()) {
+ }
+
+ ~ScopedGeneric() {
+ FreeIfNecessary();
+ }
+
+ // Frees the currently owned object, if any. Then takes ownership of a new
+ // object, if given. Self-resets are not allowd as on scoped_ptr. See
+ // http://crbug.com/162971
+ void reset(const element_type& value = traits_type::InvalidValue()) {
+ if (data_.generic != traits_type::InvalidValue() && data_.generic == value)
+ abort();
+ FreeIfNecessary();
+ data_.generic = value;
+ }
+
+ void swap(ScopedGeneric& other) {
+ // Standard swap idiom: 'using std::swap' ensures that std::swap is
+ // present in the overload set, but we call swap unqualified so that
+ // any more-specific overloads can be used, if available.
+ using std::swap;
+ swap(static_cast<Traits&>(data_), static_cast<Traits&>(other.data_));
+ swap(data_.generic, other.data_.generic);
+ }
+
+ // Release the object. The return value is the current object held by this
+ // object. After this operation, this object will hold a null value, and
+ // will not own the object any more.
+ element_type release() WARN_UNUSED_RESULT {
+ element_type old_generic = data_.generic;
+ data_.generic = traits_type::InvalidValue();
+ return old_generic;
+ }
+
+ const element_type& get() const { return data_.generic; }
+
+ // Returns true if this object doesn't hold the special null value for the
+ // associated data type.
+ bool is_valid() const { return data_.generic != traits_type::InvalidValue(); }
+
+ bool operator==(const element_type& value) const {
+ return data_.generic == value;
+ }
+ bool operator!=(const element_type& value) const {
+ return data_.generic != value;
+ }
+
+ Traits& get_traits() { return data_; }
+ const Traits& get_traits() const { return data_; }
+
+ private:
+ void FreeIfNecessary() {
+ if (data_.generic != traits_type::InvalidValue()) {
+ data_.Free(data_.generic);
+ data_.generic = traits_type::InvalidValue();
+ }
+ }
+
+ // Forbid comparison. If U != T, it totally doesn't make sense, and if U ==
+ // T, it still doesn't make sense because you should never have the same
+ // object owned by two different ScopedGenerics.
+ template <typename T2, typename Traits2> bool operator==(
+ const ScopedGeneric<T2, Traits2>& p2) const;
+ template <typename T2, typename Traits2> bool operator!=(
+ const ScopedGeneric<T2, Traits2>& p2) const;
+
+ Data data_;
+};
+
+template<class T, class Traits>
+void swap(const ScopedGeneric<T, Traits>& a,
+ const ScopedGeneric<T, Traits>& b) {
+ a.swap(b);
+}
+
+template<class T, class Traits>
+bool operator==(const T& value, const ScopedGeneric<T, Traits>& scoped) {
+ return value == scoped.get();
+}
+
+template<class T, class Traits>
+bool operator!=(const T& value, const ScopedGeneric<T, Traits>& scoped) {
+ return value != scoped.get();
+}
+
+} // namespace base
+
+#endif // BASE_SCOPED_GENERIC_H_
diff --git a/base/scoped_generic_unittest.cc b/base/scoped_generic_unittest.cc
new file mode 100644
index 0000000..f0dca22
--- /dev/null
+++ b/base/scoped_generic_unittest.cc
@@ -0,0 +1,153 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <vector>
+
+#include "base/scoped_generic.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace base {
+
+namespace {
+
+struct IntTraits {
+ IntTraits(std::vector<int>* freed) : freed_ints(freed) {}
+
+ static int InvalidValue() {
+ return -1;
+ }
+ void Free(int value) {
+ freed_ints->push_back(value);
+ }
+
+ std::vector<int>* freed_ints;
+};
+
+typedef ScopedGeneric<int, IntTraits> ScopedInt;
+
+} // namespace
+
+TEST(ScopedGenericTest, ScopedGeneric) {
+ std::vector<int> values_freed;
+ IntTraits traits(&values_freed);
+
+ // Invalid case, delete should not be called.
+ {
+ ScopedInt a(IntTraits::InvalidValue(), traits);
+ }
+ EXPECT_TRUE(values_freed.empty());
+
+ // Simple deleting case.
+ static const int kFirst = 0;
+ {
+ ScopedInt a(kFirst, traits);
+ }
+ ASSERT_EQ(1u, values_freed.size());
+ ASSERT_EQ(kFirst, values_freed[0]);
+ values_freed.clear();
+
+ // Release should return the right value and leave the object empty.
+ {
+ ScopedInt a(kFirst, traits);
+ EXPECT_EQ(kFirst, a.release());
+
+ ScopedInt b(IntTraits::InvalidValue(), traits);
+ EXPECT_EQ(IntTraits::InvalidValue(), b.release());
+ }
+ ASSERT_TRUE(values_freed.empty());
+
+ // Reset should free the old value, then the new one should go away when
+ // it goes out of scope.
+ static const int kSecond = 1;
+ {
+ ScopedInt b(kFirst, traits);
+ b.reset(kSecond);
+ ASSERT_EQ(1u, values_freed.size());
+ ASSERT_EQ(kFirst, values_freed[0]);
+ }
+ ASSERT_EQ(2u, values_freed.size());
+ ASSERT_EQ(kSecond, values_freed[1]);
+ values_freed.clear();
+
+ // Swap.
+ {
+ ScopedInt a(kFirst, traits);
+ ScopedInt b(kSecond, traits);
+ a.swap(b);
+ EXPECT_TRUE(values_freed.empty()); // Nothing should be freed.
+ EXPECT_EQ(kSecond, a.get());
+ EXPECT_EQ(kFirst, b.get());
+ }
+ // Values should be deleted in the opposite order.
+ ASSERT_EQ(2u, values_freed.size());
+ EXPECT_EQ(kFirst, values_freed[0]);
+ EXPECT_EQ(kSecond, values_freed[1]);
+ values_freed.clear();
+
+ // Pass.
+ {
+ ScopedInt a(kFirst, traits);
+ ScopedInt b(a.Pass());
+ EXPECT_TRUE(values_freed.empty()); // Nothing should be freed.
+ ASSERT_EQ(IntTraits::InvalidValue(), a.get());
+ ASSERT_EQ(kFirst, b.get());
+ }
+ ASSERT_EQ(1u, values_freed.size());
+ ASSERT_EQ(kFirst, values_freed[0]);
+}
+
+TEST(ScopedGenericTest, Operators) {
+ std::vector<int> values_freed;
+ IntTraits traits(&values_freed);
+
+ static const int kFirst = 0;
+ static const int kSecond = 1;
+ {
+ ScopedInt a(kFirst, traits);
+ EXPECT_TRUE(a == kFirst);
+ EXPECT_FALSE(a != kFirst);
+ EXPECT_FALSE(a == kSecond);
+ EXPECT_TRUE(a != kSecond);
+
+ EXPECT_TRUE(kFirst == a);
+ EXPECT_FALSE(kFirst != a);
+ EXPECT_FALSE(kSecond == a);
+ EXPECT_TRUE(kSecond != a);
+ }
+
+ // is_valid().
+ {
+ ScopedInt a(kFirst, traits);
+ EXPECT_TRUE(a.is_valid());
+ a.reset();
+ EXPECT_FALSE(a.is_valid());
+ }
+}
+
+// Cheesy manual "no compile" test for manually validating changes.
+#if 0
+TEST(ScopedGenericTest, NoCompile) {
+ // Assignment shouldn't work.
+ /*{
+ ScopedInt a(kFirst, traits);
+ ScopedInt b(a);
+ }*/
+
+ // Comparison shouldn't work.
+ /*{
+ ScopedInt a(kFirst, traits);
+ ScopedInt b(kFirst, traits);
+ if (a == b) {
+ }
+ }*/
+
+ // Implicit conversion to bool shouldn't work.
+ /*{
+ ScopedInt a(kFirst, traits);
+ bool result = a;
+ }*/
+}
+#endif
+
+} // namespace base