diff options
author | Marc Laukien <marc@zeroc.com> | 2001-10-08 23:00:04 +0000 |
---|---|---|
committer | Marc Laukien <marc@zeroc.com> | 2001-10-08 23:00:04 +0000 |
commit | b6d77023ad984fb88e01f2c4a12a30827a28c1c7 (patch) | |
tree | 31cb6ffb9787430b76262e5a73776c2eb52ba03b /cpp/src/Slice/CPlusPlusUtil.cpp | |
parent | Ice::Object now inherits virtually from IceUtil::Shared in order to allow (diff) | |
download | ice-b6d77023ad984fb88e01f2c4a12a30827a28c1c7.tar.bz2 ice-b6d77023ad984fb88e01f2c4a12a30827a28c1c7.tar.xz ice-b6d77023ad984fb88e01f2c4a12a30827a28c1c7.zip |
moved generic C++ stuff to Slice library
Diffstat (limited to 'cpp/src/Slice/CPlusPlusUtil.cpp')
-rw-r--r-- | cpp/src/Slice/CPlusPlusUtil.cpp | 494 |
1 files changed, 494 insertions, 0 deletions
diff --git a/cpp/src/Slice/CPlusPlusUtil.cpp b/cpp/src/Slice/CPlusPlusUtil.cpp new file mode 100644 index 00000000000..6a1b4176062 --- /dev/null +++ b/cpp/src/Slice/CPlusPlusUtil.cpp @@ -0,0 +1,494 @@ +// ********************************************************************** +// +// Copyright (c) 2001 +// MutableRealms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +#include <Slice/CPlusPlusUtil.h> + +using namespace std; +using namespace Slice; + +char +Slice::ToIfdef::operator()(char c) +{ + if (!isalnum(c)) + { + return '_'; + } + else + { + return c; + } +} + +string +Slice::changeInclude(const string& orig, const vector<string>& includePaths) +{ + string file = orig; + + for (vector<string>::const_iterator p = includePaths.begin(); p != includePaths.end(); ++p) + { + if (orig.compare(0, p->length(), *p) == 0) + { + string s = orig.substr(p->length()); + if (s.size() < file.size()) + { + file = s; + } + } + } + + string::size_type pos = file.rfind('.'); + if (pos != string::npos) + { + file.erase(pos); + } + + return file; +} + +void +Slice::printHeader(Output& out) +{ + static const char* header = +"// **********************************************************************\n" +"//\n" +"// Copyright (c) 2001\n" +"// MutableRealms, Inc.\n" +"// Huntsville, AL, USA\n" +"//\n" +"// All Rights Reserved\n" +"//\n" +"// **********************************************************************\n" + ; + + out << header; + out << "\n// Ice version " << ICE_STRING_VERSION; +} + +void +Slice::printVersionCheck(Output& out) +{ + out << "\n"; + out << "\n#ifndef ICE_IGNORE_VERSION"; + out << "\n# if ICE_INT_VERSION != 0x" << hex << ICE_INT_VERSION; + out << "\n# error Ice version mismatch!"; + out << "\n# endif"; + out << "\n#endif"; +} + +void +Slice::printDllExportStuff(Output& out, const string& dllExport) +{ + if (dllExport.size()) + { + out << sp; + out << "\n#ifdef WIN32"; + out << "\n# ifdef " << dllExport.substr(0, dllExport.size() - 1) << "_EXPORTS"; + out << "\n# define " << dllExport << "__declspec(dllexport)"; + out << "\n# else"; + out << "\n# define " << dllExport << "__declspec(dllimport)"; + out << "\n# endif"; + out << "\n#else"; + out << "\n# define " << dllExport << "/**/"; + out << "\n#endif"; + } +} + +string +Slice::typeToString(const TypePtr& type) +{ + static const char* builtinTable[] = + { + "::Ice::Byte", + "bool", + "::Ice::Short", + "::Ice::Int", + "::Ice::Long", + "::Ice::Float", + "::Ice::Double", + "::std::string", + "::std::wstring", + "::Ice::ObjectPtr", + "::Ice::ObjectPrx", + "::Ice::LocalObjectPtr" + }; + + BuiltinPtr builtin = BuiltinPtr::dynamicCast(type); + if (builtin) + { + return builtinTable[builtin->kind()]; + } + + ClassDeclPtr cl = ClassDeclPtr::dynamicCast(type); + if (cl) + { + return cl->scoped() + "Ptr"; + } + + ProxyPtr proxy = ProxyPtr::dynamicCast(type); + if (proxy) + { + return proxy->_class()->scoped() + "Prx"; + } + + ContainedPtr contained = ContainedPtr::dynamicCast(type); + if (contained) + { + return contained->scoped(); + } + + return "???"; +} + +string +Slice::returnTypeToString(const TypePtr& type) +{ + if (!type) + { + return "void"; + } + + return typeToString(type); +} + +string +Slice::inputTypeToString(const TypePtr& type) +{ + static const char* inputBuiltinTable[] = + { + "::Ice::Byte", + "bool", + "::Ice::Short", + "::Ice::Int", + "::Ice::Long", + "::Ice::Float", + "::Ice::Double", + "const ::std::string&", + "const ::std::wstring&", + "const ::Ice::ObjectPtr&", + "const ::Ice::ObjectPrx&", + "const ::Ice::LocalObjectPtr&" + }; + + BuiltinPtr builtin = BuiltinPtr::dynamicCast(type); + if (builtin) + { + return inputBuiltinTable[builtin->kind()]; + } + + ClassDeclPtr cl = ClassDeclPtr::dynamicCast(type); + if (cl) + { + return "const " + cl->scoped() + "Ptr&"; + } + + ProxyPtr proxy = ProxyPtr::dynamicCast(type); + if (proxy) + { + return "const " + proxy->_class()->scoped() + "Prx&"; + } + + EnumPtr en = EnumPtr::dynamicCast(type); + if (en) + { + return en->scoped(); + } + + NativePtr native = NativePtr::dynamicCast(type); + if (native) + { + return native->scoped(); + } + + ContainedPtr contained = ContainedPtr::dynamicCast(type); + if (contained) + { + return "const " + contained->scoped() + "&"; + } + + return "???"; +} + +string +Slice::outputTypeToString(const TypePtr& type) +{ + static const char* outputBuiltinTable[] = + { + "::Ice::Byte&", + "bool&", + "::Ice::Short&", + "::Ice::Int&", + "::Ice::Long&", + "::Ice::Float&", + "::Ice::Double&", + "::std::string&", + "::std::wstring&", + "::Ice::ObjectPtr&", + "::Ice::ObjectPrx&", + "::Ice::LocalObjectPtr&" + }; + + BuiltinPtr builtin = BuiltinPtr::dynamicCast(type); + if (builtin) + { + return outputBuiltinTable[builtin->kind()]; + } + + ClassDeclPtr cl = ClassDeclPtr::dynamicCast(type); + if (cl) + { + return cl->scoped() + "Ptr&"; + } + + ProxyPtr proxy = ProxyPtr::dynamicCast(type); + if (proxy) + { + return proxy->_class()->scoped() + "Prx&"; + } + + NativePtr native = NativePtr::dynamicCast(type); + if (native) + { + return native->scoped(); + } + + ContainedPtr contained = ContainedPtr::dynamicCast(type); + if (contained) + { + return contained->scoped() + "&"; + } + + return "???"; +} + +string +Slice::exceptionTypeToString(const TypePtr& type) +{ + static const char* inputBuiltinTable[] = + { + "::Ice::Byte", + "bool", + "::Ice::Short", + "::Ice::Int", + "::Ice::Long", + "::Ice::Float", + "::Ice::Double", + "const ::std::string&", + "const ::std::wstring&", + "const ::Ice::ObjectPtrE&", + "const ::Ice::ObjectPrxE&", + "const ::Ice::LocalObjectPtrE&" + }; + + BuiltinPtr builtin = BuiltinPtr::dynamicCast(type); + if (builtin) + { + return inputBuiltinTable[builtin->kind()]; + } + + ClassDeclPtr cl = ClassDeclPtr::dynamicCast(type); + if (cl) + { + return "const " + cl->scoped() + "PtrE&"; + } + + ProxyPtr proxy = ProxyPtr::dynamicCast(type); + if (proxy) + { + return "const " + proxy->_class()->scoped() + "PrxE&"; + } + + EnumPtr en = EnumPtr::dynamicCast(type); + if (en) + { + return en->scoped(); + } + + NativePtr native = NativePtr::dynamicCast(type); + if (native) + { + return native->scoped(); + } + + ContainedPtr contained = ContainedPtr::dynamicCast(type); + if (contained) + { + return "const " + contained->scoped() + "&"; + } + + return "???"; +} + +void +Slice::writeMarshalUnmarshalCode(Output& out, const TypePtr& type, const string& param, bool marshal, + const string& str, bool pointer) +{ + string stream; + if (str.empty()) + { + stream = marshal ? "__os" : "__is"; + } + else + { + stream = str; + } + + string deref; + if (pointer) + { + deref = "->"; + } + else + { + deref = '.'; + } + + string obj; + if (stream.find("__") == 0) + { + obj = "__obj"; + } + else + { + obj = "obj;"; + } + + string func = marshal ? "write(" : "read("; + if (!pointer) + { + func += '&'; + } + + if (BuiltinPtr::dynamicCast(type)) + { + out << nl << stream << deref << func << param << ");"; + return; + } + + ClassDeclPtr cl = ClassDeclPtr::dynamicCast(type); + if (cl) + { + out << sb; + if (marshal) + { + out << nl << "::Ice::ObjectPtr " << obj << ' ' << param << ';'; + out << nl << stream << deref << "write(" << obj << ");"; + } + else + { + out << nl << "::Ice::ObjectPtr " << obj << ';'; + out << nl << stream << deref << func << obj << ", " << cl->scoped() << "::__classIds[0]);"; + out << nl << "if (!" << obj << ')'; + out << sb; + ClassDefPtr def = cl->definition(); + if (def && !def->isAbstract()) + { + out << nl << obj << " = new " << cl->scoped() << ";"; + out << nl << obj << "->" << func << stream << ");"; + } + else + { + out << nl << "throw ::Ice::NoServantFactoryException(__FILE__, __LINE__);"; + } + out << eb; + out << nl << param << " = " << cl->scoped() << "Ptr::dynamicCast(" << obj << ");"; + out << nl << "if (!" << param << ')'; + out << sb; + out << nl << "throw ::Ice::ServantUnmarshalException(__FILE__, __LINE__);"; + out << eb; + } + out << eb; + + return; + } + + StructPtr st = StructPtr::dynamicCast(type); + if (st) + { + out << nl << param << ".__" << func << stream << ");"; + return; + } + + SequencePtr seq = SequencePtr::dynamicCast(type); + if (seq) + { + if (BuiltinPtr::dynamicCast(seq->type())) + { + out << nl << stream << deref << func << param << ");"; + } + else + { + out << nl << seq->scope() << "__" << func << stream << ", " << param << ", " << seq->scope() + << "__U__" << seq->name() << "());"; + } + return; + } + + DictionaryPtr dict = DictionaryPtr::dynamicCast(type); + if (dict) + { + out << nl << dict->scope() << "__" << func << stream << ", " << param << ", " << dict->scope() + << "__U__" << dict->name() << "());"; + return; + } + + NativePtr native = NativePtr::dynamicCast(type); + assert(!native); // TODO + + ConstructedPtr constructed = ConstructedPtr::dynamicCast(type); + if (!constructed) + { + ProxyPtr proxy = ProxyPtr::dynamicCast(type); + assert(proxy); + constructed = proxy->_class(); + } + + out << nl << constructed->scope() << "__" << func << stream << ", " << param << ");"; +} + +void +Slice::writeMarshalCode(Output& out, const list<pair<TypePtr, string> >& params, const TypePtr& ret) +{ + for (list<pair<TypePtr, string> >::const_iterator p = params.begin(); p != params.end(); ++p) + { + writeMarshalUnmarshalCode(out, p->first, p->second, true); + } + if (ret) + { + writeMarshalUnmarshalCode(out, ret, "__ret", true); + } +} + +void +Slice::writeUnmarshalCode(Output& out, const list<pair<TypePtr, string> >& params, const TypePtr& ret) +{ + for (list<pair<TypePtr, string> >::const_iterator p = params.begin(); p != params.end(); ++p) + { + writeMarshalUnmarshalCode(out, p->first, p->second, false); + } + if (ret) + { + writeMarshalUnmarshalCode(out, ret, "__ret", false); + } +} + +void +Slice::writeAllocateCode(Output& out, const list<pair<TypePtr, string> >& params, const TypePtr& ret) +{ + list<pair<TypePtr, string> > ps = params; + if (ret) + { + ps.push_back(make_pair(ret, string("__ret"))); + } + + for (list<pair<TypePtr, string> >::const_iterator p = ps.begin(); p != ps.end(); ++p) + { + out << nl << typeToString(p->first) << ' ' << p->second << ';'; + } +} |