From a350d0beb0e13d0f58698510bd6a96d894cd06fd Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Thu, 9 Jan 2020 22:46:41 +0100 Subject: [PATCH] json-to-value: use unique_ptr instead of raw pointers --- src/libexpr/json-to-value.cc | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/libexpr/json-to-value.cc b/src/libexpr/json-to-value.cc index 19d9a9b90..67feb9e1f 100644 --- a/src/libexpr/json-to-value.cc +++ b/src/libexpr/json-to-value.cc @@ -5,6 +5,7 @@ #include using json = nlohmann::json; +using std::unique_ptr; namespace nix { @@ -13,14 +14,14 @@ namespace nix { class JSONSax : nlohmann::json_sax { class JSONState { protected: - JSONState* parent; + unique_ptr parent; Value * v; public: - virtual JSONState* resolve(EvalState &) + virtual unique_ptr resolve(EvalState &) { throw std::logic_error("tried to close toplevel json parser state"); }; - explicit JSONState(JSONState* p) : parent(p), v(nullptr) {}; + explicit JSONState(unique_ptr&& p) : parent(std::move(p)), v(nullptr) {}; explicit JSONState(Value* v) : v(v) {}; JSONState(JSONState& p) = delete; Value& value(EvalState & state) @@ -36,13 +37,13 @@ class JSONSax : nlohmann::json_sax { class JSONObjectState : public JSONState { using JSONState::JSONState; ValueMap attrs = ValueMap(); - virtual JSONState* resolve(EvalState & state) override + virtual unique_ptr resolve(EvalState & state) override { Value& v = parent->value(state); state.mkAttrs(v, attrs.size()); for (auto & i : attrs) v.attrs->push_back(Attr(i.first, i.second)); - return parent; + return std::move(parent); } virtual void add() override { v = nullptr; }; public: @@ -54,28 +55,28 @@ class JSONSax : nlohmann::json_sax { class JSONListState : public JSONState { ValueVector values = ValueVector(); - virtual JSONState* resolve(EvalState & state) override + virtual unique_ptr resolve(EvalState & state) override { Value& v = parent->value(state); state.mkList(v, values.size()); for (size_t n = 0; n < values.size(); ++n) { v.listElems()[n] = values[n]; } - return parent; + return std::move(parent); } virtual void add() override { values.push_back(v); v = nullptr; }; public: - JSONListState(JSONState* p, std::size_t reserve) : JSONState(p) + JSONListState(unique_ptr&& p, std::size_t reserve) : JSONState(std::move(p)) { values.reserve(reserve); } }; EvalState & state; - JSONState* rs; + unique_ptr rs; template inline bool handle_value(T f, Args... args) { @@ -86,7 +87,6 @@ class JSONSax : nlohmann::json_sax { public: JSONSax(EvalState & state, Value & v) : state(state), rs(new JSONState(&v)) {}; - ~JSONSax() { delete rs; }; bool null() { @@ -120,21 +120,18 @@ public: bool start_object(std::size_t len) { - JSONState* old = rs; - rs = new JSONObjectState(old); + rs = std::make_unique(std::move(rs)); return true; } bool key(string_t& name) { - dynamic_cast(rs)->key(name, state); + dynamic_cast(rs.get())->key(name, state); return true; } bool end_object() { - JSONState* old = rs; - rs = old->resolve(state); - delete old; + rs = rs->resolve(state); rs->add(); return true; } @@ -144,8 +141,8 @@ public: } bool start_array(size_t len) { - JSONState* old = rs; - rs = new JSONListState(old, len != std::numeric_limits::max() ? len : 128); + rs = std::make_unique(std::move(rs), + len != std::numeric_limits::max() ? len : 128); return true; }