Skip to content

Commit 6676d78

Browse files
authored
Merge pull request #595 from scratchcpp/simplify_value
Simplify Value class
2 parents a33681e + 3ac5779 commit 6676d78

File tree

14 files changed

+220
-820
lines changed

14 files changed

+220
-820
lines changed

include/scratchcpp/value.h

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,18 @@ class LIBSCRATCHCPP_EXPORT Value
2222
{
2323
public:
2424
/*! Constructs a number Value. */
25-
Value(float numberValue)
26-
{
27-
value_init(&m_data);
28-
value_assign_float(&m_data, numberValue);
29-
}
30-
31-
/*! Constructs a number Value. */
32-
Value(double numberValue)
25+
Value(double numberValue = 0.0)
3326
{
3427
value_init(&m_data);
3528
value_assign_double(&m_data, numberValue);
3629
}
3730

3831
/*! Constructs a number Value. */
39-
Value(int numberValue = 0)
40-
{
41-
value_init(&m_data);
42-
value_assign_int(&m_data, numberValue);
43-
}
44-
45-
/*! Constructs a number Value. */
46-
Value(size_t numberValue)
47-
{
48-
value_init(&m_data);
49-
value_assign_size_t(&m_data, numberValue);
50-
}
51-
52-
/*! Constructs a number Value. */
53-
Value(long numberValue)
32+
template<typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
33+
Value(T numberValue)
5434
{
5535
value_init(&m_data);
56-
value_assign_long(&m_data, numberValue);
36+
value_assign_double(&m_data, numberValue);
5737
}
5838

5939
/*! Constructs a boolean Value. */
@@ -77,13 +57,6 @@ class LIBSCRATCHCPP_EXPORT Value
7757
value_assign_cstring(&m_data, stringValue);
7858
}
7959

80-
/*! Constructs a special Value. */
81-
Value(SpecialValue specialValue)
82-
{
83-
value_init(&m_data);
84-
value_assign_special(&m_data, specialValue);
85-
}
86-
8760
/*! Constructs value from ValueData. */
8861
Value(const ValueData &v)
8962
{
@@ -181,27 +154,16 @@ class LIBSCRATCHCPP_EXPORT Value
181154
/*! Replaces the value with modulo of the value and the given value. */
182155
void mod(const Value &v) { value_mod(&m_data, &v.m_data, &m_data); }
183156

184-
const Value &operator=(float v)
185-
{
186-
value_assign_float(&m_data, v);
187-
return *this;
188-
}
189-
190157
const Value &operator=(double v)
191158
{
192159
value_assign_double(&m_data, v);
193160
return *this;
194161
}
195162

196-
const Value &operator=(int v)
163+
template<typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
164+
const Value &operator=(T v)
197165
{
198-
value_assign_int(&m_data, v);
199-
return *this;
200-
}
201-
202-
const Value &operator=(long v)
203-
{
204-
value_assign_long(&m_data, v);
166+
value_assign_double(&m_data, v);
205167
return *this;
206168
}
207169

include/scratchcpp/value_functions.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,10 @@ extern "C"
1111

1212
LIBSCRATCHCPP_EXPORT void value_init(ValueData *v);
1313

14-
LIBSCRATCHCPP_EXPORT void value_assign_float(ValueData *v, float numberValue);
1514
LIBSCRATCHCPP_EXPORT void value_assign_double(ValueData *v, double numberValue);
16-
LIBSCRATCHCPP_EXPORT void value_assign_int(ValueData *v, int numberValue);
17-
LIBSCRATCHCPP_EXPORT void value_assign_size_t(ValueData *v, size_t numberValue);
18-
LIBSCRATCHCPP_EXPORT void value_assign_long(ValueData *v, long numberValue);
1915
LIBSCRATCHCPP_EXPORT void value_assign_bool(ValueData *v, bool boolValue);
2016
LIBSCRATCHCPP_EXPORT void value_assign_string(ValueData *v, const std::string &stringValue);
2117
LIBSCRATCHCPP_EXPORT void value_assign_cstring(ValueData *v, const char *stringValue);
22-
LIBSCRATCHCPP_EXPORT void value_assign_special(ValueData *v, SpecialValue specialValue);
2318
LIBSCRATCHCPP_EXPORT void value_assign_copy(ValueData *v, const ValueData *another);
2419

2520
LIBSCRATCHCPP_EXPORT bool value_isInfinity(const ValueData *v);

include/scratchcpp/valuedata.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,11 @@
99
namespace libscratchcpp
1010
{
1111

12-
enum class LIBSCRATCHCPP_EXPORT SpecialValue
13-
{
14-
Infinity,
15-
NegativeInfinity,
16-
NaN
17-
};
18-
1912
enum class LIBSCRATCHCPP_EXPORT ValueType
2013
{
2114
Number = 0,
2215
Bool = 1,
23-
String = 2,
24-
Infinity = -1,
25-
NegativeInfinity = -2,
26-
NaN = -3
16+
String = 2
2717
};
2818

2919
extern "C"

src/blocks/operatorblocks.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,9 @@ unsigned int OperatorBlocks::op_ln(VirtualMachine *vm)
261261
{
262262
const Value &v = *vm->getInput(0, 1);
263263
if (v < 0)
264-
vm->replaceReturnValue(Value(SpecialValue::NaN), 1);
264+
vm->replaceReturnValue(std::numeric_limits<double>::quiet_NaN(), 1);
265265
else if (v == 0 || v.isNaN())
266-
vm->replaceReturnValue(Value(SpecialValue::NegativeInfinity), 1);
266+
vm->replaceReturnValue(-std::numeric_limits<double>::infinity(), 1);
267267
else if (!v.isInfinity())
268268
vm->replaceReturnValue(std::log(v.toDouble()), 1);
269269
return 0;
@@ -273,9 +273,9 @@ unsigned int OperatorBlocks::op_log(VirtualMachine *vm)
273273
{
274274
const Value &v = *vm->getInput(0, 1);
275275
if (v < 0)
276-
vm->replaceReturnValue(Value(SpecialValue::NaN), 1);
276+
vm->replaceReturnValue(std::numeric_limits<double>::quiet_NaN(), 1);
277277
else if (v == 0 || v.isNaN())
278-
vm->replaceReturnValue(Value(SpecialValue::NegativeInfinity), 1);
278+
vm->replaceReturnValue(-std::numeric_limits<double>::infinity(), 1);
279279
else if (!v.isInfinity())
280280
vm->replaceReturnValue(std::log10(v.toDouble()), 1);
281281
return 0;
@@ -284,7 +284,9 @@ unsigned int OperatorBlocks::op_log(VirtualMachine *vm)
284284
unsigned int OperatorBlocks::op_eexp(VirtualMachine *vm)
285285
{
286286
const Value *v = vm->getInput(0, 1);
287-
if (v->isNegativeInfinity())
287+
if (v->isNaN())
288+
vm->replaceReturnValue(1, 1);
289+
else if (v->isNegativeInfinity())
288290
vm->replaceReturnValue(0, 1);
289291
else if (!v->isInfinity())
290292
vm->replaceReturnValue(std::exp(v->toDouble()), 1);
@@ -294,7 +296,9 @@ unsigned int OperatorBlocks::op_eexp(VirtualMachine *vm)
294296
unsigned int OperatorBlocks::op_10exp(VirtualMachine *vm)
295297
{
296298
const Value *v = vm->getInput(0, 1);
297-
if (v->isNegativeInfinity())
299+
if (v->isNaN())
300+
vm->replaceReturnValue(1, 1);
301+
else if (v->isNegativeInfinity())
298302
vm->replaceReturnValue(0, 1);
299303
else if (!v->isInfinity())
300304
vm->replaceReturnValue(std::pow(10, v->toDouble()), 1);

src/dev/engine/internal/llvmcodebuilder.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@
1010

1111
using namespace libscratchcpp;
1212

13-
static std::unordered_map<ValueType, Compiler::StaticType> TYPE_MAP = {
14-
{ ValueType::Number, Compiler::StaticType::Number }, { ValueType::Bool, Compiler::StaticType::Bool },
15-
{ ValueType::String, Compiler::StaticType::String }, { ValueType::Infinity, Compiler::StaticType::Number },
16-
{ ValueType::NegativeInfinity, Compiler::StaticType::Number }, { ValueType::NaN, Compiler::StaticType::Number }
17-
};
13+
static std::unordered_map<ValueType, Compiler::StaticType>
14+
TYPE_MAP = { { ValueType::Number, Compiler::StaticType::Number }, { ValueType::Bool, Compiler::StaticType::Bool }, { ValueType::String, Compiler::StaticType::String } };
1815

1916
LLVMCodeBuilder::LLVMCodeBuilder(const std::string &id, bool warp) :
2017
m_id(id),

src/engine/virtualmachine_p.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
461461
{
462462
const Value *v = READ_REG(0, 1);
463463
if (v->isNegativeInfinity())
464-
REPLACE_RET_VALUE(Value(SpecialValue::Infinity), 1);
464+
REPLACE_RET_VALUE(std::numeric_limits<double>::infinity(), 1);
465465
else if (!v->isInfinity())
466466
REPLACE_RET_VALUE(std::abs(v->toDouble()), 1);
467467
DISPATCH();
@@ -487,7 +487,7 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
487487
{
488488
const Value &v = *READ_REG(0, 1);
489489
if (v < 0)
490-
REPLACE_RET_VALUE(Value(SpecialValue::NaN), 1);
490+
REPLACE_RET_VALUE(std::numeric_limits<double>::quiet_NaN(), 1);
491491
else if (!v.isInfinity())
492492
REPLACE_RET_VALUE(std::sqrt(v.toDouble()), 1);
493493
DISPATCH();
@@ -497,7 +497,7 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
497497
{
498498
const Value *v = READ_REG(0, 1);
499499
if (v->isInfinity() || v->isNegativeInfinity())
500-
REPLACE_RET_VALUE(Value(SpecialValue::NaN), 1);
500+
REPLACE_RET_VALUE(std::numeric_limits<double>::quiet_NaN(), 1);
501501
else
502502
REPLACE_RET_VALUE(std::sin(v->toDouble() * pi / 180), 1);
503503
DISPATCH();
@@ -507,7 +507,7 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
507507
{
508508
const Value *v = READ_REG(0, 1);
509509
if (v->isInfinity() || v->isNegativeInfinity())
510-
REPLACE_RET_VALUE(Value(SpecialValue::NaN), 1);
510+
REPLACE_RET_VALUE(std::numeric_limits<double>::quiet_NaN(), 1);
511511
else
512512
REPLACE_RET_VALUE(std::cos(v->toDouble() * pi / 180), 1);
513513
DISPATCH();
@@ -517,17 +517,17 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
517517
{
518518
const Value *v = READ_REG(0, 1);
519519
if (v->isInfinity() || v->isNegativeInfinity())
520-
REPLACE_RET_VALUE(Value(SpecialValue::NaN), 1);
520+
REPLACE_RET_VALUE(std::numeric_limits<double>::quiet_NaN(), 1);
521521
else {
522522
long mod;
523523
if (v->toLong() < 0)
524524
mod = (v->toLong() + 360) % 360;
525525
else
526526
mod = v->toLong() % 360;
527527
if (mod == 90)
528-
REPLACE_RET_VALUE(Value(SpecialValue::Infinity), 1);
528+
REPLACE_RET_VALUE(std::numeric_limits<double>::infinity(), 1);
529529
else if (mod == 270)
530-
REPLACE_RET_VALUE(Value(SpecialValue::NegativeInfinity), 1);
530+
REPLACE_RET_VALUE(-std::numeric_limits<double>::infinity(), 1);
531531
else
532532
REPLACE_RET_VALUE(std::tan(v->toDouble() * pi / 180), 1);
533533
}
@@ -538,7 +538,7 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
538538
{
539539
const Value &v = *READ_REG(0, 1);
540540
if (v < -1 || v > 1)
541-
REPLACE_RET_VALUE(Value(SpecialValue::NaN), 1);
541+
REPLACE_RET_VALUE(std::numeric_limits<double>::quiet_NaN(), 1);
542542
else
543543
REPLACE_RET_VALUE(std::asin(v.toDouble()) * 180 / pi, 1);
544544
DISPATCH();
@@ -548,7 +548,7 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
548548
{
549549
const Value &v = *READ_REG(0, 1);
550550
if (v < -1 || v > 1)
551-
REPLACE_RET_VALUE(Value(SpecialValue::NaN), 1);
551+
REPLACE_RET_VALUE(std::numeric_limits<double>::quiet_NaN(), 1);
552552
else
553553
REPLACE_RET_VALUE(std::acos(v.toDouble()) * 180 / pi, 1);
554554
DISPATCH();

src/scratch/inputvalue.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@
1717

1818
using namespace libscratchcpp;
1919

20-
static const std::map<ValueType, InputValue::Type> VALUE_TYPE_MAP = {
21-
{ ValueType::Number, InputValue::Type::Number }, { ValueType::Bool, InputValue::Type::String },
22-
{ ValueType::String, InputValue::Type::String }, { ValueType::Infinity, InputValue::Type::String },
23-
{ ValueType::NegativeInfinity, InputValue::Type::String }, { ValueType::NaN, InputValue::Type::String }
24-
};
20+
static const std::map<ValueType, InputValue::Type>
21+
VALUE_TYPE_MAP = { { ValueType::Number, InputValue::Type::Number }, { ValueType::Bool, InputValue::Type::String }, { ValueType::String, InputValue::Type::String } };
2522

2623
/*! Constructs InputValue with the given type. */
2724
InputValue::InputValue(Type type) :

0 commit comments

Comments
 (0)