finnow/finnow-api/source/model/base.d

37 lines
836 B
D

module model.base;
/**
* The base class for all persistent entities with a unique integer id.
* It offers some basic utilities like equality and comparison by default.
*/
abstract class IdEntity {
const ulong id;
this(ulong id) {
this.id = id;
}
override bool opEquals(Object other) const {
if (IdEntity e = cast(IdEntity) other) {
return e.id == id;
}
return false;
}
override size_t toHash() const {
import std.conv : to;
return id.to!size_t;
}
override string toString() const {
import std.format : format;
return format!"IdEntity(id = %d)"(id);
}
override int opCmp(Object other) const {
if (IdEntity e = cast(IdEntity) other) {
return this.id < e.id;
}
return 1;
}
}