Oolite 1.91.0.7646-241128-10e222e
Loading...
Searching...
No Matches
OOShipGroup Class Reference

#include <OOShipGroup.h>

+ Inheritance diagram for OOShipGroup:
+ Collaboration diagram for OOShipGroup:

Instance Methods

(id) - init
 
(id) - initWithName:
 
(NSString *) - name
 
(void) - setName:
 
(ShipEntity *) - leader
 
(void) - setLeader:
 
(NSEnumerator *) - objectEnumerator
 
(NSEnumerator *) - mutationSafeEnumerator
 
(NSSet *) - members
 
(NSArray *) - memberArray
 
(NSSet *) - membersExcludingLeader
 
(NSArray *) - memberArrayExcludingLeader
 
(BOOL) - containsShip:
 
(BOOL) - addShip:
 
(BOOL) - removeShip:
 
(NSUInteger) - count
 
(BOOL) - isEmpty
 
(void) - dealloc [implementation]
 
(NSString *) - descriptionComponents [implementation]
 
(BOOL) - resizeTo: [implementation]
 
(void) - cleanUp [implementation]
 
(NSUInteger) - updateCount [implementation]
 
(NSUInteger) - countByEnumeratingWithState:objects:count: [implementation]
 
(BOOL) - suppressClangStuff [implementation]
 
(NSString *) - inspDescription [implementation]
 
(NSString *) - inspBasicIdentityLine [implementation]
 
(NSArray *) - debugInspectorModules [implementation]
 
(jsval) - oo_jsValueInContext: [implementation]
 
(void) - oo_clearJSSelf: [implementation]
 
- Instance Methods inherited from OOWeakRefObject
(id) - weakSelf
 
(id) - weakRetain [implementation]
 
(void) - weakRefDied: [implementation]
 
- Instance Methods inherited from <OOWeakReferenceSupport>
(id) - OO_RETURNS_RETAINED
 

Class Methods

(instancetype) + groupWithName:
 
(instancetype) + groupWithName:leader:
 
(static id) + ShipGroupIterate [implementation]
 

Protected Attributes

< NSFastEnumeration > unsigned long _updateCount
 
OOWeakReference ** _members
 
OOWeakReference_leader
 
NSString * _name
 
struct JSObject * _jsSelf
 
- Protected Attributes inherited from OOWeakRefObject
OOWeakReferenceweakSelf
 

Detailed Description

Definition at line 33 of file OOShipGroup.h.

Method Documentation

◆ addShip:

- (BOOL) addShip: (ShipEntity *) ship

Definition at line 54 of file OOShipGroup.m.

342 :(ShipEntity *)ship
343{
344 _updateCount++;
345
346 if ([self containsShip:ship]) return YES; // it's in the group already, result!
347
348 // Ensure there's space.
349 if (_count == _capacity)
350 {
351 if (![self resizeTo:(_capacity > kMaxFreeSpace) ? (_capacity + kMaxFreeSpace) : (_capacity * 2)])
352 {
353 if (![self resizeTo:_capacity + 1])
354 {
355 // Out of memory?
356 return NO;
357 }
358 }
359 }
360
361 _members[_count++] = [ship weakRetain];
362 return YES;
363}
@ kMaxFreeSpace
Definition OOShipGroup.m:44
OOWeakReference ** _members
Definition OOShipGroup.h:41
< NSFastEnumeration > unsigned long _updateCount
Definition OOShipGroup.h:40

◆ cleanUp

- (void) cleanUp
implementation

Provided by category OOShipGroup(Private).

Definition at line 54 of file OOShipGroup.m.

444{
445 NSUInteger newCapacity = _capacity;
446
447 if (_count >= kMaxFreeSpace)
448 {
449 if (_capacity > _count + kMaxFreeSpace)
450 {
451 newCapacity = _count + 1; // +1 keeps us at powers of two + multiples of kMaxFreespace.
452 }
453 }
454 else
455 {
456 if (_capacity > _count * 2)
457 {
458 newCapacity = OORoundUpToPowerOf2_NS(_count);
459 if (newCapacity < kMinSize) newCapacity = kMinSize;
460 }
461 }
462
463 if (newCapacity != _capacity) [self resizeTo:newCapacity];
464}
@ kMinSize
Definition OOShipGroup.m:43

Referenced by countByEnumeratingWithState:objects:count:.

+ Here is the caller graph for this function:

◆ containsShip:

- (BOOL) containsShip: (ShipEntity *) ship

Definition at line 54 of file OOShipGroup.m.

267 :(ShipEntity *)ship
268{
269 ShipEntity *containedShip = nil;
270
271 for (containedShip in self)
272 {
273 if ([ship isEqual:containedShip])
274 {
275 return YES;
276 }
277 }
278
279 return NO;
280}
return nil

◆ count

- (NSUInteger) count

Definition at line 54 of file OOShipGroup.m.

404{
405 NSEnumerator *memberEnum = nil;
406 NSUInteger result = 0;
407
408 if (_count != 0)
409 {
410 memberEnum = [self objectEnumerator];
411 while ([memberEnum nextObject] != nil) result++;
412 }
413
414 assert(result == _count);
415
416 return result;
417}

◆ countByEnumeratingWithState:objects:count:

- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState *) state
objects: (id *) stackbuf
count: (NSUInteger) len 
implementation

Definition at line 473 of file OOShipGroup.m.

515 :(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len
516{
517 NSUInteger srcIndex, dstIndex = 0;
518 ShipEntity *item = nil;
519 BOOL cleanupNeeded = NO;
520
521 srcIndex = state->state;
522 while (srcIndex < _count && dstIndex < len)
523 {
524 item = [_members[srcIndex] weakRefUnderlyingObject];
525 if (item != nil)
526 {
527 stackbuf[dstIndex++] = item;
528 srcIndex++;
529 }
530 else
531 {
532 _members[srcIndex] = _members[--_count];
533 cleanupNeeded = YES;
534 }
535 }
536
537 if (cleanupNeeded) [self cleanUp];
538
539 state->state = srcIndex;
540 state->itemsPtr = stackbuf;
541 state->mutationsPtr = &_updateCount;
542
543 return dstIndex;
544}
NSUInteger count()

References OOShipGroupEnumerator::_cleanupNeeded, OOShipGroupEnumerator::_considerCleanup, OOShipGroupEnumerator::_group, OOShipGroupEnumerator::_index, _members, _updateCount, OOShipGroupEnumerator::_updateCount, cleanUp, and nil.

+ Here is the call graph for this function:

◆ dealloc

- (void) dealloc
implementation

Reimplemented from OOWeakRefObject.

Definition at line 54 of file OOShipGroup.m.

120{
121 NSUInteger i;
122
123 for (i = 0; i < _count; i++)
124 {
125 [_members[i] release];
126 }
127 free(_members);
128 [_name release];
129
130 [super dealloc];
131}

◆ debugInspectorModules

- (NSArray *) debugInspectorModules
implementation

Provided by category OOShipGroup(OOInspectorExtensions).

Definition at line 1 of file OOShipGroupDebugInspectorModule.m.

131{
132 return [[super debugInspectorModules] arrayByAddingInspectorModuleOfClass:[OOShipGroupDebugInspectorModule class]
133 forObject:(id)self];
134}

◆ descriptionComponents

- (NSString *) descriptionComponents
implementation

Definition at line 54 of file OOShipGroup.m.

135{
136 NSString *desc = [NSString stringWithFormat:@"%llu ships", (unsigned long long)_count];
137 if ([self name] != nil)
138 {
139 desc = [NSString stringWithFormat:@"\"%@\", %@", [self name], desc];
140 }
141 if ([self leader] != nil)
142 {
143 desc = [NSString stringWithFormat:@"%@, leader: %@", desc, [[self leader] shortDescription]];
144 }
145 return desc;
146}
NSString * name()
ShipEntity * leader()

◆ groupWithName:

+ (instancetype) groupWithName: (NSString *) name

Definition at line 54 of file OOShipGroup.m.

105 :(NSString *)name
106{
107 return [[[self alloc] initWithName:name] autorelease];
108}

◆ groupWithName:leader:

+ (instancetype) groupWithName: (NSString *) name
leader: (ShipEntity *) leader 

Definition at line 54 of file OOShipGroup.m.

111 :(NSString *)name leader:(ShipEntity *)leader
112{
113 OOShipGroup *result = [self groupWithName:name];
114 [result setLeader:leader];
115 return result;
116}
void setLeader:(ShipEntity *leader)

◆ init

- (id) init

Definition at line 54 of file OOShipGroup.m.

81{
82 return [self initWithName:nil];
83}

◆ initWithName:

- (id) initWithName: (NSString *) name

Definition at line 54 of file OOShipGroup.m.

86 :(NSString *)name
87{
88 if ((self = [super init]))
89 {
90 _capacity = kMinSize;
91 _members = malloc(sizeof *_members * _capacity);
92 if (_members == NULL)
93 {
94 [self release];
95 return nil;
96 }
97
98 [self setName:name];
99 }
100
101 return self;
102}

◆ inspBasicIdentityLine

- (NSString *) inspBasicIdentityLine
implementation

Provided by category OOShipGroup(OOInspectorExtensions).

Definition at line 1 of file OOShipGroupDebugInspectorModule.m.

123{
124 NSString *name = [self name];
125 if (name != nil) return [NSString stringWithFormat:@"Group \"%@\"", name];
126 else return @"Anonymous group";
127}

◆ inspDescription

- (NSString *) inspDescription
implementation

Provided by category OOShipGroup(OOInspectorExtensions).

Definition at line 1 of file OOShipGroupDebugInspectorModule.m.

113{
114 NSString *name = [self name];
115 if (name != nil) name = [NSString stringWithFormat:@"\"%@\"", name];
116 else name = @"anonymous";
117
118 return [NSString stringWithFormat:@"%@, %lu ships", name, [self count]];
119}

◆ isEmpty

- (BOOL) isEmpty

Definition at line 54 of file OOShipGroup.m.

421{
422 if (_count == 0) return YES;
423
424 return [[self objectEnumerator] nextObject] == nil;
425}

◆ leader

- (ShipEntity *) leader

Definition at line 54 of file OOShipGroup.m.

168{
169 ShipEntity *result = [_leader weakRefUnderlyingObject];
170
171 // If reference is stale, delete weakref object.
172 if (result == nil && _leader != nil)
173 {
174 [_leader release];
175 _leader = nil;
176 }
177
178 return result;
179}
OOWeakReference * _leader
Definition OOShipGroup.h:42

Referenced by ShipGroupAddShip(), and ShipGroupGetProperty().

+ Here is the caller graph for this function:

◆ memberArray

- (NSArray *) memberArray

Definition at line 54 of file OOShipGroup.m.

221{
222 id *objects = NULL;
223 NSUInteger count = 0;
224 NSArray *result = nil;
225
226 if (_count == 0) return [NSArray array];
227
228 objects = malloc(sizeof *objects * _count);
229 for (id ship in self)
230 {
231 objects[count++] = ship;
232 }
233
234 result = [NSArray arrayWithObjects:objects count:count];
235 free(objects);
236
237 return result;
238}

Referenced by ShipGroupGetProperty().

+ Here is the caller graph for this function:

◆ memberArrayExcludingLeader

- (NSArray *) memberArrayExcludingLeader

Definition at line 54 of file OOShipGroup.m.

242{
243 id *objects = NULL;
244 NSUInteger count = 0;
245 NSArray *result = nil;
247
248 if (_count == 0) return [NSArray array];
249 leader = self.leader;
250
251 objects = malloc(sizeof *objects * _count);
252 for (id ship in self)
253 {
254 if (ship != leader)
255 {
256 objects[count++] = ship;
257 }
258 }
259
260 result = [NSArray arrayWithObjects:objects count:count];
261 free(objects);
262
263 return result;
264}

Referenced by ShipGetProperty().

+ Here is the caller graph for this function:

◆ members

- (NSSet *) members

Definition at line 54 of file OOShipGroup.m.

208{
209 return [NSSet setWithArray:[self memberArray]];
210}

◆ membersExcludingLeader

- (NSSet *) membersExcludingLeader

Definition at line 54 of file OOShipGroup.m.

214{
215 return [NSSet setWithArray:[self memberArrayExcludingLeader]];
216}

◆ mutationSafeEnumerator

- (NSEnumerator *) mutationSafeEnumerator

Definition at line 54 of file OOShipGroup.m.

202{
203 return [[self memberArray] objectEnumerator];
204}

◆ name

- (NSString *) name

Definition at line 54 of file OOShipGroup.m.

150{
151 return _name;
152}
NSString * _name
Definition OOShipGroup.h:43

Referenced by ShipGroupGetProperty().

+ Here is the caller graph for this function:

◆ objectEnumerator

- (NSEnumerator *) objectEnumerator

Definition at line 54 of file OOShipGroup.m.

196{
197 return [[[OOShipGroupEnumerator alloc] initWithShipGroup:self] autorelease];
198}

◆ oo_clearJSSelf:

- (void) oo_clearJSSelf: (JSObject *) selfVal
implementation

Provided by category OOShipGroup(OOJavaScriptExtensions).

Definition at line 353 of file OOJSShipGroup.m.

246 :(JSObject *)selfVal
247{
248 if (_jsSelf == selfVal) _jsSelf = NULL;
249}

◆ oo_jsValueInContext:

- (jsval) oo_jsValueInContext: (JSContext *) context
implementation

Provided by category OOShipGroup(OOJavaScriptExtensions).

Definition at line 353 of file OOJSShipGroup.m.

227 :(JSContext *)context
228{
229 jsval result = JSVAL_NULL;
230
231 if (_jsSelf == NULL)
232 {
233 _jsSelf = JS_NewObject(context, &sShipGroupClass, sShipGroupPrototype, NULL);
234 if (_jsSelf != NULL)
235 {
236 if (!JS_SetPrivate(context, _jsSelf, [self retain])) _jsSelf = NULL;
237 }
238 }
239
240 if (_jsSelf != NULL) result = OBJECT_TO_JSVAL(_jsSelf);
241
242 return result;
243}
static JSObject * sShipGroupPrototype
static JSClass sShipGroupClass

◆ removeShip:

- (BOOL) removeShip: (ShipEntity *) ship

Definition at line 54 of file OOShipGroup.m.

366 :(ShipEntity *)ship
367{
368 OOShipGroupEnumerator *shipEnum = nil;
369 ShipEntity *containedShip = nil;
370 NSUInteger index;
371 BOOL foundIt = NO;
372
373 _updateCount++;
374
375 if (ship == [self leader]) [self setLeader:nil];
376
377 shipEnum = (OOShipGroupEnumerator *)[self objectEnumerator];
378 [shipEnum setPerformCleanup:NO];
379 while ((containedShip = [shipEnum nextObject]))
380 {
381 if ([ship isEqual:containedShip])
382 {
383 index = [shipEnum index] - 1;
384 _members[index] = _members[--_count];
385 foundIt = YES;
386
387 // Clean up
388 [ship setGroup:nil];
389 [ship setOwner:ship];
390 [self cleanUp];
391 break;
392 }
393 }
394 return foundIt;
395}
void setPerformCleanup:(BOOL flag)
NSEnumerator * objectEnumerator()
void setGroup:(OOShipGroup *group)
void setOwner:(Entity *who_owns_entity)

◆ resizeTo:

- (BOOL) resizeTo: (NSUInteger) newCapacity
implementation

Provided by category OOShipGroup(Private).

Definition at line 54 of file OOShipGroup.m.

428 :(NSUInteger)newCapacity
429{
430 OOWeakReference **temp = NULL;
431
432 if (newCapacity < _count) return NO;
433
434 temp = realloc(_members, newCapacity * sizeof *_members);
435 if (temp == NULL) return NO;
436
437 _members = temp;
438 _capacity = newCapacity;
439 return YES;
440}

◆ setLeader:

- (void) setLeader: (ShipEntity *) leader

Definition at line 54 of file OOShipGroup.m.

183{
184 _updateCount++;
185
186 if (leader != [self leader])
187 {
188 [_leader release];
189 [self addShip:leader];
190 _leader = [leader weakRetain];
191 }
192}

Referenced by ShipGroupSetProperty().

+ Here is the caller graph for this function:

◆ setName:

- (void) setName: (NSString *) name

Definition at line 54 of file OOShipGroup.m.

155 :(NSString *)name
156{
157 _updateCount++;
158
159 if (_name != name)
160 {
161 [_name release];
162 _name = [name retain];
163 }
164}

Referenced by ShipGroupSetProperty().

+ Here is the caller graph for this function:

◆ ShipGroupIterate

+ (static id) ShipGroupIterate (OOShipGroupEnumerator *) enumerator
implementation

Definition at line 473 of file OOShipGroup.m.

474{
475 // The work is done here so that we can have access to both OOShipGroup's and OOShipGroupEnumerator's ivars.
476
477 OOShipGroup *group = enumerator->_group;
478 ShipEntity *result = nil;
479 BOOL cleanupNeeded = NO;
480
481 if (enumerator->_updateCount != group->_updateCount)
482 {
483 [NSException raise:NSGenericException format:@"Collection <OOShipGroup: %p> was mutated while being enumerated.", group];
484 }
485
486 while (enumerator->_index < group->_count)
487 {
488 result = [group->_members[enumerator->_index] weakRefUnderlyingObject];
489 if (result != nil)
490 {
491 enumerator->_index++;
492 break;
493 }
494
495 // If we got here, the group contains a stale reference to a dead ship.
496 group->_members[enumerator->_index] = group->_members[--group->_count];
497 cleanupNeeded = YES;
498 }
499
500 // Clean-up handling. Only perform actual clean-up at end of iteration.
501 if (enumerator->_considerCleanup)
502 {
503 enumerator->_cleanupNeeded = enumerator->_cleanupNeeded && cleanupNeeded;
504 if (enumerator->_cleanupNeeded && result == nil)
505 {
506 [group cleanUp];
507 }
508 }
509
510 return result;
511}
NSUInteger _updateCount
Definition OOShipGroup.m:53
OOShipGroup * _group
Definition OOShipGroup.m:52

◆ suppressClangStuff

- (BOOL) suppressClangStuff
implementation

Definition at line 473 of file OOShipGroup.m.

554{
555 return !_jsSelf;
556}
struct JSObject * _jsSelf
Definition OOShipGroup.h:45

◆ updateCount

- (NSUInteger) updateCount
implementation

Provided by category OOShipGroup(Private).

Definition at line 54 of file OOShipGroup.m.

468{
469 return _updateCount;
470}

Member Data Documentation

◆ _jsSelf

- (struct JSObject*) _jsSelf
protected

Definition at line 45 of file OOShipGroup.h.

◆ _leader

- (OOWeakReference*) _leader
protected

Definition at line 42 of file OOShipGroup.h.

◆ _members

- (OOWeakReference**) _members
protected

Definition at line 41 of file OOShipGroup.h.

Referenced by countByEnumeratingWithState:objects:count:.

◆ _name

- (NSString*) _name
protected

Definition at line 43 of file OOShipGroup.h.

◆ _updateCount

- (<NSFastEnumeration> unsigned long) _updateCount
protected
Initial value:
{
@private
NSUInteger _count, _capacity

Definition at line 40 of file OOShipGroup.h.

Referenced by countByEnumeratingWithState:objects:count:.


The documentation for this class was generated from the following files: