GEGELATI

Loading...
Searching...
No Matches
addPrimitiveType.h
1
39#ifndef INSTRUCTION_ADD_H
40#define INSTRUCTION_ADD_H
41
42#include <memory>
43#include <type_traits>
44#include <typeinfo>
45
46#include "data/untypedSharedPtr.h"
47#include "instructions/instruction.h"
48
49 namespace Instructions {
50
55 template <class T> class AddPrimitiveType : public Instruction
56 {
57 static_assert(std::is_fundamental<T>::value,
58 "Template class AddPrimitiveType<T> can only be used for "
59 "primitive types.");
60
61#ifdef CODE_GENERATION
62 public:
70 AddPrimitiveType(const std::string& printTemplate = "");
71#endif // CODE_GENERATION
72
73 public:
74#ifndef CODE_GENERATION
78 AddPrimitiveType();
79#endif // CODE_GENERATION
81 virtual double execute(
82 const std::vector<Data::UntypedSharedPtr>& args) const override;
83
84 private:
89 void setUpOperand();
90 };
91#ifndef CODE_GENERATION
92 template <class T> AddPrimitiveType<T>::AddPrimitiveType()
93 {
94 setUpOperand();
95 }
96#endif // CODE_GENERATION
97 template <class T>
98 double AddPrimitiveType<T>::execute(
99 const std::vector<Data::UntypedSharedPtr>& args) const
100 {
101
102#ifndef NDEBUG
103 if (Instruction::execute(args) != 1.0) {
104 return 0.0;
105 }
106#endif
107
108 return *(args.at(0).getSharedPointer<const T>()) +
109 (double)*(args.at(1).getSharedPointer<const T>());
110 }
111
112#ifdef CODE_GENERATION
113 template <class T>
114 AddPrimitiveType<T>::AddPrimitiveType(const std::string& printTemplate)
115 : Instruction(printTemplate)
116 {
117 setUpOperand();
118 }
119#endif // CODE_GENERATION
120
121 template <class T> void AddPrimitiveType<T>::setUpOperand()
122 {
123 this->operandTypes.push_back(typeid(T));
124 this->operandTypes.push_back(typeid(T));
125 }
126} // namespace Instructions
127
128#endif
Instructions::AddPrimitiveType
Template class for add instruction on all types of data: double, int, ...
Definition addPrimitiveType.h:56
Instructions::AddPrimitiveType::execute
virtual double execute(const std::vector< Data::UntypedSharedPtr > &args) const override
Inherited from Instruction.
Definition addPrimitiveType.h:98
Instructions::AddPrimitiveType::AddPrimitiveType
AddPrimitiveType(const std::string &printTemplate="")
Constructor for the AddPrimitiveType class, so it can be use during the code gen.
Definition addPrimitiveType.h:114
Instructions::Instruction
This abstract class is the base class for any instruction to be used in a Program.
Definition instruction.h:61
Instructions::Instruction::execute
virtual double execute(const std::vector< Data::UntypedSharedPtr > &args) const =0
Execute the Instruction for the given arguments.
Definition instruction.cpp:82
Instructions::Instruction::printTemplate
std::string printTemplate
Definition instruction.h:112
Instructions
Definition addPrimitiveType.h:49