Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in dep resolution when requesting a "const T*" #347

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion include/boost/sml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ struct remove_reference<T &&> {
};
template <class T>
using remove_reference_t = typename remove_reference<T>::type;

template <class T>
kris-jusiak marked this conversation as resolved.
Show resolved Hide resolved
struct remove_pointer {
using type = T;
};
template <class T>
struct remove_pointer<T *> {
using type = T;
};
template <class T>
using remove_pointer_t = typename remove_pointer<T>::type;
} // namespace aux
namespace aux {
using swallow = int[];
Expand Down Expand Up @@ -401,6 +412,14 @@ template <class T>
T &try_get(const pool_type<T &> *object) {
return object->value;
}
template <class T>
const T *try_get(const pool_type<const T *> *object) {
return object->value;
}
template <class T>
T *try_get(const pool_type<T *> *object) {
return object->value;
}
template <class T, class TPool>
T &get(TPool &p) {
return static_cast<pool_type<T> &>(p).value;
Expand All @@ -409,13 +428,22 @@ template <class T, class TPool>
const T &cget(const TPool &p) {
return static_cast<const pool_type<T> &>(p).value;
}
template <class T, class TPool>
T *get(TPool *p) {
return static_cast<pool_type<T> &>(p).value;
}
template <class T, class TPool>
const T *cget(const TPool *p) {
return static_cast<const pool_type<T> &>(p).value;
}
template <class... Ts>
struct pool : pool_type<Ts>... {
using boost_di_inject__ = type_list<Ts...>;
pool() = default;
explicit pool(Ts... ts) : pool_type<Ts>(ts)... {}
template <class... TArgs>
pool(init, const pool<TArgs...> &p) : pool_type<Ts>(try_get<aux::remove_const_t<aux::remove_reference_t<Ts>>>(&p))... {}
pool(init, const pool<TArgs...> &p)
: pool_type<Ts>(try_get<aux::remove_const_t<aux::remove_reference_t<aux::remove_pointer_t<Ts>>>>(&p))... {}
template <class... TArgs>
pool(const pool<TArgs...> &p) : pool_type<Ts>(init{}, p)... {}
};
Expand Down Expand Up @@ -580,6 +608,7 @@ struct string<T> {
static auto c_str_impl(...) { return get_type_name<T>(); }
};
} // namespace aux

template <class T>
constexpr auto wrap(T callback) {
return aux::zero_wrapper<T, T>{callback};
Expand Down
10 changes: 10 additions & 0 deletions include/boost/sml/aux_/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ struct remove_reference<T &&> {
template <class T>
using remove_reference_t = typename remove_reference<T>::type;

template <class T>
struct remove_pointer {
using type = T;
};
template <class T>
struct remove_pointer<T *> {
using type = T;
};
template <class T>
using remove_pointer_t = typename remove_pointer<T>::type;
} // namespace aux

#endif
20 changes: 19 additions & 1 deletion include/boost/sml/aux_/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,15 @@ T &try_get(const pool_type<T &> *object) {
return object->value;
}

template <class T>
const T *try_get(const pool_type<const T *> *object) {
return object->value;
}
template <class T>
T *try_get(const pool_type<T *> *object) {
return object->value;
}

template <class T, class TPool>
T &get(TPool &p) {
return static_cast<pool_type<T> &>(p).value;
Expand All @@ -219,6 +228,15 @@ const T &cget(const TPool &p) {
return static_cast<const pool_type<T> &>(p).value;
}

template <class T, class TPool>
T *get(TPool *p) {
return static_cast<pool_type<T> &>(p).value;
}
template <class T, class TPool>
const T *cget(const TPool *p) {
return static_cast<const pool_type<T> &>(p).value;
}

template <class... Ts>
struct pool : pool_type<Ts>... {
using boost_di_inject__ = type_list<Ts...>;
Expand All @@ -228,7 +246,7 @@ struct pool : pool_type<Ts>... {
explicit pool(Ts... ts) : pool_type<Ts>(ts)... {}

template <class... TArgs>
pool(init, const pool<TArgs...> &p) : pool_type<Ts>(try_get<aux::remove_const_t<aux::remove_reference_t<Ts>>>(&p))... {}
pool(init, const pool<TArgs...> &p) : pool_type<Ts>(try_get<aux::remove_const_t<aux::remove_reference_t<aux::remove_pointer_t<Ts>>>>(&p))... {}

template <class... TArgs>
pool(const pool<TArgs...> &p) : pool_type<Ts>(init{}, p)... {}
Expand Down
84 changes: 84 additions & 0 deletions test/ft/dependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,90 @@ test dependencies_smart_ptrs = [] {
expect(sm.is(sml::X));
};

test dependencies_with_reference = [] {
struct Data {
int m_member { 42 };
};

struct c {
auto operator()() noexcept {
const auto action = [](Data& data) { expect(data.m_member == 42); };

using namespace sml;
return make_transition_table(*idle + event<e1> / action = X);
}
};

Data data;
sml::sm<c> sm{data};
sm.process_event(e1{});
expect(sm.is(sml::X));
};

test dependencies_with_const_reference = [] {
struct Data {
int m_member { 42 };
};

struct c {
auto operator()() noexcept {
const auto action = [](const Data& data) {
expect(data.m_member == 42);
};

using namespace sml;
return make_transition_table(*idle + event<e1> / action = X);
}
};

Data data;
sml::sm<c> sm{data};
sm.process_event(e1{});
expect(sm.is(sml::X));
};

test dependencies_with_pointer = [] {
struct Data {
int m_member { 42 };
};

struct c {
auto operator()() noexcept {
const auto action = [](Data* data) { expect(data->m_member == 42); };

using namespace sml;
return make_transition_table(*idle + event<e1> / action = X);
}
};

Data data;
sml::sm<c> sm{&data};
sm.process_event(e1{});
expect(sm.is(sml::X));
};

test dependencies_with_const_pointer = [] {
struct Data {
int m_member { 42 };
};

struct c {
auto operator()() noexcept {
const auto action = [](const Data* data) {
expect(data->m_member == 42);
};

using namespace sml;
return make_transition_table(*idle + event<e1> / action = X);
}
};

Data data;
sml::sm<c> sm{&data};
sm.process_event(e1{});
expect(sm.is(sml::X));
};

#if (_MSC_VER >= 1910) // MSVC 2017
test dependencies_multiple_subs = [] {
struct update {
Expand Down