10#ifndef XTENSOR_XEXPRESSION_HOLDER_HPP
11#define XTENSOR_XEXPRESSION_HOLDER_HPP
15#include <nlohmann/json.hpp>
19#include "xtensor_config.hpp"
20#include "xtl/xany.hpp"
27 class xexpression_holder_impl;
30 class xexpression_wrapper;
37 using implementation_type = detail::xexpression_holder_impl;
53 void to_json(nlohmann::json&)
const;
54 void from_json(
const nlohmann::json&);
58 void init_pointer_from_json(
const nlohmann::json&);
59 void check_holder()
const;
61 std::unique_ptr<implementation_type> p_holder;
76 class xexpression_holder_impl
80 xexpression_holder_impl(xexpression_holder_impl&&) =
delete;
82 xexpression_holder_impl& operator=(
const xexpression_holder_impl&) =
delete;
83 xexpression_holder_impl& operator=(xexpression_holder_impl&&) =
delete;
85 virtual xexpression_holder_impl* clone()
const = 0;
86 virtual void to_json(nlohmann::json&)
const = 0;
87 virtual void from_json(
const nlohmann::json&) = 0;
88 virtual ~xexpression_holder_impl() =
default;
92 xexpression_holder_impl() =
default;
93 xexpression_holder_impl(
const xexpression_holder_impl&) =
default;
97 class xexpression_wrapper :
public xexpression_holder_impl
102 xexpression_wrapper(E&& expr);
104 xexpression_wrapper* clone()
const;
106 void to_json(nlohmann::json&)
const;
107 void from_json(
const nlohmann::json&);
109 ~xexpression_wrapper() =
default;
113 xexpression_wrapper(
const xexpression_wrapper&);
122 inline xexpression_holder::xexpression_holder(E&& expr)
123 : p_holder(new detail::xexpression_wrapper<E>(std::forward<E>(expr)))
127 inline xexpression_holder::xexpression_holder(implementation_type* holder)
132 inline xexpression_holder::xexpression_holder(
const xexpression_holder& holder)
133 : p_holder(holder.p_holder->clone())
137 inline xexpression_holder::xexpression_holder(xexpression_holder&& holder)
138 : p_holder(std::move(holder.p_holder))
142 inline xexpression_holder& xexpression_holder::operator=(
const xexpression_holder& holder)
144 xexpression_holder tmp(holder);
149 inline xexpression_holder& xexpression_holder::operator=(xexpression_holder&& holder)
155 inline void xexpression_holder::swap(xexpression_holder& holder)
157 std::swap(p_holder, holder.p_holder);
160 inline void xexpression_holder::to_json(nlohmann::json& j)
const
162 if (p_holder ==
nullptr)
166 p_holder->to_json(j);
169 inline void xexpression_holder::from_json(
const nlohmann::json& j)
173 XTENSOR_THROW(std::runtime_error,
"Received a JSON that does not contain a tensor");
176 if (p_holder ==
nullptr)
178 init_pointer_from_json(j);
180 p_holder->from_json(j);
183 inline void xexpression_holder::init_pointer_from_json(
const nlohmann::json& j)
187 return init_pointer_from_json(j[0]);
193 p_holder.reset(
new detail::xexpression_wrapper<
xt::xarray<double>>(std::move(empty_arr)));
199 p_holder.reset(
new detail::xexpression_wrapper<
xt::xarray<bool>>(std::move(empty_arr)));
208 XTENSOR_THROW(std::runtime_error,
"Received a JSON with a tensor that contains unsupported data type");
211 inline void xexpression_holder::check_holder()
const
213 if (p_holder ==
nullptr)
215 XTENSOR_THROW(std::runtime_error,
"The holder does not contain an expression");
224 inline void to_json(nlohmann::json& j,
const xexpression_holder& o)
229 inline void from_json(
const nlohmann::json& j, xexpression_holder& o)
240 inline xexpression_wrapper<CTE>::xexpression_wrapper(E&& expr)
241 : xexpression_holder_impl()
242 , m_expression(std::forward<E>(expr))
247 inline xexpression_wrapper<CTE>* xexpression_wrapper<CTE>::clone()
const
249 return new xexpression_wrapper<CTE>(*
this);
253 inline void xexpression_wrapper<CTE>::to_json(nlohmann::json& j)
const
259 inline void xexpression_wrapper<CTE>::from_json(
const nlohmann::json& j)
265 inline xexpression_wrapper<CTE>::xexpression_wrapper(
const xexpression_wrapper& wrapper)
266 : xexpression_holder_impl()
267 , m_expression(wrapper.m_expression)
standard mathematical functions for xexpressions
enable_xcontainer_semantics< E > from_json(const nlohmann::basic_json< M > &, E &)
JSON deserialization of a xtensor expression with a container or a view semantics.
enable_xexpression< E > to_json(nlohmann::basic_json< M > &, const E &)
JSON serialization of an xtensor expression.