Class IDGenerator
Class designed as a helper for situations where you need to generate and assign a unique integer to each instance of a class, eg. for a class implementing IHasID.
Implements
Namespace: SadRogue.Primitives
Assembly: TheSadRogue.Primitives.dll
Syntax
public class IDGenerator : Object, IMatchable<IDGenerator>
Remarks
This is one way to fulfill the requirement of assigning a persistant, unique ID to each instance of an object, which is a fairly common requirement when defining objects that exist on a grid.
The class may be initialized with a starting unsigned integer -- if none is given, 0 is the default starting point. To assign an ID, call UseID(), and assign the value that it returns. This class is NOT thread-safe on its own -- if it needs to be, you can simply use a lock to wrap any calls to UseID.
Constructors
IDGenerator(UInt32, Boolean)
Constructor.
Declaration
public IDGenerator(uint startingInt = 0U, bool lastAssigned = false)
Parameters
Type | Name | Description |
---|---|---|
UInt32 | startingInt | Unsigned integer to start at (one that will be returned first time UseID() is called). Defaults to 0. |
Boolean | lastAssigned | Whether or not the last possible ID has been assigned. If this is set to true, the
|
Properties
CurrentInteger
The integer ID that will be returned the next time UseID() is called.
Declaration
public uint CurrentInteger { get; }
Property Value
Type | Description |
---|---|
UInt32 |
LastAssigned
If true, the ID generator has assigned the last ID in the uint range, and will throw an InvalidOperationException the next time UseID is called.
Declaration
public bool LastAssigned { get; }
Property Value
Type | Description |
---|---|
Boolean |
Methods
Matches(IDGenerator)
Compares the two IDGenerator objects to see if they are in an identical state.
Declaration
public bool Matches(IDGenerator other)
Parameters
Type | Name | Description |
---|---|---|
IDGenerator | other |
Returns
Type | Description |
---|---|
Boolean | True if the IDGenerator object specified is in the same state as the current one; false otherwise. |
UseID()
Call every time you wish to "assign" an ID. The integer returned will never be returned again (each integer returned by this function will be unique).
Declaration
public uint UseID()
Returns
Type | Description |
---|---|
UInt32 | The ID that has been assigned. |