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

Private Attributes

NSUInteger _count
NSUInteger _capacity
unsigned long _updateCount
OOWeakReference ** _members
OOWeakReference * _leader
NSString * _name
struct JSObject * _jsSelf

Additional Inherited Members

Protected Attributes inherited from OOWeakRefObject
OOWeakReference * weakSelf

Detailed Description

Definition at line 36 of file OOShipGroup.h.

Method Documentation

◆ addShip:

- (BOOL) addShip: (ShipEntity*) ship

Definition at line 247 of file OOShipGroup.m.

247 :(ShipEntity*)ship
248{
249 _updateCount++;
250
251 if ([self containsShip:ship])
252 return YES; // it's in the group already, result!
253
254 // Ensure there's space.
255 if (_count == _capacity) {
256 if (![self resizeTo:(_capacity > kMaxFreeSpace) ? (_capacity + kMaxFreeSpace) : (_capacity * 2)]) {
257 if (![self resizeTo:_capacity + 1]) {
258 // Out of memory?
259 return NO;
260 }
261 }
262 }
263
264 _members[_count++] = [ship weakRetain];
265 return YES;
266}
@ kMaxFreeSpace
Definition OOShipGroup.m:42
OOWeakReference ** _members
Definition OOShipGroup.h:40
unsigned long _updateCount
Definition OOShipGroup.h:39
NSUInteger _count
Definition OOShipGroup.h:38
NSUInteger _capacity
Definition OOShipGroup.h:38

References _capacity, _count, _members, _updateCount, kMaxFreeSpace, and OOWeakRefObject::weakRetain.

Referenced by ShipEntity::acceptAsEscort:, ShipEntity::setGroup:, and setLeader:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cleanUp

- (void) cleanUp
implementation

Provided by category OOShipGroup(Private).

Definition at line 344 of file OOShipGroup.m.

345{
346 NSUInteger newCapacity = _capacity;
347
348 if (_count >= kMaxFreeSpace) {
350 newCapacity = _count + 1; // +1 keeps us at powers of two + multiples of kMaxFreespace.
351 }
352 } else {
353 if (_capacity > _count * 2) {
354 newCapacity = OORoundUpToPowerOf2_NS(_count);
355 if (newCapacity < kMinSize)
356 newCapacity = kMinSize;
357 }
358 }
359
360 if (newCapacity != _capacity)
361 [self resizeTo:newCapacity];
362}
@ kMinSize
Definition OOShipGroup.m:41

References OOShipGroup(Private)::cleanUp, kMaxFreeSpace, kMinSize, and resizeTo:.

Referenced by OOShipGroup(Private)::cleanUp, countByEnumeratingWithState:objects:count:, removeShip:, and ShipGroupIterate.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ containsShip:

- (BOOL) containsShip: (ShipEntity*) ship

Definition at line 234 of file OOShipGroup.m.

234 :(ShipEntity*)ship
235{
236 ShipEntity* containedShip = nil;
237
238 for (containedShip in self) {
239 if ([ship isEqual:containedShip]) {
240 return YES;
241 }
242 }
243
244 return NO;
245}
return nil

References nil.

◆ count

- (NSUInteger) count

Definition at line 304 of file OOShipGroup.m.

305{
306 NSEnumerator* memberEnum = nil;
307 NSUInteger result = 0;
308
309 if (_count != 0) {
310 memberEnum = [self objectEnumerator];
311 while ([memberEnum nextObject] != nil)
312 result++;
313 }
314
315 assert(result == _count);
316
317 return result;
318}

References _count, count, nil, and objectEnumerator.

Referenced by ShipEntity::acceptAsEscort:, ShipEntity(PureAI)::checkGroupOddsVersusTarget, count, ShipEntity::deployEscorts, ShipEntity::escortCount, ShipEntity::hasEscorts, memberArray, memberArrayExcludingLeader, ShipEntity::setUpEscorts, and ShipEntity(Private)::setUpMixedEscorts.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ countByEnumeratingWithState:objects:count:

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

Definition at line 404 of file OOShipGroup.m.

404 :(NSFastEnumerationState*)state objects:(id*)stackbuf count:(NSUInteger)len
405{
406 NSUInteger srcIndex, dstIndex = 0;
407 ShipEntity* item = nil;
408 BOOL cleanupNeeded = NO;
409
410 srcIndex = state->state;
411 while (srcIndex < _count && dstIndex < len) {
412 item = [_members[srcIndex] weakRefUnderlyingObject];
413 if (item != nil) {
414 stackbuf[dstIndex++] = item;
415 srcIndex++;
416 } else {
417 _members[srcIndex] = _members[--_count];
418 cleanupNeeded = YES;
419 }
420 }
421
422 if (cleanupNeeded)
423 [self cleanUp];
424
425 state->state = srcIndex;
426 state->itemsPtr = stackbuf;
427 state->mutationsPtr = &_updateCount;
428
429 return dstIndex;
430}
NSUInteger count()

References _count, _members, _updateCount, cleanUp, nil, and OOWeakReference::weakRefUnderlyingObject.

Here is the call graph for this function:

◆ dealloc

- (void) dealloc
implementation

Reimplemented from OOWeakRefObject.

Definition at line 106 of file OOShipGroup.m.

107{
108 NSUInteger i;
109
110 for (i = 0; i < _count; i++) {
111 [_members[i] release];
112 }
113 free(_members);
114 [_name release];
115
116 [super dealloc];
117}

References _count, _members, dealloc, and OOWeakRefObject::dealloc.

Referenced by dealloc.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ descriptionComponents

- (NSString *) descriptionComponents
implementation

Definition at line 119 of file OOShipGroup.m.

120{
121 NSString* desc = [NSString stringWithFormat:@"%zu ships", _count];
122 if ([self name] != nil) {
123 desc = [NSString stringWithFormat:@"\"%@\", %@", [self name], desc];
124 }
125 if ([self leader] != nil) {
126 desc = [NSString stringWithFormat:@"%@, leader: %@", desc, [[self leader] shortDescription]];
127 }
128 return desc;
129}
NSString * name()
ShipEntity * leader()

References descriptionComponents, leader, name, and nil.

Referenced by descriptionComponents.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ groupWithName:

+ (instancetype) groupWithName: (NSString*) name

Definition at line 94 of file OOShipGroup.m.

94 :(NSString*)name
95{
96 return [[[self alloc] initWithName:name] autorelease];
97}

Referenced by Universe::addShipsAt:withRole:quantity:withinRadius:asGroup:, ShipEntity::fireMissileWithIdentifier:andTarget:, and groupWithName:leader:.

Here is the caller graph for this function:

◆ groupWithName:leader:

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

Definition at line 99 of file OOShipGroup.m.

99 :(NSString*)name leader:(ShipEntity*)leader
100{
101 OOShipGroup* result = [self groupWithName:name];
102 [result setLeader:leader];
103 return result;
104}
void setLeader:(ShipEntity *leader)

References groupWithName:, and setLeader:.

Here is the call graph for this function:

◆ init

- (id) init

Definition at line 73 of file OOShipGroup.m.

74{
75 return [self initWithName:nil];
76}

References init, and initWithName:.

Referenced by init, and initWithName:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initWithName:

- (id) initWithName: (NSString*) name

Definition at line 78 of file OOShipGroup.m.

78 :(NSString*)name
79{
80 if ((self = [super init])) {
82 _members = malloc(sizeof *_members * _capacity);
83 if (_members == NULL) {
84 [self release];
85 return nil;
86 }
87
88 [self setName:name];
89 }
90
91 return self;
92}

References _capacity, _members, init, kMinSize, nil, and setName:.

Referenced by init.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isEmpty

- (BOOL) isEmpty

Definition at line 320 of file OOShipGroup.m.

321{
322 if (_count == 0)
323 return YES;
324
325 return [[self objectEnumerator] nextObject] == nil;
326}

References _count, isEmpty, nil, and objectEnumerator.

Referenced by isEmpty.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ leader

- (ShipEntity *) leader

Definition at line 146 of file OOShipGroup.m.

147{
148 ShipEntity* result = [_leader weakRefUnderlyingObject];
149
150 // If reference is stale, delete weakref object.
151 if (result == nil && _leader != nil) {
152 [_leader release];
153 _leader = nil;
154 }
155
156 return result;
157}
OOWeakReference * _leader
Definition OOShipGroup.h:41

References _leader, leader, nil, and OOWeakReference::weakRefUnderlyingObject.

Referenced by ShipEntity(PureAI)::checkForMotherStation, descriptionComponents, ShipEntity::fireMissileWithIdentifier:andTarget:, leader, memberArrayExcludingLeader, ShipEntity(PureAI)::patrolReportIn, removeShip:, ShipEntity(PureAI)::requestNewTarget, ShipEntity::respondToAttackFrom:becauseOf:, ShipEntity::setGroup:, setLeader:, ShipEntity(PureAI)::setPlanetPatrolCoordinates, ShipEntity::setTargetToNearestStationIncludingHostiles:, ShipGroupAddShip(), ShipGroupAddShip(), ShipGroupGetProperty(), ShipGroupGetProperty(), ShipEntity(LoadRestore)::shipRestoredFromDictionary:useFallback:context:, WormholeEntity::suckInShip:, ShipEntity::takeEnergyDamage:from:becauseOf:weaponIdentifier:, StationEntity::takeEnergyDamage:from:becauseOf:weaponIdentifier:, ShipEntity(PureAI)::thargonCheckMother, and ShipEntity::update:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ memberArray

- (NSArray *) memberArray

Definition at line 190 of file OOShipGroup.m.

191{
192 id* objects = NULL;
193 NSUInteger count = 0;
194 NSArray* result = nil;
195
196 if (_count == 0)
197 return [NSArray array];
198
199 objects = malloc(sizeof *objects * _count);
200 for (id ship in self) {
201 objects[count++] = ship;
202 }
203
204 result = [NSArray arrayWithObjects:objects count:count];
205 free(objects);
206
207 return result;
208}

References _count, count, memberArray, and nil.

Referenced by memberArray, members, mutationSafeEnumerator, ShipGroupGetProperty(), and ShipGroupGetProperty().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ memberArrayExcludingLeader

- (NSArray *) memberArrayExcludingLeader

Definition at line 210 of file OOShipGroup.m.

211{
212 id* objects = NULL;
213 NSUInteger count = 0;
214 NSArray* result = nil;
215 ShipEntity* leader = nil;
216
217 if (_count == 0)
218 return [NSArray array];
219 leader = self.leader;
220
221 objects = malloc(sizeof *objects * _count);
222 for (id ship in self) {
223 if (ship != leader) {
224 objects[count++] = ship;
225 }
226 }
227
228 result = [NSArray arrayWithObjects:objects count:count];
229 free(objects);
230
231 return result;
232}

References _count, count, leader, memberArrayExcludingLeader, and nil.

Referenced by memberArrayExcludingLeader, membersExcludingLeader, ShipGetProperty(), and ShipGetProperty().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ members

- (NSSet *) members

Definition at line 180 of file OOShipGroup.m.

181{
182 return [NSSet setWithArray:[self memberArray]];
183}

References memberArray, and members.

Referenced by members.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ membersExcludingLeader

- (NSSet *) membersExcludingLeader

Definition at line 185 of file OOShipGroup.m.

186{
187 return [NSSet setWithArray:[self memberArrayExcludingLeader]];
188}

References memberArrayExcludingLeader, and membersExcludingLeader.

Referenced by membersExcludingLeader.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mutationSafeEnumerator

- (NSEnumerator *) mutationSafeEnumerator

Definition at line 175 of file OOShipGroup.m.

176{
177 return [[self memberArray] objectEnumerator];
178}

References memberArray, and mutationSafeEnumerator.

Referenced by ShipEntity::escortEnumerator, and mutationSafeEnumerator.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ name

- (NSString *) name

Definition at line 131 of file OOShipGroup.m.

132{
133 return _name;
134}
NSString * _name
Definition OOShipGroup.h:42

References _name, and name.

Referenced by descriptionComponents, name, setName:, ShipGroupGetProperty(), and ShipGroupGetProperty().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ objectEnumerator

- (NSEnumerator *) objectEnumerator

Definition at line 170 of file OOShipGroup.m.

171{
172 return [[[OOShipGroupEnumerator alloc] initWithShipGroup:self] autorelease];
173}

References objectEnumerator.

Referenced by count, isEmpty, objectEnumerator, ShipEntity::realAlertCondition, and removeShip:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ oo_clearJSSelf: [1/2]

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

Provided by category OOShipGroup(OOJavaScriptExtensions).

Definition at line 226 of file OOJSShipGroup.m.

226 :(JSObject*)selfVal
227{
228 if (_jsSelf == selfVal)
229 _jsSelf = NULL;
230}
struct JSObject * _jsSelf
Definition OOShipGroup.h:47

◆ oo_clearJSSelf: [2/2]

- (void) oo_clearJSSelf: (JSValueConst) selfVal
implementation

Provided by category OOShipGroup(OOJavaScriptExtensions).

Definition at line 219 of file OOJSShipGroup.m.

219 :(JSValueConst)selfVal
220{
221 if (JS_IsObject(_jsSelf) && JS_VALUE_GET_PTR(_jsSelf) == JS_VALUE_GET_PTR(selfVal))
222 _jsSelf = JS_UNDEFINED;
223}

◆ oo_jsValueInContext:

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

Provided by category OOShipGroup(OOJavaScriptExtensions).

Definition at line 208 of file OOJSShipGroup.m.

208 :(JSContext*)context
209{
210 jsval result = JSVAL_NULL;
211
212 if (_jsSelf == NULL) {
213 _jsSelf = JS_NewObject(context, &sShipGroupClass, sShipGroupPrototype, NULL);
214 if (_jsSelf != NULL) {
215 if (!JS_SetPrivate(context, _jsSelf, [self retain]))
216 _jsSelf = NULL;
217 }
218 }
219
220 if (_jsSelf != NULL)
221 result = OBJECT_TO_JSVAL(_jsSelf);
222
223 return result;
224}
static JSObject * sShipGroupPrototype
static JSClass sShipGroupClass

◆ removeShip:

- (BOOL) removeShip: (ShipEntity*) ship

Definition at line 268 of file OOShipGroup.m.

268 :(ShipEntity*)ship
269{
270 OOShipGroupEnumerator* shipEnum = nil;
271 ShipEntity* containedShip = nil;
272 NSUInteger index;
273 BOOL foundIt = NO;
274
275 _updateCount++;
276
277 if (ship == [self leader])
278 [self setLeader:nil];
279
280 shipEnum = (OOShipGroupEnumerator*)[self objectEnumerator];
281 [shipEnum setPerformCleanup:NO];
282 while ((containedShip = [shipEnum nextObject])) {
283 if ([ship isEqual:containedShip]) {
284 index = [shipEnum index] - 1;
285 _members[index] = _members[--_count];
286 foundIt = YES;
287
288 // Clean up
289 [ship setGroup:nil];
290 [ship setOwner:ship];
291 [self cleanUp];
292 break;
293 }
294 }
295 return foundIt;
296}
void setPerformCleanup:(BOOL flag)
NSEnumerator * objectEnumerator()
void setGroup:(OOShipGroup *group)
void setOwner:(Entity *who_owns_entity)

References _members, _updateCount, cleanUp, OOShipGroupEnumerator::index, leader, nil, objectEnumerator, ShipEntity::setGroup:, setLeader:, ShipEntity::setOwner:, and OOShipGroupEnumerator::setPerformCleanup:.

Referenced by ShipEntity::dealloc, ShipEntity::respondToAttackFrom:becauseOf:, ShipEntity::setGroup:, and ShipEntity::update:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ resizeTo:

- (BOOL) resizeTo: (NSUInteger) newCapacity
implementation

Provided by category OOShipGroup(Private).

Definition at line 328 of file OOShipGroup.m.

328 :(NSUInteger)newCapacity
329{
330 OOWeakReference** temp = NULL;
331
332 if (newCapacity < _count)
333 return NO;
334
335 temp = realloc(_members, newCapacity * sizeof *_members);
336 if (temp == NULL)
337 return NO;
338
339 _members = temp;
340 _capacity = newCapacity;
341 return YES;
342}

Referenced by OOShipGroup(Private)::cleanUp.

Here is the caller graph for this function:

◆ setLeader:

- (void) setLeader: (ShipEntity*) leader

Definition at line 159 of file OOShipGroup.m.

159 :(ShipEntity*)leader
160{
161 _updateCount++;
162
163 if (leader != [self leader]) {
164 [_leader release];
165 [self addShip:leader];
166 _leader = [leader weakRetain];
167 }
168}

References _leader, _updateCount, addShip:, leader, and OOWeakRefObject::weakRetain.

Referenced by ShipEntity::escortGroup, groupWithName:leader:, StationEntity::launchIndependentShip:, removeShip:, ShipEntity::setEscortGroup:, ShipEntity::setGroup:, ShipEntity::setUpEscorts, ShipEntity(Private)::setUpMixedEscorts, ShipGroupSetProperty(), ShipGroupSetProperty(), ShipEntity(LoadRestore)::shipRestoredFromDictionary:useFallback:context:, and ShipEntity::stationGroup.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setName:

- (void) setName: (NSString*) name

Definition at line 136 of file OOShipGroup.m.

136 :(NSString*)name
137{
138 _updateCount++;
139
140 if (_name != name) {
141 [_name release];
142 _name = [name retain];
143 }
144}

References _name, _updateCount, and name.

Referenced by initWithName:, ShipGroupSetProperty(), ShipGroupSetProperty(), and ShipEntity(LoadRestore)::shipRestoredFromDictionary:useFallback:context:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ShipGroupIterate

+ (id) ShipGroupIterate (OOShipGroupEnumerator *) enumerator
implementation

Definition at line 369 of file OOShipGroup.m.

370{
371 // The work is done here so that we can have access to both OOShipGroup's and OOShipGroupEnumerator's ivars.
372
373 OOShipGroup* group = enumerator->_group;
374 ShipEntity* result = nil;
375 BOOL cleanupNeeded = NO;
376
377 if (enumerator->_updateCount != group->_updateCount) {
378 [NSException raise:NSGenericException format:@"Collection <OOShipGroup: %p> was mutated while being enumerated.", group];
379 }
380
381 while (enumerator->_index < group->_count) {
382 result = [group->_members[enumerator->_index] weakRefUnderlyingObject];
383 if (result != nil) {
384 enumerator->_index++;
385 break;
386 }
387
388 // If we got here, the group contains a stale reference to a dead ship.
389 group->_members[enumerator->_index] = group->_members[--group->_count];
390 cleanupNeeded = YES;
391 }
392
393 // Clean-up handling. Only perform actual clean-up at end of iteration.
394 if (enumerator->_considerCleanup) {
395 enumerator->_cleanupNeeded = enumerator->_cleanupNeeded && cleanupNeeded;
396 if (enumerator->_cleanupNeeded && result == nil) {
397 [group cleanUp];
398 }
399 }
400
401 return result;
402}
NSUInteger _updateCount
Definition OOShipGroup.m:49
OOShipGroup * _group
Definition OOShipGroup.m:48

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

Here is the call graph for this function:

◆ suppressClangStuff

- (BOOL) suppressClangStuff
implementation

Definition at line 437 of file OOShipGroup.m.

438{
439#if OOJS_QUICKJS
440 return !JS_IsObject(_jsSelf);
441#else
442 return !_jsSelf;
443#endif
444}

References _jsSelf, and suppressClangStuff.

Referenced by suppressClangStuff.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ updateCount

- (NSUInteger) updateCount
implementation

Provided by category OOShipGroup(Private).

Definition at line 364 of file OOShipGroup.m.

365{
366 return _updateCount;
367}

References OOShipGroup(Private)::updateCount.

Referenced by OOShipGroupEnumerator::initWithShipGroup:, and OOShipGroup(Private)::updateCount.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _capacity

- (NSUInteger) _capacity
private

Definition at line 38 of file OOShipGroup.h.

Referenced by addShip:, and initWithName:.

◆ _count

- (NSUInteger) _count
private

◆ _jsSelf

- (struct JSObject*) _jsSelf
private

Definition at line 47 of file OOShipGroup.h.

Referenced by suppressClangStuff.

◆ _leader

- (OOWeakReference*) _leader
private

Definition at line 41 of file OOShipGroup.h.

Referenced by leader, and setLeader:.

◆ _members

- (OOWeakReference**) _members
private

◆ _name

- (NSString*) _name
private

Definition at line 42 of file OOShipGroup.h.

Referenced by name, and setName:.

◆ _updateCount

- (unsigned long) _updateCount
private

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