Menu
Home
Create new Paste
Log in
Code
Theme: cobalt
Theme: eclipse
Theme: elegant
Theme: monokai
Theme: neat
Theme: night
Theme: rubyblue
import std.traits; import std.conv : ConvException, assertThrown; import std.string; import std.math; import std.stdio; /** Convert a value that is implicitly convertible to the enum base type into an Enum value. If the value does not match any enum member values, or if it matches more than one member value throw a ConvException. */ T toImpl(T, S)(S value) if (is(T == enum) && is(S : OriginalType!T) && !isFloatingPoint!(OriginalType!T)) { T result; size_t matches; foreach (Member; EnumMembers!T) { if (Member == value) { result = Member; if (++matches > 1) throw new ConvException(format("Value (%s) matches more than one member value of enum '%s'", value, fullyQualifiedName!T)); } } if (!matches) throw new ConvException(format("Value (%s) does not match any member value of enum '%s'", value, fullyQualifiedName!T)); return result; } /** Ditto: Specialization for Enums that have a floating point base type. @equal must be a function which takes the enum base type as its first parameter, the type of @value as its second parameter, and return true if the two compare equal. */ T toImpl(T, alias equal, S)(S value) if (is(T == enum) && is(S : OriginalType!T) && isFloatingPoint!(OriginalType!T)) { T result; size_t matches; foreach (Member; EnumMembers!T) { if (equal(Member, value)) { result = Member; if (++matches > 1) throw new ConvException(format("Value (%s) matches more than one member value of enum '%s'", value, fullyQualifiedName!T)); } } if (!matches) throw new ConvException(format("Value (%s) does not match any member value of enum '%s'", value, fullyQualifiedName!T)); return result; } alias toImpl to; void test() { enum En : int { A = 10, B = 20, C = 30, D = 20 } En en1 = to!En(10); assert(en1 == En.A); assertThrown!ConvException(to!En(5)); // matches more than one assertThrown!ConvException(to!En(20)); static bool equal(float a, float b) { return feqrel(a, b) >= 24; } enum EF : float { C = 4.9 } float f = 4.9; EF enf = to!(EF, equal)(f); enum EF2 : float { A = 4.9, B = 1.0, C = 4.9 } float f2 = 4.9; // matches more than one assertThrown!ConvException(to!(EF2, equal)(f2)); } void main() { test(); }
Result:
Success
/
Return code: 0
/
Compilation time:
0.266
seconds
/
Run time:
0.001
seconds
Disassembly
Username
Message
Add comment
Paste info
Author:
Guest
Views:
101
Private:
no
Expires:
Never
Uploaded:
24.10.12 19:13
Votes
:
0
Tweet
Compilation
Compiler:
DMD 2.060
Pointer size:
m32
Actions
Download
Fork
Raw
×
Confirm
Are you sure you want to delete this paste?
There's no way back!
×
Confirm
Reason