diff options
author | Mark Spruiell <mes@zeroc.com> | 2002-05-09 23:09:07 +0000 |
---|---|---|
committer | Mark Spruiell <mes@zeroc.com> | 2002-05-09 23:09:07 +0000 |
commit | f0c11a26b5c99a7e21782c0e39c596874a842857 (patch) | |
tree | 1aacf3d3d952aa1edd0fea66ccb447488f05bc2f /cpp/src | |
parent | fixing trace message (diff) | |
download | ice-f0c11a26b5c99a7e21782c0e39c596874a842857.tar.bz2 ice-f0c11a26b5c99a7e21782c0e39c596874a842857.tar.xz ice-f0c11a26b5c99a7e21782c0e39c596874a842857.zip |
adding --clone, which generates a simple clone() implementation for
non-abstract classes and structs
Diffstat (limited to 'cpp/src')
-rw-r--r-- | cpp/src/slice2java/Gen.cpp | 126 | ||||
-rw-r--r-- | cpp/src/slice2java/Gen.h | 10 | ||||
-rw-r--r-- | cpp/src/slice2java/Main.cpp | 13 |
3 files changed, 131 insertions, 18 deletions
diff --git a/cpp/src/slice2java/Gen.cpp b/cpp/src/slice2java/Gen.cpp index a27a06ed821..92a2ae38f03 100644 --- a/cpp/src/slice2java/Gen.cpp +++ b/cpp/src/slice2java/Gen.cpp @@ -595,11 +595,12 @@ Slice::JavaVisitor::writeDispatch(Output& out, const ClassDefPtr& p) } Slice::Gen::Gen(const string& name, const string& base, const vector<string>& includePaths, const string& package, - const string& dir) : + const string& dir, bool clone) : _base(base), _includePaths(includePaths), _package(package), - _dir(dir) + _dir(dir), + _clone(clone) { } @@ -619,7 +620,7 @@ Slice::Gen::generate(const UnitPtr& unit) OpsVisitor opsVisitor(_dir, _package); unit->visit(&opsVisitor); - TypesVisitor typesVisitor(_dir, _package); + TypesVisitor typesVisitor(_dir, _package, _clone); unit->visit(&typesVisitor); HolderVisitor holderVisitor(_dir, _package); @@ -917,8 +918,10 @@ Slice::Gen::TieVisitor::visitClassDefStart(const ClassDefPtr& p) } Slice::Gen::TypesVisitor::TypesVisitor(const string& dir, - const string& package) : - JavaVisitor(dir, package) + const string& package, + bool clone) : + JavaVisitor(dir, package), + _clone(clone) { } @@ -985,24 +988,44 @@ Slice::Gen::TypesVisitor::visitClassDefStart(const ClassDefPtr& p) bases.pop_front(); } - if (p->isAbstract() || !bases.empty()) + // + // Implement interfaces + // + StringList implements; + if (p->isAbstract()) + { + implements.push_back(name + "Operations"); + } + if (!bases.empty()) + { + ClassList::const_iterator q = bases.begin(); + while (q != bases.end()) + { + implements.push_back(getAbsolute((*q)->scoped(), scope)); + q++; + } + } + if (_clone && !p->isAbstract()) + { + implements.push_back("java.lang.Cloneable"); + } + + if (!implements.empty()) { out << nl << " implements "; out.useCurrentPosAsIndent(); - out << name << "Operations"; - // - // Implement interfaces - // - if (!bases.empty()) + StringList::const_iterator q = implements.begin(); + while (q != implements.end()) { - ClassList::const_iterator q = bases.begin(); - while (q != bases.end()) + if (q != implements.begin()) { - out << ',' << nl << getAbsolute((*q)->scoped(), scope); - q++; + out << nl << ','; } + out << *q; + q++; } + out.restoreIndent(); } @@ -1061,6 +1084,49 @@ Slice::Gen::TypesVisitor::visitClassDefStart(const ClassDefPtr& p) } } + // + // clone + // + if (_clone && !p->isAbstract()) + { + out << sp << nl << "public java.lang.Object" + << nl << "clone()"; + out << sb; + out << nl << name << " __result = new " << name << "();"; + + // + // Perform a shallow copy. Start with the base members. + // + ClassList cl = p->bases(); + while (!cl.empty()) + { + if (cl.front()->isAbstract()) + { + break; + } + DataMemberList members = cl.front()->dataMembers(); + DataMemberList::const_iterator d; + for (d = members.begin(); d != members.end(); ++d) + { + string memberName = fixKwd((*d)->name()); + out << nl << "__result." << memberName << " = " << memberName << ';'; + } + + cl = cl.front()->bases(); + } + + DataMemberList members = p->dataMembers(); + DataMemberList::const_iterator d; + for (d = members.begin(); d != members.end(); ++d) + { + string memberName = fixKwd((*d)->name()); + out << nl << "__result." << memberName << " = " << memberName << ';'; + } + + out << nl << "return __result;"; + out << eb; + } + if (!p->isAbstract()) { out << sp; @@ -1374,6 +1440,10 @@ Slice::Gen::TypesVisitor::visitStructStart(const StructPtr& p) Output& out = output(); out << sp << nl << "public final class " << name; + if (_clone) + { + out << " implements java.lang.Cloneable"; + } out << sb; return true; @@ -1477,6 +1547,32 @@ Slice::Gen::TypesVisitor::visitStructEnd(const StructPtr& p) out << eb; } + // + // clone() + // + if (_clone) + { + string name = fixKwd(p->name()); + + out << sp << nl << "public java.lang.Object" + << nl << "clone()"; + out << sb; + out << nl << name << " __result = new " << name << "();"; + + // + // Perform a shallow copy. + // + DataMemberList::const_iterator d; + for (d = members.begin(); d != members.end(); ++d) + { + string memberName = fixKwd((*d)->name()); + out << nl << "__result." << memberName << " = " << memberName << ';'; + } + + out << nl << "return __result;"; + out << eb; + } + if (!p->isLocal()) { // diff --git a/cpp/src/slice2java/Gen.h b/cpp/src/slice2java/Gen.h index 9a616f859da..02e6128317e 100644 --- a/cpp/src/slice2java/Gen.h +++ b/cpp/src/slice2java/Gen.h @@ -67,7 +67,8 @@ public: const std::string&, const std::vector<std::string>&, const std::string&, - const std::string&); + const std::string&, + bool); ~Gen(); bool operator!() const; // Returns true if there was a constructor error @@ -83,6 +84,7 @@ private: std::vector<std::string> _includePaths; std::string _package; std::string _dir; + bool _clone; class OpsVisitor : public JavaVisitor { @@ -108,7 +110,7 @@ private: { public: - TypesVisitor(const std::string&, const std::string&); + TypesVisitor(const std::string&, const std::string&, bool); virtual bool visitClassDefStart(const ClassDefPtr&); virtual void visitClassDefEnd(const ClassDefPtr&); @@ -118,6 +120,10 @@ private: virtual void visitStructEnd(const StructPtr&); virtual void visitEnum(const EnumPtr&); virtual void visitDataMember(const DataMemberPtr&); + + private: + + bool _clone; }; class HolderVisitor : public JavaVisitor diff --git a/cpp/src/slice2java/Main.cpp b/cpp/src/slice2java/Main.cpp index 4e76f0351d1..94103c2f0f0 100644 --- a/cpp/src/slice2java/Main.cpp +++ b/cpp/src/slice2java/Main.cpp @@ -31,6 +31,7 @@ usage(const char* n) "--tie Generate TIE classes.\n" "--impl Generate sample implementations.\n" "--impl-tie Generate sample TIE implementations.\n" + "--clone Generate clone().\n" "-d, --debug Print debug messages.\n" ; } @@ -45,6 +46,7 @@ main(int argc, char* argv[]) bool tie = false; bool impl = false; bool implTie = false; + bool clone = false; bool debug = false; int idx = 1; @@ -162,6 +164,15 @@ main(int argc, char* argv[]) } --argc; } + else if (strcmp(argv[idx], "--clone") == 0) + { + clone = true; + for (int i = idx ; i + 1 < argc ; ++i) + { + argv[i] = argv[i + 1]; + } + --argc; + } else if (argv[idx][0] == '-') { cerr << argv[0] << ": unknown option `" << argv[idx] << "'" @@ -245,7 +256,7 @@ main(int argc, char* argv[]) } else { - Gen gen(argv[0], base, includePaths, package, output); + Gen gen(argv[0], base, includePaths, package, output, clone); if (!gen) { unit->destroy(); |