Commit 364a5303 authored by Celian Gossec's avatar Celian Gossec
Browse files

Update: any_span: now uses any_ref as values for type-safety, as well as a lot...

Update: any_span: now uses any_ref as values for type-safety, as well as a lot of minor fixes regarding compilation errors
parent 0e8f5db6
......@@ -58,12 +58,12 @@ target_sources(Pylene PRIVATE
target_compile_features(Pylene PUBLIC cxx_std_17)
# Add concepts support for gcc > 7.2 with -fconcepts
if (CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.2)
target_compile_options(Pylene PUBLIC -fconcepts)
find_package(cmcstl2 REQUIRED)
target_link_libraries(Pylene PUBLIC stl2)
target_compile_definitions(Pylene PUBLIC PYLENE_CONCEPT_TS_ENABLED)
endif ()
# if (CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.2)
# target_compile_options(Pylene PUBLIC -fconcepts)
# find_package(cmcstl2 REQUIRED)
# target_link_libraries(Pylene PUBLIC stl2)
# target_compile_definitions(Pylene PUBLIC PYLENE_CONCEPT_TS_ENABLED)
# endif ()
# IDE configuration
......
......@@ -5,6 +5,8 @@
namespace mln::py
{
namespace impl
{
template <class T>
image2d<uint8_t> invert(const image2d<T>& src)
{
......@@ -13,20 +15,26 @@ namespace mln::py
const auto& vs = src.get_value_set();
std::transform(span.begin(), span.end(), res.values().begin(),
[&vs](auto val) -> uint8_t {
return 255 - val;
auto tmp = vs.max();
auto new_val = vs.template cast<float>(val) / vs.template cast<float>(tmp);
return (1 - new_val) * 255;
});
return res;
}
}
namespace detail
image2d<uint8_t> invert(const image2d<>& src)
{
template <typename T>
struct apply_invert_t
switch (src.type().tid())
{
auto operator()(const image2d<>& src) { return invert(*src.cast_to<T>()); }
case (Info::INT8_V):
return impl::invert(*src.cast_to<int8_t>());
case (Info::INT32_V):
return impl::invert(*src.cast_to<int32_t>());
default:
return impl::invert(src);
}
};
} // namespace detail
image2d<uint8_t> invert(const image2d<>& src) { return visit<detail::apply_invert_t>(src.type().tid(), src); };
image2d<> invert_py(const image2d<>& src) { return invert(src); };
} // namespace mln::py
\ No newline at end of file
......@@ -20,7 +20,7 @@ namespace mln::py
std::transform(span.begin(), span.end(), res.values().begin(),
[&vs](auto val) -> float {
auto tmp = vs.max();
return generic_cast<float>(val) / generic_cast<float>(tmp);
return vs.template cast<float>(val) / vs.template cast<float>(tmp);
});
return res;
}
......
......@@ -17,10 +17,11 @@ namespace mln::py
{
public:
any_ref() = default;
any_ref(std::any& elm);
any_ref(const any_ref& other) = default;
any_ref(any_ref&& other) noexcept = default;
any_ref(std::byte* buf, Info i);
template <typename T>
template <typename T, class = std::enable_if_t<!std::is_same_v<any_ref, T>>>
any_ref(T& elm)
{
new (&m_held) placeholder<T>(elm);
......
......@@ -44,42 +44,7 @@ namespace mln::py
{
m_buffer = static_cast<std::byte*>(buf);
m_size = sz;
switch (typeid(T))
{
case (typeid(int8_t)):
m_type = Info::INT8_V;
break;
case (typeid(int16_t)):
m_type = Info::INT16_V;
break;
case (typeid(int32_t)):
m_type = Info::INT32_V;
break;
case (typeid(int64_t)):
m_type = Info::INT64_V;
break;
case (typeid(uint8_t)):
m_type = Info::UINT8_V;
break;
case (typeid(uint16_t)):
m_type = Info::UINT16_V;
break;
case (typeid(uint32_t)):
m_type = Info::UINT32_V;
break;
case (typeid(uint64_t)):
m_type = Info::UINT64_V;
break;
case (typeid(float)):
m_type = Info::FLOAT_V;
break;
case (typeid(double)):
m_type = Info::DOUBLE_V;
break;
default:
m_type = Info::OTHER;
break;
}
m_type = Trait<T>::value;
}
private:
......
......@@ -13,6 +13,7 @@ namespace mln::py
struct image2d_data_base
{
image2d_data_base() = default;
image2d_data_base(std::size_t n);
virtual ~image2d_data_base() = default;
......
......@@ -22,9 +22,15 @@ namespace mln::py
std::any cast(any_ref v, const Info& dest_type) const { return _cast(v, dest_type); }
template <typename Dest_type>
Dest_type cast(any_ref v) const
{
return std::any_cast<Dest_type>(_cast(v, Trait<Dest_type>::value));
}
private:
virtual std::any _max() const = 0;
virtual std::any _cast(any_ref v, const Info& info) const = 0;
virtual std::any _cast(any_ref v, const Info& dest_type) const = 0;
};
template <typename T>
......@@ -37,6 +43,11 @@ namespace mln::py
return static_cast<U>(v.as<T>());
}
template <typename U>
U cast(T v) const
{
return static_cast<U>(v);
}
T max() const { return std::numeric_limits<T>::max(); }
......@@ -45,7 +56,6 @@ namespace mln::py
return val1.as<T>() / denominator.as<T>();
}
T divide(T val, T denominator) const
{
return val / denominator;
......@@ -54,44 +64,26 @@ namespace mln::py
private:
std::any _max() const override final { return max(); }
template <typename VT>
struct cast_dispatcher_t
{
explicit cast_dispatcher_t(const value_set<VT>* vs)
explicit cast_dispatcher_t(const value_set* vs)
: m_vs(vs)
{
}
template <typename U> // U return type
template <typename Dest_type>
std::any call(any_ref v)
{
return m_vs->template cast<U>(v);
return m_vs->template cast<Dest_type>(v);
}
private:
const value_set<T>* m_vs;
};
std::any _cast(any_ref val, const Info& info) const override final
std::any _cast(any_ref val, const Info& dest_type) const override final
{
return visit_f(info.tid(), cast_dispatcher_t{this}, val);
return visit_f(dest_type.tid(), cast_dispatcher_t{this}, val);
}
}; // value_set
template <typename T, typename Arg>
T generic_cast(Arg&& val)
{
return static_cast<T>(std::forward<Arg>(val));
}
template <typename T>
T generic_cast(any_ref val)
{
return val.as<T>();
}
template <typename T> //TODO constness of any?
T generic_cast(std::any val)
{
return std::any_cast<T>(val);
}
} // namespace mln::py
......@@ -14,13 +14,12 @@ namespace mln::py
}
};
any_ref::any_ref(std::byte* ptr, Info i)
{
visit_f(i.tid(), cast_dispatcher_t(), reinterpret_cast<void*>(&m_held), ptr);
}
any_ref::any_ref(std::any& elm) { new (&m_held) placeholder<std::any>(elm); }
void* any_ref::data() { return reinterpret_cast<placeholder_base*>(&m_held)->m_data; }
const std::type_info& any_ref::type() const { return reinterpret_cast<const placeholder_base*>(&m_held)->type(); }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment