jmespath.cpp
C++ implementation of JMESPath, a query language for JSON http://jmespath.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
contextvaluevisitoradaptor.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Author: Róbert Márki <gsmiko@gmail.com>
4 ** Copyright (c) 2016 Róbert Márki
5 **
6 ** This file is part of the jmespath.cpp project which is distributed under
7 ** the MIT License (MIT).
8 **
9 ** Permission is hereby granted, free of charge, to any person obtaining a copy
10 ** of this software and associated documentation files (the "Software"), to
11 ** deal in the Software without restriction, including without limitation the
12 ** rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
13 ** sell copies of the Software, and to permit persons to whom the Software is
14 ** furnished to do so, subject to the following conditions:
15 **
16 ** The above copyright notice and this permission notice shall be included in
17 ** all copies or substantial portions of the Software.
18 **
19 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 ** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 ** DEALINGS IN THE SOFTWARE.
26 **
27 ****************************************************************************/
28 #ifndef CONTEXTVALUEVISITORADAPTOR_H
29 #define CONTEXTVALUEVISITORADAPTOR_H
31 #include "jmespath/exceptions.h"
32 #include <boost/variant.hpp>
33 #include <boost/hana.hpp>
34 
35 namespace jmespath{ namespace interpreter {
36 
42 template <typename VisitorT, bool ForceMove = false>
43 class ContextValueVisitorAdaptor : public boost::static_visitor<>
44 {
45 public:
52  ContextValueVisitorAdaptor(VisitorT&& visitor)
53  : boost::static_visitor<>{},
54  m_visitor{std::move(visitor)}
55  {
56  }
57 
63  template <typename T>
64  std::enable_if_t<std::is_same<T, JsonRef>::value && ForceMove, void>
65  operator()(const T& value)
66  {
67  m_visitor(Json(value.get()));
68  }
69 
75  template <typename T>
76  std::enable_if_t<std::is_same<T, JsonRef>::value && !ForceMove, void>
77  operator()(const T& value)
78  {
79  m_visitor(value.get());
80  }
81 
86  void operator()(Json& value)
87  {
88  m_visitor(std::move(value));
89  }
90 
91 private:
95  VisitorT m_visitor;
96 };
97 
108 inline decltype(auto) makeVisitor(
109  std::function<void(const Json&)> &&lvalueFunc,
110  std::function<void(Json&&)> &&rvalueFunc)
111 {
112  auto functions = boost::hana::overload_linearly(
113  std::move(rvalueFunc),
114  std::move(lvalueFunc)
115  );
117  std::move(functions)
118  };
119 }
120 
130 inline decltype(auto) makeMoveOnlyVisitor(
131  std::function<void(Json&&)> rvalueFunc)
132 {
134  std::move(rvalueFunc)
135  };
136  return result;
137 }
138 }} // namespace jmespath::interpreter
139 #endif // CONTEXTVALUEVISITORADAPTOR_H
The ContextValueVisitorAdaptor class adapts a visitor object, which is callable with const lvalue ref...
Definition: contextvaluevisitoradaptor.h:43
nlohmann::json Json
JSON data type.
Definition: types.h:66
decltype(auto) makeVisitor(std::function< void(const Json &)> &&lvalueFunc, std::function< void(Json &&)> &&rvalueFunc)
Create visitor object which accepts ContextValue objects, and calls lvalueFunc callable with a const ...
Definition: contextvaluevisitoradaptor.h:108
ContextValueVisitorAdaptor(VisitorT &&visitor)
Constructs a ContextValueVisitorAdaptor object, adapting the visitor object to be able to consume Con...
Definition: contextvaluevisitoradaptor.h:52
m_visitor
Definition: contextvaluevisitoradaptor.h:54
decltype(auto) makeMoveOnlyVisitor(std::function< void(Json &&)> rvalueFunc)
Create visitor object which accepts ContextValue objects, and calls the rvalueFunc callable with an r...
Definition: contextvaluevisitoradaptor.h:130