示例#1
0
bool
FreezeScript::invokeGlobalFunction(const Ice::CommunicatorPtr& communicator, const string& name, const DataList& args,
                                   DataPtr& result, const DataFactoryPtr& factory, 
                                   const ErrorReporterPtr& errorReporter)
{
    //
    // Global function.
    //
    if(name == "typeOf")
    {
        if(args.size() != 1)
        {
            errorReporter->error("typeOf() requires one argument");
        }
        result = factory->createString(typeToString(args.front()->getType()), false);
        return true;
    }
    else if(name == "generateUUID")
    {
        if(args.size() != 0)
        {
            errorReporter->error("generateUUID() accepts no arguments");
        }
        result = factory->createString(IceUtil::generateUUID(), false);
        return true;
    }
    else if(name == "stringToIdentity")
    {
        StringDataPtr str;
        if(args.size() > 0)
        {
            str = StringDataPtr::dynamicCast(args.front());
        }
        if(args.size() != 1 || !str)
        {
            errorReporter->error("stringToIdentity() requires a string argument");
        }

        //
        // Parse the identity string.
        //
        string idstr = str->stringValue();
        Ice::Identity id;
        try
        {
            id = communicator->stringToIdentity(idstr);
        }
        catch(const Ice::IdentityParseException& ex)
        {
            errorReporter->error("error in stringToIdentity():\n" + ex.str);
        }

        //
        // Create a data representation of Ice::Identity.
        //
        Slice::UnitPtr unit = str->getType()->unit();
        Slice::TypeList l = unit->lookupType("::Ice::Identity", false);
        assert(!l.empty());
        DataPtr identity = factory->create(l.front(), false);
        StringDataPtr member;
        member = StringDataPtr::dynamicCast(identity->getMember("name"));
        assert(member);
        member->setValue(id.name);
        member = StringDataPtr::dynamicCast(identity->getMember("category"));
        assert(member);
        member->setValue(id.category);
        result = identity;
        return true;
    }
    else if(name == "identityToString")
    {
        StructDataPtr identity;
        if(args.size() > 0)
        {
            identity = StructDataPtr::dynamicCast(args.front());
        }
        if(identity)
        {
            Slice::TypePtr argType = identity->getType();
            Slice::StructPtr st = Slice::StructPtr::dynamicCast(argType);
            if(!st || st->scoped() != "::Ice::Identity")
            {
                identity = 0;
            }
        }
        if(args.size() != 1 || !identity)
        {
            errorReporter->error("identityToString() requires a argument of type ::Ice::Identity");
        }

        //
        // Compose the identity.
        //
        Ice::Identity id;
        StringDataPtr member;
        member = StringDataPtr::dynamicCast(identity->getMember("name"));
        assert(member);
        id.name = member->stringValue();
        member = StringDataPtr::dynamicCast(identity->getMember("category"));
        assert(member);
        id.category = member->stringValue();

        result = factory->createString(communicator->identityToString(id), false);
        return true;
    }
    else if(name == "stringToProxy")
    {
        StringDataPtr str;
        if(args.size() > 0)
        {
            str = StringDataPtr::dynamicCast(args.front());
        }
        if(args.size() != 1 || !str)
        {
            errorReporter->error("stringToProxy() requires a string argument");
        }

        //
        // Parse the proxy;
        //
        string sprx = str->stringValue();
        Ice::ObjectPrx prx;
        try
        {
            prx = factory->getCommunicator()->stringToProxy(sprx);
        }
        catch(const Ice::ProxyParseException& ex)
        {
            errorReporter->error("error in stringToProxy():\n" + ex.str);
        }

        Slice::UnitPtr unit = str->getType()->unit();
        ProxyDataPtr p =
            ProxyDataPtr::dynamicCast(factory->create(unit->builtin(Slice::Builtin::KindObjectProxy), false));
        p->setValue(prx);
        result = p;
        return true;
    }
    else if(name == "proxyToString")
    {
        ProxyDataPtr prx;
        if(args.size() > 0)
        {
            prx = ProxyDataPtr::dynamicCast(args.front());
        }
        if(args.size() != 1 || !prx)
        {
            errorReporter->error("proxyToString() requires a proxy argument");
        }

        result = factory->createString(prx->toString(), false);
        return true;
    }
    else if(name == "lowercase")
    {
        StringDataPtr str;
        if(args.size() > 0)
        {
            str = StringDataPtr::dynamicCast(args.front());
        }
        if(args.size() != 1 || !str)
        {
            errorReporter->error("lowercase() requires a string argument");
        }
        string val = IceUtilInternal::toLower(str->stringValue());
        result = factory->createString(val, false);
        return true;
    }

    return false;
}
示例#2
0
文件: Util.cpp 项目: ming-hai/freeze
void
FreezeScript::createEvictorSliceTypes(const Slice::UnitPtr& u)
{
    Slice::TypeList l;
    Slice::ContainedList c;

    //
    // Create the Ice module if necessary.
    //
    c = u->lookupContained("Ice", false);
    Slice::ModulePtr ice;
    if(c.empty())
    {
        ice = u->createModule("Ice");
    }
    else
    {
        ice = Slice::ModulePtr::dynamicCast(c.front());
        if(!ice)
        {
            throw FailureException(__FILE__, __LINE__, "the symbol `::Ice' is defined in Slice but is not a module");
        }
    }

    //
    // Create the Slice definition for Ice::Identity if it doesn't exist.
    //
    string scoped = "::Ice::Identity";
    l = u->lookupTypeNoBuiltin(scoped, false);
    Slice::StructPtr identity;
    if(l.empty())
    {
        identity = ice->createStruct("Identity", false);
        Slice::TypePtr str = u->builtin(Slice::Builtin::KindString);
        identity->createDataMember("name", str, false, 0, 0, "", "");
        identity->createDataMember("category", str, false, 0, 0, "", "");
    }
    else
    {
        identity = Slice::StructPtr::dynamicCast(l.front());
        if(!identity)
        {
            throw FailureException(__FILE__, __LINE__,
                                   "the symbol `::Ice::Identity' is defined in Slice but is not a struct");
        }
    }

    //
    // Create the Freeze module if necessary.
    //
    c = u->lookupContained("Freeze", false);
    Slice::ModulePtr freeze;
    if(c.empty())
    {
        freeze = u->createModule("Freeze");
    }
    else
    {
        freeze = Slice::ModulePtr::dynamicCast(c.front());
        if(!freeze)
        {
            throw FailureException(__FILE__, __LINE__,
                                   "the symbol `::Freeze' is defined in Slice but is not a module");
        }
    }

    //
    // Create the Slice definition for Freeze::Statistics if it doesn't exist.
    //
    scoped = "::Freeze::Statistics";
    l = u->lookupTypeNoBuiltin(scoped, false);
    Slice::StructPtr stats;
    if(l.empty())
    {
        stats = freeze->createStruct("Statistics", false);
        Slice::TypePtr tl = u->builtin(Slice::Builtin::KindLong);
        stats->createDataMember("creationTime", tl, false, 0, 0, "", "");
        stats->createDataMember("lastSaveTime", tl, false, 0, 0, "", "");
        stats->createDataMember("avgSaveTime", tl, false, 0, 0, "", "");
    }
    else
    {
        stats = Slice::StructPtr::dynamicCast(l.front());
        if(!stats)
        {
            throw FailureException(__FILE__, __LINE__, "the symbol `::Freeze::Statistics' is defined in "
                                   "Slice but is not a struct");
        }
    }

    //
    // Create the Slice definition for Freeze::ObjectRecord if it doesn't exist.
    //
    scoped = "::Freeze::ObjectRecord";
    l = u->lookupTypeNoBuiltin(scoped, false);
    if(l.empty())
    {
        Slice::StructPtr rec = freeze->createStruct("ObjectRecord", false);
        Slice::TypePtr obj = u->builtin(Slice::Builtin::KindObject);
        rec->createDataMember("servant", obj, false, 0, 0, "", "");
        rec->createDataMember("stats", stats, false, 0, 0, "", "");
    }
    else
    {
        if(!Slice::StructPtr::dynamicCast(l.front()))
        {
            throw FailureException(__FILE__, __LINE__, "the symbol `::Freeze::ObjectRecord' is defined in "
                                   "Slice but is not a struct");
        }
    }
}