Skip to content
Snippets Groups Projects

Refactor/recipies

Merged vincent lorrain requested to merge refactor/recipies into main
7 files
+ 229
104
Compare changes
  • Side-by-side
  • Inline
Files
7
@@ -22,7 +22,7 @@ namespace Aidge{
/////////////////////////////
/**
* @brief class used to register any lambda function without context,
* it encapsulates the source lambda in a lambda which takes as argument ConditionalData* which are any type.
* it encapsulates the source lambda in a lambda which takes as argument std::shared_ptr<ConditionalData> which are any type.
* @see ConditionalData
*/
class ConditionalRegisterFunction {
@@ -31,12 +31,12 @@ class ConditionalRegisterFunction {
//////////////////////////
/**
* @brief recast the ConditionalData* to the argument type of the lambda
* @brief recast the std::shared_ptr<ConditionalData> to the argument type of the lambda
* @tparam T type of the lambda argument
* @see ConditionalData
*/
template <typename T>
T safeCastInput(ConditionalData* data) {
T safeCastInput( std::shared_ptr<ConditionalData> data) {
//cnvertion and type cheking
if (data->isTypeEqualTo<T>()){
return data->getValue<T>();
@@ -48,14 +48,14 @@ class ConditionalRegisterFunction {
/**
* @brief recaste the output of the lambda to a ConditionalData*
* @brief recaste the output of the lambda to a std::shared_ptr<ConditionalData>
* @tparam T type of the lambda return
* @see ConditionalData
*/
template <typename T>
ConditionalData* safeCastOutput(T data) {
std::shared_ptr<ConditionalData> safeCastOutput(T data) {
ConditionalData* out = new ConditionalData;
std::shared_ptr<ConditionalData> out = std::make_shared<ConditionalData>();
out->setValue<T>(data);
return out;
@@ -111,11 +111,11 @@ class ConditionalRegisterFunction {
};
/////////////////////
//change the function to ConditionalData*(std::vector<ConditionalData*>)
//change the function to std::shared_ptr<ConditionalData>(std::vector< std::shared_ptr<ConditionalData>>)
/////////////////////
/**
* @brief Converts a function to a ConditionalData*(std::vector<ConditionalData*>).
* @brief Converts a function to a std::shared_ptr<ConditionalData>(std::vector< std::shared_ptr<ConditionalData>>).
* @tparam F The type of the function to convert.
* @tparam ParamsIdx The indices of the function parameters.
* @param f The function to convert.
@@ -124,25 +124,31 @@ class ConditionalRegisterFunction {
template <class F, std::size_t... ParamsIdx>
auto funcPointer(F f, std::index_sequence<ParamsIdx...>) {
//wrapp the lambda in a new one that as ConditionalData as inputs and output
return [this,f](std::vector<ConditionalData*> &args) {
if (args.size() != sizeof...(ParamsIdx)){
return [this,f](std::vector< std::shared_ptr<ConditionalData>> &args) {
if (args.size() < sizeof...(ParamsIdx)){
std::ostringstream errorMessage;
errorMessage << "bad Number of argument: get " << args.size() << " need " << sizeof...(ParamsIdx) << "\n";
throw std::runtime_error(errorMessage.str());
}
//assert(args.size() == sizeof...(ParamsIdx));//the size of the vector valide
//we used std::vector< std::shared_ptr<ConditionalData>> as a fifo
std::size_t offset = args.size()-sizeof...(ParamsIdx);
using FuncTraits = function_traits<decltype(f)>;
using outType = typename FuncTraits::return_type;
outType result = f(safeCastInput<typename FuncTraits::template argument<ParamsIdx>::type>(args[ParamsIdx])...);
outType result = f(safeCastInput<typename FuncTraits::template argument<ParamsIdx>::type>(args[offset+ParamsIdx])...);
//suppress what we used
for (size_t i = 0; i < sizeof...(ParamsIdx); ++i) {
args.pop_back();
}
//typename
return safeCastOutput<outType>(result);
};
}
/**
* @brief Converts a function pointer to a ConditionalData*(std::vector<ConditionalData*>).
* @brief Converts a function pointer to a std::shared_ptr<ConditionalData>(std::vector< std::shared_ptr<ConditionalData>>).
* @tparam R The return type of the function.
* @tparam Params The parameter types of the function.
* @param f The function pointer to convert.
@@ -154,7 +160,7 @@ class ConditionalRegisterFunction {
}
/**
* @brief Converts a std::function to a ConditionalData*(std::vector<ConditionalData*>).
* @brief Converts a std::function to a std::shared_ptr<ConditionalData>(std::vector< std::shared_ptr<ConditionalData>>).
* @tparam R The return type of the function.
* @tparam Params The parameter types of the function.
* @param f The function pointer to convert.
@@ -196,7 +202,7 @@ class ConditionalRegisterFunction {
* @param datas The vector of input data.
* @return A pointer to the output ConditionalData object.
*/
ConditionalData* run(const std::string key,std::vector<ConditionalData*> & datas);
std::shared_ptr<ConditionalData> run(const std::string key,std::vector< std::shared_ptr<ConditionalData>> & datas);
bool isLambdaRegister(const std::string &key) {
if(mWlambda.find(key) != mWlambda.end()){
@@ -207,7 +213,7 @@ class ConditionalRegisterFunction {
private:
/// @brief map of name and the converted function.
std::map<const std::string, std::function<ConditionalData*(std::vector<ConditionalData*> &)>> mWlambda;
std::map<const std::string, std::function< std::shared_ptr<ConditionalData>(std::vector< std::shared_ptr<ConditionalData>> &)>> mWlambda;
};
///////////////////
@@ -237,15 +243,15 @@ class ConditionalInterpreter
ConditionalRegisterFunction mLambdaRegister;
std::vector<ConditionalData*> mResolution ;
std::vector< std::shared_ptr<ConditionalData>> mResolution ;
void clearRes(){
// void clearRes(){
for (std::size_t i = 0; i < mResolution.size(); ++i) {
delete mResolution[i];
}
mResolution.clear();
}
// for (std::size_t i = 0; i < mResolution.size(); ++i) {
// delete mResolution[i];
// }
// mResolution.clear();
// }
public:
@@ -258,7 +264,7 @@ class ConditionalInterpreter
ConditionalInterpreter(const std::string key,const std::string ConditionalExpressions);
~ConditionalInterpreter(){clearRes();}
~ConditionalInterpreter(){}
/**
* @brief get the condition key
@@ -293,12 +299,12 @@ class ConditionalInterpreter
* @param NodeOp The node currently being tested
* @param nodes The AST given by the parsing process
*/
std::vector<ConditionalData*> visit(const ASTNodeCh& nodes, const NodePtr NodeOp );
std::vector< std::shared_ptr<ConditionalData>> visit(const ASTNodeCh& nodes, const NodePtr NodeOp );
/**
* @defgroup ASTnodeInterpreterF Functions for interpreting AST nodes
* @brief For each node type in the AST, function defines the processing to be performed
* they return a std::vector<ConditionalData*> which corresponds to the value(s) obtained
* they return a std::vector< std::shared_ptr<ConditionalData>> which corresponds to the value(s) obtained
*/
/**
@@ -308,38 +314,38 @@ class ConditionalInterpreter
void fLambda(const std::shared_ptr<AstNode<ConditionalTokenTypes>>& node);
/**
* @ingroup ASTnodeInterpreterF
* @brief Converted the lexeme to a int and to ConditionalData*
* @brief Converted the lexeme to a int and to std::shared_ptr<ConditionalData>
*/
void fStrToInteger(const std::shared_ptr<AstNode<ConditionalTokenTypes>>& node);
/**
* @ingroup ASTnodeInterpreterF
* @brief Converted the lexeme to a float and to ConditionalData*
* @brief Converted the lexeme to a float and to std::shared_ptr<ConditionalData>
*/
void fStrToFloat(const std::shared_ptr<AstNode<ConditionalTokenTypes>>& node);
/**
* @ingroup ASTnodeInterpreterF
* @brief Converted the lexeme to a str and to ConditionalData*
* @brief Converted the lexeme to a str and to std::shared_ptr<ConditionalData>
*/
void fStrToStr(const std::shared_ptr<AstNode<ConditionalTokenTypes>>& node);
/**
* @ingroup ASTnodeInterpreterF
* @brief makes the == operation between two previously converted ConditionalData*
* @brief makes the == operation between two previously converted std::shared_ptr<ConditionalData>
*/
void fEq(void);
/**
* @ingroup ASTnodeInterpreterF
* @brief makes the != operation between two previously converted ConditionalData*
* @brief makes the != operation between two previously converted std::shared_ptr<ConditionalData>
*/
void fNeq(void);
/**
* @ingroup ASTnodeInterpreterF
* @brief makes the && operation between two previously converted ConditionalData* in bool
* @brief makes the && operation between two previously converted std::shared_ptr<ConditionalData> in bool
*/
void fAnd(void);
/**
* @ingroup ASTnodeInterpreterF
* @brief makes the || operation between two previously converted ConditionalData* in bool
* @brief makes the || operation between two previously converted std::shared_ptr<ConditionalData> in bool
*/
void fOr(void);
Loading