Show / Hide Table of Contents

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.

Inheritance
object
IDGenerator
Implements
IMatchable<IDGenerator>
Inherited Members
object.GetType()
object.MemberwiseClone()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: SadRogue.Primitives
Assembly: TheSadRogue.Primitives.dll
Syntax
[DataContract]
public class IDGenerator : IMatchable<IDGenerator>
Remarks

This is one way to fulfill the requirement of assigning a persistent, 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

View Source

IDGenerator(uint, bool)

Constructor.

Declaration
public IDGenerator(uint startingInt = 0, bool lastAssigned = false)
Parameters
Type Name Description
uint startingInt

Unsigned integer to start at (one that will be returned first time UseID() is called). Defaults to 0.

bool lastAssigned

Whether or not the last possible ID has been assigned. If this is set to true, the startingInt value must be uint.maxValue. Generally, you should leave this parameter alone; it is designed primarily to facilitate serialization/deserialization.

Properties

View Source

CurrentInteger

The integer ID that will be returned the next time UseID() is called.

Declaration
[DataMember]
public uint CurrentInteger { get; }
Property Value
Type Description
uint
View Source

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
[DataMember]
public bool LastAssigned { get; }
Property Value
Type Description
bool

Methods

View Source

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
bool

True if the IDGenerator object specified is in the same state as the current one; false otherwise.

View Source

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
uint

The ID that has been assigned.

Implements

IMatchable<T>

Extension Methods

PropertyChangedEventHelpers.SafelySetProperty<TObject, TProperty>(TObject, ref TProperty, TProperty, EventHandler<ValueChangedEventArgs<TProperty>>?)
PropertyChangedEventHelpers.SafelySetProperty<TObject, TProperty>(TObject, ref TProperty, TProperty, EventHandler<ValueChangedEventArgs<TProperty>>?, EventHandler<ValueChangedEventArgs<TProperty>>?)
  • View Source
In this article
Back to top Generated by DocFX