From 74ab788024c3a740b158c56594ac3d46e53c87b3 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 31 Jan 2019 17:20:30 +0000 Subject: Remove calls to naked new --- libadhocutil/cache.impl.h | 10 +++++----- libadhocutil/globalStatic.h | 5 ++++- libadhocutil/globalStatic.impl.h | 12 +++++------- libadhocutil/lexer-regex.cpp | 2 +- libadhocutil/nvpParse.h | 2 +- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/libadhocutil/cache.impl.h b/libadhocutil/cache.impl.h index 8fdc958..d1929a9 100644 --- a/libadhocutil/cache.impl.h +++ b/libadhocutil/cache.impl.h @@ -19,7 +19,7 @@ Cacheable::Cacheable(const K & k, time_t vu) : template ObjectCacheable::ObjectCacheable(const T & t, const K & k, time_t vu) : Cacheable(k, vu), - value(new T(t)) + value(std::make_shared(t)) { } @@ -90,7 +90,7 @@ void Cache::add(const K & k, const T & t, time_t validUntil) { Lock(lock); - cached.insert(Element(new ObjectCacheable(t, k, validUntil))); + cached.insert(std::make_shared>(t, k, validUntil)); } template @@ -98,7 +98,7 @@ void Cache::addPointer(const K & k, Value & t, time_t validUntil) { Lock(lock); - cached.insert(Element(new ObjectCacheable(t, k, validUntil))); + cached.insert(std::make_shared>(t, k, validUntil)); } template @@ -106,7 +106,7 @@ void Cache::addFactory(const K & k, const Factory & tf, time_t validUntil) { Lock(lock); - cached.insert(Element(new CallCacheable(tf, k, validUntil))); + cached.insert(std::make_shared>(tf, k, validUntil)); } template @@ -114,7 +114,7 @@ void Cache::addPointerFactory(const K & k, const PointerFactory & tf, time_t validUntil) { Lock(lock); - cached.insert(Element(new PointerCallCacheable(tf, k, validUntil))); + cached.insert(std::make_shared>(tf, k, validUntil)); } template diff --git a/libadhocutil/globalStatic.h b/libadhocutil/globalStatic.h index d379146..5808551 100644 --- a/libadhocutil/globalStatic.h +++ b/libadhocutil/globalStatic.h @@ -1,6 +1,8 @@ #ifndef ADHOCUTIL_GLOBALSTATIC_H #define ADHOCUTIL_GLOBALSTATIC_H +#include + namespace AdHoc { /** * Wrapper class for initialising/destroying a global static object via @@ -16,10 +18,11 @@ namespace AdHoc { static Object * get(); private: + typedef std::unique_ptr Ptr; static void createObject() __attribute__((constructor(101))); static void deleteObject() __attribute__((destructor(101))); - inline static Object * & instance(); + inline static Ptr & instance(); }; } diff --git a/libadhocutil/globalStatic.impl.h b/libadhocutil/globalStatic.impl.h index 2a5cf05..fcd8096 100644 --- a/libadhocutil/globalStatic.impl.h +++ b/libadhocutil/globalStatic.impl.h @@ -7,27 +7,25 @@ namespace AdHoc { template Object * GlobalStatic::get() { - return instance(); + return instance().get(); } template void GlobalStatic::createObject() { - instance() = new Object(); + instance() = std::make_unique(); } template void GlobalStatic::deleteObject() { - auto & i = instance(); - delete i; - i = nullptr; + instance().reset(); } template - Object * & GlobalStatic::instance() + typename GlobalStatic::Ptr & GlobalStatic::instance() { - static Object * _instance = nullptr; + static Ptr _instance; return _instance; } }; diff --git a/libadhocutil/lexer-regex.cpp b/libadhocutil/lexer-regex.cpp index c283798..9b2be79 100644 --- a/libadhocutil/lexer-regex.cpp +++ b/libadhocutil/lexer-regex.cpp @@ -71,7 +71,7 @@ namespace AdHoc { Lexer::PatternPtr regex(const Glib::ustring & pattern, GRegexCompileFlags compile, GRegexMatchFlags match) { - return Lexer::PatternPtr(new Regex(pattern, compile, match)); + return std::make_shared(pattern, compile, match); } } } diff --git a/libadhocutil/nvpParse.h b/libadhocutil/nvpParse.h index ce851a6..3c6b03a 100644 --- a/libadhocutil/nvpParse.h +++ b/libadhocutil/nvpParse.h @@ -61,7 +61,7 @@ class NvpParse : public yyFlexLexer { /// @endcond #define NvpTarget(T) std::map>> -#define NvpValue(c, m) { #m, std::shared_ptr<::AdHoc::NvpParse::Target>(new ::AdHoc::NvpParse::Target(&c::m)) } +#define NvpValue(c, m) { #m, std::make_shared<::AdHoc::NvpParse::Target>(&c::m) } /** Parse an input stream into the given object. * @param in The input stream. -- cgit v1.2.3