10#ifndef XTENSOR_XEXPRESSION_HOLDER_HPP
11#define XTENSOR_XEXPRESSION_HOLDER_HPP
15#include <nlohmann/json.hpp>
17#include "../containers/xarray.hpp"
18#include "../core/xtensor_config.hpp"
19#include "../io/xjson.hpp"
26 class xexpression_holder_impl;
29 class xexpression_wrapper;
32 class xexpression_holder
36 using implementation_type = detail::xexpression_holder_impl;
38 xexpression_holder() =
default;
41 xexpression_holder(E&& expr);
43 xexpression_holder(implementation_type* holder);
44 xexpression_holder(
const xexpression_holder& holder);
45 xexpression_holder(xexpression_holder&& holder);
47 xexpression_holder& operator=(
const xexpression_holder&);
48 xexpression_holder& operator=(xexpression_holder&&);
50 void swap(xexpression_holder&);
52 void to_json(nlohmann::json&)
const;
53 void from_json(
const nlohmann::json&);
57 void init_pointer_from_json(
const nlohmann::json&);
58 void check_holder()
const;
60 std::unique_ptr<implementation_type> p_holder;
75 class xexpression_holder_impl
79 xexpression_holder_impl(xexpression_holder_impl&&) =
delete;
81 xexpression_holder_impl& operator=(
const xexpression_holder_impl&) =
delete;
82 xexpression_holder_impl& operator=(xexpression_holder_impl&&) =
delete;
84 virtual xexpression_holder_impl* clone()
const = 0;
85 virtual void to_json(nlohmann::json&)
const = 0;
86 virtual void from_json(
const nlohmann::json&) = 0;
87 virtual ~xexpression_holder_impl() =
default;
91 xexpression_holder_impl() =
default;
92 xexpression_holder_impl(
const xexpression_holder_impl&) =
default;
96 class xexpression_wrapper :
public xexpression_holder_impl
101 xexpression_wrapper(E&& expr);
103 xexpression_wrapper* clone()
const;
105 void to_json(nlohmann::json&)
const;
106 void from_json(
const nlohmann::json&);
108 ~xexpression_wrapper() =
default;
112 xexpression_wrapper(
const xexpression_wrapper&);
121 inline xexpression_holder::xexpression_holder(E&& expr)
122 : p_holder(new detail::xexpression_wrapper<E>(std::forward<E>(expr)))
126 inline xexpression_holder::xexpression_holder(implementation_type* holder)
132 : p_holder(holder.p_holder->clone())
137 : p_holder(std::move(holder.p_holder))
143 xexpression_holder tmp(holder);
156 std::swap(p_holder, holder.p_holder);
159 inline void xexpression_holder::to_json(nlohmann::json& j)
const
161 if (p_holder ==
nullptr)
165 p_holder->to_json(j);
168 inline void xexpression_holder::from_json(
const nlohmann::json& j)
172 XTENSOR_THROW(std::runtime_error,
"Received a JSON that does not contain a tensor");
175 if (p_holder ==
nullptr)
177 init_pointer_from_json(j);
179 p_holder->from_json(j);
182 inline void xexpression_holder::init_pointer_from_json(
const nlohmann::json& j)
186 return init_pointer_from_json(j[0]);
192 p_holder.reset(
new detail::xexpression_wrapper<
xt::xarray<double>>(std::move(empty_arr)));
198 p_holder.reset(
new detail::xexpression_wrapper<
xt::xarray<bool>>(std::move(empty_arr)));
207 XTENSOR_THROW(std::runtime_error,
"Received a JSON with a tensor that contains unsupported data type");
210 inline void xexpression_holder::check_holder()
const
212 if (p_holder ==
nullptr)
214 XTENSOR_THROW(std::runtime_error,
"The holder does not contain an expression");
239 inline xexpression_wrapper<CTE>::xexpression_wrapper(E&& expr)
240 : xexpression_holder_impl()
241 , m_expression(std::forward<E>(expr))
246 inline xexpression_wrapper<CTE>* xexpression_wrapper<CTE>::clone()
const
248 return new xexpression_wrapper<CTE>(*
this);
252 inline void xexpression_wrapper<CTE>::to_json(nlohmann::json& j)
const
258 inline void xexpression_wrapper<CTE>::from_json(
const nlohmann::json& j)
264 inline xexpression_wrapper<CTE>::xexpression_wrapper(
const xexpression_wrapper& wrapper)
265 : xexpression_holder_impl()
266 , 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.