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 ...@@ -58,12 +58,12 @@ target_sources(Pylene PRIVATE
target_compile_features(Pylene PUBLIC cxx_std_17) target_compile_features(Pylene PUBLIC cxx_std_17)
# Add concepts support for gcc > 7.2 with -fconcepts # Add concepts support for gcc > 7.2 with -fconcepts
if (CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.2) # if (CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.2)
target_compile_options(Pylene PUBLIC -fconcepts) # target_compile_options(Pylene PUBLIC -fconcepts)
find_package(cmcstl2 REQUIRED) # find_package(cmcstl2 REQUIRED)
target_link_libraries(Pylene PUBLIC stl2) # target_link_libraries(Pylene PUBLIC stl2)
target_compile_definitions(Pylene PUBLIC PYLENE_CONCEPT_TS_ENABLED) # target_compile_definitions(Pylene PUBLIC PYLENE_CONCEPT_TS_ENABLED)
endif () # endif ()
# IDE configuration # IDE configuration
......
...@@ -5,28 +5,36 @@ ...@@ -5,28 +5,36 @@
namespace mln::py namespace mln::py
{ {
template <class T> namespace impl
image2d<uint8_t> invert(const image2d<T>& src)
{ {
template <class T>
image2d<uint8_t> invert(const image2d<T>& src)
{
auto res = image2d<uint8_t>(src.width(), src.height()); auto res = image2d<uint8_t>(src.width(), src.height());
auto span = src.values(); auto span = src.values();
const auto& vs = src.get_value_set(); const auto& vs = src.get_value_set();
std::transform(span.begin(), span.end(), res.values().begin(), std::transform(span.begin(), span.end(), res.values().begin(),
[&vs](auto val) -> uint8_t { [&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; return res;
}
} }
namespace detail image2d<uint8_t> invert(const image2d<>& src)
{ {
template <typename T> switch (src.type().tid())
struct apply_invert_t
{ {
auto operator()(const image2d<>& src) { return invert(*src.cast_to<T>()); } case (Info::INT8_V):
}; return impl::invert(*src.cast_to<int8_t>());
} // namespace detail case (Info::INT32_V):
image2d<uint8_t> invert(const image2d<>& src) { return visit<detail::apply_invert_t>(src.type().tid(), src); }; return impl::invert(*src.cast_to<int32_t>());
default:
return impl::invert(src);
}
};
image2d<> invert_py(const image2d<>& src) { return invert(src); }; image2d<> invert_py(const image2d<>& src) { return invert(src); };
} // namespace mln::py } // namespace mln::py
\ No newline at end of file
...@@ -20,7 +20,7 @@ namespace mln::py ...@@ -20,7 +20,7 @@ namespace mln::py
std::transform(span.begin(), span.end(), res.values().begin(), std::transform(span.begin(), span.end(), res.values().begin(),
[&vs](auto val) -> float { [&vs](auto val) -> float {
auto tmp = vs.max(); 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; return res;
} }
......
...@@ -17,10 +17,11 @@ namespace mln::py ...@@ -17,10 +17,11 @@ namespace mln::py
{ {
public: public:
any_ref() = default; 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); 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) any_ref(T& elm)
{ {
new (&m_held) placeholder<T>(elm); new (&m_held) placeholder<T>(elm);
......
...@@ -44,42 +44,7 @@ namespace mln::py ...@@ -44,42 +44,7 @@ namespace mln::py
{ {
m_buffer = static_cast<std::byte*>(buf); m_buffer = static_cast<std::byte*>(buf);
m_size = sz; m_size = sz;
switch (typeid(T)) m_type = Trait<T>::value;
{
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;
}
} }
private: private:
......
...@@ -13,6 +13,7 @@ namespace mln::py ...@@ -13,6 +13,7 @@ namespace mln::py
struct image2d_data_base struct image2d_data_base
{ {
image2d_data_base() = default;
image2d_data_base(std::size_t n); image2d_data_base(std::size_t n);
virtual ~image2d_data_base() = default; virtual ~image2d_data_base() = default;
......
...@@ -22,9 +22,15 @@ namespace mln::py ...@@ -22,9 +22,15 @@ namespace mln::py
std::any cast(any_ref v, const Info& dest_type) const { return _cast(v, dest_type); } 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: private:
virtual std::any _max() const = 0; 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> template <typename T>
...@@ -37,6 +43,11 @@ namespace mln::py ...@@ -37,6 +43,11 @@ namespace mln::py
return static_cast<U>(v.as<T>()); 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(); } T max() const { return std::numeric_limits<T>::max(); }
...@@ -45,7 +56,6 @@ namespace mln::py ...@@ -45,7 +56,6 @@ namespace mln::py
return val1.as<T>() / denominator.as<T>(); return val1.as<T>() / denominator.as<T>();
} }
T divide(T val, T denominator) const T divide(T val, T denominator) const
{ {
return val / denominator; return val / denominator;
...@@ -54,44 +64,26 @@ namespace mln::py ...@@ -54,44 +64,26 @@ namespace mln::py
private: private:
std::any _max() const override final { return max(); } std::any _max() const override final { return max(); }
template <typename VT>
struct cast_dispatcher_t struct cast_dispatcher_t
{ {
explicit cast_dispatcher_t(const value_set<VT>* vs) explicit cast_dispatcher_t(const value_set* vs)
: m_vs(vs) : m_vs(vs)
{ {
} }
template <typename U> // U return type
template <typename Dest_type>
std::any call(any_ref v) std::any call(any_ref v)
{ {
return m_vs->template cast<U>(v); return m_vs->template cast<Dest_type>(v);
} }
private: private:
const value_set<T>* m_vs; 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 }; // 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 } // namespace mln::py
...@@ -14,13 +14,12 @@ namespace mln::py ...@@ -14,13 +14,12 @@ namespace mln::py
} }
}; };
any_ref::any_ref(std::byte* ptr, Info i) any_ref::any_ref(std::byte* ptr, Info i)
{ {
visit_f(i.tid(), cast_dispatcher_t(), reinterpret_cast<void*>(&m_held), ptr); 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; } 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(); } 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