Can we just have struct inheritence already?
Era Scarecrow
rtcvb32 at yahoo.com
Tue Jul 2 05:05:30 UTC 2019
On Tuesday, 2 July 2019 at 04:54:07 UTC, Era Scarecrow wrote:
> 1) No Virtual functions. All functions are FINAL (replacing)
> 2) No polymorphism.
> 3) Limited casting (Casting up/back is easier than casting
And looking at my old notes... Might make it where the data size
can't change.
This crypto stuff used to work in 10/2012, or with DMD 2.060
From the polymorph.d
---
auto crypto = PolyCryptoBase.factory("CryptoROT13");
crypto.setString("(Whfg n yvggyr pelcgb grfg)");
crypto.encipher;
assert(crypto.data.crypted == "(Just a little crypto test)");
---
Here's a crypto example of the actual structs
/** This is not intended as a serious crypto library, just enough
to test several factors to ensure the polymorphic
nature is being done. As other items need to be tested this
crypto will be improved to handle those tests.*/
struct PolyCryptoBase {
///Enum, _prefix and _types don't exist until this calls.
mixin(expandTypes("Data", "Poly_",
"CryptoBase,CryptoXOR13,CryptoROT13"));
///
struct Data {
Enum polyMorphType; ///
string original; ///
char[] crypted; ///mutable copy
}
Data data; ///
///Seems opCmp opEquals and similar functions go in the poly
base. That's livable.
///if not, forward reference to Poly_opCmp
mixin PolyMorphicCompare!(PolyCryptoBase);
mixin PolyMorphic!("smartmerged.polymorphicstruct_crypto",
PolyCryptoBase, Data, "data"); ///
}
///
struct CryptoBase {
mixin PolyMorphicInclude!(PolyCryptoBase); ///
///Seems opCmp opEquals and similar functions go in the poly
base. That's livable.
///if not, forward reference to Poly_opCmp
int Poly_opCmp(ref const PolyCryptoBase rhs) const {
return data.original.length - rhs.data.original.length;
}
///if it matches the original string
bool isCrypted() @property pure @safe nothrow{
return data.original != data.crypted;
}
///Does basic enciphering, individual letters only
static void Poly_cipherChar(ref char c) pure @safe nothrow {
c ^= 127;
}
///set the plaintext (resetting crypted text as well)
void setString(string input) @property {
data.original = input;
data.crypted = input.dup;
}
///encrypt cipher string (which done twice will decrypt it)
void encipher() @property {
foreach(ref ch; data.crypted) {
this.cipherChar(ch);
}
}
}
///
struct CryptoXOR13 {
mixin PolyMorphicInclude!(PolyCryptoBase);///
/**non matching signatures, but still compatible
requires 'static' or const to work, otherwise
potential for only calling a different version
may apply making some bugs hard to find.*/
static void cipherChar(ref char c) pure @safe {
c ^= 13;
}
}
Yeah a little long winded, but it did work as a concept... I can
share the old full sources if you want.
More information about the Digitalmars-d
mailing list