swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Module_activity_ [Swarm 1.4.1]


From: Marcus G. Daniels
Subject: Re: Module_activity_ [Swarm 1.4.1]
Date: 21 Nov 2000 17:39:34 -0800
User-agent: Gnus/5.070084 (Pterodactyl Gnus v0.84) Emacs/20.4

>>>>> "GP" == Gary Polhill <address@hidden> writes:

GP> For some odd reason, I get a segfault when c in the above loop is
GP> Module_activity_

It's a bug that +conformsTo: doesn't work with classes without parents, i.e.
generated classes like Module_{defobj,activity,collections,super}_.

It should work fine to do a test like this:

   c->protocols && [c conformsTo: p]

(instead of just the latter)

For example, using the current Swarm sources, this program collects
protocol-conforming classes as you describe:

#import <simtools.h> // initSwarm
#import <defobj.h> // Zone, Create
#import <defobj/Create.h> // CreateDrop
#import <collections.h> // List
#include <misc.h> // xmalloc

@protocol Foo
- (void)foo;
@end

@interface Class1: CreateDrop <Foo>
- (void)foo;
@end

@implementation Class1
- (void)foo
{
}
@end

@interface Class2: CreateDrop <Foo>
- (void)foo;
@end

@implementation Class2
- (void)foo
{
}
@end

@interface Class3: CreateDrop
- (void)bar;
@end

@implementation Class3
- (void)bar
{
}
@end

@interface ClassInfo: CreateDrop
{
  id <List> cl;
}
+ create: (id <Zone>)aZone;
- (void)getClassesForProtocol: (Protocol *)p inList: (id <List>)l;
@end

@implementation ClassInfo

+ create: (id <Zone>)aZone
{
  void *enum_state;
  struct list_of_classes {
    Class theClass;
    struct list_of_classes *next;
  } *l, *m, *n;
  Class aClass;
  struct objc_protocol_list *pl;
  
  ClassInfo *info = [super create: aZone];
  
  /* Loop through all the classes. For some reason this is messed up if you
     make any method calls during the loop. So, first we build the list using
     conventional memory. Then we'll put it into a swarm list. */
  n = l = NULL;
  enum_state = NULL;
  while ((aClass = objc_next_class (&enum_state)) != Nil)
    {
      m = n;
      n = (struct list_of_classes *) xmalloc (sizeof (struct list_of_classes));
      if (l == NULL)
        l = n;
      if (n == NULL)
        {
          fprintf (stderr, "Memory allocation error\n");
          exit (1);
        }
      n->theClass = aClass;
      n->next = NULL;
      if (m != NULL)
        m->next = n;
    }
  
  /* Right, now we build the swarm list. The Class type is a pointer, so it's
     OK to treat it like an id. Whilst we build the Swarm list, we destroy
     the one we've just created. Thus all memory for the object sits in the
     Swarm Zone, and the object can be destroyed when the Zone is destroyed. */
  
  info->cl = [List create: aZone];
  
  m = NULL;
  for (n = l; n != NULL; n = n->next)
    {
      if (m != NULL)
        free(m);
      if (n->theClass != Nil)
        [info->cl addFirst: n->theClass];
      m = n;
    }
  if (m != NULL)
    free(m);
  return info;
}

// When I want to know all the classes that follow a particular protocol,
// I search through the Swarm List using the conformsTo: method:

-(void)getClassesForProtocol: (Protocol *)p inList: (id <List>)l
{
  id <Index>i = [cl begin: scratchZone];
  Class c;
  
  for (c = (Class) [i next]; [i getLoc] == Member; c = (Class) [i next])
    {
      if (c->protocols && [c conformsTo: p])
        [l addFirst: c];
    }
  [i drop];
}

@end

int
main (int argc, const char **argv)
{
  initSwarmBatch (argc, argv);

  {
    id classInfo = [ClassInfo create: globalZone];
    id <List> l = [List create: globalZone];
    id <Index> index;
    id member;
    
    [classInfo getClassesForProtocol: @protocol (Foo) inList: l];

    index = [l begin: scratchZone];
    for (member = [index next];
           [index getLoc] == Member;
         member = [index next])
      {
        printf ("Class: `%s'\n", ((Class) member)->name);
      }
  }
  return 0;
}

/*
Local Variables:
compile-command: "$SWARMHOME/bin/libtool-swarm --mode=link gcc 
-DAPPNAME=searchClasses -D_GNU_SOURCE -o searchClasses -g -Wno-import 
-I$SWARMHOME/include/swarm -L$SWARMHOME/lib/swarm searchClasses.m -lswarm 
-lobjc"
End:
*/

                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]