1*fce4895dSRene Gollent /*
2*fce4895dSRene Gollent * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3*fce4895dSRene Gollent * Distributed under the terms of the MIT License.
4*fce4895dSRene Gollent */
5*fce4895dSRene Gollent
6*fce4895dSRene Gollent
7*fce4895dSRene Gollent #include "PrimitiveValueNode.h"
8*fce4895dSRene Gollent
9*fce4895dSRene Gollent #include <new>
10*fce4895dSRene Gollent
11*fce4895dSRene Gollent #include "BoolValue.h"
12*fce4895dSRene Gollent #include "FloatValue.h"
13*fce4895dSRene Gollent #include "IntegerValue.h"
14*fce4895dSRene Gollent #include "Tracing.h"
15*fce4895dSRene Gollent #include "Type.h"
16*fce4895dSRene Gollent #include "ValueLoader.h"
17*fce4895dSRene Gollent #include "ValueLocation.h"
18*fce4895dSRene Gollent
19*fce4895dSRene Gollent
PrimitiveValueNode(ValueNodeChild * nodeChild,PrimitiveType * type)20*fce4895dSRene Gollent PrimitiveValueNode::PrimitiveValueNode(ValueNodeChild* nodeChild,
21*fce4895dSRene Gollent PrimitiveType* type)
22*fce4895dSRene Gollent :
23*fce4895dSRene Gollent ChildlessValueNode(nodeChild),
24*fce4895dSRene Gollent fType(type)
25*fce4895dSRene Gollent {
26*fce4895dSRene Gollent fType->AcquireReference();
27*fce4895dSRene Gollent }
28*fce4895dSRene Gollent
29*fce4895dSRene Gollent
~PrimitiveValueNode()30*fce4895dSRene Gollent PrimitiveValueNode::~PrimitiveValueNode()
31*fce4895dSRene Gollent {
32*fce4895dSRene Gollent fType->ReleaseReference();
33*fce4895dSRene Gollent }
34*fce4895dSRene Gollent
35*fce4895dSRene Gollent
36*fce4895dSRene Gollent Type*
GetType() const37*fce4895dSRene Gollent PrimitiveValueNode::GetType() const
38*fce4895dSRene Gollent {
39*fce4895dSRene Gollent return fType;
40*fce4895dSRene Gollent }
41*fce4895dSRene Gollent
42*fce4895dSRene Gollent
43*fce4895dSRene Gollent status_t
ResolvedLocationAndValue(ValueLoader * valueLoader,ValueLocation * & _location,Value * & _value)44*fce4895dSRene Gollent PrimitiveValueNode::ResolvedLocationAndValue(ValueLoader* valueLoader,
45*fce4895dSRene Gollent ValueLocation*& _location, Value*& _value)
46*fce4895dSRene Gollent {
47*fce4895dSRene Gollent // get the location
48*fce4895dSRene Gollent ValueLocation* location = NodeChild()->Location();
49*fce4895dSRene Gollent if (location == NULL)
50*fce4895dSRene Gollent return B_BAD_VALUE;
51*fce4895dSRene Gollent
52*fce4895dSRene Gollent // get the value type
53*fce4895dSRene Gollent type_code valueType = fType->TypeConstant();
54*fce4895dSRene Gollent if (!BVariant::TypeIsNumber(valueType) && valueType != B_BOOL_TYPE) {
55*fce4895dSRene Gollent TRACE_LOCALS(" -> unknown type constant\n");
56*fce4895dSRene Gollent return B_UNSUPPORTED;
57*fce4895dSRene Gollent }
58*fce4895dSRene Gollent
59*fce4895dSRene Gollent bool shortValueIsFine = BVariant::TypeIsInteger(valueType)
60*fce4895dSRene Gollent || valueType == B_BOOL_TYPE;
61*fce4895dSRene Gollent
62*fce4895dSRene Gollent TRACE_LOCALS(" TYPE_PRIMITIVE: '%c%c%c%c'\n",
63*fce4895dSRene Gollent int(valueType >> 24), int(valueType >> 16),
64*fce4895dSRene Gollent int(valueType >> 8), int(valueType));
65*fce4895dSRene Gollent
66*fce4895dSRene Gollent // load the value data
67*fce4895dSRene Gollent BVariant valueData;
68*fce4895dSRene Gollent status_t error = valueLoader->LoadValue(location, valueType,
69*fce4895dSRene Gollent shortValueIsFine, valueData);
70*fce4895dSRene Gollent if (error != B_OK)
71*fce4895dSRene Gollent return error;
72*fce4895dSRene Gollent
73*fce4895dSRene Gollent // create the type object
74*fce4895dSRene Gollent Value* value;
75*fce4895dSRene Gollent if (valueType == B_BOOL_TYPE)
76*fce4895dSRene Gollent value = new(std::nothrow) BoolValue(valueData.ToBool());
77*fce4895dSRene Gollent else if (BVariant::TypeIsInteger(valueType))
78*fce4895dSRene Gollent value = new(std::nothrow) IntegerValue(valueData);
79*fce4895dSRene Gollent else if (BVariant::TypeIsFloat(valueType))
80*fce4895dSRene Gollent value = new(std::nothrow) FloatValue(valueData);
81*fce4895dSRene Gollent else
82*fce4895dSRene Gollent return B_UNSUPPORTED;
83*fce4895dSRene Gollent
84*fce4895dSRene Gollent if (value == NULL)
85*fce4895dSRene Gollent return B_NO_MEMORY;
86*fce4895dSRene Gollent
87*fce4895dSRene Gollent location->AcquireReference();
88*fce4895dSRene Gollent _location = location;
89*fce4895dSRene Gollent _value = value;
90*fce4895dSRene Gollent return B_OK;
91*fce4895dSRene Gollent }
92