Skip to content

Commit

Permalink
Change casting to value_t as explicit and update tests and concepts
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesYang007 committed Apr 21, 2020
1 parent d1e3dc7 commit 328c378
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion include/autoppl/expression/variable/constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct Constant
Constant(value_t c)
: c_{c}
{}
operator value_t() const { return get_value(); }
explicit operator value_t() const { return get_value(); }
value_t get_value() const { return c_; }

private:
Expand Down
2 changes: 1 addition & 1 deletion include/autoppl/expression/variable/variable_viewer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct VariableViewer
: var_ref_{var}
{}

operator value_t() const { return get_value(); }
explicit operator value_t() const { return get_value(); }

value_t get_value() const
{ return static_cast<value_t>(var_ref_.get()); }
Expand Down
15 changes: 15 additions & 0 deletions include/autoppl/util/type_traits.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once
#include <type_traits>

namespace ppl {

/*
* Checks if type From can be explicitly converted to type To.
*/
template <class From, class To>
inline constexpr bool is_explicitly_convertible_v =
std::is_constructible_v<To, From> &&
!std::is_convertible_v<From, To>
;

} // namespace ppl
5 changes: 3 additions & 2 deletions include/autoppl/util/var_expr_traits.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include <autoppl/util/type_traits.hpp>
#include <autoppl/util/concept.hpp>
#include <autoppl/util/var_traits.hpp>

Expand Down Expand Up @@ -27,14 +28,14 @@ struct var_expr_traits<double>
* A variable expression is any class that the following:
* - is_var_v<T> must be false
* - var_expr_traits must be well-defined for T
* - T must be convertible to its value_t
* - T must be explicitly convertible to its value_t
*/
template <class T>
inline constexpr bool is_var_expr_v =
!is_var_v<T> &&
has_type_value_t_v<T> &&
has_func_get_value_v<const T> &&
std::is_convertible_v<const T, get_type_value_t_t<T>>
is_explicitly_convertible_v<const T, get_type_value_t_t<T>>
;

} // namespace util
Expand Down
6 changes: 3 additions & 3 deletions include/autoppl/util/var_traits.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include <type_traits>
#include <autoppl/util/type_traits.hpp>
#include <autoppl/util/concept.hpp>

namespace ppl {
Expand All @@ -22,7 +22,7 @@ struct var_traits
/*
* C++17 version of concepts to check var properties.
* - var_traits must be well-defined under type T
* - T must be convertible to its value_t
* - T must be explicitly convertible to its value_t
* - not possible to get overloads
*/
template <class T>
Expand All @@ -36,7 +36,7 @@ inline constexpr bool is_var_v =
has_func_set_storage_v<T> &&
has_func_set_state_v<T> &&
has_func_get_state_v<const T> &&
std::is_convertible_v<const T, get_type_value_t_t<T>>
is_explicitly_convertible_v<const T, get_type_value_t_t<T>>
;

} // namespace util
Expand Down
2 changes: 1 addition & 1 deletion include/autoppl/variable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct Variable
void set_state(state_t state) { state_ = state; }
state_t get_state() const { return state_; }

operator value_t () const { return get_value(); }
explicit operator value_t () const { return get_value(); }

/*
* Sets underlying value to "value".
Expand Down
7 changes: 4 additions & 3 deletions test/expression/model/model_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ TEST_F(var_dist_fixture, log_pdf_valid)
struct many_var_dist_fixture : ::testing::Test
{
protected:
MockVar x, y, z, w;
double xv, yv, zv, wv;
using value_t = double;
using eq_t = EqNode<MockVar, MockDistExpr>;
MockVar x, y, z, w;
value_t xv, yv, zv, wv;

using model_two_t = GlueNode<eq_t, eq_t>;
model_two_t model_two = {
Expand Down Expand Up @@ -153,7 +154,7 @@ TEST_F(many_var_dist_fixture, four_vars_traverse_pdf)
model_four.traverse([&](auto& model) {
auto& var = model.get_variable();
auto& dist = model.get_distribution();
actual *= dist.pdf(var);
actual *= dist.pdf(static_cast<value_t>(var));
});
EXPECT_EQ(actual, model_four.pdf());
}
Expand Down
4 changes: 2 additions & 2 deletions test/testutil/mock_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct MockVar
using state_t = MockState;

value_t get_value() const {return x_;}
operator value_t() const { return get_value(); }
explicit operator value_t() const { return get_value(); }

void set_value(value_t x) {x_ = x;}

Expand Down Expand Up @@ -57,7 +57,7 @@ struct MockVarExpr
{
using value_t = double;
value_t get_value() const { return x_; }
operator value_t() const { return get_value(); }
explicit operator value_t() const { return get_value(); }

/* not part of API */
MockVarExpr(value_t x)
Expand Down

0 comments on commit 328c378

Please sign in to comment.