Oolite
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]
 
(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.

281 :(ShipEntity *)ship
282{
283 _updateCount++;
284
285 if ([self containsShip:ship]) return YES; // it's in the group already, result!
286
287 // Ensure there's space.
288 if (_count == _capacity)
289 {
290 if (![self resizeTo:(_capacity > kMaxFreeSpace) ? (_capacity + kMaxFreeSpace) : (_capacity * 2)])
291 {
292 if (![self resizeTo:_capacity + 1])
293 {
294 // Out of memory?
295 return NO;
296 }
297 }
298 }
299
300 _members[_count++] = [ship weakRetain];
301 return YES;
302}
@ kMaxFreeSpace
Definition OOShipGroup.m:44
OOWeakReference ** _members
Definition OOShipGroup.h:39
< NSFastEnumeration > unsigned long _updateCount
Definition OOShipGroup.h:38

◆ cleanUp

- (void) cleanUp
implementation

Provided by category OOShipGroup(Private).

Definition at line 54 of file OOShipGroup.m.

383{
384 NSUInteger newCapacity = _capacity;
385
386 if (_count >= kMaxFreeSpace)
387 {
388 if (_capacity > _count + kMaxFreeSpace)
389 {
390 newCapacity = _count + 1; // +1 keeps us at powers of two + multiples of kMaxFreespace.
391 }
392 }
393 else
394 {
395 if (_capacity > _count * 2)
396 {
397 newCapacity = OORoundUpToPowerOf2_NS(_count);
398 if (newCapacity < kMinSize) newCapacity = kMinSize;
399 }
400 }
401
402 if (newCapacity != _capacity) [self resizeTo:newCapacity];
403}
@ 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.

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

◆ count

- (NSUInteger) count

Definition at line 54 of file OOShipGroup.m.

343{
344 NSEnumerator *memberEnum = nil;
345 NSUInteger result = 0;
346
347 if (_count != 0)
348 {
349 memberEnum = [self objectEnumerator];
350 while ([memberEnum nextObject] != nil) result++;
351 }
352
353 assert(result == _count);
354
355 return result;
356}

◆ countByEnumeratingWithState:objects:count:

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

Definition at line 412 of file OOShipGroup.m.

453 :(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len
454{
455 NSUInteger srcIndex, dstIndex = 0;
456 ShipEntity *item = nil;
457 BOOL cleanupNeeded = NO;
458
459 srcIndex = state->state;
460 while (srcIndex < _count && dstIndex < len)
461 {
462 item = [_members[srcIndex] weakRefUnderlyingObject];
463 if (item != nil)
464 {
465 stackbuf[dstIndex++] = item;
466 srcIndex++;
467 }
468 else
469 {
470 _members[srcIndex] = _members[--_count];
471 cleanupNeeded = YES;
472 }
473 }
474
475 if (cleanupNeeded) [self cleanUp];
476
477 state->state = srcIndex;
478 state->itemsPtr = stackbuf;
479 state->mutationsPtr = &_updateCount;
480
481 return dstIndex;
482}
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}

◆ 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}

◆ isEmpty

- (BOOL) isEmpty

Definition at line 54 of file OOShipGroup.m.

360{
361 if (_count == 0) return YES;
362
363 return [[self objectEnumerator] nextObject] == nil;
364}

◆ 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:40

Referenced by ShipGroupAddShip(), and ShipGroupGetProperty().

+ Here is the caller graph for this function:

◆ memberArray

- (NSArray *) memberArray

Definition at line 54 of file OOShipGroup.m.

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

Referenced by ShipGroupGetProperty().

+ Here is the caller graph for this function:

◆ memberArrayExcludingLeader

- (NSArray *) memberArrayExcludingLeader

Definition at line 54 of file OOShipGroup.m.

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

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:41

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.

305 :(ShipEntity *)ship
306{
307 OOShipGroupEnumerator *shipEnum = nil;
308 ShipEntity *containedShip = nil;
309 NSUInteger index;
310 BOOL foundIt = NO;
311
312 _updateCount++;
313
314 if (ship == [self leader]) [self setLeader:nil];
315
316 shipEnum = (OOShipGroupEnumerator *)[self objectEnumerator];
317 [shipEnum setPerformCleanup:NO];
318 while ((containedShip = [shipEnum nextObject]))
319 {
320 if ([ship isEqual:containedShip])
321 {
322 index = [shipEnum index] - 1;
323 _members[index] = _members[--_count];
324 foundIt = YES;
325
326 // Clean up
327 [ship setGroup:nil];
328 [ship setOwner:ship];
329 [self cleanUp];
330 break;
331 }
332 }
333 return foundIt;
334}
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.

367 :(NSUInteger)newCapacity
368{
369 OOWeakReference **temp = NULL;
370
371 if (newCapacity < _count) return NO;
372
373 temp = realloc(_members, newCapacity * sizeof *_members);
374 if (temp == NULL) return NO;
375
376 _members = temp;
377 _capacity = newCapacity;
378 return YES;
379}

◆ 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 412 of file OOShipGroup.m.

413{
414 // The work is done here so that we can have access to both OOShipGroup's and OOShipGroupEnumerator's ivars.
415
416 OOShipGroup *group = enumerator->_group;
417 ShipEntity *result = nil;
418 BOOL cleanupNeeded = NO;
419
420 if (enumerator->_updateCount != group->_updateCount)
421 {
422 [NSException raise:NSGenericException format:@"Collection <OOShipGroup: %p> was mutated while being enumerated.", group];
423 }
424
425 while (enumerator->_index < group->_count)
426 {
427 result = [group->_members[enumerator->_index] weakRefUnderlyingObject];
428 if (result != nil)
429 {
430 enumerator->_index++;
431 break;
432 }
433
434 // If we got here, the group contains a stale reference to a dead ship.
435 group->_members[enumerator->_index] = group->_members[--group->_count];
436 cleanupNeeded = YES;
437 }
438
439 // Clean-up handling. Only perform actual clean-up at end of iteration.
440 if (enumerator->_considerCleanup)
441 {
442 enumerator->_cleanupNeeded = enumerator->_cleanupNeeded && cleanupNeeded;
443 if (enumerator->_cleanupNeeded && result == nil)
444 {
445 [group cleanUp];
446 }
447 }
448
449 return result;
450}
NSUInteger _updateCount
Definition OOShipGroup.m:53
OOShipGroup * _group
Definition OOShipGroup.m:52

◆ suppressClangStuff

- (BOOL) suppressClangStuff
implementation

Definition at line 412 of file OOShipGroup.m.

491{
492 return !_jsSelf;
493}
struct JSObject * _jsSelf
Definition OOShipGroup.h:43

◆ updateCount

- (NSUInteger) updateCount
implementation

Provided by category OOShipGroup(Private).

Definition at line 54 of file OOShipGroup.m.

407{
408 return _updateCount;
409}

Member Data Documentation

◆ _jsSelf

- (struct JSObject*) _jsSelf
protected

Definition at line 43 of file OOShipGroup.h.

◆ _leader

- (OOWeakReference*) _leader
protected

Definition at line 40 of file OOShipGroup.h.

◆ _members

- (OOWeakReference**) _members
protected

Definition at line 39 of file OOShipGroup.h.

Referenced by countByEnumeratingWithState:objects:count:.

◆ _name

- (NSString*) _name
protected

Definition at line 41 of file OOShipGroup.h.

◆ _updateCount

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

Definition at line 38 of file OOShipGroup.h.

Referenced by countByEnumeratingWithState:objects:count:.


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