18 #include "cableClass.h"
19 #include "cableClassType.h"
20 #include "cableFundamentalType.h"
21 #include "cableFunctionType.h"
22 #include "cableMethod.h"
23 #include "cablePointerType.h"
24 #include "cableType.h"
26 #include "gxsys/ios/sstream"
74 gxsys_stl::string s(c->GetName());
90 return gxsys_stl::string(c->GetName()) +
"*";
97 cable::FunctionType *ft = m->GetFunctionType();
98 unsigned int cArgs = ft->GetNumberOfArguments();
100 cable::Type *argType = 0;
101 cable::Type *retType = ft->GetReturns();
117 if (m->GetVirtual() && !implClassName)
119 Emit(os,
"virtual ");
123 Emit(os, retType->GetCxxType().GetName().c_str());
129 Emit(os, implClassName);
134 Emit(os, m->GetName());
140 for (i= 0; i<cArgs; ++i)
142 argType = ft->GetArgument(i);
145 Emit(os, argType->GetCxxType().GetName().c_str());
170 Emit(os,
"//----------------------------------------------------------------------------\n");
171 Emit(os,
"#include \"vtkDebugLeaks.h\"\n");
176 Emit(os,
"//----------------------------------------------------------------------------\n");
180 Emit(os,
"_USE_IDISPATCH_AS_PRIMARY\n");
182 Emit(os,
"#ifdef _MSC_VER\n");
183 Emit(os,
"#define ");
185 Emit(os,
"_USE_IDISPATCH_AS_PRIMARY\n");
186 Emit(os,
"#endif\n");
190 Emit(os,
"_USE_IDISPATCH_AS_PRIMARY\n");
191 Emit(os,
"#include \"windows.h\" // HRESULT, FAILED, E_UNEXPECTED\n");
192 Emit(os,
"#define ");
194 Emit(os,
"_PrimaryType IDispatch*\n");
196 Emit(os,
"#define ");
198 Emit(os,
"_PrimaryType void*\n");
199 Emit(os,
"#define HRESULT long\n");
200 Emit(os,
"#define FAILED(hr) ((HRESULT)(hr) < 0)\n");
201 Emit(os,
"#define E_UNEXPECTED ((HRESULT)0x8000FFFFL)\n");
202 Emit(os,
"#endif\n");
207 Emit(os,
"//----------------------------------------------------------------------------\n");
211 Emit(os,
" : public ");
212 Emit(os, c->GetQualifiedName().c_str());
223 Emit(os,
"public:\n");
233 Emit(os,
"* CreateShadow(");
235 Emit(os,
" primary);");
239 Emit(os,
"void DisconnectShadowDelegates();");
248 for (gxsys_stl::vector<cable::Method*>::const_iterator mit = wrapped_methods.begin();
249 mit != wrapped_methods.end(); ++mit)
251 if ((*mit)->GetVirtual())
261 Emit(os,
"protected:\n");
266 Emit(os,
"virtual ~");
272 Emit(os,
"private:\n");
275 Emit(os,
" PrimaryDelegate;");
279 Emit(os,
" SecondaryDelegate;");
286 for (gxsys_stl::vector<cable::Method*>::const_iterator mit = wrapped_methods.begin();
287 mit != wrapped_methods.end(); ++mit)
289 if ((*mit)->GetVirtual())
292 Emit(os,
"bool CallingPrimary_");
293 Emit(os, (*mit)->GetName());
322 if (cable::Type::FundamentalTypeId == t->GetTypeId())
324 s = cable::FundamentalType::SafeDownCast(t)->GetTypeName();
326 switch (cxx::FundamentalType::SafeDownCast(t->GetCxxType().GetType())->GetId())
328 case cxx::FundamentalType::UnsignedChar:
332 case cxx::FundamentalType::UnsignedShortInt:
336 case cxx::FundamentalType::UnsignedInt:
337 case cxx::FundamentalType::UnsignedLongInt:
341 case cxx::FundamentalType::UnsignedLongLongInt:
345 case cxx::FundamentalType::SignedChar:
346 case cxx::FundamentalType::Char:
350 case cxx::FundamentalType::ShortInt:
354 case cxx::FundamentalType::Int:
355 case cxx::FundamentalType::LongInt:
359 case cxx::FundamentalType::LongLongInt:
363 case cxx::FundamentalType::Bool:
367 case cxx::FundamentalType::Float:
371 case cxx::FundamentalType::Double:
390 <<
"Unhandled variable type. GetVariantFundamentalTypeString returning the empty string...");
401 cable::Type *nested_type = 0;
403 switch (t->GetTypeId())
405 case cable::Type::EnumerationTypeId:
409 case cable::Type::FundamentalTypeId:
413 case cable::Type::ArrayTypeId:
414 s =
"ERROR_ArrayTypeId_not_yet_implemented";
418 case cable::Type::ClassTypeId:
419 s =
"ERROR_passing_class__";
420 s += cable::ClassType::SafeDownCast(t)->GetClass()->GetName();
421 s +=
"__by_value_not_allowed";
425 case cable::Type::PointerTypeId:
426 nested_type = cable::PointerType::SafeDownCast(t)->GetTarget();
434 else if (
IsChar(nested_type))
438 else if (
IsVoid(nested_type))
442 else if (cable::Type::FundamentalTypeId == nested_type->GetTypeId())
446 else if (cable::Type::PointerTypeId == nested_type->GetTypeId())
448 s =
"I4 /* pointer */";
452 s =
"ERROR_PointerTypeId_not_yet_implemented_for_nested_type";
457 case cable::Type::ReferenceTypeId:
458 s =
"ERROR_ReferenceTypeId_not_yet_implemented";
462 case cable::Type::OffsetTypeId:
463 case cable::Type::MethodTypeId:
464 case cable::Type::FunctionTypeId:
466 s =
"ERROR_No_Variant_type_for_cable_Type_TypeId";
474 <<
"Unhandled variable type. GetVariantTypeString returning the empty string...");
485 cable::Type *nested_type = 0;
487 switch (t->GetTypeId())
489 case cable::Type::EnumerationTypeId:
490 s =
"(unsigned int) ";
493 case cable::Type::FundamentalTypeId:
498 case cable::Type::ArrayTypeId:
499 s =
"ERROR_ArrayTypeId_not_yet_implemented";
503 case cable::Type::ClassTypeId:
504 s =
"ERROR_passing_class__";
505 s += cable::ClassType::SafeDownCast(t)->GetClass()->GetName();
506 s +=
"__by_value_not_allowed";
510 case cable::Type::PointerTypeId:
511 nested_type = cable::PointerType::SafeDownCast(t)->GetTarget();
519 else if (
IsChar(nested_type))
523 else if (
IsVoid(nested_type))
527 else if (cable::Type::FundamentalTypeId == nested_type->GetTypeId())
531 else if (cable::Type::PointerTypeId == nested_type->GetTypeId())
533 s =
"(int) /* pointer */";
537 s =
"ERROR_PointerTypeId_not_yet_implemented_for_nested_type";
542 case cable::Type::ReferenceTypeId:
543 s =
"ERROR_ReferenceTypeId_not_yet_implemented";
547 case cable::Type::OffsetTypeId:
548 case cable::Type::MethodTypeId:
549 case cable::Type::FunctionTypeId:
551 s =
"ERROR_No_Variant_type_for_cable_Type_TypeId";
565 gxsys_ios::ostringstream oss;
566 gxsys_stl::string vargs;
572 Emit(os,
"//----------------------------------------------------------------------------\n");
573 Emit(os,
"/* static */\n");
578 Emit(os,
"CreateShadow(");
580 Emit(os,
" primary)");
586 Emit(os,
"* rv = new ");
591 Emit(os,
"rv->PrimaryDelegate = primary;\n");
594 Emit(os,
"rv->SecondaryDelegate = ");
597 Emit(os, c->GetName());
599 Emit(os, factoryM->GetName());
610 Emit(os,
"if (rv->PrimaryDelegate)\n");
615 Emit(os,
"_USE_IDISPATCH_AS_PRIMARY\n");
617 Emit(os,
"rv->PrimaryDelegate->AddRef();\n");
619 Emit(os,
"#endif\n");
625 Emit(os,
"return rv;\n");
631 Emit(os,
"//----------------------------------------------------------------------------\n");
635 Emit(os,
"DisconnectShadowDelegates()");
640 Emit(os,
"if (this->PrimaryDelegate)\n");
645 Emit(os,
"_USE_IDISPATCH_AS_PRIMARY\n");
647 Emit(os,
"this->PrimaryDelegate->Release();\n");
649 Emit(os,
"#endif\n");
653 Emit(os,
"this->PrimaryDelegate = 0;\n");
659 Emit(os,
"if (this->SecondaryDelegate)\n");
663 Emit(os,
"this->SecondaryDelegate->");
664 Emit(os, disposalM->GetName());
671 Emit(os,
"this->SecondaryDelegate = 0;\n");
680 for (gxsys_stl::vector<cable::Method*>::const_iterator mit = wrapped_methods.begin();
681 mit != wrapped_methods.end(); ++mit)
683 if ((*mit)->GetVirtual())
685 unsigned int argi = 0;
686 unsigned int cArgs = (*mit)->GetFunctionType()->GetNumberOfArguments();
687 cable::Type *retType = (*mit)->GetFunctionType()->GetReturns();
688 bool voidReturn =
false;
696 Emit(os,
"//----------------------------------------------------------------------------\n");
704 Emit(os, retType->GetCxxType().GetName().c_str());
706 if (cable::Type::PointerTypeId == retType->GetTypeId())
710 else if (cable::Type::FundamentalTypeId == retType->GetTypeId())
713 Emit(os, retType->GetCxxType().GetName().c_str());
718 Emit(os, retType->GetCxxType().GetName().c_str());
725 Emit(os,
"HRESULT msl_hr = E_UNEXPECTED;\n");
729 Emit(os,
"if (this->PrimaryDelegate && !this->CallingPrimary_");
730 Emit(os, (*mit)->GetName());
739 Emit(os,
"this->CallingPrimary_");
740 Emit(os, (*mit)->GetName());
743 Emit(os,
" = true;\n");
748 Emit(os,
"_USE_IDISPATCH_AS_PRIMARY\n");
750 Emit(os,
"IDispatch *msl_pDisp = (IDispatch *) this->PrimaryDelegate;\n");
752 Emit(os,
"msl_pDisp->AddRef();\n");
755 Emit(os,
"DISPID msl_dispid = (DISPID) -1;\n");
758 Emit(os,
"OLECHAR *msl_name = L\"");
759 Emit(os, (*mit)->GetName());
765 Emit(os,
"VARIANTARG msl_vargs[");
766 oss.rdbuf()->str(
"");
767 oss << cArgs << gxsys_ios::ends;
769 Emit(os, vargs.c_str());
774 Emit(os,
"DISPPARAMS msl_params;\n");
776 Emit(os,
"VARIANT msl_result;\n");
778 Emit(os,
"EXCEPINFO msl_excep;\n");
780 Emit(os,
"UINT msl_arg = (UINT) -1;\n");
783 Emit(os,
"msl_hr = msl_pDisp->GetIDsOfNames(\n");
785 Emit(os,
"IID_NULL,\n");
787 Emit(os,
"&msl_name,\n");
791 Emit(os,
"LOCALE_USER_DEFAULT,\n");
793 Emit(os,
"&msl_dispid);\n");
797 Emit(os,
"if (SUCCEEDED(msl_hr))\n");
801 for (argi= 0; argi<cArgs; ++argi)
803 oss.rdbuf()->str(
"");
804 oss <<
"&msl_vargs[" << (cArgs-1-argi) <<
"]" << gxsys_ios::ends;
808 Emit(os,
"VariantInit(");
809 Emit(os, vargs.c_str());
814 Emit(os, vargs.c_str());
823 Emit(os, vargs.c_str());
833 Emit(os,
"msl_params.rgvarg = ");
840 Emit(os,
"&msl_vargs[0]");
844 Emit(os,
"msl_params.rgdispidNamedArgs = 0;\n");
846 Emit(os,
"msl_params.cArgs = ");
853 Emit(os,
"sizeof(msl_vargs)/sizeof(msl_vargs[0])");
857 Emit(os,
"msl_params.cNamedArgs = 0;\n");
860 Emit(os,
"VariantInit(&msl_result);\n");
863 Emit(os,
"memset(&msl_excep, 0, sizeof(msl_excep));\n");
866 Emit(os,
"msl_hr = msl_pDisp->Invoke(msl_dispid, IID_NULL, LOCALE_USER_DEFAULT,\n");
868 Emit(os,
"DISPATCH_METHOD, &msl_params, &msl_result, &msl_excep, &msl_arg);\n");
874 Emit(os,
"if (SUCCEEDED(msl_hr))\n");
879 Emit(os, retType->GetCxxType().GetName().c_str());
882 Emit(os,
"(&msl_result);\n");
889 Emit(os,
"VariantClear(&msl_result);\n");
891 for (argi= 0; argi<cArgs; ++argi)
893 oss.rdbuf()->str(
"");
894 oss <<
"&msl_vargs[" << argi <<
"]" << gxsys_ios::ends;
898 Emit(os,
"VariantClear(");
899 Emit(os, vargs.c_str());
908 Emit(os,
"msl_pDisp->Release();\n");
910 Emit(os,
"#endif\n");
914 Emit(os,
"this->CallingPrimary_");
915 Emit(os, (*mit)->GetName());
918 Emit(os,
" = false;\n");
926 Emit(os,
"if (FAILED(msl_hr) || this->CallingPrimary_");
927 Emit(os, (*mit)->GetName());
941 int secondaryIndent = 3;
943 if (*mit != registerM && *mit != unRegisterM)
948 Emit(os,
"if (this->SecondaryDelegate)\n");
958 Emit(os,
"this->SecondaryDelegate->");
959 Emit(os, (*mit)->GetName());
962 for (argi= 0; argi<cArgs; ++argi)
993 if ((*mit)->GetPureVirtual())
996 Emit(os,
"// pure virtual method in parent class...\n");
998 if (*mit == registerM || *mit == unRegisterM)
1001 Emit(os,
"// Register and UnRegister methods should *not* be\n");
1003 Emit(os,
"// pure virtual methods in a shadowed class...\n");
1006 <<
"Register and UnRegister methods *cannot* be pure "
1007 <<
"virtual methods in a shadowed class..."
1019 Emit(os, c->GetQualifiedName().c_str());
1021 Emit(os, (*mit)->GetName());
1024 for (argi= 0; argi<cArgs; ++argi)
1036 if (*mit != registerM && *mit != unRegisterM)
1052 Emit(os,
"return rv;\n");
1066 Emit(os,
"//----------------------------------------------------------------------------\n");
1067 Emit(os, s.c_str());
1069 Emit(os, s.c_str());
1076 Emit(os,
"this->PrimaryDelegate = 0;\n");
1079 Emit(os,
"this->SecondaryDelegate = 0;\n");
1082 for (gxsys_stl::vector<cable::Method*>::const_iterator mit = wrapped_methods.begin();
1083 mit != wrapped_methods.end(); ++mit)
1085 if ((*mit)->GetVirtual())
1088 Emit(os,
"this->CallingPrimary_");
1089 Emit(os, (*mit)->GetName());
1092 Emit(os,
" = false;\n");
1101 Emit(os,
"#ifdef VTK_DEBUG_LEAKS\n");
1104 Emit(os,
"vtkDebugLeaks::ConstructClass(\"");
1105 Emit(os, c->GetName());
1108 Emit(os,
"#endif\n");
1118 Emit(os,
"//----------------------------------------------------------------------------\n");
1119 Emit(os, s.c_str());
1121 Emit(os, s.c_str());
1125 Emit(os,
"this->DisconnectShadowDelegates();\n");
1134 Emit(os,
"//----------------------------------------------------------------------------\n");
1135 Emit(os,
"extern \"C\" MUMMY_DLL_EXPORT\n");
1137 Emit(os, s.c_str());
1139 Emit(os, s.c_str());
1140 Emit(os,
"_CreateShadow(");
1142 Emit(os,
" primary)\n");
1147 Emit(os, s.c_str());
1148 Emit(os,
"* rv = ");
1149 Emit(os, s.c_str());
1150 Emit(os,
"::CreateShadow(primary);\n");
1153 Emit(os,
"return rv;\n");
1164 gxsys_stl::vector<cable::Method*> wrapped_methods;
1165 cable::Method *factoryM = 0;
1166 cable::Method *disposalM = 0;
1167 cable::Method *registerM = 0;
1168 cable::Method *unRegisterM = 0;
1175 wrapped_methods.insert(wrapped_methods.begin(), unRegisterM);
1176 wrapped_methods.insert(wrapped_methods.begin(), registerM);