Пример #1
0
bool VirtualValue::operator==(const VirtualValue& that) const
{
   if ( mKind == that.mKind )
   {
      switch ( mKind )
      {
         case eEmpty:
            return true;

         case eNumber:
            return asNumber() == that.asNumber();

         case eReal:
            return asReal() == that.asReal();

         case eChar:
            return asChar() == that.asChar();

         case eBool:
            return asBoolean() == that.asBoolean();

         case eString:
            return asString() == that.asString();

         case eObject:
            return &asObject() == &that.asObject();

         case eArray:
            return &asArray() == &that.asArray();
      }
   }

   return false;
}
Пример #2
0
ConfigImpl::ConfigImpl(MemoryPool& p) : ConfigRoot(p), confMessage(p)
{
    // Prepare some stuff

    ConfigFile file(p);
    root_dir = getRootDirectory();
    const int size = FB_NELEM(entries);
    values = FB_NEW(p) ConfigValue[size];

    //string val_sep = ",";
    file.setConfigFilePath(getConfigFilePath());

    // Iterate through the known configuration entries

    for (int i = 0; i < size; i++)
    {
        const ConfigEntry entry = entries[i];
        const string value = getValue(file, entries[i].key);

        if (!value.length())
        {
            // Assign the default value

            values[i] = entries[i].default_value;
            continue;
        }

        // Assign the actual value

        switch (entry.data_type)
        {
        case TYPE_BOOLEAN:
            values[i] = (ConfigValue) asBoolean(value);
            break;
        case TYPE_INTEGER:
            values[i] = (ConfigValue) asInteger(value);
            break;
        case TYPE_STRING:
        {
            const char* src = asString(value);
            char* dst = FB_NEW(p) char[strlen(src) + 1];
            strcpy(dst, src);
            values[i] = (ConfigValue) dst;
        }
        break;
            //case TYPE_STRING_VECTOR:
            //	break;
        }
    }

    if (file.getMessage())
    {
        confMessage = file.getMessage();
    }
}
Пример #3
0
std::optional<double> JSValue::toNumberFromPrimitive() const
{
    if (isEmpty())
        return std::nullopt;
    if (isNumber())
        return asNumber();
    if (isBoolean())
        return asBoolean();
    if (isUndefined())
        return PNaN;
    if (isNull())
        return 0;
    return std::nullopt;
}
Пример #4
0
   // - LuaValue::operator== ---------------------------------------------------
   bool LuaValue::operator== (const LuaValue& rhs) const
   {
      std::string lhsTypeName = typeName();
      std::string rhsTypeName = rhs.typeName();

      if (typeName() != rhs.typeName())
         return false;
      else switch (type())
      {
         case LUA_TNIL:
            return true;

         case LUA_TBOOLEAN:
            return asBoolean() == rhs.asBoolean();

         case LUA_TNUMBER:
            return asNumber() == rhs.asNumber();

         case LUA_TSTRING:
            return asString() == rhs.asString();

         case LUA_TTABLE:
            return asTable() == rhs.asTable();

         case LUA_TFUNCTION:
            return asFunction() == rhs.asFunction();

         case LUA_TUSERDATA:
            return asUserData() == rhs.asUserData();

         default:
         {
            assert(
               false
               && "Invalid type found in a call to 'LuaValue::operator==()'.");
            return 0; // make compilers happy
         }
      }
   }
Пример #5
0
bool Item::isTrue() const
{
   switch( dereference()->type() )
   {
      case FLC_ITEM_BOOL:
         return asBoolean() != 0;

      case FLC_ITEM_INT:
         return asInteger() != 0;

      case FLC_ITEM_NUM:
         return asNumeric() != 0.0;

      case FLC_ITEM_RANGE:
         return asRangeStart() != asRangeEnd() || asRangeIsOpen();

      case FLC_ITEM_STRING:
         return asString()->size() != 0;

      case FLC_ITEM_ARRAY:
         return asArray()->length() != 0;

      case FLC_ITEM_DICT:
         return asDict()->length() != 0;

      case FLC_ITEM_FUNC:
      case FLC_ITEM_OBJECT:
      case FLC_ITEM_CLASS:
      case FLC_ITEM_METHOD:
      case FLC_ITEM_MEMBUF:
      case FLC_ITEM_LBIND:
         // methods are always filled, so they are always true.
         return true;
   }

   return false;
}
Пример #6
0
   // - LuaValue::operator> ----------------------------------------------------
   bool LuaValue::operator> (const LuaValue& rhs) const
   {
      std::string lhsTypeName = typeName();
      std::string rhsTypeName = rhs.typeName();

      if (lhsTypeName > rhsTypeName)
         return true;
      else if (lhsTypeName < rhsTypeName)
         return false;
      else // lhsTypeName == rhsTypeName
      {
         if (lhsTypeName == "nil")
            return false;
         else if (lhsTypeName == "boolean")
            return asBoolean() > rhs.asBoolean();
         else if (lhsTypeName == "number")
            return asNumber() > rhs.asNumber();
         else if (lhsTypeName == "string")
            return asString() > rhs.asString();
         else if (lhsTypeName == "function")
            return asFunction() > rhs.asFunction();
         else if (lhsTypeName == "userdata")
            return asUserData() > rhs.asUserData();
         else if (lhsTypeName == "table")
         {
            const LuaValueMap lhsMap = asTable();
            const LuaValueMap rhsMap = rhs.asTable();

            if (lhsMap.size() > rhsMap.size())
               return true;
            else if (lhsMap.size() < rhsMap.size())
               return false;
            else // lhsMap.size() == rhsMap.size()
            {
               typedef LuaValueMap::const_iterator iter_t;

               iter_t pLHS = lhsMap.begin();
               iter_t pRHS = rhsMap.begin();
               const iter_t end = lhsMap.end();

               while (pLHS != end)
               {
                  // check the key first
                  if (pLHS->first > pRHS->first)
                     return true;
                  else if (pLHS->first < pRHS->first)
                     return false;

                  // then check the value
                  if (pLHS->second > pRHS->second)
                     return true;
                  else if (pLHS->second < pRHS->second)
                     return false;

                  // make the iterators iterate
                  ++pRHS;
                  ++pLHS;
               }
               return false;
            }
         }
         else
         {
            assert (false && "Unsupported type found at a call "
                    "to 'LuaValue::operator>()'");
            return false; // make the compiler happy.
         }
      }
   }
Пример #7
0
// compare values. looks strange in order to be compatible with Excel
int KCValue::compare(const KCValue& v) const
{
    KCValue::Type t1 = d->type;
    KCValue::Type t2 = v.type();

    // errors always less than everything else
    if ((t1 == Error) && (t2 != Error))
        return -1;
    if ((t2 == Error) && (t1 != Error))
        return 1;

    // comparing errors only yields 0 if they are the same
    if ((t1 == Error) && (t2 == Error))
        return errorMessage() != v.errorMessage();

    // empty == empty
    if ((t1 == Empty) && (t2 == Empty))
        return 0;

    // empty value is always less than string
    // (except when the string is empty)
    if ((t1 == Empty) && (t2 == String))
        return(v.asString().isEmpty()) ? 0 : -1;

    // boolean vs boolean
    if ((t1 == Boolean) && (t2 == Boolean)) {
        bool p = asBoolean();
        bool q = v.asBoolean();
        if (p) return q ? 0 : 1;
        else return q ? -1 : 0;
    }

    // boolean is always greater than integer
    if ((t1 == Boolean) && (t2 == Integer))
        return 1;

    // boolean is always greater than float
    if ((t1 == Boolean) && (t2 == Float))
        return 1;

    // boolean is always greater than string
    if ((t1 == Boolean) && (t2 == String))
        return 1;

    // integer is always less than boolean
    if ((t1 == Integer) && (t2 == Boolean))
        return -1;

    // integer vs integer
    if ((t1 == Integer) && (t2 == Integer)) {
        qint64 p = asInteger();
        qint64 q = v.asInteger();
        return (p == q) ? 0 : (p < q) ? -1 : 1;
    }

    // integer vs float
    if ((t1 == Integer) && (t2 == Float))
        return compare(asFloat(), v.asFloat());

    // integer is always less than string
    if ((t1 == Integer) && (t2 == String))
        return -1;

    // float is always less than boolean
    if ((t1 == Float) && (t2 == Boolean))
        return -1;

    // float vs integer
    if ((t1 == Float) && (t2 == Integer))
        return compare(asFloat(), v.asFloat());

    // float vs float
    if ((t1 == Float) && (t2 == Float))
        return compare(asFloat(), v.asFloat());

    // float is always less than string
    if ((t1 == Float) && (t2 == String))
        return -1;

    // TODO Stefan: Complex

    // string is always greater than empty value
    // (except when the string is empty)
    if ((t1 == String) && (t2 == Empty))
        return(asString().isEmpty()) ? 0 : 1;

    // string is always less than boolean
    if ((t1 == String) && (t2 == Boolean))
        return -1;

    // string is always greater than integer
    if ((t1 == String) && (t2 == Integer))
        return 1;

    // string is always greater than float
    if ((t1 == String) && (t2 == Float))
        return 1;

    // The-Real-String comparison
    if ((t1 == String) && (t2 == String))
        return asString().compare(v.asString());

    // Undefined, actually allowComparison would return false
    return 0;
}
Пример #8
0
void Item::toString( String &target ) const
{
   target.size(0);

   switch( this->type() )
   {
      case FLC_ITEM_NIL:
         target = "Nil";
      break;

      case FLC_ITEM_UNB:
         target = "_";
      break;

      case FLC_ITEM_BOOL:
         target = asBoolean() ? "true" : "false";
      break;


      case FLC_ITEM_INT:
         target.writeNumber( this->asInteger() );
      break;

      case FLC_ITEM_RANGE:
         target = "[";
         target.writeNumber( (int64) this->asRangeStart() );
         target += ":";
         if ( ! this->asRangeIsOpen() )
         {
            target.writeNumber( (int64) this->asRangeEnd() );
            if ( this->asRangeStep() != 0 )
            {
               target += ":";
               target.writeNumber( (int64) this->asRangeStep() );
            }
         }
         target += "]";
      break;

      case FLC_ITEM_NUM:
      {
         target.writeNumber( this->asNumeric(), "%.16g" );
      }
      break;

      case FLC_ITEM_MEMBUF:
         target = "MemBuf( ";
         target.writeNumber( (int64) this->asMemBuf()->length() );
         target += ", ";
            target.writeNumber( (int64) this->asMemBuf()->wordSize() );
         target += " )";
      break;

      case FLC_ITEM_STRING:
         target = *asString();
      break;

      case FLC_ITEM_LBIND:
         if ( isFutureBind() )
         {
            String temp;
            asFutureBind().toString(temp);
            target = *asLBind() + "|" + temp;
         }
         else
            target = "&" + *asLBind();
      break;

      case FLC_ITEM_REFERENCE:
         dereference()->toString( target );
      break;

      case FLC_ITEM_OBJECT:
         target = "Object from " + asObjectSafe()->generator()->symbol()->name();
      break;

      case FLC_ITEM_ARRAY:
         target = "Array";
      break;

      case FLC_ITEM_DICT:
         target = "Dictionary";
      break;

      case FLC_ITEM_FUNC:
         target = "Function " + this->asFunction()->symbol()->name();
      break;

      case FLC_ITEM_CLASS:
         target = "Class " + this->asClass()->symbol()->name();
      break;

      case FLC_ITEM_METHOD:
         {
            Item orig;
            this->getMethodItem( orig );
            String temp;
            orig.dereference()->toString( temp );
            target = "Method (" + temp + ")." + this->asMethodFunc()->name();
         }
      break;

      case FLC_ITEM_CLSMETHOD:
         target = "ClsMethod " + this->asMethodClass()->symbol()->name();
      break;

      default:
         target = "<?>";
   }
}