add epics::auto_ptr<T> and epics::swap()

Avoid the flood of auto_ptr deprecation warnings
in the common cases of using auto_ptr
to automatically delete.
This commit is contained in:
Michael Davidsaver
2017-11-06 12:30:40 -06:00
parent 284e49c807
commit c590204cf9
6 changed files with 58 additions and 15 deletions

View File

@@ -9,7 +9,9 @@
#ifndef SHAREDPTR_H
#define SHAREDPTR_H
/*
#include <memory> /* for auto_ptr */
/** @file sharedPtr.h
* Pulls in the std::tr1 namespace with the following names
*
* class shared_ptr
@@ -190,4 +192,36 @@ inline std::ostream& operator<<(std::ostream& strm, const ::detail::ref_shower<T
typedef std::tr1::weak_ptr<clazz> weak_pointer; \
typedef std::tr1::weak_ptr<const clazz> const_weak_pointer
/* A semi-hack to help with migration from std::auto_ptr to std::unique_ptr,
* and avoid copious deprecation warning spam
* which may be hiding legitimate issues.
*
* Provides epics::auto_ptr<T> and epics::swap()
*
* epics::auto_ptr<T> is std::auto_ptr<T> for c++98
* and std::unique_ptr<T> for >= c++11.
*
* epics::swap() is the only supported operation.
* copy/assignment/return are not supported
* (use auto_ptr or unique_ptr explicitly).
*/
namespace epics{
#if __cplusplus>=201103L
template<typename T>
using auto_ptr = std::unique_ptr<T>;
template<typename T>
static inline void swap(auto_ptr<T>& lhs, auto_ptr<T>& rhs) {
lhs.swap(rhs);
}
#else
using std::auto_ptr;
template<typename T>
static inline void swap(auto_ptr<T>& lhs, auto_ptr<T>& rhs) {
auto_ptr<T> temp(lhs);
lhs = rhs;
rhs = temp;
}
#endif
}
#endif // SHAREDPTR_H

View File

@@ -141,11 +141,7 @@ public:
std::ostringstream p_strm;
bool p_autostart;
Runnable *p_runner;
#if __cplusplus>=201103L
typedef std::unique_ptr<Runnable> p_owned_runner_t;
#else
typedef std::auto_ptr<Runnable> p_owned_runner_t;
#endif
typedef epics::auto_ptr<Runnable> p_owned_runner_t;
p_owned_runner_t p_owned_runner;
friend class Thread;
Runnable& x_getrunner()

View File

@@ -20,6 +20,7 @@
#define epicsExportSharedSymbols
#include <pv/pvdVersion.h>
#include <pv/sharedPtr.h>
#include "pv/reftrack.h"
namespace {
@@ -171,7 +172,7 @@ std::ostream& operator<<(std::ostream& strm, const RefSnapshot& snap)
struct RefMonitor::Impl : public epicsThreadRunable
{
RefMonitor& owner;
std::auto_ptr<epicsThread> worker;
epics::auto_ptr<epicsThread> worker;
epicsMutex lock;
epicsEvent wakeup;
RefSnapshot prev;
@@ -229,11 +230,11 @@ void RefMonitor::start(double period)
void RefMonitor::stop()
{
std::auto_ptr<epicsThread> W;
epics::auto_ptr<epicsThread> W;
{
Guard G(impl->lock);
if(!impl->worker.get()) return;
W = impl->worker;
epics::swap(W, impl->worker);
impl->done = true;
}

View File

@@ -180,7 +180,7 @@ void ValueBuilder::_add(const std::string& name, ScalarType stype, const void *V
THROW_EXCEPTION2(std::logic_error, "Not allowed to replace field. wrong type");
}
std::auto_ptr<child> store;
epics::auto_ptr<child> store;
switch(stype) {
#define STYPE(stype) case stype: store.reset(new child_scalar<ScalarTypeTraits<stype>::type>(V)); break
STYPE(pvBoolean);
@@ -216,7 +216,7 @@ void ValueBuilder::_add(const std::string& name, const shared_vector<const void>
THROW_EXCEPTION2(std::logic_error, "Not allowed to replace field. wrong type");
}
std::auto_ptr<child> store(new child_scalar_array(V));
epics::auto_ptr<child> store(new child_scalar_array(V));
children[name] = store.get();
store.release();
@@ -229,7 +229,7 @@ ValueBuilder& ValueBuilder::addNested(const std::string& name, Type type, const
child_struct *sub;
children_t::const_iterator it(children.find(name));
if(it==children.end()) {
std::auto_ptr<child_struct> store(new child_struct(this, id));
epics::auto_ptr<child_struct> store(new child_struct(this, id));
sub = store.get();
children[name] = store.get();
store.release();

View File

@@ -26,7 +26,7 @@ using std::cout;
static
void testBasicOperations() {
std::auto_ptr<ByteBuffer> buff(new ByteBuffer(32));
epics::auto_ptr<ByteBuffer> buff(new ByteBuffer(32));
testOk1(buff->getSize()==32);
@@ -185,7 +185,7 @@ static
void testInverseEndianness(int order, const char *expect) {
testDiag("check byte swapping features order=%d", order);
std::auto_ptr<ByteBuffer> buf(new ByteBuffer(32,order));
epics::auto_ptr<ByteBuffer> buf(new ByteBuffer(32,order));
buf->putShort(0x6162);
buf->putInt(0x63646566);

View File

@@ -620,9 +620,20 @@ void testBad()
//I = epics::pvData::that(CF);
}
static
void testAutoSwap()
{
epics::auto_ptr<int> A(new int(42)), B(new int(43));
epics::swap(A, B);
testOk1(43==*A);
testOk1(42==*B);
}
MAIN(testSharedVector)
{
testPlan(167);
testPlan(169);
testDiag("Tests for shared_vector");
testDiag("sizeof(shared_vector<int32>)=%lu",
@@ -643,5 +654,6 @@ MAIN(testSharedVector)
testWeak();
testICE();
testBad();
testAutoSwap();
return testDone();
}