Exemple #1
0
double LLBC_Variant::AsDouble() const
{
    if (IsNil())
    {
        return 0.0;
    }
    else if (IsDict())
    {
        return 0.0;
    }
    else if (IsStr())
    {
        return LLBC_Str2Double(_holder.str.c_str());
    }

    if (IsDouble() || IsFloat())
    {
        return _holder.raw.doubleVal;
    }

    if (IsSignedRaw())
    {
        return static_cast<double>(_holder.raw.int64Val);
    }

    return static_cast<double>(_holder.raw.uint64Val);
}
Exemple #2
0
void LLBC_Variant::Serialize(LLBC_Stream &stream) const
{
    stream.Write(_holder.type);

    if (IsRaw())
    {
        stream.Write(_holder.raw.uint64Val);
    }
    else if (IsStr())
    {
        stream.Write(_holder.str);
    }
    else if (IsDict())
    {
        if (!_holder.dict)
        {
            stream.Write(static_cast<uint32>(0));
        }
        else
        {
            stream.Write(static_cast<uint32>(_holder.dict->size()));
            for (DictConstIter it = _holder.dict->begin();
                it != _holder.dict->end();
                it ++)
            {
                stream.Write(it->first);
                stream.Write(it->second);
            }
        }
    }
}
Exemple #3
0
const Dict &LLBC_Variant::AsDict() const
{
    if (!IsDict())
    {
        return LLBC_INL_NS __g_nullDict;
    }

    return *_holder.dict;
}
Exemple #4
0
LLBC_Variant &LLBC_Variant::BecomeDict()
{
    if (!IsDict())
    {
        *this = AsDict();
    }

    return *this;
}
Exemple #5
0
void LLBC_Variant::OptimizePerformance()
{
    if (IsDict())
    {
        if (_holder.dict && _holder.dict->empty())
        {
            LLBC_XDelete(_holder.dict);
        }
    }
}
Exemple #6
0
uint64 LLBC_Variant::AsUInt64() const
{
    if (IsNil())
    {
        return 0;
    }
    else if (IsDict())
    {
        return 0;
    }
    else if (IsStr())
    {
        return LLBC_Str2UInt64(_holder.str.c_str());
    }

    if (IsDouble() || IsFloat())
    {
        return static_cast<uint64>(_holder.raw.doubleVal);
    }

    return _holder.raw.uint64Val;
}
Exemple #7
0
bool LLBC_Variant::AsBool() const
{
    if (IsNil())
    {
        return false;
    }
    else if (IsDict())
    {
        return false;
    }
    else if (IsStr())
    {
        LLBC_String trimedData(LLBC_Trim(_holder.str));
        if (trimedData.length() != 4 && trimedData.length() != 5)
        {
            return (AsInt64() != 0 ? true : false);
        }

        LLBC_String lowerData(LLBC_ToLower(trimedData.c_str()));
        return (lowerData == "true" ? true : false);
    }

    return _holder.raw.uint64Val != 0;
}
/**********************************************************************
* CTextRun::Concatenate *
*-----------------------*
*	Description:  
*		If possible, concatenates pNext (pPrev if fConcatAfter is false)
*       onto the end of this.
*       Another CTextRun can always be concatenated on, unless it 
*       contains dictation.
*
* 	Return:
*		E_NOMERGE if could not be merged (because pTextRun is dictation)
*       E_FULLMERGE
**********************************************************************/
MERGERESULT CTextRun::Concatenate( CTextRun *pTextRun, bool fConcatAfter )
{
    if ( !pTextRun || !m_cpTextRange )
    {
        return E_NOMERGE;
    }

    // Check for compatibility: In this case, neither mergee can be 
    // a dict run
    if ( IsDict() || pTextRun->IsDict() )
    {
        return E_NOMERGE;
    }

    // lNewBound will be the new end (resp. start) of the run, if the 
    // concatenation is successful
    long lNewBound;
   
    // Concatenation is possible iff one run ends exactly where the other
    // begins.
    // If concatenation is possible, do it.
    if ( fConcatAfter )
    {
        // Will be concatenating pTextRun onto the end of this
        if ( GetEnd() != pTextRun->GetStart() )
        {
            // They are not consecutive runs
            return E_NOMERGE;
        }

        // lNewBound will be the new end of the run, if the 
        // concatenation is successful
        lNewBound = pTextRun->GetEnd();
        
        // Swallow up pTextRun by setting our end to its end
        SetEnd( lNewBound );

        // Make pTextRun degenerate
        pTextRun->SetStart( lNewBound );
        
    }
    else
    {
        // Will be concatenating pTextRun onto the beginning of this
        if ( GetStart() != pTextRun->GetEnd() )
        {
            return E_NOMERGE;
        }

        // lNewBound will be the new start of the run, if the 
        // concatenation is successful
        lNewBound = pTextRun->GetStart();

        // Swallow up pTextRun by setting our start to its start
        SetStart( lNewBound );

        // Make pTextRun degenerate
        pTextRun->SetEnd( lNewBound );
    }

    return E_FULLMERGE;
}   /* CTextRun::Concatenate */
Exemple #9
0
bool LLBC_Variant::DeSerialize(LLBC_Stream &stream)
{
    BecomeNil();

    if (!stream.Read(_holder.type))
    {
        return false;
    }

    if (IsNil())
    {
        return true;
    }
    if (IsRaw())
    {
        if (!stream.Read(_holder.raw.uint64Val))
        {
            _holder.type = LLBC_VariantType::VT_NIL;
            return false;
        }
    }
    else if (IsStr())
    {
        if (!stream.Read(_holder.str))
        {
            _holder.type = LLBC_VariantType::VT_NIL;
            return false;
        }
    }
    else if (IsDict())
    {
        uint32 count = 0;
        if (!stream.Read(count))
        {
            _holder.type = LLBC_VariantType::VT_NIL;
            return false;
        }

        if (count == 0)
        {
            return true;
        }

        _holder.dict = new Dict;
        for (uint32 i = 0; i < count; i ++)
        {
            LLBC_Variant key;
            LLBC_Variant val;
            if (!stream.Read(key) || !stream.Read(val))
            {
                LLBC_XDelete(_holder.dict);
                _holder.type = LLBC_VariantType::VT_NIL;
                return false;
            }

            _holder.dict->insert(std::make_pair(key, val));
        }

        return true;
    }
    
    return false;
}
Exemple #10
0
LLBC_String LLBC_Variant::ValueToString() const
{
    if (IsStr())
    {
        return _holder.str;
    }
    else if (IsDict())
    {

        LLBC_String content;
        content.append("{");

        if (_holder.dict)
        {
            for (DictConstIter it = _holder.dict->begin();
                it != _holder.dict->end();
               )
            {
                content.append(it->first.ValueToString());
                content.append(":");
                content.append(it->second.ValueToString());

                if (++ it != _holder.dict->end())
                {
                    content.append("|");
                }
            }
        }

        content.append("}");
        return content;
    }
    else if (IsNil())
    {
        return "nil";
    }

    // RAW type var data.
    switch(_holder.type)
    {
    case LLBC_VariantType::VT_RAW_BOOL:
        return _holder.raw.int64Val != 0 ? "true" : "false";

    case LLBC_VariantType::VT_RAW_SINT8:
    case LLBC_VariantType::VT_RAW_SINT16:
    case LLBC_VariantType::VT_RAW_SINT32:
    case LLBC_VariantType::VT_RAW_LONG:
    case LLBC_VariantType::VT_RAW_SINT64:
        return LLBC_Num2Str(_holder.raw.int64Val);

    case LLBC_VariantType::VT_RAW_UINT8:
    case LLBC_VariantType::VT_RAW_UINT16:
    case LLBC_VariantType::VT_RAW_UINT32:
    case LLBC_VariantType::VT_RAW_ULONG:
    case LLBC_VariantType::VT_RAW_UINT64:
        return LLBC_Num2Str(_holder.raw.uint64Val);

    case LLBC_VariantType::VT_RAW_FLOAT:
    case LLBC_VariantType::VT_RAW_DOUBLE:
        return LLBC_Num2Str(_holder.raw.doubleVal);

    default:
        break;
    }

    return "";
}