Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2796)

Unified Diff: base/scoped_ptr.h

Issue 2804042: Incomplete changes to make scoped_ptr_malloc use plain functions. (Closed)
Patch Set: chrome now builds & links Created 10 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/process_util_posix.cc ('k') | chrome/browser/geolocation/wifi_data_provider_linux.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/scoped_ptr.h
diff --git a/base/scoped_ptr.h b/base/scoped_ptr.h
index c716e7722f336ae0a89df618903e4d0a950fe6ac..ce2743ed319e188c165468472811296d3c12928e 100644
--- a/base/scoped_ptr.h
+++ b/base/scoped_ptr.h
@@ -256,19 +256,25 @@ bool operator!=(C* p1, const scoped_array<C>& p2) {
return p1 != p2.get();
}
-// This class wraps the c library function free() in a class that can be
-// passed as a template argument to scoped_ptr_malloc below.
-class ScopedPtrMallocFree {
- public:
- inline void operator()(void* x) const {
- free(x);
- }
-};
+// This template function will adapt any function that takes a void* and
+// returns void for use with scoped_ptr_malloc below.
+template<void (*FreeFn)(void*), class C>
+void FreeFnAdapter(C* x) {
+ FreeFn(x);
+}
+template<int (*FreeFn)(void*), class C>
+void FreeFnAdapterIgnoreReturn(C* x) {
+ FreeFn(x);
+}
+template<class C, int (*FreeFn)(C*)>
+void FreeFnIgnoreReturn(C* x) {
+ FreeFn(x);
+}
// scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a
-// second template argument, the functor used to free the object.
+// second template argument, the function used to free the object.
-template<class C, class FreeProc = ScopedPtrMallocFree>
+template<class C, void (*FreeProc)(C*) = FreeFnAdapter<free> >
class scoped_ptr_malloc {
public:
@@ -284,7 +290,7 @@ class scoped_ptr_malloc {
// Destructor. If there is a C object, call the Free functor.
~scoped_ptr_malloc() {
- free_(ptr_);
+ FreePointer(ptr_);
}
// Reset. Calls the Free functor on the current owned object, if any.
@@ -292,7 +298,7 @@ class scoped_ptr_malloc {
// this->reset(this->get()) works.
void reset(C* p = NULL) {
if (ptr_ != p) {
- free_(ptr_);
+ FreePointer(ptr_);
ptr_ = p;
}
}
@@ -346,35 +352,34 @@ class scoped_ptr_malloc {
}
private:
+ void FreePointer(C* x) {
+ if (x)
+ FreeProc(x);
+ }
C* ptr_;
// no reason to use these: each scoped_ptr_malloc should have its own object
- template <class C2, class GP>
+ template <class C2, void GP(C*)>
bool operator==(scoped_ptr_malloc<C2, GP> const& p) const;
- template <class C2, class GP>
+ template <class C2, void GP(C*)>
bool operator!=(scoped_ptr_malloc<C2, GP> const& p) const;
- static FreeProc const free_;
-
// Disallow evil constructors
scoped_ptr_malloc(const scoped_ptr_malloc&);
void operator=(const scoped_ptr_malloc&);
};
-template<class C, class FP>
-FP const scoped_ptr_malloc<C, FP>::free_ = FP();
-
-template<class C, class FP> inline
+template<class C, void FP(C*)> inline
void swap(scoped_ptr_malloc<C, FP>& a, scoped_ptr_malloc<C, FP>& b) {
a.swap(b);
}
-template<class C, class FP> inline
+template<class C, void FP(C*)> inline
bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) {
return p == b.get();
}
-template<class C, class FP> inline
+template<class C, void FP(C*)> inline
bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) {
return p != b.get();
}
« no previous file with comments | « base/process_util_posix.cc ('k') | chrome/browser/geolocation/wifi_data_provider_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698