forwarding.m
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:9k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

  1. #import <simtools.h> // initSwarmBatch
  2. #import <defobj.h> // FArguments, FCall
  3. #import <defobj/Create.h>
  4. #import <defobj/defalloc.h> // getCZone, getZone
  5. #include <misc.h> // printf, stpcpy, strlen
  6. // mframe_build_signature
  7. #ifdef GNUSTEP
  8. #include <Foundation/NSMethodSignature.h>
  9. #include <Foundation/NSInvocation.h>
  10. #include <Foundation/NSAutoreleasePool.h>
  11. #include <mframe.h>
  12. #else
  13. #include <objc/mframe.h>
  14. #endif
  15. @interface AnotherObject: CreateDrop
  16. {
  17.   const char *name;
  18. }
  19. - setName: (const char *)name;
  20. - (const char *)getNewName;
  21. @end
  22. @implementation AnotherObject: CreateDrop
  23. - setName: (const char *)theName
  24. {
  25.   name = theName;
  26.   return self;
  27. }
  28. - (const char *)getNewName
  29. {
  30.   char *buf = [[self getZone] alloc: 1 + strlen (name) + 1 + 1], *p = buf;
  31.   
  32.   p = stpcpy (buf, "<");
  33.   p = stpcpy (p, name);
  34.   p = stpcpy (p, ">");
  35.   return buf;
  36. }
  37. @end
  38. @interface DelegateObject: CreateDrop
  39. - (const char *)m1: (int)num float: (float)val double: (double)val2;
  40. - (const char *)m2: (int)num double: (double)val float: (float)val2;
  41. - (const char *)m3: (float)val double: (double)val2 int: (int)num; 
  42. - (const char *)m4: (double)val float: (float)val2 int: (int)num;
  43. - (const char *)m5: (float)val double: (double)val2 int: (int)num int2: (int)num2; 
  44. - (const char *)m6: (float)fval float2: (float)fval2 double: (double)dval int: (int)num int2: (int)num2; 
  45. - (int)im1i: (int)val1;
  46. - (float)fm1i: (int)val1;
  47. - (double)dm1i: (int)val1;
  48. - (const char *)testObject;
  49. - (const char *)testObject: obj0;
  50. - (const char *)testObject: obj0 : obj1;
  51. - (const char *)testObject: obj0 : obj1 : obj2;
  52. @end
  53. @implementation DelegateObject
  54. - (const char *)m1: (int)num float: (float)val1 double: (double)val2
  55. {
  56.   static char buf[20];
  57.   sprintf (buf, "%d %.2f %.2f", num, val1, val2);
  58.   printf ("m1: [%s]n", buf);
  59.   return buf;
  60. }
  61. - (const char *)m2: (int)num double: (double)val1 float: (float)val2
  62. {
  63.   static char buf[20];
  64.   sprintf (buf, "%d %.2f %.2f", num, val2, val1);
  65.   printf ("m2: [%s]n", buf);
  66.   return buf;
  67. }
  68. - (const char *)m3: (float)val1 double: (double)val2 int: (int)num
  69. {
  70.   static char buf[20];
  71.   sprintf (buf, "%d %.2f %.2f", num, val1, val2);
  72.   printf ("m3: [%s]n", buf);
  73.   return buf;
  74. }
  75. - (const char *)m4: (double)val1 float: (float)val2 int: (int)num
  76. {
  77.   static char buf[20];
  78.   sprintf (buf, "%d %.2f %.2f", num, val2, val1);
  79.   printf ("m4: [%s]n", buf);
  80.   return buf;
  81. }
  82. - (const char *)m5: (float)val1 double: (double)val2 int: (int)num int2: (int)num2
  83. {
  84.   static char buf[30];
  85.   sprintf (buf, "%d %d %.2f %.2f", num, num2, val1, val2);
  86.   printf ("m5: [%s]n", buf);
  87.   return buf;
  88. }
  89. - (const char *)m6: (float)fval1 float2: (float)fval2 double: (double)dval int: (int)num int2: (int)num2
  90. {
  91.   static char buf[40];
  92.   sprintf (buf, "%d %d %.2f %.2f %.2f", num, num2, fval1, fval2, dval);
  93.   printf ("m6: [%s]n", buf);
  94.   return buf;
  95. }
  96. - (char)cm1i: (int)val1
  97. {
  98.   char result = 'A' + (char)val1;
  99.   printf ("cm1i: `%c'n", result);
  100.   return result;
  101. }
  102. - (short)sm1i: (int)val1
  103. {
  104.   short result = val1 + 1;
  105.   printf ("sm1i: %hdn", result);
  106.   return result;
  107. }
  108. - (int)im1i: (int)val1
  109. {
  110.   int result = val1 + 2;
  111.   
  112.   printf ("im1i: %dn", result);
  113.   return result;
  114. }
  115. - (long)lm1i: (int)val1
  116. {
  117.   long result = val1 + 3;
  118.   printf ("lm1i: %ldn", result);
  119.   return result;
  120. }
  121. - (float)fm1i: (int)val1
  122. {
  123.   float result = val1 + 4;
  124.   printf ("fm1i: %fn", result);
  125.   return result;
  126. }
  127. - (double)dm1i: (int)val1
  128. {
  129.   double result = val1 + 5;
  130.   printf ("dm1i: %fn", result);
  131.   return result;
  132. }
  133. - (const char *)testObject
  134. {
  135.   return [self name];
  136. }
  137. - (const char *)testObject: obj
  138. {
  139.   return [obj getNewName];
  140. }
  141. - (const char *)testObject: obj0 : obj1
  142. {
  143.   const char *buf0 = [obj0 getNewName];
  144.   const char *buf1 = [obj1 getNewName];
  145.   char buf[strlen (buf0) + 1 + strlen (buf1) + 1], *p = buf;
  146.   p = stpcpy (buf, buf0);
  147.   p = stpcpy (p, ",");
  148.   p = stpcpy (p, buf1);
  149.   return SSTRDUP (buf);
  150. }
  151. - (const char *)testObject: obj0 : obj1 : obj2
  152. {
  153.   const char *buf0 = [obj0 getNewName];
  154.   const char *buf1 = [obj1 getNewName];
  155.   const char *buf2 = [obj2 getNewName];
  156.   char buf[strlen (buf0) + 1 + strlen (buf1) + strlen (buf2) + 1], *p = buf;
  157.   p = stpcpy (buf, buf0);
  158.   p = stpcpy (p, ",");
  159.   p = stpcpy (p, buf1);
  160.   p = stpcpy (p, ",");
  161.   p = stpcpy (p, buf2);
  162.   return SSTRDUP (buf);
  163. }
  164. @end
  165. @interface BaseObject: CreateDrop
  166. {
  167.   id delegateObject;
  168. }
  169. - setDelegateObject: delegateObject;
  170. - (retval_t)forward: (SEL)sel :(arglist_t)argFrame;
  171. @end
  172. @implementation BaseObject
  173. - setDelegateObject: anObject
  174. {
  175.   delegateObject = anObject;
  176.   return self;
  177. }
  178. #define isnumeric(c) (isdigit ((int) c) || c == '+' || c == '-')
  179. static const char *
  180. strip_type_sig (const char *sig)
  181. {
  182.   size_t i;
  183.   size_t newlen = 0;
  184.   
  185.   for (i = 0; sig[i]; i++)
  186.     if (!isnumeric (sig[i]))
  187.       newlen++;
  188.   
  189.   {
  190.     char *newsig = xmalloc (newlen + 1);
  191.     
  192.     newlen = 0;
  193.     for (i = 0; sig[i]; i++)
  194.       if (!isnumeric (sig[i]))
  195.         newsig[newlen++] = sig[i];
  196.     newsig[newlen] = '';
  197.     return newsig;
  198.   }
  199. }
  200. #ifdef GNUSTEP
  201. - (void) forwardInvocation: (NSInvocation*)anInvocation
  202. {
  203.   id fa, fc;
  204.   SEL aSel = [anInvocation selector];
  205.   const char *type = sel_get_type (aSel);
  206.   NSArgumentInfo info;  
  207.   types_t val;
  208.   const char *stripped_type;
  209.   fprintf(stderr, "GNUstep forwardInvocation: n");
  210.   fprintf(stderr, "type %sn", type);
  211.   [anInvocation invokeWithTarget: delegateObject];
  212. }
  213. #endif
  214. - (retval_t)forward: (SEL)aSel :(arglist_t)argFrame
  215. {
  216.   id fa, fc;
  217.   const char *type = sel_get_type (aSel);  
  218.   NSArgumentInfo info;  
  219.   types_t val;
  220.   const char *stripped_type;
  221.   fa = [FArguments createBegin: getCZone (getZone (self))];
  222.   if (!type)
  223.     {
  224.       aSel = sel_get_any_typed_uid (sel_get_name (aSel));
  225.       type = sel_get_type (aSel);
  226.       if (!type)
  227.         abort ();
  228.     }
  229.   stripped_type = strip_type_sig (type);
  230.   {
  231.     const char *sig = mframe_build_signature (stripped_type, NULL, NULL, NULL);
  232.     printf ("{%s} {%s}{%s}n", stripped_type, type, sig);
  233.     sig = mframe_next_arg (sig, &info);
  234.     mframe_get_arg (argFrame, &info, &val);
  235.     [fa setObjCReturnType: *info.type];
  236.     /* skip object and selector */
  237.     sig = mframe_next_arg (sig, &info);
  238.     sig = mframe_next_arg (sig, &info);
  239.     while ((sig = mframe_next_arg (sig, &info)))
  240.       {
  241.         mframe_get_arg (argFrame, &info, &val);
  242. [fa addArgument: &val ofObjCType: *info.type];
  243.       }
  244.     XFREE (sig);
  245.   }
  246.   fa = [fa createEnd];
  247.   fc = [FCall createBegin: getCZone (getZone (self))];
  248.   fc = [fc setArguments: fa];
  249.   fc = [fc setMethodFromSelector: aSel inObject: delegateObject];
  250.   fc = [fc createEnd];
  251.   [fc performCall];
  252.   
  253.   {
  254.     extern void *alloca (size_t);
  255.     types_t retBuf;
  256.     retval_t retValBuf = alloca (MFRAME_RESULT_SIZE);
  257.     retval_t ret = [fc getRetVal: retValBuf buf: &retBuf];
  258.    
  259.     [fc dropAllocations: YES];
  260.     [fa dropAllocations: YES];
  261.     
  262.     return ret;
  263.   }
  264. }
  265. @end
  266. int
  267. main (int argc, const char **argv)
  268. {
  269.   id obj;
  270. #ifdef GNUSTEP
  271.   NSAutoreleasePool *pool;
  272. #endif
  273.   initSwarmBatch (argc, argv);
  274. #ifdef GNUSTEP
  275.   pool = [NSAutoreleasePool new];
  276. #endif
  277.   obj = [[BaseObject createBegin: globalZone]
  278.              setDelegateObject: [DelegateObject create: globalZone]];
  279.   if (strcmp ([obj m1: 1 float: 2.0 double: 3.0], "1 2.00 3.00") != 0)
  280.     return 1;
  281.   if (strcmp ([obj m2: 4 double: 5.0 float: 6.0], "4 6.00 5.00") != 0)
  282.     return 1;
  283.   if (strcmp ([obj m3: 7.0 double: 8.0 int: 9], "9 7.00 8.00") != 0)
  284.     return 1;
  285.   if (strcmp ([obj m4: 10.0 float: 11.0 int: 12], "12 11.00 10.00") != 0)
  286.     return 1;
  287.   if (strcmp ([obj m5: 13.0 double: 14.0 int: 15 int2: 16], "15 16 13.00 14.00") != 0)
  288.     return 1;
  289.   if (strcmp ([obj m6: 17.0 float2: 18.0 double: 19.0 int: 20 int2: 21], "20 21 17.00 18.00 19.00") != 0)
  290.     return 1;
  291.   {
  292.     char ret = [obj cm1i: 1];
  293.     
  294.     if (ret != 'B')
  295.       {
  296.         fprintf (stderr, "got: `%c' (%d)n", ret, (int) ret);
  297.         return 1;
  298.       }
  299.   }
  300.   {
  301.     short ret = [obj sm1i: 2];
  302.     if (ret != 3)
  303.       {
  304.         fprintf (stderr, "got: %hdn", ret);
  305.         return 1;
  306.       }
  307.   }
  308.   if ([obj im1i: 3] != 5)
  309.     return 1;
  310.   if ([obj lm1i: 4] != 7)
  311.     return 1;
  312.   if ([obj fm1i: 5] != 9.0)
  313.     return 1;
  314.   if ([obj dm1i: 6] != 11.0)
  315.     return 1;
  316.   {
  317.     id anObject0 = [[[AnotherObject createBegin: globalZone]
  318.                       setName: "anObject0"]
  319.                      createEnd];
  320.     id anObject1 = [[[AnotherObject createBegin: globalZone]
  321.                       setName: "anObject1"]
  322.                      createEnd];
  323.     id anObject2 = [[[AnotherObject createBegin: globalZone]
  324.                       setName: "anObject2"]
  325.                      createEnd];
  326.     const char *ret;
  327.     ret = (const char *) [obj perform: M(testObject)];
  328.     printf ("[testObject] `%s'n", ret);
  329.     if (strcmp (ret, "DelegateObject") != 0)
  330.       return 1;
  331.     ret = (const char *) [obj perform: M(testObject:)
  332.                               with: anObject0];
  333.     printf ("[testObject:] `%s'n", ret);
  334.     if (strcmp (ret, "<anObject0>") != 0)
  335.       return 1;
  336.     
  337.     ret = (const char *) [obj perform: M(testObject::)
  338.                               with: anObject0
  339.                               with: anObject1];
  340.     printf ("[testObject::] `%s'n", ret);
  341.     if (strcmp (ret, "<anObject0>,<anObject1>") != 0)
  342.       return 1;
  343.     ret = (const char *) [obj perform: M(testObject:::)
  344.                               with: anObject0
  345.                               with: anObject1
  346.                               with: anObject2];
  347.     printf ("[testObject:::] `%s'n", ret);
  348.     if (strcmp (ret, "<anObject0>,<anObject1>,<anObject2>") != 0)
  349.       return 1;
  350.   }
  351.   return 0;
  352. }