From 28b6a545a560dca8b7212b81cae4fc1e69932e75 Mon Sep 17 00:00:00 2001 From: Travis Gockel Date: Tue, 7 Oct 2014 23:16:47 -0600 Subject: [PATCH] Issue #4: Greatly simplify building a path with implicit construction of path_elements. --- include/jsonv/path.hpp | 15 +++++---------- src/jsonv/path.cpp | 38 ++++++++++++-------------------------- 2 files changed, 17 insertions(+), 36 deletions(-) diff --git a/include/jsonv/path.hpp b/include/jsonv/path.hpp index f0fa5ed..3cb6cc1 100644 --- a/include/jsonv/path.hpp +++ b/include/jsonv/path.hpp @@ -37,8 +37,11 @@ JSONV_PUBLIC std::string to_string(const path_element_kind&); class JSONV_PUBLIC path_element { public: - explicit path_element(std::size_t idx); - explicit path_element(std::string key); + path_element(std::size_t idx); + path_element(int idx); + path_element(std::string key); + path_element(string_ref key); + path_element(const char* key); path_element(const path_element&); path_element& operator=(const path_element&); path_element(path_element&&) noexcept; @@ -101,14 +104,6 @@ class JSONV_PUBLIC path : /** Return a new path with the given \a elem appended to the back. **/ path operator+(path_element elem) const; path& operator+=(path_element elem); - - /** Return a new path with the given object \a key appended to the back. **/ - path operator+(std::string key) const; - path& operator+=(std::string key); - - /** Return a new path with the given array \a index appended to the back. **/ - path operator+(std::size_t index) const; - path& operator+=(std::size_t index); }; JSONV_PUBLIC std::ostream& operator<<(std::ostream&, const path&); diff --git a/src/jsonv/path.cpp b/src/jsonv/path.cpp index b3e03bc..bce9f7c 100644 --- a/src/jsonv/path.cpp +++ b/src/jsonv/path.cpp @@ -65,11 +65,23 @@ path_element::path_element(std::size_t idx) : _data(idx) { } +path_element::path_element(int idx) : + path_element(std::size_t(idx)) +{ } + path_element::path_element(std::string key) : _kind(path_element_kind::object_key), _data(std::move(key)) { } +path_element::path_element(string_ref key) : + path_element(std::string(key)) +{ } + +path_element::path_element(const char* key) : + path_element(std::string(key)) +{ } + path_element::path_element(const path_element& src) : _kind(src._kind), _data(0) @@ -312,32 +324,6 @@ path path::operator+(path_element elem) const return clone; } -path& path::operator+=(std::string key) -{ - _data.emplace_back(std::move(key)); - return *this; -} - -path path::operator+(std::string key) const -{ - path clone(*this); - clone += key; - return clone; -} - -path& path::operator+=(std::size_t index) -{ - _data.emplace_back(index); - return *this; -} - -path path::operator+(std::size_t index) const -{ - path clone(*this); - clone += index; - return clone; -} - std::ostream& operator<<(std::ostream& os, const path& val) { for (const path_element& elem : val)