diff options
| author | Markus Mittendrein <git@maxmitti.tk> | 2019-04-20 01:54:54 +0200 |
|---|---|---|
| committer | Markus Mittendrein <git@maxmitti.tk> | 2019-04-20 02:43:37 +0200 |
| commit | 43931c7e187bda85e2be12bccb14bd826a6c34c0 (patch) | |
| tree | 1987ad09d84049a87bbf482af7569b0f93e7d5fe /src/cppc4group.hpp | |
| parent | 712a61956cdd0e2e2907fb730b6dde9b5cc3524a (diff) | |
| download | cc4group-43931c7e187bda85e2be12bccb14bd826a6c34c0.tar.gz cc4group-43931c7e187bda85e2be12bccb14bd826a6c34c0.zip | |
Add custom memory management strategies to cppc4group
Diffstat (limited to 'src/cppc4group.hpp')
| -rw-r--r-- | src/cppc4group.hpp | 41 |
1 files changed, 32 insertions, 9 deletions
diff --git a/src/cppc4group.hpp b/src/cppc4group.hpp index f11387c..d946cee 100644 --- a/src/cppc4group.hpp +++ b/src/cppc4group.hpp @@ -76,6 +76,35 @@ public: // or an empty optional on failure using TmpMemoryCallback = std::optional<TmpMemory>(*)(size_t size); + // the C++ memory management strategies work exactly the same way as in C, but with std::function instead + class MemoryManagement { + public: + struct MemoryManagementStrategy_t; + using MemoryManagementStrategy = MemoryManagementStrategy_t*; + + using Start = std::function<void*(void* memory, size_t size)>; + using End = std::function<void(void* memory)>; + + private: + struct Private; + Private* p; + + public: + MemoryManagement(Start&& start, End&& end); + MemoryManagement(MemoryManagementStrategy strategy); + + ~MemoryManagement(); + + MemoryManagementStrategy operator()() const; + + // this may be used as start function if it need not do anything but return the original pointer + static constexpr auto NoOpStart = [](void* memory, size_t) { return memory; }; + + static const MemoryManagement Take; + static const MemoryManagement Copy; + static const MemoryManagement Reference; + }; + // these are equivalent to their C counterparts using ReadCallback = bool(*)(const void** const data, size_t* const size, void* const arg); using SetupCallback = bool(*)(void* const arg); @@ -88,12 +117,6 @@ public: Auto }; - enum MemoryManagement { - Take, - Copy, - Reference - }; - // use this to set one of the predefined strategies static void setTmpMemoryStrategy(const TmpMemoryStrategy strategy); @@ -115,8 +138,8 @@ public: bool openExisting(const std::string& path); bool openFd(const int fd); bool openFilePointer(FILE* file); - bool openMemory(const void* const data, const size_t size, const MemoryManagement management = Reference); - bool openWithReadCallback(const ReadCallback callback, void* const callbackArg, const MemoryManagement management = Take, SetupCallback initCallback = nullptr, SetupCallback deinitCallback = nullptr); + bool openMemory(const void* const data, const size_t size, const MemoryManagement& management = MemoryManagement::Reference); + bool openWithReadCallback(const ReadCallback callback, void* const callbackArg, const MemoryManagement& management = MemoryManagement::Take, SetupCallback initCallback = nullptr, SetupCallback deinitCallback = nullptr); // save actually maps to both cc4group.save and cc4group.saveOverwrite, thanks to default arguments (yes, thats the reason why they are separate in the C-API) bool save(const std::string& path, const bool overwrite = false); @@ -148,7 +171,7 @@ public: bool createFile(const std::string& path); - bool setEntryData(const std::string& path, const void* const data = nullptr, const size_t size = 0, const MemoryManagement management = Copy); + bool setEntryData(const std::string& path, const void* const data = nullptr, const size_t size = 0, const MemoryManagement& management = MemoryManagement::Copy); // to get the child group out of the optional in case of success, construct a new CppC4Group with the move constructor: CppC4Group child{std::move(*optionalChild)}; std::optional<CppC4Group> openAsChild(const std::string& path); |
