Skip to content

Commit

Permalink
Issue #4: Greatly simplify building a path with implicit construction…
Browse files Browse the repository at this point in the history
… of path_elements.
  • Loading branch information
tgockel committed Oct 8, 2014
1 parent 805c628 commit 28b6a54
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 36 deletions.
15 changes: 5 additions & 10 deletions include/jsonv/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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&);
Expand Down
38 changes: 12 additions & 26 deletions src/jsonv/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 28b6a54

Please sign in to comment.