1
0
Fork 0

Nasal expose propertyValue as a helper

Allow other code to map property values to an naRef, without exposing
the generic property node on their API. This avoids creating a wrapper
props.Node frequently for simple value access.
This commit is contained in:
James Turner 2022-03-06 20:04:31 +00:00
parent cbd5ef9e7b
commit 77501672df
2 changed files with 40 additions and 24 deletions

View file

@ -156,6 +156,13 @@ public:
string_list getAndClearErrorList();
/**
@brief Convert the value of an SGPropertyNode to its Nasal representation. Used by
props.Node.getValue internally, but exposed here for other use cases which don't want to create
a props.Node wrapper each time.
*/
static naRef getPropertyValue(naContext c, SGPropertyNode* node);
private:
void initLogLevelConstants();

View file

@ -305,30 +305,7 @@ static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
using namespace simgear;
NODEARG();
MOVETARGET(naVec_size(argv) > 0, false);
switch(node->getType()) {
case props::BOOL: case props::INT:
case props::LONG: case props::FLOAT:
case props::DOUBLE:
{
double dv = node->getDoubleValue();
if (SGMisc<double>::isNaN(dv)) {
SG_LOG(SG_NASAL, SG_ALERT, "Nasal getValue: property " << node->getPath() << " is NaN");
return naNil();
}
return naNum(dv);
}
case props::STRING:
case props::UNSPECIFIED:
return NASTR(node->getStringValue().c_str());
case props::VEC3D:
return makeVectorFromVec(c, node->getValue<SGVec3d>());
case props::VEC4D:
return makeVectorFromVec(c, node->getValue<SGVec4d>());
default:
return naNil();
}
return FGNasalSys::getPropertyValue(c, node);
}
template<typename T>
@ -941,3 +918,35 @@ naRef FGNasalSys::genPropsModule()
naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));
return namespc;
}
naRef FGNasalSys::getPropertyValue(naContext c, SGPropertyNode* node)
{
using namespace simgear;
if (!node)
return naNil();
switch(node->getType()) {
case props::BOOL: case props::INT:
case props::LONG: case props::FLOAT:
case props::DOUBLE:
{
double dv = node->getDoubleValue();
if (SGMisc<double>::isNaN(dv)) {
SG_LOG(SG_NASAL, SG_ALERT, "Nasal getValue: property " << node->getPath() << " is NaN");
return naNil();
}
return naNum(dv);
}
case props::STRING:
case props::UNSPECIFIED:
return NASTR(node->getStringValue().c_str());
case props::VEC3D:
return makeVectorFromVec(c, node->getValue<SGVec3d>());
case props::VEC4D:
return makeVectorFromVec(c, node->getValue<SGVec4d>());
default:
return naNil();
}
}