diff --git a/src/utils/Makefile b/src/utils/Makefile index 92b5ba7..3e4e0b8 100644 --- a/src/utils/Makefile +++ b/src/utils/Makefile @@ -12,6 +12,7 @@ INC += pv/configuration.h INC += pv/likely.h INC += pv/wildcard.h INC += pv/fairQueue.h +INC += pv/destroyable.h LIBSRCS += hexDump.cpp LIBSRCS += inetAddressUtil.cpp diff --git a/src/utils/pv/destroyable.h b/src/utils/pv/destroyable.h new file mode 100644 index 0000000..7f109c5 --- /dev/null +++ b/src/utils/pv/destroyable.h @@ -0,0 +1,59 @@ +/* destroyable.h */ +/* + * Copyright information and license terms for this software can be + * found in the file LICENSE that is included with the distribution + */ +/** + * @author mse + */ +#ifndef DESTROYABLE_H +#define DESTROYABLE_H + +#include + +#include + +namespace epics { namespace pvData { + + + /** + * @brief Instance declaring destroy method. + * + * @author mse + */ + class epicsShareClass Destroyable { + public: + POINTER_DEFINITIONS(Destroyable); + /** + * Destroy this instance. + */ + virtual void destroy() = 0; + + protected: + /** + * Do not allow delete on this instance and derived classes, destroy() must be used instead. + */ + virtual ~Destroyable() {}; + public: + + /** for use with shared_ptr<> when wrapping + * + @code + shared_ptr inner(new foo), + outer(inner.get, Destroyable::cleaner(inner)); + @endcode + */ + class cleaner { + Destroyable::shared_pointer ptr; + public: + cleaner(const Destroyable::shared_pointer& ptr) :ptr(ptr) {} + void operator()(Destroyable*) { + Destroyable::shared_pointer P; + P.swap(ptr); + P->destroy(); + } + }; + }; + +}} +#endif /* DESTROYABLE_H */