zdzhaoyong / Svar

A Tiny Modern C++ Header Brings Unified Interface for Different Languages
Other
149 stars 17 forks source link

Why not unique_ptr return cpptype. #10

Open zdzhaoyong opened 2 years ago

zdzhaoyong commented 2 years ago

Why not unique_ptr return cpptype.

template <typename T>
class SvarValue_<std::shared_ptr<T>>: public SvarValue{
public:
    explicit SvarValue_(const std::shared_ptr<T>& v):_var(v){}
    explicit SvarValue_(std::shared_ptr<T>&& v):_var(std::move(v)){}

    virtual TypeID          cpptype()const{return typeid(T);}
    virtual SvarClass*     classObject()const{return SvarClass::instance<T>();}
    virtual Svar            clone(int depth=0)const{return _var;}
    virtual const void*     as(const TypeID& tp)const{
        if(tp==typeid(T)) return _var.get();
        else if(tp==typeid(std::shared_ptr<T>)) return &_var;
        else return nullptr;
    }

    std::shared_ptr<T> _var;
};

template <typename T>
class SvarValue_<std::unique_ptr<T>>: public SvarValue{
public:
    explicit SvarValue_(std::unique_ptr<T>&& v):_var(std::move(v)){}

    virtual SvarClass*     classObject()const{return SvarClass::instance<T>();}
    virtual const void*     as(const TypeID& tp)const{
        if(tp==typeid(T)) return _var.get();
        else if(tp==typeid(std::unique_ptr<T>)) return &_var;
        else return nullptr;
    }

    std::unique_ptr<T> _var;
};