mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
core: make owning pointers a top-level features
The undo-code uses owning pointers based on std::unique_ptr to manage lifetime of C-objects. Since these are generally useful, move them from the undo-code to the core-code. In fact, this eliminates one instance of code duplication. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
5b1557ccb1
commit
c5d6e0f44f
7 changed files with 50 additions and 31 deletions
|
|
@ -132,6 +132,7 @@ set(SUBSURFACE_CORE_LIB_SRCS
|
|||
metrics.cpp
|
||||
metrics.h
|
||||
ostctools.c
|
||||
owning_ptrs.h
|
||||
parse-gpx.cpp
|
||||
parse-xml.c
|
||||
parse.c
|
||||
|
|
|
|||
41
core/owning_ptrs.h
Normal file
41
core/owning_ptrs.h
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
// Convenience classes defining owning pointers to C-objects that
|
||||
// automatically clean up the objects if the pointers go out of
|
||||
// scope. Based on unique_ptr<>.
|
||||
// In the future, we should replace these by real destructors.
|
||||
#ifndef OWNING_PTR_H
|
||||
#define OWNING_PTR_H
|
||||
|
||||
#include <memory>
|
||||
#include <cstdlib>
|
||||
|
||||
struct dive;
|
||||
struct dive_trip;
|
||||
struct dive_site;
|
||||
struct event;
|
||||
|
||||
extern "C" void free_dive(struct dive *);
|
||||
extern "C" void free_trip(struct dive_trip *);
|
||||
extern "C" void free_dive_site(struct dive_site *);
|
||||
|
||||
// Classes used to automatically call the appropriate free_*() function for owning pointers that go out of scope.
|
||||
struct DiveDeleter {
|
||||
void operator()(dive *d) { free_dive(d); }
|
||||
};
|
||||
struct TripDeleter {
|
||||
void operator()(dive_trip *t) { free_trip(t); }
|
||||
};
|
||||
struct DiveSiteDeleter {
|
||||
void operator()(dive_site *ds) { free_dive_site(ds); }
|
||||
};
|
||||
struct EventDeleter {
|
||||
void operator()(event *ev) { free(ev); }
|
||||
};
|
||||
|
||||
// Owning pointers to dive, dive_trip, dive_site and event objects.
|
||||
using OwningDivePtr = std::unique_ptr<dive, DiveDeleter>;
|
||||
using OwningTripPtr = std::unique_ptr<dive_trip, TripDeleter>;
|
||||
using OwningDiveSitePtr = std::unique_ptr<dive_site, DiveSiteDeleter>;
|
||||
using OwningEventPtr = std::unique_ptr<event, EventDeleter>;
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue