GEGELATI
multByConstant.h
1
38#ifndef INST_MULT_BY_CONST_H
39#define INST_MULT_BY_CONST_H
40
41#include <memory>
42#include <type_traits>
43#include <typeinfo>
44
45#include "data/constantHandler.h"
46#include "data/untypedSharedPtr.h"
47#include "instructions/instruction.h"
48
49namespace Instructions {
50
55 template <class T> class MultByConstant : public Instruction
56 {
57 static_assert(std::is_fundamental<T>::value,
58 "Template class MultByConstParam<> can only be used for "
59 "primitive types.");
60#ifdef CODE_GENERATION
61 public:
68 MultByConstant(const std::string& printTemplate = "$0 = $1 * $2;");
69#endif // CODE_GENERATION
70
71 public:
72#ifndef CODE_GENERATION
77#endif // CODE_GENERATION
78
80 double execute(
81 const std::vector<Data::UntypedSharedPtr>& args) const override;
82
83 private:
88 void setUpOperand();
89 };
90
91#ifdef CODE_GENERATION
92 template <class T>
93 MultByConstant<T>::MultByConstant(const std::string& printTemplate)
94 : Instruction(printTemplate)
95 {
96 setUpOperand();
97 }
98
99#endif // CODE_GENERATION
100
101#ifndef CODE_GENERATION
102 template <class T> MultByConstant<T>::MultByConstant() : Instruction()
103 {
104 setUpOperand();
105 }
106#endif // CODE_GENERATION
107 template <class T>
109 const std::vector<Data::UntypedSharedPtr>& args) const
110 {
111#ifndef NDEBUG
112 if (Instruction::execute(args) != 1.0)
113 return 0;
114#endif
115 const Data::Constant constantValue = (const Data::Constant&)*(
116 args.at(1).getSharedPointer<const Data::Constant>());
117 return *(args.at(0).getSharedPointer<const T>()) *
118 (double)constantValue;
119 }
120
121 template <class T> void MultByConstant<T>::setUpOperand()
122 {
123 this->operandTypes.push_back(typeid(T));
124 this->operandTypes.push_back(typeid(Data::Constant));
125 }
126} // namespace Instructions
127#endif // INST_MULT_BY_CONST_H
This abstract class is the base class for any instruction to be used in a Program.
Definition: instruction.h:59
virtual double execute(const std::vector< Data::UntypedSharedPtr > &args) const =0
Execute the Instruction for the given arguments.
Definition: instruction.cpp:81
std::string printTemplate
Definition: instruction.h:110
Template class for multiplying a unique argument of type T by a constant parameter.
Definition: multByConstant.h:56
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:93
double execute(const std::vector< Data::UntypedSharedPtr > &args) const override
Inherited from Instruction.
Definition: multByConstant.h:108
Definition: addPrimitiveType.h:48
Data type used in Program::Program to define constant values, accessible to Instructions,...
Definition: constant.h:48