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