swarm-support
[Top][All Lists]
Advanced

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

GridData patch effort; Protocol of arguments in obj-c


From: pauljohn
Subject: GridData patch effort; Protocol of arguments in obj-c
Date: Tue, 07 Aug 2001 22:18:48 -0500

I've worked out my patch for the Space library to make it easier
to provide Grid objects to Object2dDisplay and Value2dDisplay. 
It creates a small protocol GridData which is implemented by
Discrete2d. That way, to provide a "GridData" object, one may
supply a much simpler class than a Discrete2d in the
setDiscrete2dToDisplay methods.  For example, my MultiGrid2d (a
variant of multiple occupancy grid) class can serve as a data
provider for a graph. At least it will, I hope; please see
pathetic whining below.

The  attached patch, GridData.patch, applies to the Swarm space
library and Swarm does compile.  (if I figure out what is wrong
below, I'll soon make the rpms available. These are for
Swarm-2.1.100 (2001-08-07) Here is the GridData protocol:


@protocol GridData

USING
- (unsigned)getSizeX;
- (unsigned)getSizeY;
- (id *)getLattice;
- (long *)getOffsets;

@end

I put in USING because without it the Swarm build fails saying I
have to declare a USING phase.

Now I run into a usage problem, however, after installing this
patched swarm.
The class I created has those 4 methods, it imports space.h, and
it has:

@interface MultiGrid2d: SwarmObject <GridData> {

So I expected everything was a go. When I try to compile a
program that uses my MultGrid2d class, I get this:

MultiGrid2d.m:583: warning: incomplete implementation of class
`MultiGrid2d'
MultiGrid2d.m:583: warning: method definition for `-_I_Using'
not found
MultiGrid2d.m:583: warning: class `MultiGrid2d' does not fully
implement the `GridData' protocol

I don't understand what _I_Using is?  I see it in
swarm/src/deftype.h but I dont' know how I'm supposed to find
it.

 

Also, I ran into this question. <incoherent rant mode begins>
The way Objective-C implements the idea of a protocol is
frustrating! Originally, in Swarm src/space/space.h, I found
this:

- setDiscrete2dToDisplay: (id <Discrete2d>)c;

but in the Object2dDisplay class, the implementing method's
argument is given without protocol:

- setDiscrete2dToDisplay: c
{
  if (![c conformsTo: @protocol (Discrete2d)])
    [ProtocolViolation
      raiseEvent:
        "Argument `%s' to Object2dDisplay setDiscrete2dDisplay:
does not\n"
      "conform to Discrete2d protocol\n",
      [c name]];
  discrete2d = c;
  return self;
}

Looking at that, I wondered why the compiler accepts this as an
implementation of the protocol at all, since the argument is not
typed. My boy scout instinct is this:

- setDiscrete2dToDisplay: (id <GridData>) c
{
  if (![c conformsTo: @protocol (Discrete2d)])
    [ProtocolViolation
      raiseEvent:
        "Argument `%s' to Object2dDisplay setDiscrete2dDisplay:
does not\n"
      "conform to GridData protocol\n",
      [c name]];
  
  discrete2d = c;

  return self;
}

But that does no good, because when the Swarm build gets to that
method, it quits because the protocol <GridData> does not list
Obj-c Object class methods conformsTo: and name.

home/pauljohn/LinuxDownloads/redhat/BUILD/swarm-2.1.100.20010806/src/space/Object2dDisplay.m:
In function `-[Object2dDisplay setDiscrete2dToDisplay:]':
/home/pauljohn/LinuxDownloads/redhat/BUILD/swarm-2.1.100.20010806/src/space/Object2dDisplay.m:40:
warning: method `conformsTo:' not implemented by protocol.
/home/pauljohn/LinuxDownloads/redhat/BUILD/swarm-2.1.100.20010806/src/space/Object2dDisplay.m:45:
warning: method `name' not implemented by protocol.
/home/pauljohn/LinuxDownloads/redhat/BUILD/swarm-2.1.100.20010806/src/space/Object2dDisplay.m:47:
warning: object does not conform to the `Discrete2d' protocol
make[3]: *** [Object2dDisplay.lo] Error 1

So then I realized it was no accident that the argument was not
typed in the first place, and I had to take another run at it.

This, and other experience with protocols in Objective-C, leaves
me feeling a bit frustrated.  It seems to me the compiler ought
to be nice enough to assume that a class is subclassed from
Object.  When I declare a protocol, it does not have the minimal
sort of promise I intend to make,  like "I have these methods
for you (and maybe some others)," instead, it amounts to a
statement "I have these methods and you, mister compiler, should
act as if I have no others."  So, If I want to overcome this
compiler warning and compile swarm, I either have to add
conformsTo: and name to the <GridData> protocol, or perhaps
create a protocol for the Obj-C base Object--like <Object> ? and
then declare that.

-- 
Paul E. Johnson                       email: address@hidden
Dept. of Political Science           
http://lark.cc.ukans.edu/~pauljohn
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66045                FAX: (785) 864-5700
diff -cr space-orig/Object2dDisplay.m space/Object2dDisplay.m
*** space-orig/Object2dDisplay.m        Tue Aug  7 19:46:22 2001
--- space/Object2dDisplay.m     Tue Aug  7 21:15:16 2001
***************
*** 41,47 ****
      [ProtocolViolation
        raiseEvent:
          "Argument `%s' to Object2dDisplay setDiscrete2dDisplay: does not\n"
!       "conform to Discrete2d protocol\n",
        [c name]];
    
    discrete2d = c;
--- 41,47 ----
      [ProtocolViolation
        raiseEvent:
          "Argument `%s' to Object2dDisplay setDiscrete2dDisplay: does not\n"
!       "conform to GridData protocol\n",
        [c name]];
    
    discrete2d = c;
diff -cr space-orig/Value2dDisplay.h space/Value2dDisplay.h
*** space-orig/Value2dDisplay.h Tue Aug  7 19:46:22 2001
--- space/Value2dDisplay.h      Tue Aug  7 21:13:42 2001
***************
*** 19,25 ****
    int modFactor;
    int colorConstant;
  }
! + create: aZone setDisplayWidget: (id <Raster>)r colormap: (id <Colormap>)c 
setDiscrete2dToDisplay: d;
  - setDisplayWidget: (id <Raster>)r colormap: (id <Colormap>)c;
  - setDiscrete2dToDisplay: c;
  - setDisplayMappingM: (int)m C: (int)c;         // linear mapping
--- 19,25 ----
    int modFactor;
    int colorConstant;
  }
! + create: aZone setDisplayWidget: (id <Raster>)r colormap: (id <Colormap>)c 
setDiscrete2dToDisplay:  (id <GridData>) d;
  - setDisplayWidget: (id <Raster>)r colormap: (id <Colormap>)c;
  - setDiscrete2dToDisplay: c;
  - setDisplayMappingM: (int)m C: (int)c;         // linear mapping
diff -cr space-orig/Value2dDisplay.m space/Value2dDisplay.m
*** space-orig/Value2dDisplay.m Tue Aug  7 19:46:22 2001
--- space/Value2dDisplay.m      Tue Aug  7 21:14:31 2001
***************
*** 36,46 ****
  
  - setDiscrete2dToDisplay: c
  {
!   if (![c conformsTo: @protocol (Discrete2d)])
      [ProtocolViolation
        raiseEvent:
          "Argument `%s' to Value2dDisplay setDiscrete2dDisplay: does\n"
!       "not conform to Discrete2d protocol\n",
        [c name]];
    
    discrete2d = c;
--- 36,46 ----
  
  - setDiscrete2dToDisplay: c
  {
!   if (![c conformsTo: @protocol (GridData)])
      [ProtocolViolation
        raiseEvent:
          "Argument `%s' to Value2dDisplay setDiscrete2dDisplay: does\n"
!       "not conform to GridData protocol\n",
        [c name]];
    
    discrete2d = c;
diff -cr space-orig/space.h space/space.h
*** space-orig/space.h  Tue Aug  7 19:46:22 2001
--- space/space.h       Tue Aug  7 20:25:55 2001
***************
*** 23,29 ****
  #import <objectbase.h>
  #import <gui.h> // Raster, Colormap
  
! @protocol Discrete2d <SwarmObject, CREATABLE>
  //S: Root class of all 2d discrete spaces.
  
  //D: A Discrete2d is basically a 2d array of ids.  
--- 23,58 ----
  #import <objectbase.h>
  #import <gui.h> // Raster, Colormap
  
! 
! 
! @protocol GridData 
! //S: Methods required by widgets that display grids. User defined 
! //S: space objects must adopt this or any implementor of it 
! //S: in order to be accepted as data providers 
! //S: in the setDiscrete2dToDisplay method of  Value2dDisplay and 
! //S: Object2dDisplay  objects. User spaces must also define the 
! //S: macro discrete2dSiteAt(), versions of which can be 
! //S: found in Grid2d.h or Discrete2d.h.
! 
! 
! USING 
!  
! //M: Get the size of the lattice in the X dimension. 
! - (unsigned)getSizeX; 
!  
! //M: Get the size of the lattice in the Y dimension. 
! - (unsigned)getSizeY; 
! 
! //M: Returns the lattice pointer - use this for fast access. 
! - (id *)getLattice; 
! 
! - (long *)getOffsets; 
! 
! @end 
! 
! 
! 
! @protocol Discrete2d <SwarmObject, CREATABLE, GridData>
  //S: Root class of all 2d discrete spaces.
  
  //D: A Discrete2d is basically a 2d array of ids.  
***************
*** 98,108 ****
  - setLattice: (id *)lattice;
  
  USING
- //M: Get the size of the lattice in the X dimension.
- - (unsigned)getSizeX;
- 
- //M: Get the size of the lattice in the Y dimension.
- - (unsigned)getSizeY;
  
  //M: Return the pointer stored at (x,y).
  - getObjectAtX: (unsigned)x Y: (unsigned)y;
--- 127,132 ----
***************
*** 128,136 ****
  //M: Fills the space using putObject.
  - fillWithObject: anObj;
  
- //M: Returns the lattice pointer - use this for fast access. 
- - (id *)getLattice;
- 
  //M: This method reads a PGM formatted file and pipes the data into
  //M: a Discrete2d object. 
  - (int)setDiscrete2d: (id <Discrete2d>)a toFile: (const char *)filename;
--- 152,157 ----
***************
*** 139,146 ****
  //M: another Discrete2d object. It assumes that both objects already exist.
  - copyDiscrete2d: (id <Discrete2d>)a toDiscrete2d: (id <Discrete2d>)b;
  
- - (long *)getOffsets;
- 
  @end
  
  @protocol DblBuffer2d <Discrete2d, CREATABLE>
--- 160,165 ----
***************
*** 203,215 ****
  
  CREATING
  //M: Convenience constructor for Value2dDisplay
! + create: (id <Zone>)aZone setDisplayWidget: (id <Raster>)r colormap: (id 
<Colormap>)c setDiscrete2dToDisplay: (id <Discrete2d>)d;
  
  //M: Set the display widget and the colourmap to use to draw the value array. 
  - setDisplayWidget: (id <Raster>)r colormap: (id <Colormap>)c;
  
  //M: Set which array to draw. 
! - setDiscrete2dToDisplay: (id <Discrete2d>)c;
  
  USING
  //M: Linear transform of states to colours for drawing. 
--- 222,234 ----
  
  CREATING
  //M: Convenience constructor for Value2dDisplay
! + create: (id <Zone>)aZone setDisplayWidget: (id <Raster>)r colormap: (id 
<Colormap>)c setDiscrete2dToDisplay: (id <GridData>)d;
  
  //M: Set the display widget and the colourmap to use to draw the value array. 
  - setDisplayWidget: (id <Raster>)r colormap: (id <Colormap>)c;
  
  //M: Set which array to draw. 
! - setDiscrete2dToDisplay: (id <GridData>)c;
  
  USING
  //M: Linear transform of states to colours for drawing. 
***************
*** 315,327 ****
  
  CREATING
  //M: Convenience constructor for Object2dDisplay
! + create: (id <Zone>)aZone setDisplayWidget: (id <Raster>)r 
setDiscrete2dToDisplay: (id <Discrete2d>)c setDisplayMessage: (SEL)s;
  
  //M: Set the display widget to use for drawing.
  - setDisplayWidget: (id <Raster>)r;
  
  //M: Set the 2d array to draw.
! - setDiscrete2dToDisplay: (id <Discrete2d>)c;
  
  //M: Set the message to be sent to each object in the grid to make it
  //M: draw itself. 
--- 334,346 ----
  
  CREATING
  //M: Convenience constructor for Object2dDisplay
! + create: (id <Zone>)aZone setDisplayWidget: (id <Raster>)r 
setDiscrete2dToDisplay: (id <GridData>)c setDisplayMessage: (SEL)s;
  
  //M: Set the display widget to use for drawing.
  - setDisplayWidget: (id <Raster>)r;
  
  //M: Set the 2d array to draw.
! - setDiscrete2dToDisplay: (id <GridData>)c;
  
  //M: Set the message to be sent to each object in the grid to make it
  //M: draw itself. 

reply via email to

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