1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-30 20:53:52 +00:00

Move to shared pointer, much cleaner

This commit is contained in:
Henry Fredrick Schreiner 2017-01-30 19:28:08 -05:00
parent 0667d7a88e
commit 074d0339a4

View File

@ -383,13 +383,15 @@ template <typename T>
class Value { class Value {
friend App; friend App;
protected: protected:
std::unique_ptr<std::unique_ptr<T>> value {new std::unique_ptr<T>()}; std::shared_ptr<std::unique_ptr<T>> value {new std::unique_ptr<T>()};
std::string name; std::string name;
public: public:
Value(std::string name) : name(name) {} Value(std::string name) : name(name) {}
operator bool() const {return (bool) *value;} operator bool() const {return (bool) *value;}
//explicit operator T () const {return **this;} /// Note this does not throw on assignment, though
T operator *() const { /// afterwards it seems to work fine. Best to use
/// explicit * notation.
T& operator *() const {
if(*value) { if(*value) {
//std::cout << "Succ" << std::endl; //std::cout << "Succ" << std::endl;
return **value; return **value;
@ -594,7 +596,7 @@ public:
) { ) {
Value<T> out(name); Value<T> out(name);
std::unique_ptr<T> *ptr = out.value.get(); std::shared_ptr<std::unique_ptr<T>> ptr = out.value;
CLI::callback_t fun = [ptr](CLI::results_t res){ CLI::callback_t fun = [ptr](CLI::results_t res){
if(res.size()!=1) { if(res.size()!=1) {
@ -603,11 +605,11 @@ public:
if(res[0].size()!=1) { if(res[0].size()!=1) {
return false; return false;
} }
ptr->reset(new T()); ptr->reset(new T()); // resets the internal ptr
return lexical_cast(res[0][0], **ptr); return lexical_cast(res[0][0], **ptr);
}; };
add_option(name, fun, discription, opts); add_option(name, fun, discription, opts);
return std::move(out); return out;
} }