summaryrefslogtreecommitdiff
path: root/cpp/src/slice2cpp/Gen.cpp
diff options
context:
space:
mode:
authorMark Spruiell <mes@zeroc.com>2002-01-22 21:05:19 +0000
committerMark Spruiell <mes@zeroc.com>2002-01-22 21:05:19 +0000
commitaf80ebed14a45803dede0c5c3a934a5227717ab7 (patch)
treebd5d9e6bc895e17e4da2b6912e3a24d46ecc6f66 /cpp/src/slice2cpp/Gen.cpp
parentadded nillable (diff)
downloadice-af80ebed14a45803dede0c5c3a934a5227717ab7.tar.bz2
ice-af80ebed14a45803dede0c5c3a934a5227717ab7.tar.xz
ice-af80ebed14a45803dede0c5c3a934a5227717ab7.zip
adding --impl option
Diffstat (limited to 'cpp/src/slice2cpp/Gen.cpp')
-rw-r--r--cpp/src/slice2cpp/Gen.cpp399
1 files changed, 396 insertions, 3 deletions
diff --git a/cpp/src/slice2cpp/Gen.cpp b/cpp/src/slice2cpp/Gen.cpp
index eb8a3c781bb..686c689e5b6 100644
--- a/cpp/src/slice2cpp/Gen.cpp
+++ b/cpp/src/slice2cpp/Gen.cpp
@@ -13,15 +13,18 @@
#include <Gen.h>
#include <limits>
+#include <sys/stat.h>
+
using namespace std;
using namespace Slice;
Slice::Gen::Gen(const string& name, const string& base, const string& include, const vector<string>& includePaths,
- const string& dllExport, const string& dir) :
+ const string& dllExport, const string& dir, bool imp) :
_base(base),
_include(include),
_includePaths(includePaths),
- _dllExport(dllExport)
+ _dllExport(dllExport),
+ _impl(imp)
{
for (vector<string>::iterator p = _includePaths.begin(); p != _includePaths.end(); ++p)
{
@@ -37,6 +40,57 @@ Slice::Gen::Gen(const string& name, const string& base, const string& include, c
_base.erase(0, pos + 1);
}
+ if (_impl)
+ {
+ string fileImplH = _base + "I.h";
+ string fileImplC = _base + "I.cpp";
+ if (!dir.empty())
+ {
+ fileImplH = dir + '/' + fileImplH;
+ fileImplC = dir + '/' + fileImplC;
+ }
+
+ struct stat st;
+ if (stat(fileImplH.c_str(), &st) == 0)
+ {
+ cerr << name << ": `" << fileImplH
+ << "' already exists - will not overwrite" << endl;
+ return;
+ }
+ if (stat(fileImplC.c_str(), &st) == 0)
+ {
+ cerr << name << ": `" << fileImplC
+ << "' already exists - will not overwrite" << endl;
+ return;
+ }
+
+ implH.open(fileImplH.c_str());
+ if (!implH)
+ {
+ cerr << name << ": can't open `" << fileImplH << "' for writing: "
+ << strerror(errno) << endl;
+ return;
+ }
+
+ implC.open(fileImplC.c_str());
+ if (!implC)
+ {
+ cerr << name << ": can't open `" << fileImplC << "' for writing: "
+ << strerror(errno) << endl;
+ return;
+ }
+
+ string s = fileImplH;
+ if (_include.size())
+ {
+ s = _include + '/' + s;
+ }
+ transform(s.begin(), s.end(), s.begin(), ToIfdef());
+ implH << "#ifndef __" << s << "__";
+ implH << "\n#define __" << s << "__";
+ implH << '\n';
+ }
+
string fileH = _base + ".h";
string fileC = _base + ".cpp";
if (!dir.empty())
@@ -79,12 +133,26 @@ Slice::Gen::~Gen()
{
H << "\n\n#endif\n";
C << '\n';
+
+ if (_impl)
+ {
+ implH << "\n\n#endif\n";
+ implC << '\n';
+ }
}
bool
Slice::Gen::operator!() const
{
- return !H || !C;
+ if (!H || !C)
+ {
+ return true;
+ }
+ if (_impl && (!implH || !implC))
+ {
+ return true;
+ }
+ return false;
}
void
@@ -161,6 +229,26 @@ Slice::Gen::generate(const UnitPtr& unit)
ObjectVisitor objectVisitor(H, C, _dllExport);
unit->visit(&objectVisitor);
+
+ if (_impl)
+ {
+ implH << "\n#include <";
+ if (_include.size())
+ {
+ implH << _include << '/';
+ }
+ implH << _base << ".h>";
+
+ implC << "#include <";
+ if (_include.size())
+ {
+ implC << _include << '/';
+ }
+ implC << _base << "I.h>";
+
+ ImplVisitor implVisitor(implH, implC, _dllExport);
+ unit->visit(&implVisitor);
+ }
}
Slice::Gen::TypesVisitor::TypesVisitor(Output& h, Output& c, const string& dllExport) :
@@ -2004,6 +2092,311 @@ Slice::Gen::ObjectVisitor::visitDataMember(const DataMemberPtr& p)
H << nl << s << ' ' << name << ';';
}
+Slice::Gen::ImplVisitor::ImplVisitor(Output& h, Output& c,
+ const string& dllExport) :
+ H(h), C(c), _dllExport(dllExport)
+{
+}
+
+void
+Slice::Gen::ImplVisitor::writeAssign(Output& out, const TypePtr& type,
+ const string& name, int& iter)
+{
+ BuiltinPtr builtin = BuiltinPtr::dynamicCast(type);
+ if (builtin)
+ {
+ switch (builtin->kind())
+ {
+ case Builtin::KindByte:
+ {
+ out << nl << name << " = (Ice::Byte)0;";
+ break;
+ }
+ case Builtin::KindBool:
+ {
+ out << nl << name << " = false;";
+ break;
+ }
+ case Builtin::KindShort:
+ {
+ out << nl << name << " = (Ice::Short)0;";
+ break;
+ }
+ case Builtin::KindInt:
+ {
+ out << nl << name << " = (Ice::Int)0;";
+ break;
+ }
+ case Builtin::KindLong:
+ {
+ out << nl << name << " = (Ice::Long)0;";
+ break;
+ }
+ case Builtin::KindFloat:
+ {
+ out << nl << name << " = (Ice::Float)0;";
+ break;
+ }
+ case Builtin::KindDouble:
+ {
+ out << nl << name << " = (Ice::Double)0;";
+ break;
+ }
+ case Builtin::KindString:
+ case Builtin::KindObject:
+ case Builtin::KindObjectProxy:
+ case Builtin::KindLocalObject:
+ {
+ //
+ // No initialization needed
+ //
+ break;
+ }
+ }
+ return;
+ }
+
+ ProxyPtr prx = ProxyPtr::dynamicCast(type);
+ if (prx)
+ {
+ //
+ // No initialization needed
+ //
+ return;
+ }
+
+ ClassDeclPtr cl = ClassDeclPtr::dynamicCast(type);
+ if (cl)
+ {
+ //
+ // No initialization needed
+ //
+ return;
+ }
+
+ StructPtr st = StructPtr::dynamicCast(type);
+ if (st)
+ {
+ DataMemberList members = st->dataMembers();
+ DataMemberList::const_iterator d;
+ for (d = members.begin(); d != members.end(); ++d)
+ {
+ string memberName = name + "." + (*d)->name();
+ writeAssign(out, (*d)->type(), memberName, iter);
+ }
+ return;
+ }
+
+ EnumPtr en = EnumPtr::dynamicCast(type);
+ if (en)
+ {
+ EnumeratorList enumerators = en->getEnumerators();
+ out << nl << name << " = " << en->scope()
+ << enumerators.front()->name() << ';';
+ return;
+ }
+
+ SequencePtr seq = SequencePtr::dynamicCast(type);
+ if (seq)
+ {
+ out << nl << name << ".resize(5);";
+ out << nl << "for (int __i" << iter << " = 0; __i" << iter << " < "
+ << name << ".size(); __i" << iter << "++)";
+ out << sb;
+ ostringstream elem;
+ elem << name << "[__i" << iter << ']';
+ iter++;
+ writeAssign(out, seq->type(), elem.str(), iter);
+ out << eb;
+ return;
+ }
+
+ DictionaryPtr dict = DictionaryPtr::dynamicCast(type);
+ assert(dict);
+ //
+ // No initialization needed
+ //
+}
+
+bool
+Slice::Gen::ImplVisitor::visitModuleStart(const ModulePtr& p)
+{
+ if (!p->hasClassDefs())
+ {
+ return false;
+ }
+
+ string name = p->name();
+
+ H << sp << nl << "namespace " << name << nl << '{';
+
+ return true;
+}
+
+void
+Slice::Gen::ImplVisitor::visitModuleEnd(const ModulePtr& p)
+{
+ H << sp;
+ H << nl << '}';
+}
+
+bool
+Slice::Gen::ImplVisitor::visitClassDefStart(const ClassDefPtr& p)
+{
+ if (!p->isAbstract())
+ {
+ return false;
+ }
+
+ string name = p->name();
+ string scope = p->scope();
+ string scoped = p->scoped();
+ string cls = scope.substr(2) + name + "I";
+
+ ClassList bases = p->bases();
+ ClassDefPtr base;
+ if (!bases.empty() && !bases.front()->isInterface())
+ {
+ base = bases.front();
+ }
+
+ H << sp;
+ H << nl << "class " << name << "I : public " << name;
+ H << sb;
+ H.dec();
+ H << nl << "public:";
+ H.inc();
+
+ H << sp << nl << name << "I(const Ice::CommunicatorPtr&);";
+ H << nl << "virtual ~" << name << "I();";
+
+ C << sp << nl << cls << "::" << name
+ << "I(const Ice::CommunicatorPtr& communicator)";
+ C.inc();
+ C << nl << "_communicator(communicator)";
+ C.dec();
+ C << sb;
+ C << eb;
+
+ C << sp << nl << cls << "::~" << name << "I()";
+ C << sb;
+ C << eb;
+
+ OperationList ops = p->allOperations();
+ OperationList::const_iterator r;
+
+ //
+ // Operations
+ //
+ for (r = ops.begin(); r != ops.end(); ++r)
+ {
+ OperationPtr op = (*r);
+ string opName = op->name();
+
+ TypePtr ret = op->returnType();
+ string retS = returnTypeToString(ret);
+
+ TypeStringList inParams = op->inputParameters();
+ TypeStringList outParams = op->outputParameters();
+ TypeStringList::const_iterator q;
+
+ H << sp << nl << "virtual " << retS << ' ' << opName << '(';
+ H.useCurrentPosAsIndent();
+ for (q = inParams.begin(); q != inParams.end(); ++q)
+ {
+ if (q != inParams.begin())
+ {
+ H << ',' << nl;
+ }
+ string typeString = inputTypeToString(q->first);
+ H << typeString;
+ }
+ for (q = outParams.begin(); q != outParams.end(); ++q)
+ {
+ if (!inParams.empty() || q != outParams.begin())
+ {
+ H << ',' << nl;
+ }
+ string typeString = outputTypeToString(q->first);
+ H << typeString;
+ }
+ if (!p->isLocal())
+ {
+ if (!inParams.empty() || !outParams.empty())
+ {
+ H << ',' << nl;
+ }
+ H << "const Ice::Current&";
+ }
+ H.restoreIndent();
+ H << ");";
+
+ C << sp << nl << retS << nl << scoped.substr(2) << "I::" << opName
+ << '(';
+ C.useCurrentPosAsIndent();
+ for (q = inParams.begin(); q != inParams.end(); ++q)
+ {
+ if (q != inParams.begin())
+ {
+ C << ',' << nl;
+ }
+ string typeString = inputTypeToString(q->first);
+ C << typeString << ' ' << q->second;
+ }
+ for (q = outParams.begin(); q != outParams.end(); ++q)
+ {
+ if (!inParams.empty() || q != outParams.begin())
+ {
+ C << ',' << nl;
+ }
+ string typeString = outputTypeToString(q->first);
+ C << typeString << ' ' << q->second;
+ }
+ if (!p->isLocal())
+ {
+ if (!inParams.empty() || !outParams.empty())
+ {
+ C << ',' << nl;
+ }
+ C << "const Ice::Current& current";
+ }
+ C.restoreIndent();
+ C << ")";
+ C << sb;
+
+ int iter = 0;
+
+ //
+ // Assign values to 'out' params
+ //
+ for (q = outParams.begin(); q != outParams.end(); ++q)
+ {
+ writeAssign(C, q->first, q->second, iter);
+ }
+
+ //
+ // Return value
+ //
+ if (ret)
+ {
+ C << sp << nl << retS << " __r;";
+ writeAssign(C, ret, "__r", iter);
+ C << nl << "return __r;";
+ }
+
+ C << eb;
+ }
+
+ H << sp;
+ H.dec();
+ H << nl << "private:";
+ H.inc();
+ H << sp << nl << "Ice::CommunicatorPtr _communicator;";
+ H << eb << ';';
+
+ return true;
+}
+
Slice::Gen::IceInternalVisitor::IceInternalVisitor(Output& h, Output& c, const string& dllExport) :
H(h), C(c), _dllExport(dllExport)
{