Skip to content

Commit

Permalink
use path_node
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Nov 13, 2023
1 parent 58cd5c6 commit a3184ee
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 53 deletions.
48 changes: 18 additions & 30 deletions include/jsoncons_ext/jsonpath/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2070,19 +2070,13 @@ namespace detail {
using reference = JsonReference;
using value_pointer = typename std::conditional<std::is_const<typename std::remove_reference<JsonReference>::type>::value,typename Json::const_pointer,typename Json::pointer>::type;
using path_node_type = basic_path_node<typename Json::char_type>;
using json_location_type = basic_json_location<char_type>;
using path_pointer = const path_node_type*;

json_location_type path_;
path_pointer path_ptr_;
value_pointer value_ptr_;

path_value_pair(const json_location_type& path, reference value) noexcept
: path_(path), value_ptr_(std::addressof(value))
{
}

path_value_pair(json_location_type&& path, value_pointer valp) noexcept
: path_(std::move(path)), value_ptr_(valp)
path_value_pair(const path_node_type& path, reference value) noexcept
: path_ptr_(std::addressof(path)), value_ptr_(std::addressof(value))
{
}

Expand All @@ -2091,9 +2085,9 @@ namespace detail {
path_value_pair& operator=(const path_value_pair&) = default;
path_value_pair& operator=(path_value_pair&& other) = default;

json_location_type path() const
path_node_type path() const
{
return path_;
return *path_ptr_;
}

reference value()
Expand Down Expand Up @@ -2131,7 +2125,6 @@ namespace detail {
using reference = JsonReference;
using value_pointer = typename std::conditional<std::is_const<typename std::remove_reference<JsonReference>::type>::value,typename Json::const_pointer,typename Json::pointer>::type;
using path_node_type = basic_path_node<typename Json::char_type>;
using json_location_type = basic_json_location<char_type>;
using path_pointer = const path_node_type*;
private:
const path_node_type* last_ptr_;
Expand Down Expand Up @@ -2164,8 +2157,7 @@ namespace detail {

virtual ~node_receiver() noexcept = default;

virtual void add(const path_node_type& tail_nodes,
reference value) = 0;
virtual void add(const path_node_type& base_path, reference value) = 0;
};

template <class Json,class JsonReference>
Expand All @@ -2177,7 +2169,6 @@ namespace detail {
using char_type = typename Json::char_type;
using string_type = typename Json::string_type;
using path_node_type = basic_path_node<typename Json::char_type>;
using json_location_type = basic_json_location<char_type>;
using path_value_pair_type = path_value_pair<Json,JsonReference>;

allocator_type alloc_;
Expand All @@ -2188,10 +2179,9 @@ namespace detail {
{
}

void add(const path_node_type& tail_nodes,
reference value) override
void add(const path_node_type& base_path, reference value) override
{
nodes.emplace_back(json_location_type(tail_nodes), std::addressof(value));
nodes.emplace_back(base_path, value);
}
};

Expand All @@ -2207,10 +2197,9 @@ namespace detail {

std::vector<path_component_value_pair_type> nodes;

void add(const path_node_type& tail_nodes,
reference value) override
void add(const path_node_type& base_path, reference value) override
{
nodes.emplace_back(tail_nodes, value);
nodes.emplace_back(base_path, value);
}
};

Expand Down Expand Up @@ -2320,7 +2309,6 @@ namespace detail {
using pointer = typename std::conditional<std::is_const<typename std::remove_reference<JsonReference>::type>::value,typename Json::const_pointer,typename Json::pointer>::type;
using path_value_pair_type = path_value_pair<Json,JsonReference>;
using path_node_type = basic_path_node<typename Json::char_type>;
using json_location_type = basic_json_location<char_type>;
using node_receiver_type = node_receiver<Json,JsonReference>;
using selector_type = jsonpath_selector<Json,JsonReference>;

Expand Down Expand Up @@ -2350,14 +2338,14 @@ namespace detail {

virtual void select(dynamic_resources<Json,JsonReference>& resources,
reference root,
const path_node_type& tail_nodes,
const path_node_type& base_path,
reference val,
node_receiver_type& receiver,
result_options options) const = 0;

virtual reference evaluate(dynamic_resources<Json,JsonReference>& resources,
reference root,
const path_node_type& tail_nodes,
const path_node_type& base_path,
reference current,
result_options options,
std::error_code& ec) const = 0;
Expand Down Expand Up @@ -3016,10 +3004,10 @@ namespace detail {
{
}

void add(const path_node_type& tail_nodes,
void add(const path_node_type& base_path,
reference value) override
{
callback_(tail_nodes, value);
callback_(base_path, value);
}
};

Expand Down Expand Up @@ -3128,7 +3116,7 @@ namespace detail {
receiver.nodes.erase(last,receiver.nodes.end());
for (auto& node : receiver.nodes)
{
callback(node.path().base_node(), node.value());
callback(node.path(), node.value());
}
}
else
Expand All @@ -3151,15 +3139,15 @@ namespace detail {
}
for (auto& node : temp2)
{
callback(node.path().base_node(), node.value());
callback(node.path(), node.value());
}
}
}
else
{
for (auto& node : receiver.nodes)
{
callback(node.path().base_node(), node.value());
callback(node.path(), node.value());
}
}
}
Expand Down Expand Up @@ -3196,7 +3184,7 @@ namespace detail {
for (std::size_t i = receiver.nodes.size(); i-- > 0;)
{
auto& node = receiver.nodes[i];
callback(node.path().base_node(), node.value());
callback(node.path(), node.value());
}
}
}
Expand Down
22 changes: 2 additions & 20 deletions include/jsoncons_ext/jsonpath/json_location.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,6 @@ namespace jsonpath {
{
}

basic_path_node(basic_path_node&& other)
: root_(other.root_),
parent_(other.parent_),
node_kind_(other.node_kind_),
name_(other.node_kind_ == path_node_kind::root ? string_view_type(&root_, 1) : other.name_),
index_(other.index_)
{
}

basic_path_node& operator=(const basic_path_node& other)
{
root_ = other.root_;
Expand All @@ -86,16 +77,6 @@ namespace jsonpath {
return *this;
}

basic_path_node& operator=(basic_path_node&& other)
{
root_ = other.root_;
parent_ = other.parent_;
node_kind_ = other.node_kind_;
index_ = other.index_;
name_ = other.node_kind_ == path_node_kind::root ? string_view_type(&root_, 1) : other.name_;
return *this;
}

const basic_path_node* parent() const { return parent_;}

path_node_kind node_kind() const
Expand Down Expand Up @@ -348,11 +329,12 @@ namespace jsonpath {
private:
allocator_type alloc_;
std::vector<path_element_type> elements_;
const path_node_type* base_node_;
public:
using iterator = typename std::vector<path_element_type>::iterator;
using const_iterator = typename std::vector<path_element_type>::const_iterator;

const path_node_type* base_node_;

basic_json_location(const path_node_type& node, const allocator_type& alloc = allocator_type())
: alloc_(alloc), elements_(node.size(), path_element_type{}), base_node_(std::addressof(node))
{
Expand Down
9 changes: 6 additions & 3 deletions include/jsoncons_ext/jsonpath/jsonpath_selector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,14 @@ namespace detail {

supertype* tail_;
public:
using char_type = typename Json::char_type;
using string_type = typename Json::string_type;
using value_type = typename supertype::value_type;
using reference = typename supertype::reference;
using pointer = typename supertype::pointer;
using path_value_pair_type = typename supertype::path_value_pair_type;
using path_node_type = typename supertype::path_node_type;
using json_location_type = typename supertype::json_location_type;
using json_location_type = typename basic_json_location<char_type>;
using node_receiver_type = typename supertype::node_receiver_type;
using selector_type = typename supertype::selector_type;

Expand Down Expand Up @@ -552,12 +553,13 @@ namespace detail {
int ancestor_depth_;

public:
using char_type = typename Json::char_type;
using value_type = typename supertype::value_type;
using reference = typename supertype::reference;
using pointer = typename supertype::pointer;
using path_value_pair_type = typename supertype::path_value_pair_type;
using path_node_type = typename supertype::path_node_type;
using json_location_type = typename supertype::json_location_type;
using json_location_type = basic_json_location<char_type>;
using path_generator_type = path_generator<Json,JsonReference>;
using node_receiver_type = typename supertype::node_receiver_type;

Expand Down Expand Up @@ -887,12 +889,13 @@ namespace detail {
{
using supertype = jsonpath_selector<Json,JsonReference>;
public:
using char_type = typename Json::char_type;
using value_type = typename supertype::value_type;
using reference = typename supertype::reference;
using pointer = typename supertype::pointer;
using path_value_pair_type = typename supertype::path_value_pair_type;
using path_node_type = typename supertype::path_node_type;
using json_location_type = typename supertype::json_location_type;
using json_location_type = basic_json_location<char_type>;
using path_expression_type = path_expression<Json, JsonReference>;
using path_generator_type = path_generator<Json,JsonReference>;
using node_receiver_type = typename supertype::node_receiver_type;
Expand Down

0 comments on commit a3184ee

Please sign in to comment.