From 074d0339a4b024da708d0d9b758d973d0b817a8d Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Mon, 30 Jan 2017 19:28:08 -0500 Subject: [PATCH] Move to shared pointer, much cleaner --- include/CLI.hpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/include/CLI.hpp b/include/CLI.hpp index 2245c662..c9e9ae45 100644 --- a/include/CLI.hpp +++ b/include/CLI.hpp @@ -383,13 +383,15 @@ template class Value { friend App; protected: - std::unique_ptr> value {new std::unique_ptr()}; + std::shared_ptr> value {new std::unique_ptr()}; std::string name; public: Value(std::string name) : name(name) {} operator bool() const {return (bool) *value;} - //explicit operator T () const {return **this;} - T operator *() const { + /// Note this does not throw on assignment, though + /// afterwards it seems to work fine. Best to use + /// explicit * notation. + T& operator *() const { if(*value) { //std::cout << "Succ" << std::endl; return **value; @@ -594,7 +596,7 @@ public: ) { Value out(name); - std::unique_ptr *ptr = out.value.get(); + std::shared_ptr> ptr = out.value; CLI::callback_t fun = [ptr](CLI::results_t res){ if(res.size()!=1) { @@ -603,11 +605,11 @@ public: if(res[0].size()!=1) { return false; } - ptr->reset(new T()); + ptr->reset(new T()); // resets the internal ptr return lexical_cast(res[0][0], **ptr); }; add_option(name, fun, discription, opts); - return std::move(out); + return out; }