diff options
author | Mark Spruiell <mes@zeroc.com> | 2002-01-21 22:45:56 +0000 |
---|---|---|
committer | Mark Spruiell <mes@zeroc.com> | 2002-01-21 22:45:56 +0000 |
commit | 068c1bf5949890cceb3b471a20d9e6b1207a2618 (patch) | |
tree | 5ef71ae9a98f20840275cfeffb801fbb25a1e142 /cpp/src/slice2java/Gen.cpp | |
parent | Fixed (I believe) the spin bug. (diff) | |
download | ice-068c1bf5949890cceb3b471a20d9e6b1207a2618.tar.bz2 ice-068c1bf5949890cceb3b471a20d9e6b1207a2618.tar.xz ice-068c1bf5949890cceb3b471a20d9e6b1207a2618.zip |
revising servant mapping for interfaces
Diffstat (limited to 'cpp/src/slice2java/Gen.cpp')
-rw-r--r-- | cpp/src/slice2java/Gen.cpp | 705 |
1 files changed, 347 insertions, 358 deletions
diff --git a/cpp/src/slice2java/Gen.cpp b/cpp/src/slice2java/Gen.cpp index ad19d1d52d6..0240d86d6ec 100644 --- a/cpp/src/slice2java/Gen.cpp +++ b/cpp/src/slice2java/Gen.cpp @@ -1040,6 +1040,333 @@ Slice::JavaVisitor::writeHashCode(Output& out, const TypePtr& type, } void +Slice::JavaVisitor::writeDispatch(Output& out, const ClassDefPtr& p) +{ + string name = fixKwd(p->name()); + string scope = p->scope(); + string scoped = p->scoped(); + ClassList bases = p->bases(); + + ClassList allBases = p->allBases(); + StringList ids; + transform(allBases.begin(), allBases.end(), back_inserter(ids), + ::IceUtil::memFun(&ClassDef::scoped)); + StringList other; + other.push_back(scoped); + other.push_back("::Ice::Object"); + other.sort(); + ids.merge(other); + ids.unique(); + + ClassList allBaseClasses; + ClassDefPtr cl; + if (!bases.empty()) + { + cl = bases.front(); + } + else + { + cl = 0; + } + while (cl && !cl->isInterface()) + { + allBaseClasses.push_back(cl); + ClassList baseBases = cl->bases(); + if (!baseBases.empty()) + { + cl = baseBases.front(); + } + else + { + cl = 0; + } + } + StringList classIds; + transform(allBaseClasses.begin(), allBaseClasses.end(), + back_inserter(classIds), + ::IceUtil::memFun(&ClassDef::scoped)); + classIds.push_front(scoped); + classIds.push_back("::Ice::Object"); + + StringList::const_iterator q; + + out << sp << nl << "public static final String[] __ids ="; + out << sb; + q = ids.begin(); + while (q != ids.end()) + { + out << nl << '"' << *q << '"'; + if (++q != ids.end()) + { + out << ','; + } + } + out << eb << ';'; + + out << sp << nl << "public static final String[] __classIds ="; + out << sb; + q = classIds.begin(); + while (q != classIds.end()) + { + out << nl << '"' << *q << '"'; + if (++q != classIds.end()) + { + out << ','; + } + } + out << eb << ';'; + + // + // ice_isA + // + out << sp << nl << "public boolean" + << nl << "ice_isA(String s, Ice.Current current)"; + out << sb; + out << nl << "return java.util.Arrays.binarySearch(__ids, s) >= 0;"; + out << eb; + + // + // __getClassIds + // + out << sp << nl << "public String[]" << nl << "__getClassIds()"; + out << sb; + out << nl << "return __classIds;"; + out << eb; + + // + // Dispatch operations + // + OperationList ops = p->operations(); + OperationList::const_iterator r; + for (r = ops.begin(); r != ops.end(); ++r) + { + OperationPtr op = (*r); + ContainerPtr container = op->container(); + ClassDefPtr cl = ClassDefPtr::dynamicCast(container); + assert(cl); + + string opName = fixKwd(op->name()); + out << sp << nl << "public static IceInternal.DispatchStatus" + << nl << "___" << opName << "(" << name + << " __obj, IceInternal.Incoming __in, Ice.Current __current)"; + out << sb; + + TypePtr ret = op->returnType(); + string retS = typeToString(ret, TypeModeReturn, scope); + int iter; + + TypeStringList inParams = op->inputParameters(); + TypeStringList outParams = op->outputParameters(); + TypeStringList::const_iterator q; + + ExceptionList throws = op->throws(); + throws.sort(); + throws.unique(); + + remove_if(throws.begin(), throws.end(), + ::IceUtil::memFun(&Exception::isLocal)); + + if (!inParams.empty()) + { + out << nl << "IceInternal.BasicStream __is = __in.is();"; + } + if (!outParams.empty() || ret || throws.size() > 0) + { + out << nl << "IceInternal.BasicStream __os = __in.os();"; + } + + // + // Unmarshal 'in' params + // + iter = 0; + for (q = inParams.begin(); q != inParams.end(); ++q) + { + string typeS = typeToString(q->first, TypeModeIn, scope); + out << nl << typeS << ' ' << fixKwd(q->second) << ';'; + writeMarshalUnmarshalCode(out, scope, q->first, fixKwd(q->second), + false, iter); + } + + // + // Create holders for 'out' params + // + for (q = outParams.begin(); q != outParams.end(); ++q) + { + string typeS = typeToString(q->first, TypeModeOut, scope); + out << nl << typeS << ' ' << fixKwd(q->second) << " = new " + << typeS << "();"; + } + + if (!throws.empty()) + { + out << nl << "try"; + out << sb; + } + + // + // Call servant + // + out << nl; + if (ret) + { + out << retS << " __ret = "; + } + out << "__obj." << opName << '('; + for (q = inParams.begin(); q != inParams.end(); ++q) + { + out << fixKwd(q->second) << ", "; + } + for (q = outParams.begin(); q != outParams.end(); ++q) + { + out << fixKwd(q->second) << ", "; + } + out << "__current);"; + + // + // Marshal 'out' params + // + for (q = outParams.begin(); q != outParams.end(); ++q) + { + writeMarshalUnmarshalCode(out, scope, q->first, fixKwd(q->second), + true, iter, true); + } + // + // Marshal result + // + if (ret) + { + writeMarshalUnmarshalCode(out, scope, ret, "__ret", true, iter); + } + + out << nl << "return IceInternal.DispatchStatus.DispatchOK;"; + + // + // User exceptions + // + if (!throws.empty()) + { + out << eb; + ExceptionList::const_iterator r; + for (r = throws.begin(); r != throws.end(); ++r) + { + string exS = getAbsolute((*r)->scoped(), scope); + out << nl << "catch (" << exS << " ex)"; + out << sb; + out << nl << "__os.writeUserException(ex);"; + out << nl << "return IceInternal.DispatchStatus." + << "DispatchUserException;"; + out << eb; + } + } + + out << eb; + } + + // + // __dispatch + // + OperationList allOps = p->allOperations(); + if (!allOps.empty()) + { + StringList allOpNames; + transform(allOps.begin(), allOps.end(), back_inserter(allOpNames), + ::IceUtil::memFun(&Operation::name)); + allOpNames.push_back("ice_isA"); + allOpNames.push_back("ice_ping"); + allOpNames.sort(); + allOpNames.unique(); + + StringList::const_iterator q; + + out << sp << nl << "private final static String[] __all ="; + out << sb; + q = allOpNames.begin(); + while (q != allOpNames.end()) + { + out << nl << '"' << *q << '"'; + if (++q != allOpNames.end()) + { + out << ','; + } + } + out << eb << ';'; + + out << sp << nl << "public IceInternal.DispatchStatus" + << nl << "__dispatch(IceInternal.Incoming in, Ice.Current current)"; + out << sb; + out << nl << "int pos = java.util.Arrays.binarySearch(__all, " + << "current.operation);"; + out << nl << "if (pos < 0)"; + out << sb; + out << nl << "return IceInternal.DispatchStatus." + << "DispatchOperationNotExist;"; + out << eb; + out << sp << nl << "switch (pos)"; + out << sb; + int i = 0; + for (q = allOpNames.begin(); q != allOpNames.end(); ++q) + { + string opName = fixKwd(*q); + + out << nl << "case " << i++ << ':'; + out << sb; + if (opName == "ice_isA") + { + out << nl << "return ___ice_isA(this, in, current);"; + } + else if (opName == "ice_ping") + { + out << nl << "return ___ice_ping(this, in, current);"; + } + else + { + // + // There's probably a better way to do this + // + for (OperationList::const_iterator r = allOps.begin(); + r != allOps.end(); + ++r) + { + if ((*r)->name() == (*q)) + { + ContainerPtr container = (*r)->container(); + ClassDefPtr cl = ClassDefPtr::dynamicCast(container); + assert(cl); + if (cl->name() == p->name()) + { + out << nl << "return ___" << opName + << "(this, in, current);"; + } + else + { + string base; + if (cl->isInterface()) + { + base = getAbsolute(cl->scoped(), scope, "_", + "Disp"); + } + else + { + base = getAbsolute(cl->scoped(), scope); + } + out << nl << "return " << base << ".___" << opName + << "(this, in, current);"; + } + break; + } + } + } + out << eb; + } + out << eb; + out << sp << nl << "assert(false);"; + out << nl << "return IceInternal.DispatchStatus." + << "DispatchOperationNotExist;"; + out << eb; + } +} + +void Slice::JavaVisitor::printHeader() { static const char* header = @@ -1257,99 +1584,10 @@ Slice::Gen::TypesVisitor::visitClassDefStart(const ClassDefPtr& p) if (!p->isInterface() && !p->isLocal()) { - ClassList allBases = p->allBases(); - StringList ids; - transform(allBases.begin(), allBases.end(), back_inserter(ids), - ::IceUtil::memFun(&ClassDef::scoped)); - StringList other; - other.push_back(scoped); - other.push_back("::Ice::Object"); - other.sort(); - ids.merge(other); - ids.unique(); - - ClassList allBaseClasses; - ClassDefPtr cl; - if (!bases.empty()) - { - cl = bases.front(); - } - else - { - cl = 0; - } - while (cl && !cl->isInterface()) - { - allBaseClasses.push_back(cl); - ClassList baseBases = cl->bases(); - if (!baseBases.empty()) - { - cl = baseBases.front(); - } - else - { - cl = 0; - } - } - StringList classIds; - transform(allBaseClasses.begin(), allBaseClasses.end(), - back_inserter(classIds), - ::IceUtil::memFun(&ClassDef::scoped)); - classIds.push_front(scoped); - classIds.push_back("::Ice::Object"); - - StringList::const_iterator q; - - out << sp << nl << "public static final String[] __ids ="; - out << sb; - q = ids.begin(); - while (q != ids.end()) - { - out << nl << '"' << *q << '"'; - if (++q != ids.end()) - { - out << ','; - } - } - out << eb << ';'; - - out << sp << nl << "public static final String[] __classIds ="; - out << sb; - q = classIds.begin(); - while (q != classIds.end()) - { - out << nl << '"' << *q << '"'; - if (++q != classIds.end()) - { - out << ','; - } - } - out << eb << ';'; - // - // ice_isA + // __dispatch, etc. // - out << sp << nl << "public boolean" - << nl << "ice_isA(String s, Ice.Current current)"; - out << sb; - out << nl << "return java.util.Arrays.binarySearch(__ids, s) >= 0;"; - out << eb; - - // - // __getClassIds - // - out << sp << nl << "public String[]" << nl << "__getClassIds()"; - out << sb; - out << nl << "return __classIds;"; - out << eb; - - // - // __dispatcher - // - out << sp << nl << "public Ice.Dispatcher" << nl << "__dispatcher()"; - out << sb; - out << nl << "return new _" << name << "Disp(this);"; - out << eb; + writeDispatch(out, p); DataMemberList members = p->dataMembers(); DataMemberList::const_iterator d; @@ -2938,7 +3176,7 @@ Slice::Gen::DispatcherVisitor::DispatcherVisitor(const string& dir, bool Slice::Gen::DispatcherVisitor::visitClassDefStart(const ClassDefPtr& p) { - if (p->isLocal()) + if (p->isLocal() || !p->isInterface()) { return false; } @@ -2956,263 +3194,11 @@ Slice::Gen::DispatcherVisitor::visitClassDefStart(const ClassDefPtr& p) Output& out = output(); - out << sp << nl << "public final class _" << name - << "Disp extends Ice._ObjectDisp"; - out << sb; - - out << nl << "public" - << nl << '_' << name << "Disp(" << name << " __d)"; + out << sp << nl << "public abstract class _" << name + << "Disp extends Ice.Object implements " << name; out << sb; - if (p->isInterface()) - { - out << nl << "super((Ice.Object)__d);"; - } - else - { - out << nl << "super(__d);"; - } - out << nl << "_delegate = __d;"; - out << eb; - - OperationList ops = p->operations(); - - OperationList::const_iterator r; - for (r = ops.begin(); r != ops.end(); ++r) - { - OperationPtr op = (*r); - ContainerPtr container = op->container(); - ClassDefPtr cl = ClassDefPtr::dynamicCast(container); - assert(cl); - - string opName = fixKwd(op->name()); - out << sp << nl << "public static IceInternal.DispatchStatus" - << nl << opName << "(" << name - << " __obj, IceInternal.Incoming __in, Ice.Current __current)"; - out << sb; - - TypePtr ret = op->returnType(); - string retS = typeToString(ret, TypeModeReturn, scope); - int iter; - - TypeStringList inParams = op->inputParameters(); - TypeStringList outParams = op->outputParameters(); - TypeStringList::const_iterator q; - - ExceptionList throws = op->throws(); - throws.sort(); - throws.unique(); - - remove_if(throws.begin(), throws.end(), - ::IceUtil::memFun(&Exception::isLocal)); - - if (!inParams.empty()) - { - out << nl << "IceInternal.BasicStream __is = __in.is();"; - } - if (!outParams.empty() || ret || throws.size() > 0) - { - out << nl << "IceInternal.BasicStream __os = __in.os();"; - } - - // - // Unmarshal 'in' params - // - iter = 0; - for (q = inParams.begin(); q != inParams.end(); ++q) - { - string typeS = typeToString(q->first, TypeModeIn, scope); - out << nl << typeS << ' ' << fixKwd(q->second) << ';'; - writeMarshalUnmarshalCode(out, scope, q->first, fixKwd(q->second), - false, iter); - } - - // - // Create holders for 'out' params - // - for (q = outParams.begin(); q != outParams.end(); ++q) - { - string typeS = typeToString(q->first, TypeModeOut, scope); - out << nl << typeS << ' ' << fixKwd(q->second) << " = new " - << typeS << "();"; - } - - if (!throws.empty()) - { - out << nl << "try"; - out << sb; - } - // - // Call servant - // - out << nl; - if (ret) - { - out << retS << " __ret = "; - } - out << "__obj." << opName << '('; - for (q = inParams.begin(); q != inParams.end(); ++q) - { - out << fixKwd(q->second) << ", "; - } - for (q = outParams.begin(); q != outParams.end(); ++q) - { - out << fixKwd(q->second) << ", "; - } - out << "__current);"; - - // - // Marshal 'out' params - // - for (q = outParams.begin(); q != outParams.end(); ++q) - { - writeMarshalUnmarshalCode(out, scope, q->first, fixKwd(q->second), - true, iter, true); - } - // - // Marshal result - // - if (ret) - { - writeMarshalUnmarshalCode(out, scope, ret, "__ret", true, iter); - } - - out << nl << "return IceInternal.DispatchStatus.DispatchOK;"; - - // - // User exceptions - // - if (!throws.empty()) - { - out << eb; - ExceptionList::const_iterator r; - for (r = throws.begin(); r != throws.end(); ++r) - { - string exS = getAbsolute((*r)->scoped(), scope); - out << nl << "catch (" << exS << " ex)"; - out << sb; - out << nl << "__os.writeUserException(ex);"; - out << nl << "return IceInternal.DispatchStatus." - << "DispatchUserException;"; - out << eb; - } - } - - out << eb; - } - - // - // __dispatch - // - OperationList allOps = p->allOperations(); - if (!allOps.empty()) - { - StringList allOpNames; - transform(allOps.begin(), allOps.end(), back_inserter(allOpNames), - ::IceUtil::memFun(&Operation::name)); - allOpNames.push_back("ice_isA"); - allOpNames.push_back("ice_ping"); - allOpNames.sort(); - allOpNames.unique(); - - StringList::const_iterator q; - - out << sp << nl << "private final static String[] __all ="; - out << sb; - q = allOpNames.begin(); - while (q != allOpNames.end()) - { - out << nl << '"' << *q << '"'; - if (++q != allOpNames.end()) - { - out << ','; - } - } - out << eb << ';'; - - out << sp << nl << "public IceInternal.DispatchStatus" - << nl << "__dispatch(IceInternal.Incoming in, Ice.Current current)"; - out << sb; - out << nl << "int pos = java.util.Arrays.binarySearch(__all, " - << "current.operation);"; - out << nl << "if (pos < 0)"; - out << sb; - out << nl << "return IceInternal.DispatchStatus." - << "DispatchOperationNotExist;"; - out << eb; - out << sp << nl << "switch (pos)"; - out << sb; - int i = 0; - for (q = allOpNames.begin(); q != allOpNames.end(); ++q) - { - string opName = fixKwd(*q); - - out << nl << "case " << i++ << ':'; - out << sb; - if (opName == "ice_isA") - { - if (p->isInterface()) - { - out << nl << "return ice_isA((Ice.Object)_delegate, in, " - << "current);"; - } - else - { - out << nl << "return ice_isA(_delegate, in, current);"; - } - } - else if (opName == "ice_ping") - { - if (p->isInterface()) - { - out << nl << "return ice_ping((Ice.Object)_delegate, in, " - << "current);"; - } - else - { - out << nl << "return ice_ping(_delegate, in, current);"; - } - } - else - { - // - // There's probably a better way to do this - // - for (OperationList::const_iterator r = allOps.begin(); - r != allOps.end(); - ++r) - { - if ((*r)->name() == (*q)) - { - ContainerPtr container = (*r)->container(); - ClassDefPtr cl = ClassDefPtr::dynamicCast(container); - assert(cl); - if (cl->name() == p->name()) - { - out << nl << "return " << opName - << "(_delegate, in, current);"; - } - else - { - out << nl << "return " - << getAbsolute(cl->scoped(), scope, "_", "Disp") - << '.' << opName - << "(_delegate, in, current);"; - } - break; - } - } - } - out << eb; - } - out << eb; - out << sp << nl << "assert(false);"; - out << nl << "return IceInternal.DispatchStatus." - << "DispatchOperationNotExist;"; - out << eb; - } - - out << sp << nl << "private " << name << " _delegate;"; + writeDispatch(out, p); out << eb; close(); @@ -3327,7 +3313,7 @@ Slice::Gen::ImplVisitor::writeAssign(Output& out, const string& scope, EnumPtr en = EnumPtr::dynamicCast(type); if (en) { - string typeS = getAbsolute(st->scoped(), scope); + string typeS = getAbsolute(en->scoped(), scope); EnumeratorList enumerators = en->getEnumerators(); out << nl << name << " = " << typeS << '.' << fixKwd(enumerators.front()->name()) << ';'; @@ -3376,6 +3362,11 @@ Slice::Gen::ImplVisitor::writeAssign(Output& out, const string& scope, bool Slice::Gen::ImplVisitor::visitClassDefStart(const ClassDefPtr& p) { + if (!p->isAbstract()) + { + return false; + } + string name = fixKwd(p->name()); string scoped = p->scoped(); ClassList bases = p->bases(); @@ -3392,7 +3383,14 @@ Slice::Gen::ImplVisitor::visitClassDefStart(const ClassDefPtr& p) out << sp << nl << "public final class " << name << 'I'; if (p->isInterface()) { - out << " implements " << name; + if (p->isLocal()) + { + out << " implements " << name; + } + else + { + out << " extends _" << name << "Disp"; + } } else { @@ -3404,15 +3402,6 @@ Slice::Gen::ImplVisitor::visitClassDefStart(const ClassDefPtr& p) out << sb; out << eb; - if (p->isInterface()) - { - out << sp << nl << "public Ice.Dispatcher" - << nl << "__dispatcher()"; - out << sb; - out << nl << "return new _" << name << "Disp(this);"; - out << eb; - } - OperationList ops = p->allOperations(); OperationList::const_iterator r; |