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

Unified Diff: core/include/fxcrt/fx_memory.h

Issue 1805683002: Add a Retained Pointer smart class. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Move FreeDeleter and such to fx_memory.h Created 4 years, 9 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
Index: core/include/fxcrt/fx_memory.h
diff --git a/core/include/fxcrt/fx_memory.h b/core/include/fxcrt/fx_memory.h
index e1cc07090a7079344932d2242f0ecc79c9d5a247..33deb9602d738242f0e6e72ae2eef2ae2e3dd602 100644
--- a/core/include/fxcrt/fx_memory.h
+++ b/core/include/fxcrt/fx_memory.h
@@ -74,11 +74,36 @@ inline void* FX_ReallocOrDie(void* ptr,
#define FX_Free(ptr) free(ptr)
+// The FX_ArraySize(arr) macro returns the # of elements in an array arr.
+// The expression is a compile-time constant, and therefore can be
+// used in defining new arrays, for example. If you use FX_ArraySize on
+// a pointer by mistake, you will get a compile-time error.
+//
+// One caveat is that FX_ArraySize() doesn't accept any array of an
+// anonymous type or a type defined inside a function.
+#define FX_ArraySize(array) (sizeof(ArraySizeHelper(array)))
+
+// This template function declaration is used in defining FX_ArraySize.
+// Note that the function doesn't need an implementation, as we only
+// use its type.
+template <typename T, size_t N>
+char(&ArraySizeHelper(T(&array)[N]))[N];
+
+// Used with std::unique_ptr to FX_Free raw memory.
+struct FxFreeDeleter {
+ inline void operator()(void* ptr) const { FX_Free(ptr); }
+};
+
+// Used with std::unique_ptr to Release() objects that can't be deleted.
+template <class T>
+struct ReleaseDeleter {
+ inline void operator()(T* ptr) const { ptr->Release(); }
+};
+
class CFX_DestructObject {
public:
virtual ~CFX_DestructObject() {}
};
#endif // __cplusplus
-
#endif // CORE_INCLUDE_FXCRT_FX_MEMORY_H_

Powered by Google App Engine
This is Rietveld 408576698