ICG 2
Electric Boogaloo
SpecifiedPrimitiveDataType.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 #include "Value/IntegerValue.hpp"
6 
7 #include <stdlib.h>
8 #include <iostream>
9 
12 template <class T>
14 
15 public:
16 
18 
19  /* ==================================================================== */
20  /* RULE OF THREE INTERFACE */
21  /* ==================================================================== */
22 
23  SpecifiedPrimitiveDataType<T> ( const SpecifiedPrimitiveDataType<T> & original ) = default;
24  ~SpecifiedPrimitiveDataType<T> () = default;
26 
27  /* ==================================================================== */
28  /* VIRTUAL INTERFACE */
29  /* ==================================================================== */
30 
31  bool validate(DataTypeInator * dataTypeInator = NULL) override { return true; }
32 
33  bool isValid() const override { return true; }
34 
38  size_t getSize() const override {
39  return sizeof(T);
40  }
41 
45  void* createInstance(unsigned int num) const override {
46  T* temp = (T*)calloc(num, sizeof(T));
47  return ((void *)temp);
48  }
49 
52  void deleteInstance(void* address) const override {
53  delete (T*)address;
54  }
55 
58  void clearValue(void * address) const {
59  *(T*)address = 0;
60  }
61 
67  bool assignValue(void * address, Value * value) const {
68 
69  if (value->getValueType() == Value::ValueType::INTEGER || value->getValueType() == Value::ValueType::FLOATING_POINT) {
70  NumericValue * numeric_value_p = static_cast<NumericValue*>(value);
71  if ( isFloatingPoint() ) {
72  *(T*)address = numeric_value_p->getFloatingPointValue();
73  } else {
74  *(T*)address = numeric_value_p->getIntegerValue();
75  }
76  } else {
77  std::cerr << "ERROR: Attempt to assign non-numeric value to a numeric type." << std::endl;
78  return false;
79  }
80 
81  return true;
82  }
83 
84  Value * getValue(void *address) const {
85  if (isFloatingPoint()) {
86  return new FloatingPointValue(*(T*)address);
87  } else {
88  return new IntegerValue(*(T*)address);
89  }
90  }
91 
97  void printValue(std::ostream &s, void *address ) const {
98  s << *(T*)address ;
99  }
100 
104  std::string getTypeSpecName() const {
105  std::string s("<invalid-primitive-type>");
106  return s;
107  }
108 
109  /* ==================================================================== */
110  /* CLASS SPECIFIC INTERFACE */
111  /* ==================================================================== */
112 
116  bool isFloatingPoint() const { return false; }
117 
121  bool isSigned() const { return false; }
122 
126  bool isVoid() const { return false; }
127 
128 };
129 
130 /*
131  Template specializations for SpecifiedPrimitiveDataType<T>::isFloatingPoint()
132  */
135 
136 /*
137  Template specializations for SpecifiedPrimitiveDataType<T>::isSigned()
138 */
144 
145 /*
146  Template specializations for SpecifiedPrimitiveDataType<T>::isVoid()
147 */
149 
150 /*
151  Template specializations for SpecifiedPrimitiveDataType<char>::printValue()
152 */
153 template <> void SpecifiedPrimitiveDataType<char>::printValue(std::ostream &s, void *address ) const;
154 template <> void SpecifiedPrimitiveDataType<unsigned char>::printValue(std::ostream &s, void *address ) const;
155 
156 /*
157  Template specializations for SpecifiedPrimitiveDataType<void>
158 */
159 template <> size_t SpecifiedPrimitiveDataType<void>::getSize() const;
160 template <> void* SpecifiedPrimitiveDataType<void>::createInstance(unsigned int num) const;
161 template <> void SpecifiedPrimitiveDataType<void>::deleteInstance(void* address) const;
162 template <> void SpecifiedPrimitiveDataType<void>::clearValue(void * address) const;
163 template <> bool SpecifiedPrimitiveDataType<void>::assignValue(void * address, Value * value) const;
164 template <> void SpecifiedPrimitiveDataType<void>::printValue(std::ostream &s, void *address ) const;
165 template <> Value * SpecifiedPrimitiveDataType<void>::getValue(void *address) const;
166 
167 
168 
184 
SpecifiedPrimitiveDataType< int > IntDataType
Definition: SpecifiedPrimitiveDataType.hpp:170
SpecifiedPrimitiveDataType< long > LongDataType
Definition: SpecifiedPrimitiveDataType.hpp:175
SpecifiedPrimitiveDataType< unsigned short > UnsignedShortDataType
Definition: SpecifiedPrimitiveDataType.hpp:178
SpecifiedPrimitiveDataType< float > FloatDataType
Definition: SpecifiedPrimitiveDataType.hpp:182
SpecifiedPrimitiveDataType< double > DoubleDataType
Definition: SpecifiedPrimitiveDataType.hpp:183
SpecifiedPrimitiveDataType< void > VoidDataType
Definition: SpecifiedPrimitiveDataType.hpp:169
SpecifiedPrimitiveDataType< char > CharDataType
Definition: SpecifiedPrimitiveDataType.hpp:173
SpecifiedPrimitiveDataType< long long > LongLongDataType
Definition: SpecifiedPrimitiveDataType.hpp:176
SpecifiedPrimitiveDataType< unsigned int > UnsignedIntDataType
Definition: SpecifiedPrimitiveDataType.hpp:171
SpecifiedPrimitiveDataType< unsigned long long > UnsignedLongLongDataType
Definition: SpecifiedPrimitiveDataType.hpp:181
SpecifiedPrimitiveDataType< unsigned char > UnsignedCharDataType
Definition: SpecifiedPrimitiveDataType.hpp:177
SpecifiedPrimitiveDataType< short > ShortDataType
Definition: SpecifiedPrimitiveDataType.hpp:174
SpecifiedPrimitiveDataType< unsigned long > UnsignedLongDataType
Definition: SpecifiedPrimitiveDataType.hpp:180
Register and manage datatypes at runtime.
Definition: DataTypeInator.hpp:16
Definition: FloatingPointValue.hpp:8
IntegerValue is a Value that represents an integer value on the right-hand-side of an equation.
Definition: IntegerValue.hpp:9
Definition: NumericValue.hpp:8
virtual double getFloatingPointValue() const =0
virtual long long getIntegerValue() const =0
Definition: PrimitiveDataType.hpp:12
Definition: SpecifiedPrimitiveDataType.hpp:13
bool isSigned() const
Definition: SpecifiedPrimitiveDataType.hpp:121
bool assignValue(void *address, Value *value) const
Definition: SpecifiedPrimitiveDataType.hpp:67
void * createInstance(unsigned int num) const override
Definition: SpecifiedPrimitiveDataType.hpp:45
std::string getTypeSpecName() const
Definition: SpecifiedPrimitiveDataType.hpp:104
bool validate(DataTypeInator *dataTypeInator=NULL) override
Verify that all of the types that are named by this DataType or subordinate DataTypes are resolvable ...
Definition: SpecifiedPrimitiveDataType.hpp:31
bool isVoid() const
Definition: SpecifiedPrimitiveDataType.hpp:126
size_t getSize() const override
Definition: SpecifiedPrimitiveDataType.hpp:38
SpecifiedPrimitiveDataType< T > & operator=(SpecifiedPrimitiveDataType< T > &rhs)=default
Value * getValue(void *address) const
Creates a Value object for the variable at the given address.
Definition: SpecifiedPrimitiveDataType.hpp:84
void deleteInstance(void *address) const override
Delete this instance.
Definition: SpecifiedPrimitiveDataType.hpp:52
bool isValid() const override
Determine whether this type has already been successfully validated.
Definition: SpecifiedPrimitiveDataType.hpp:33
void printValue(std::ostream &s, void *address) const
Definition: SpecifiedPrimitiveDataType.hpp:97
bool isFloatingPoint() const
Definition: SpecifiedPrimitiveDataType.hpp:116
void clearValue(void *address) const
Clear the variable at the given address.
Definition: SpecifiedPrimitiveDataType.hpp:58
Value is an abstract base-class that represents a value on the right-hand-side of an assignment.
Definition: Value.hpp:9
virtual ValueType getValueType() const =0