-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
begin drafting binary operation overloading
- Loading branch information
1 parent
7341f48
commit a4ebd7f
Showing
1 changed file
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#pragma once | ||
|
||
#include <type_traits> | ||
#include <autoppl/util/traits.hpp> | ||
#include <autoppl/expression/var_expr.hpp> | ||
|
||
namespace ppl { | ||
|
||
template <class LHSValueType, class RHSValueType, class BinaryOp> | ||
struct BinaryOpNode | ||
{ | ||
using binop_result_t = std::common_type_t< | ||
typename node_traits<left_node_t>::binop_result_t, | ||
typename node_traits<right_node_t>::binop_result_t | ||
>; | ||
|
||
BinaryOpNode(Variable<LHSValueType> lhs, Variable<RHSValueType> rhs) | ||
: lhs_{lhs}, rhs_{rhs} | ||
{} | ||
|
||
binop_result_t get_value() | ||
{ | ||
LHSValueType lhs_value = lhs_.get_value(); | ||
RHSValueType rhs_value = rhs_.get_value(); | ||
return BinaryOp::evaluate(lhs_value, rhs_value); | ||
} | ||
|
||
private: | ||
Variable<LHSValueType> lhs_; | ||
Variable<RHSValueType> rhs_; | ||
|
||
} | ||
|
||
template <class LHSValueType, class RHSValueType> | ||
BinaryOpNode BinaryOp::operator+( | ||
const Variable<LHSValueType>& lhs, | ||
const Variable<RHSValueType>& rhs) | ||
{ | ||
return BinaryOpNode<Variable<LHSValueType>, Variable<RHSValueType>, AddOp>(lhs, rhs); | ||
} | ||
|
||
template <class LHSValueType, class RHSValueType> | ||
BinaryOpNode BinaryOp::operator*( | ||
const Variable<LHSValueType>& lhs, | ||
const Variable<RHSValueType>& rhs) | ||
{ | ||
return BinaryOpNode<Variable<LHSValueType>, Variable<RHSValueType>, MultOp>(lhs, rhs); | ||
} | ||
|
||
template <class LHSValueType, class RHSValueType> | ||
struct BinaryOp { | ||
|
||
using left_node_t = LHSValueType; | ||
using right_node_t = RHSValueType; | ||
using binop_result_t = std::common_type_t< | ||
typename node_traits<left_node_t>::binop_result_t, | ||
typename node_traits<right_node_t>::binop_result_t | ||
>; | ||
|
||
ReturnType BinaryOp::evaluate(); | ||
|
||
} | ||
|
||
template <class LHSValueType, class RHSValueType> | ||
struct AddOp : BinaryOp { | ||
|
||
using binop_result_t = std::common_type_t< | ||
typename node_traits<left_node_t>::binop_result_t, | ||
typename node_traits<right_node_t>::binop_result_t | ||
>; | ||
|
||
static binop_result_t evaluate(LHSValueType x, RHSValueType y) | ||
{ | ||
return x + y; | ||
} | ||
|
||
} | ||
|
||
template <class LHSValueType, class RHSValueType> | ||
struct MultOp : BinaryOp { | ||
|
||
using binop_result_t = std::common_type_t< | ||
typename node_traits<left_node_t>::binop_result_t, | ||
typename node_traits<right_node_t>::binop_result_t | ||
>; | ||
|
||
static binop_result_t evaluate(LHSValueType x, RHSValueType y) | ||
{ | ||
return x * y; | ||
} | ||
|
||
} | ||
|
||
|
||
#ifdef AUTOPPL_USE_CONCEPTS | ||
#endif | ||
|
||
} // namespace ppl |