10#ifndef XTENSOR_XEXPRESSION_HOLDER_HPP
11#define XTENSOR_XEXPRESSION_HOLDER_HPP
17#include <nlohmann/json.hpp>
19#include "../containers/xarray.hpp"
20#include "../core/xtensor_config.hpp"
21#include "../io/xjson.hpp"
28 class xexpression_holder_impl;
31 class xexpression_wrapper;
34 class xexpression_holder
38 using implementation_type = detail::xexpression_holder_impl;
40 xexpression_holder() =
default;
43 requires(!std::same_as<std::decay_t<E>, xexpression_holder>)
44 xexpression_holder(E&& expr);
46 xexpression_holder(implementation_type* holder);
47 xexpression_holder(
const xexpression_holder& holder);
48 xexpression_holder(xexpression_holder&& holder);
50 xexpression_holder& operator=(
const xexpression_holder&);
51 xexpression_holder& operator=(xexpression_holder&&);
53 void swap(xexpression_holder&);
55 void to_json(nlohmann::json&)
const;
56 void from_json(
const nlohmann::json&);
60 void init_pointer_from_json(
const nlohmann::json&);
61 void check_holder()
const;
63 std::unique_ptr<implementation_type> p_holder;
78 class xexpression_holder_impl
82 xexpression_holder_impl(xexpression_holder_impl&&) =
delete;
84 xexpression_holder_impl& operator=(
const xexpression_holder_impl&) =
delete;
85 xexpression_holder_impl& operator=(xexpression_holder_impl&&) =
delete;
87 virtual xexpression_holder_impl* clone()
const = 0;
88 virtual void to_json(nlohmann::json&)
const = 0;
89 virtual void from_json(
const nlohmann::json&) = 0;
90 virtual ~xexpression_holder_impl() =
default;
94 xexpression_holder_impl() =
default;
95 xexpression_holder_impl(
const xexpression_holder_impl&) =
default;
99 class xexpression_wrapper :
public xexpression_holder_impl
104 xexpression_wrapper(E&& expr);
106 xexpression_wrapper* clone()
const;
108 void to_json(nlohmann::json&)
const;
109 void from_json(
const nlohmann::json&);
111 ~xexpression_wrapper() =
default;
115 xexpression_wrapper(
const xexpression_wrapper&);
125 inline xexpression_holder::xexpression_holder(E&& expr)
126 : p_holder(
new detail::xexpression_wrapper<E>(std::forward<E>(expr)))
130 inline xexpression_holder::xexpression_holder(implementation_type* holder)
136 : p_holder(holder.p_holder->clone())
141 : p_holder(std::move(holder.p_holder))
147 xexpression_holder tmp(holder);
160 std::swap(p_holder, holder.p_holder);
163 inline void xexpression_holder::to_json(nlohmann::json& j)
const
165 if (p_holder ==
nullptr)
169 p_holder->to_json(j);
172 inline void xexpression_holder::from_json(
const nlohmann::json& j)
176 XTENSOR_THROW(std::runtime_error,
"Received a JSON that does not contain a tensor");
179 if (p_holder ==
nullptr)
181 init_pointer_from_json(j);
183 p_holder->from_json(j);
186 inline void xexpression_holder::init_pointer_from_json(
const nlohmann::json& j)
188 if (j.is_array() && !j.empty())
190 init_pointer_from_json(j[0]);
192 else if (j.is_number())
195 p_holder.reset(
new detail::xexpression_wrapper<
xt::xarray<double>>(std::move(empty_arr)));
197 else if (j.is_boolean())
200 p_holder.reset(
new detail::xexpression_wrapper<
xt::xarray<bool>>(std::move(empty_arr)));
202 else if (j.is_string())
209 XTENSOR_THROW(std::runtime_error,
"Received a JSON with a tensor that contains unsupported data type");
213 inline void xexpression_holder::check_holder()
const
215 if (p_holder ==
nullptr)
217 XTENSOR_THROW(std::runtime_error,
"The holder does not contain an expression");
242 inline xexpression_wrapper<CTE>::xexpression_wrapper(E&& expr)
243 : xexpression_holder_impl()
244 , m_expression(std::forward<E>(expr))
249 inline xexpression_wrapper<CTE>* xexpression_wrapper<CTE>::clone()
const
251 return new xexpression_wrapper<CTE>(*
this);
255 inline void xexpression_wrapper<CTE>::to_json(nlohmann::json& j)
const
261 inline void xexpression_wrapper<CTE>::from_json(
const nlohmann::json& j)
267 inline xexpression_wrapper<CTE>::xexpression_wrapper(
const xexpression_wrapper& wrapper)
268 : xexpression_holder_impl()
269 , m_expression(wrapper.m_expression)
standard mathematical functions for xexpressions
xarray_container< uvector< T, A >, L, xt::svector< typename uvector< T, A >::size_type, 4, SA, true > > xarray
Alias template on xarray_container with default parameters for data container type and shape / stride...
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.