diff options
author | Mark Spruiell <mes@zeroc.com> | 2009-05-18 14:03:42 -0700 |
---|---|---|
committer | Mark Spruiell <mes@zeroc.com> | 2009-05-18 14:03:42 -0700 |
commit | b30ccc77d3a9822c6ffcebf9b45945822df200bc (patch) | |
tree | 94105ea42fa81ad0b8731b05a46c7f64304dec55 /cpp/src/slice2freezej/Main.cpp | |
parent | Removed Freeze.UseNonmutating (diff) | |
download | ice-b30ccc77d3a9822c6ffcebf9b45945822df200bc.tar.bz2 ice-b30ccc77d3a9822c6ffcebf9b45945822df200bc.tar.xz ice-b30ccc77d3a9822c6ffcebf9b45945822df200bc.zip |
bug 252 - Freeze finalizers
bug 2552 - Update Freeze for Java5
Diffstat (limited to 'cpp/src/slice2freezej/Main.cpp')
-rw-r--r-- | cpp/src/slice2freezej/Main.cpp | 628 |
1 files changed, 357 insertions, 271 deletions
diff --git a/cpp/src/slice2freezej/Main.cpp b/cpp/src/slice2freezej/Main.cpp index df70e3d8267..c2f6cbb5824 100644 --- a/cpp/src/slice2freezej/Main.cpp +++ b/cpp/src/slice2freezej/Main.cpp @@ -80,6 +80,7 @@ public: void generate(UnitPtr&, const Index&); private: + string typeToObjectString(const TypePtr&); string varToObject(const TypePtr&, const string&); string objectToVar(const TypePtr&, const string&); @@ -97,10 +98,39 @@ FreezeGenerator::~FreezeGenerator() } string +FreezeGenerator::typeToObjectString(const TypePtr& type) +{ + static const char* builtinTable[] = + { + "java.lang.Byte", + "java.lang.Boolean", + "java.lang.Short", + "java.lang.Integer", + "java.lang.Long", + "java.lang.Float", + "java.lang.Double", + "java.lang.String", + "Ice.Object", + "Ice.ObjectPrx", + "Ice.LocalObject" + }; + + BuiltinPtr b = BuiltinPtr::dynamicCast(type); + if(b) + { + return builtinTable[b->kind()]; + } + else + { + return typeToString(type, TypeModeIn); + } +} + +string FreezeGenerator::varToObject(const TypePtr& type, const string& param) { string result = param; - + BuiltinPtr b = BuiltinPtr::dynamicCast(type); if(b != 0) { @@ -151,11 +181,11 @@ FreezeGenerator::varToObject(const TypePtr& type, const string& param) return result; } -string +string FreezeGenerator::objectToVar(const TypePtr& type, const string& param) { - string result = string("((") + typeToString(type, TypeModeIn) + ")" + param + ")"; - + string result = param; + BuiltinPtr b = BuiltinPtr::dynamicCast(type); if(b != 0) { @@ -163,37 +193,37 @@ FreezeGenerator::objectToVar(const TypePtr& type, const string& param) { case Builtin::KindByte: { - result = string("((java.lang.Byte)") + param + ").byteValue()"; + result = param + ".byteValue()"; break; } case Builtin::KindBool: { - result = string("((java.lang.Boolean)") + param + ").booleanValue()"; + result = param + ".booleanValue()"; break; } case Builtin::KindShort: { - result = string("((java.lang.Short)") + param + ").shortValue()"; + result = param + ".shortValue()"; break; } case Builtin::KindInt: { - result = string("((java.lang.Integer)") + param + ").intValue()"; + result = param + ".intValue()"; break; } case Builtin::KindLong: { - result = string("((java.lang.Long)") + param + ").longValue()"; + result = param + ".longValue()"; break; } case Builtin::KindFloat: { - result = string("((java.lang.Float)") + param + ").floatValue()"; + result = param + ".floatValue()"; break; } case Builtin::KindDouble: { - result = string("((java.lang.Double)") + param + ").doubleValue()"; + result = param + ".doubleValue()"; break; } case Builtin::KindString: @@ -209,21 +239,9 @@ FreezeGenerator::objectToVar(const TypePtr& type, const string& param) void FreezeGenerator::generate(UnitPtr& u, const Dict& dict) { - static const char* builtinTable[] = - { - "java.lang.Byte", - "java.lang.Boolean", - "java.lang.Short", - "java.lang.Integer", - "java.lang.Long", - "java.lang.Float", - "java.lang.Double", - "java.lang.String", - "Ice.Object", - "Ice.ObjectPrx", - "Ice.LocalObject" - }; - + // + // The dictionary name may include a package. + // string name; string::size_type pos = dict.name.rfind('.'); if(pos == string::npos) @@ -243,7 +261,7 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) throw os.str(); } TypePtr keyType = keyTypes.front(); - + TypeList valueTypes = u->lookupType(dict.value, false); if(valueTypes.empty()) { @@ -254,10 +272,11 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) TypePtr valueType = valueTypes.front(); vector<TypePtr> indexTypes; + vector<string> members; vector<string> capitalizedMembers; vector<string> indexNames; size_t i; - + for(i = 0; i < dict.indices.size(); ++i) { const DictIndex& index = dict.indices[i]; @@ -265,6 +284,10 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) if(index.member.empty()) { + // + // No member was specified, which means we use the map's value type as the index key. + // + if(dict.indices.size() > 1) { ostringstream os; @@ -288,19 +311,18 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) if(index.caseSensitive == false) { // - // Let's check value is a string + // Verify that value type is a string. // - - BuiltinPtr builtInType = BuiltinPtr::dynamicCast(valueType); - - if(builtInType == 0 || builtInType->kind() != Builtin::KindString) + BuiltinPtr b = BuiltinPtr::dynamicCast(valueType); + if(b == 0 || b->kind() != Builtin::KindString) { ostringstream os; - os << "VALUE is a `" << dict.value << "', not a string " << endl; + os << "VALUE is a `" << dict.value << "', not a string" << endl; throw os.str(); } } indexTypes.push_back(valueType); + members.push_back("value"); capitalizedMembers.push_back("Value"); indexNames.push_back("index"); } @@ -308,7 +330,7 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) { DataMemberPtr dataMember = 0; DataMemberList dataMembers; - + ClassDeclPtr classDecl = ClassDeclPtr::dynamicCast(valueType); if(classDecl != 0) { @@ -320,11 +342,12 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) if(structDecl == 0) { ostringstream os; - os << "`" << dict.value << "' is neither a class nor a struct." << endl; + os << "`" << dict.value << "' is neither a class nor a struct" << endl; throw os.str(); } dataMembers = structDecl->dataMembers(); } + DataMemberList::const_iterator q = dataMembers.begin(); while(q != dataMembers.end() && dataMember == 0) { @@ -337,22 +360,21 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) ++q; } } - + if(dataMember == 0) { ostringstream os; - os << "The value of `" << dict.name - << "' has no data member named `" << index.member << "'" << endl; + os << "The value of `" << dict.name << "' has no data member named `" << index.member << "'" << endl; throw os.str(); } - + TypePtr dataMemberType = dataMember->type(); - + bool containsSequence = false; if(!Dictionary::legalKeyType(dataMemberType, containsSequence)) { ostringstream os; - os << "`" << index.member << "' cannot be used as an index" << endl; + os << "`" << index.member << "' cannot be used as an index key" << endl; throw os.str(); } if(containsSequence) @@ -364,18 +386,19 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) if(index.caseSensitive == false) { // - // Let's check member is a string + // Verify that member type is a string. // - BuiltinPtr memberType = BuiltinPtr::dynamicCast(dataMemberType); - if(memberType == 0 || memberType->kind() != Builtin::KindString) + BuiltinPtr b = BuiltinPtr::dynamicCast(dataMemberType); + if(b == 0 || b->kind() != Builtin::KindString) { ostringstream os; - os << "`" << index.member << "' is not a string " << endl; + os << "`" << index.member << "' is not a string" << endl; throw os.str(); } } indexTypes.push_back(dataMemberType); + members.push_back(member); string capitalizedMember = member; capitalizedMember[0] = toupper(static_cast<unsigned char>(capitalizedMember[0])); capitalizedMembers.push_back(capitalizedMember); @@ -387,45 +410,90 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) Output& out = output(); - out << sp << nl << "public class " << name << " extends Freeze.Map"; + string keyTypeS = typeToObjectString(keyType); + string valueTypeS = typeToObjectString(valueType); + + out << sp << nl << "public class " << name << " extends Freeze.MapInternal.MapI<" << keyTypeS << ", " + << valueTypeS << ">"; out << sb; + if(dict.indices.size() > 0) + { + out << sp << nl << "public static class IndexComparators"; + out << sb; + out << sp << nl << "public" << nl << "IndexComparators()"; + out << sb; + out << eb; + + out << sp << nl << "public" << nl << "IndexComparators("; + for(i = 0; i < dict.indices.size(); ++i) + { + if(i > 0) + { + out << ", "; + } + out << "java.util.Comparator<" << typeToObjectString(indexTypes[i]) << "> " << members[i] + << "Comparator"; + } + out << ")"; + out << sb; + for(i = 0; i < dict.indices.size(); ++i) + { + out << nl << "this." << members[i] << "Comparator = " << members[i] << "Comparator;"; + } + out << eb; + + out << sp; + for(i = 0; i < dict.indices.size(); ++i) + { + out << nl << "public java.util.Comparator<" << typeToObjectString(indexTypes[i]) << "> " << members[i] + << "Comparator;"; + } + out << eb; + } + // // Constructors // - - out << sp << nl << "private" << nl << name - << "(Freeze.Connection __connection, String __dbName, java.util.Comparator __comparator)"; + + out << sp << nl << "private" << nl << name + << "(Freeze.Connection __connection, String __dbName, java.util.Comparator<" << keyTypeS << "> __comparator"; + if(dict.indices.size() > 0) + { + out << ", IndexComparators __indexComparators"; + } + out << ")"; out << sb; - + out << nl << "super(__connection, __dbName, __comparator);"; if(dict.indices.size() > 0) { - out << nl << "_indices = new Freeze.Map.Index[" << dict.indices.size() << "];"; + out << nl << "_indices = new Freeze.MapIndex[" << dict.indices.size() << "];"; for(i = 0; i < dict.indices.size(); ++i) { - out << nl << "_indices[" << i << "] = new " << capitalizedMembers[i] - << "Index(\"" << indexNames[i] << "\");"; + out << nl << "_" << members[i] << "Index = new " << capitalizedMembers[i] << "Index(\"" << indexNames[i] + << "\", __indexComparators == null ? null : __indexComparators." << members[i] << "Comparator);"; + out << nl << "_indices[" << i << "] = _" << members[i] << "Index;"; } } out << eb; if(dict.indices.size() > 0) - { - out << sp << nl << "public" << nl << name + { + out << sp << nl << "public" << nl << name << "(Freeze.Connection __connection, String __dbName, boolean __createDb, " - << "java.util.Comparator __comparator, java.util.Map __indexComparators)"; + << "java.util.Comparator<" << keyTypeS << "> __comparator, " + << "IndexComparators __indexComparators)"; out << sb; - - out << nl << "this(__connection, __dbName, __comparator);"; - out << nl << "init(_indices, __dbName, \"" << keyType->typeId() << "\", \"" - << valueType->typeId() << "\", __createDb, __indexComparators);"; + out << nl << "this(__connection, __dbName, __comparator, __indexComparators);"; + out << nl << "init(_indices, __dbName, \"" << keyType->typeId() << "\", \"" << valueType->typeId() + << "\", __createDb);"; out << eb; } - out << sp << nl << "public" << nl << name + out << sp << nl << "public" << nl << name << "(Freeze.Connection __connection, String __dbName, boolean __createDb, " - << "java.util.Comparator __comparator)"; + << "java.util.Comparator<" << keyTypeS << "> __comparator)"; out << sb; if(dict.indices.size() > 0) { @@ -437,15 +505,14 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) << valueType->typeId() << "\", __createDb, __comparator);"; } out << eb; - - out << sp << nl << "public" << nl << name + + out << sp << nl << "public" << nl << name << "(Freeze.Connection __connection, String __dbName, boolean __createDb)"; out << sb; out << nl << "this(__connection, __dbName, __createDb, null);"; out << eb; - out << sp << nl << "public" << nl << name - << "(Freeze.Connection __connection, String __dbName)"; + out << sp << nl << "public" << nl << name << "(Freeze.Connection __connection, String __dbName)"; out << sb; out << nl << "this(__connection, __dbName, true);"; out << eb; @@ -455,20 +522,21 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) // if(dict.indices.size() > 0) { - out << sp << nl << "public static void" << nl + out << sp << nl << "public static void" << nl << "recreate(Freeze.Connection __connection, String __dbName, " - << "java.util.Comparator __comparator, java.util.Map __indexComparators)"; + << "java.util.Comparator<" << keyTypeS << "> __comparator, " + << "IndexComparators __indexComparators)"; out << sb; - - out << nl << name << " __tmpMap = new " << name << "(__connection, __dbName, __comparator);"; + out << nl << name << " __tmpMap = new " << name + << "(__connection, __dbName, __comparator, __indexComparators);"; out << nl << "recreate(__tmpMap, __dbName, \"" << keyType->typeId() << "\", \"" - << valueType->typeId() << "\", __tmpMap._indices, __indexComparators);"; + << valueType->typeId() << "\", __tmpMap._indices);"; out << eb; } - out << sp << nl << "public static void" << nl + out << sp << nl << "public static void" << nl << "recreate(Freeze.Connection __connection, String __dbName, " - << "java.util.Comparator __comparator)"; + << "java.util.Comparator<" << keyTypeS << "> __comparator)"; out << sb; if(dict.indices.size() > 0) { @@ -478,44 +546,92 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) { out << nl << name << " __tmpMap = new " << name << "(__connection, __dbName, __comparator);"; out << nl << "recreate(__tmpMap, __dbName, \"" << keyType->typeId() << "\", \"" - << valueType->typeId() << "\", null, null);"; + << valueType->typeId() << "\", null);"; } out << eb; // - // findBy and count methods - // + // Index methods + // for(i = 0; i < capitalizedMembers.size(); ++i) { string indexClassName = capitalizedMembers[i] + "Index"; + string indexTypeS = typeToString(indexTypes[i], TypeModeIn); + string indexObjTypeS = typeToObjectString(indexTypes[i]); + string indexObj = varToObject(indexTypes[i], "__key"); - out << sp << nl << "public Freeze.Map.EntryIterator"; - out << nl << "findBy" << capitalizedMembers[i] << "(" - << typeToString(indexTypes[i], TypeModeIn) << " __index, boolean __onlyDups)"; + out << sp << nl << "public Freeze.Map.EntryIterator<java.util.Map.Entry<" << keyTypeS << ", " << valueTypeS + << ">>"; + out << nl << "findBy" << capitalizedMembers[i] << "(" << indexTypeS << " __key, boolean __onlyDups)"; out << sb; - out << nl << "return _indices[" << i << "].untypedFind(" - << varToObject(indexTypes[i], "__index") << ", __onlyDups);"; + out << nl << "return _" << members[i] << "Index.find(" << indexObj << ", __onlyDups);"; out << eb; - out << sp << nl << "public Freeze.Map.EntryIterator"; - out << nl << "findBy" << capitalizedMembers[i] << "(" - << typeToString(indexTypes[i], TypeModeIn) << " __index)"; + out << sp << nl << "public Freeze.Map.EntryIterator<java.util.Map.Entry<" << keyTypeS << ", " << valueTypeS + << ">>"; + out << nl << "findBy" << capitalizedMembers[i] << "(" << indexTypeS << " __key)"; out << sb; - out << nl << "return _indices[" << i << "].untypedFind(" - << varToObject(indexTypes[i], "__index") << ", true);"; + out << nl << "return _" << members[i] << "Index.find(" << indexObj << ", true);"; out << eb; - - string countMethod = dict.indices[i].member.empty() ? - string("valueCount") : dict.indices[i].member + "Count"; + + string countMethod = dict.indices[i].member.empty() ? string("valueCount") : dict.indices[i].member + "Count"; out << sp << nl << "public int"; - out << nl << countMethod << "(" - << typeToString(indexTypes[i], TypeModeIn) << " __index)"; + out << nl << countMethod << "(" << indexTypeS << " __key)"; + out << sb; + out << nl << "return _" << members[i] << "Index.count(" << indexObj << ");"; + out << eb; + + string subMap = "Freeze.NavigableMap<" + indexObjTypeS + ", java.util.Set<java.util.Map.Entry<" + keyTypeS + + ", " + valueTypeS + ">>>"; + + out << sp << nl << "public " + subMap; + out << nl << "headMapFor" << capitalizedMembers[i] << "(" << indexTypeS << " __toKey, boolean __inclusive)"; + out << sb; + out << nl << "return _" << members[i] << "Index.createHeadMap(" << varToObject(indexTypes[i], "__toKey") + << ", __inclusive);"; + out << eb; + + out << sp << nl << "public " + subMap; + out << nl << "headMapFor" << capitalizedMembers[i] << "(" << indexTypeS << " __toKey)"; + out << sb; + out << nl << "return headMapFor" << capitalizedMembers[i] << "(__toKey, false);"; + out << eb; + + out << sp << nl << "public " + subMap; + out << nl << "tailMapFor" << capitalizedMembers[i] << "(" << indexTypeS << " __fromKey, boolean __inclusive)"; + out << sb; + out << nl << "return _" << members[i] << "Index.createTailMap(" << varToObject(indexTypes[i], "__fromKey") + << ", __inclusive);"; + out << eb; + + out << sp << nl << "public " + subMap; + out << nl << "tailMapFor" << capitalizedMembers[i] << "(" << indexTypeS << " __fromKey)"; + out << sb; + out << nl << "return tailMapFor" << capitalizedMembers[i] << "(__fromKey, true);"; + out << eb; + + out << sp << nl << "public " + subMap; + out << nl << "subMapFor" << capitalizedMembers[i] << "(" << indexTypeS + << " __fromKey, boolean __fromInclusive, " << indexTypeS << " __toKey, boolean __toInclusive)"; out << sb; - out << nl << "return _indices[" << i << "].untypedCount(" - << varToObject(indexTypes[i], "__index") << ");"; + out << nl << "return _" << members[i] << "Index.createSubMap(" << varToObject(indexTypes[i], "__fromKey") + << ", __fromInclusive, " << varToObject(indexTypes[i], "__toKey") << ", __toInclusive);"; + out << eb; + + out << sp << nl << "public " + subMap; + out << nl << "subMapFor" << capitalizedMembers[i] << "(" << indexTypeS << " __fromKey, " << indexTypeS + << " __toKey)"; + out << sb; + out << nl << "return subMapFor" << capitalizedMembers[i] << "(__fromKey, true, __toKey, false);"; + out << eb; + + out << sp << nl << "public " + subMap; + out << nl << "mapFor" << capitalizedMembers[i] << "()"; + out << sb; + out << nl << "return _" << members[i] << "Index.createMap();"; out << eb; } - + // // Top-level encode/decode // @@ -524,45 +640,33 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) string keyValue; TypePtr type; bool encaps; + string typeS; if(i == 0) { keyValue = "Key"; type = keyType; - // - // Do not encapsulate keys. - // - encaps = false; + typeS = keyTypeS; + encaps = false; // Do not encapsulate keys. } else { keyValue = "Value"; type = valueType; + typeS = valueTypeS; encaps = true; } - string valS = objectToVar(type, "o"); - string typeS; - - BuiltinPtr b = BuiltinPtr::dynamicCast(type); - if(b != 0) - { - typeS = builtinTable[b->kind()]; - } - else - { - typeS = typeToString(type, TypeModeIn); - } + string valS = objectToVar(type, "v"); int iter; // // encode // - out << sp << nl << "public byte[]" << nl << "encode" << keyValue - << "(Object o, Ice.Communicator communicator)"; + out << sp << nl << "public byte[]" << nl << "encode" << keyValue << "(" << typeS + << " v, Ice.Communicator communicator)"; out << sb; - out << nl << "assert(o instanceof " << typeS << ");"; out << nl << "IceInternal.BasicStream __os = " << "new IceInternal.BasicStream(IceInternal.Util.getInstance(communicator));"; if(encaps) @@ -588,10 +692,11 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) // // decode // - out << sp << nl << "public Object" << nl << "decode" << keyValue + out << sp << nl << "public " << typeS << nl << "decode" << keyValue << "(byte[] b, Ice.Communicator communicator)"; out << sb; - out << nl << "IceInternal.BasicStream __is = new IceInternal.BasicStream(IceInternal.Util.getInstance(communicator));"; + out << nl << "IceInternal.BasicStream __is = " + << "new IceInternal.BasicStream(IceInternal.Util.getInstance(communicator));"; if(type->usesClasses()) { out << nl << "__is.sliceObjects(false);"; @@ -608,6 +713,7 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) iter = 0; list<string> metaData; string patchParams; + BuiltinPtr b = BuiltinPtr::dynamicCast(type); if((b && b->kind() == Builtin::KindObject) || ClassDeclPtr::dynamicCast(type)) { out << nl << "Patcher __p = new Patcher();"; @@ -621,49 +727,49 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) { switch(b->kind()) { - case Builtin::KindByte: - { - out << nl << "__r = new java.lang.Byte(__is.readByte());"; - break; - } - case Builtin::KindBool: - { - out << nl << "__r = new java.lang.Boolean(__is.readBool());"; - break; - } - case Builtin::KindShort: - { - out << nl << "__r = new java.lang.Short(__is.readShort());"; - break; - } - case Builtin::KindInt: - { - out << nl << "__r = new java.lang.Integer(__is.readInt());"; - break; - } - case Builtin::KindLong: - { - out << nl << "__r = new java.lang.Long(__is.readLong());"; - break; - } - case Builtin::KindFloat: - { - out << nl << "__r = new java.lang.Float(__is.readFloat());"; - break; - } - case Builtin::KindDouble: - { - out << nl << "__r = new java.lang.Double(__is.readDouble());"; - break; - } - case Builtin::KindString: - case Builtin::KindObject: - case Builtin::KindObjectProxy: - case Builtin::KindLocalObject: - { - writeMarshalUnmarshalCode(out, "", type, "__r", false, iter, false, metaData, patchParams); - break; - } + case Builtin::KindByte: + { + out << nl << "__r = new java.lang.Byte(__is.readByte());"; + break; + } + case Builtin::KindBool: + { + out << nl << "__r = new java.lang.Boolean(__is.readBool());"; + break; + } + case Builtin::KindShort: + { + out << nl << "__r = new java.lang.Short(__is.readShort());"; + break; + } + case Builtin::KindInt: + { + out << nl << "__r = new java.lang.Integer(__is.readInt());"; + break; + } + case Builtin::KindLong: + { + out << nl << "__r = new java.lang.Long(__is.readLong());"; + break; + } + case Builtin::KindFloat: + { + out << nl << "__r = new java.lang.Float(__is.readFloat());"; + break; + } + case Builtin::KindDouble: + { + out << nl << "__r = new java.lang.Double(__is.readDouble());"; + break; + } + case Builtin::KindString: + case Builtin::KindObject: + case Builtin::KindObjectProxy: + case Builtin::KindLocalObject: + { + writeMarshalUnmarshalCode(out, "", type, "__r", false, iter, false, metaData, patchParams); + break; + } } } else @@ -689,21 +795,23 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) out << eb; } - // // Inner index classes // for(i = 0; i < capitalizedMembers.size(); ++i) { string indexClassName = capitalizedMembers[i] + "Index"; - out << sp << nl << "private class " << indexClassName << " extends Freeze.Map.Index"; + string indexKeyTypeS = typeToObjectString(indexTypes[i]); + + out << sp << nl << "private class " << indexClassName << " extends Freeze.MapInternal.Index<" << keyTypeS + << ", " << valueTypeS << ", " << indexKeyTypeS << ">"; out << sb; // // encodeKey // out << sp << nl << "public byte[]"; - out << nl << "encodeKey(Object key, Ice.Communicator communicator)"; + out << nl << "encodeKey(" << indexKeyTypeS << " key, Ice.Communicator communicator)"; out << sb; if(dict.indices[i].member.empty()) { @@ -713,7 +821,7 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) string keyS = "key"; if(!dict.indices[i].caseSensitive) { - keyS = "((String)key).toLowerCase()"; + keyS = "key.toLowerCase()"; } out << nl << "return encodeValue(" << keyS << ", communicator);"; @@ -723,8 +831,7 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) // // No encaps // - string keyS = dict.indices[i].caseSensitive ? - "key" : "((String)key).toLowerCase()"; + string keyS = dict.indices[i].caseSensitive ? "key" : "key.toLowerCase()"; keyS = objectToVar(indexTypes[i], keyS); @@ -742,9 +849,9 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) out << eb; // - // decodekey + // decodeKey // - out << sp << nl << "public Object"; + out << sp << nl << "public " << indexKeyTypeS; out << nl << "decodeKey(byte[] bytes, Ice.Communicator communicator)"; out << sb; if(dict.indices[i].member.empty()) @@ -756,76 +863,68 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) } else { - out << nl << "IceInternal.BasicStream __is = new IceInternal.BasicStream(IceInternal.Util.getInstance(communicator));"; + out << nl << "IceInternal.BasicStream __is = " + << "new IceInternal.BasicStream(IceInternal.Util.getInstance(communicator));"; out << nl << "__is.resize(bytes.length, true);"; out << nl << "IceInternal.Buffer buf = __is.getBuffer();"; out << nl << "buf.b.position(0);"; out << nl << "buf.b.put(bytes);"; out << nl << "buf.b.position(0);"; - + int iter = 0; list<string> metaData; string patchParams; - string typeS; + out << nl << indexKeyTypeS << " r;"; + BuiltinPtr b = BuiltinPtr::dynamicCast(indexTypes[i]); if(b != 0) { - typeS = builtinTable[b->kind()]; - } - else - { - typeS = typeToString(indexTypes[i], TypeModeIn); - } - out << nl << typeS << " r;"; - - if(b != 0) - { switch(b->kind()) { - case Builtin::KindByte: - { - out << nl << "r = new java.lang.Byte(__is.readByte());"; - break; - } - case Builtin::KindBool: - { - out << nl << "r = new java.lang.Boolean(__is.readBool());"; - break; - } - case Builtin::KindShort: - { - out << nl << "r = new java.lang.Short(__is.readShort());"; - break; - } - case Builtin::KindInt: - { - out << nl << "r = new java.lang.Integer(__is.readInt());"; - break; - } - case Builtin::KindLong: - { - out << nl << "r = new java.lang.Long(__is.readLong());"; - break; - } - case Builtin::KindFloat: - { - out << nl << "r = new java.lang.Float(__is.readFloat());"; - break; - } - case Builtin::KindDouble: - { - out << nl << "r = new java.lang.Double(__is.readDouble());"; - break; - } - case Builtin::KindString: - case Builtin::KindObject: - case Builtin::KindObjectProxy: - case Builtin::KindLocalObject: - { - writeMarshalUnmarshalCode(out, "", indexTypes[i], "r", false, iter, false, metaData, patchParams); - break; - } + case Builtin::KindByte: + { + out << nl << "r = new java.lang.Byte(__is.readByte());"; + break; + } + case Builtin::KindBool: + { + out << nl << "r = new java.lang.Boolean(__is.readBool());"; + break; + } + case Builtin::KindShort: + { + out << nl << "r = new java.lang.Short(__is.readShort());"; + break; + } + case Builtin::KindInt: + { + out << nl << "r = new java.lang.Integer(__is.readInt());"; + break; + } + case Builtin::KindLong: + { + out << nl << "r = new java.lang.Long(__is.readLong());"; + break; + } + case Builtin::KindFloat: + { + out << nl << "r = new java.lang.Float(__is.readFloat());"; + break; + } + case Builtin::KindDouble: + { + out << nl << "r = new java.lang.Double(__is.readDouble());"; + break; + } + case Builtin::KindString: + case Builtin::KindObject: + case Builtin::KindObjectProxy: + case Builtin::KindLocalObject: + { + writeMarshalUnmarshalCode(out, "", indexTypes[i], "r", false, iter, false, metaData, patchParams); + break; + } } } else @@ -837,27 +936,10 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) out << eb; // - // compare + // extractKey // - out << sp << nl << "public int"; - out << nl << "compare(Object o1, Object o2)"; - out << sb; - out << nl << "assert _comparator != null;"; - out << nl << "byte[] d1 = (byte[])o1;"; - out << nl << "byte[] d2 = (byte[])o2;"; - out << nl << "Ice.Communicator communicator = ((Freeze.Connection)_connection).getCommunicator();"; - out << nl << "return _comparator.compare("; - out.inc(); - out << nl << "decodeKey(d1, communicator),"; - out << nl << "decodeKey(d2, communicator));"; - out.dec(); - out << eb; - - // - // extractKey from value - // - out << sp << nl << "public Object"; - out << nl << "extractKey(Object value)"; + out << sp << nl << "protected " << indexKeyTypeS; + out << nl << "extractKey(" << valueTypeS << " value)"; out << sb; if(dict.indices[i].member.empty()) { @@ -872,11 +954,7 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) } else { - out << nl << typeToString(valueType, TypeModeIn) - << " typedValue = (" - << typeToString(valueType, TypeModeIn) << ")value;"; - - string member = string("typedValue.") + dict.indices[i].member; + string member = "value." + dict.indices[i].member; if(!dict.indices[i].caseSensitive) { member += ".toLowerCase()"; @@ -884,7 +962,7 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) out << nl << "return " << varToObject(indexTypes[i], member) << ";"; } out << eb; - + // // marshalKey optimization // @@ -897,9 +975,13 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) out << eb; } - out << sp << nl << "private " << indexClassName << "(String name)"; + // + // Constructor + // + out << sp << nl << "private" << nl << indexClassName << "(String name, java.util.Comparator<" << indexKeyTypeS + << "> comparator)"; out << sb; - out << nl << "super(name);"; + out << nl << "super(" << name << ".this, name, comparator);"; out << eb; out << eb; } @@ -943,7 +1025,11 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) // // Fields // - out << sp << nl << "private Freeze.Map.Index[] _indices;"; + out << sp << nl << "private Freeze.MapIndex[] _indices;"; + for(i = 0; i < dict.indices.size(); ++i) + { + out << nl << "private " << capitalizedMembers[i] << "Index _" << members[i] << "Index;"; + } out << eb; @@ -1002,7 +1088,7 @@ FreezeGenerator::generate(UnitPtr& u, const Index& index) os << "`" << index.type << "' has no data member named `" << index.member << "'" << endl; throw os.str(); } - + if(index.caseSensitive == false) { // @@ -1018,7 +1104,7 @@ FreezeGenerator::generate(UnitPtr& u, const Index& index) } string memberTypeString = typeToString(dataMember->type(), TypeModeIn); - + open(index.name); Output& out = output(); @@ -1042,19 +1128,19 @@ FreezeGenerator::generate(UnitPtr& u, const Index& index) // // find and count // - out << sp << nl << "public Ice.Identity[]" << nl + out << sp << nl << "public Ice.Identity[]" << nl << "findFirst(" << memberTypeString << " __index, int __firstN)"; out << sb; out << nl << "return untypedFindFirst(marshalKey(__index), __firstN);"; out << eb; - out << sp << nl << "public Ice.Identity[]" << nl + out << sp << nl << "public Ice.Identity[]" << nl << "find(" << memberTypeString << " __index)"; out << sb; out << nl << "return untypedFind(marshalKey(__index));"; out << eb; - - out << sp << nl << "public int" << nl + + out << sp << nl << "public int" << nl << "count(" << memberTypeString << " __index)"; out << sb; out << nl << "return untypedCount(marshalKey(__index));"; @@ -1065,12 +1151,12 @@ FreezeGenerator::generate(UnitPtr& u, const Index& index) // string typeString = typeToString(type, TypeModeIn); - out << sp << nl << "protected byte[]" << nl + out << sp << nl << "protected byte[]" << nl << "marshalKey(Ice.Object __servant)"; out << sb; out << nl << "if(__servant instanceof " << typeString << ")"; out << sb; - out << nl << memberTypeString << " __key = ((" << typeString << ")__servant)." << index.member << ";"; + out << nl << memberTypeString << " __key = ((" << typeString << ")__servant)." << index.member << ";"; out << nl << "return marshalKey(__key);"; out << eb; out << nl << "else"; @@ -1078,10 +1164,10 @@ FreezeGenerator::generate(UnitPtr& u, const Index& index) out << nl << "return null;"; out << eb; out << eb; - + string valueS = index.caseSensitive ? "__key" : "__key.toLowerCase()"; - out << sp << nl << "private byte[]" << nl + out << sp << nl << "private byte[]" << nl << "marshalKey(" << memberTypeString << " __key)"; out << sb; out << nl << "IceInternal.BasicStream __os = " @@ -1121,7 +1207,7 @@ usage(const char* n) " using KEY as key, and VALUE as value. This\n" " option may be specified multiple times for\n" " different names. NAME may be a scoped name.\n" - "--index NAME,TYPE,MEMBER[,{case-sensitive|case-insensitive}]\n" + "--index NAME,TYPE,MEMBER[,{case-sensitive|case-insensitive}]\n" " Create a Freeze evictor index with the name\n" " NAME for member MEMBER of class TYPE. This\n" " option may be specified multiple times for\n" @@ -1163,7 +1249,7 @@ main(int argc, char* argv[]) opts.addOpt("d", "debug"); opts.addOpt("", "ice"); opts.addOpt("", "meta", IceUtilInternal::Options::NeedArg, "", IceUtilInternal::Options::Repeat); - + vector<string> args; try { @@ -1220,7 +1306,7 @@ main(int argc, char* argv[]) for(i = optargs.begin(); i != optargs.end(); ++i) { string s = IceUtilInternal::removeWhitespace(*i); - + Dict dict; string::size_type pos; @@ -1267,7 +1353,7 @@ main(int argc, char* argv[]) for(i = optargs.begin(); i != optargs.end(); ++i) { string s = IceUtilInternal::removeWhitespace(*i); - + Index index; string::size_type pos; @@ -1317,7 +1403,7 @@ main(int argc, char* argv[]) usage(argv[0]); return EXIT_FAILURE; } - + if(caseString != "case-sensitive" && caseString != "case-insensitive") { getErrorStream() << argv[0] << ": error: " << *i << ": the case can be `case-sensitive' or " @@ -1336,11 +1422,11 @@ main(int argc, char* argv[]) for(vector<string>::const_iterator i = optargs.begin(); i != optargs.end(); ++i) { string s = IceUtilInternal::removeWhitespace(*i); - + string dictName; DictIndex index; string::size_type pos; - + string caseString = "case-sensitive"; pos = s.find(','); if(pos != string::npos) @@ -1505,7 +1591,7 @@ main(int argc, char* argv[]) { u->destroy(); return EXIT_FAILURE; - } + } } { @@ -1600,7 +1686,7 @@ main(int argc, char* argv[]) } } - + u->destroy(); { |