GEGELATI
Loading...
Searching...
No Matches
multByConstant.h
1
39#ifndef INST_MULT_BY_CONST_H
40#define INST_MULT_BY_CONST_H
41
42#include <memory>
43#include <type_traits>
44#include <typeinfo>
45
46#include "data/constantHandler.h"
47#include "data/untypedSharedPtr.h"
48#include "instructions/instruction.h"
49
50namespace Instructions {
51
58 {
59 public:
60 virtual ~IAbstractMultByConstant() = default;
61 };
62
67 template <class T>
69 {
70 static_assert(std::is_fundamental<T>::value,
71 "Template class MultByConstParam<> can only be used for "
72 "primitive types.");
73#ifdef CODE_GENERATION
74 public:
81 MultByConstant(const std::string& printTemplate = "$0 = $1 * $2;");
82#endif // CODE_GENERATION
83
84 public:
85#ifndef CODE_GENERATION
90#endif // CODE_GENERATION
91
93 double execute(
94 const std::vector<Data::UntypedSharedPtr>& args) const override;
95
96 private:
101 void setUpOperand();
102 };
103
104#ifdef CODE_GENERATION
105 template <class T>
106 MultByConstant<T>::MultByConstant(const std::string& printTemplate)
107 : Instruction(printTemplate)
108 {
109 setUpOperand();
110 }
111
112#endif // CODE_GENERATION
113
114#ifndef CODE_GENERATION
115 template <class T> MultByConstant<T>::MultByConstant() : Instruction()
116 {
117 setUpOperand();
118 }
119#endif // CODE_GENERATION
120 template <class T>
122 const std::vector<Data::UntypedSharedPtr>& args) const
123 {
124#ifndef NDEBUG
125 if (Instruction::execute(args) != 1.0)
126 return 0;
127#endif
128 const Data::Constant constantValue = (const Data::Constant&)*(
129 args.at(1).getSharedPointer<const Data::Constant>());
130 return *(args.at(0).getSharedPointer<const T>()) *
131 (double)constantValue;
132 }
133
134 template <class T> void MultByConstant<T>::setUpOperand()
135 {
136 this->operandTypes.push_back(typeid(T));
137 this->operandTypes.push_back(typeid(Data::Constant));
138 }
139} // namespace Instructions
140#endif // INST_MULT_BY_CONST_H
Templace class for making casting from Instruction to MultByConst easier. This is usefull because Mul...
Definition multByConstant.h:58
This abstract class is the base class for any instruction to be used in a Program.
Definition instruction.h:61
virtual double execute(const std::vector< Data::UntypedSharedPtr > &args) const =0
Execute the Instruction for the given arguments.
Definition instruction.cpp:82
std::string printTemplate
Definition instruction.h:112
Template class for multiplying a unique argument of type T by a constant parameter.
Definition multByConstant.h:69
MultByConstant(const std::string &printTemplate="$0 = $1 * $2;")
Constructor for the MultByConstant class so it can be use during the code gen.
Definition multByConstant.h:106
double execute(const std::vector< Data::UntypedSharedPtr > &args) const override
Inherited from Instruction.
Definition multByConstant.h:121
Definition addPrimitiveType.h:49
Data type used in Program::Program to define constant values, accessible to Instructions,...
Definition constant.h:49