-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
079b520
commit 1c61905
Showing
4 changed files
with
85 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* | ||
*/ | ||
module quack.membertest; | ||
import quack.extends; | ||
|
||
import std.algorithm: among; | ||
|
||
/** | ||
* Checks if Child extends Parent by having a matching set of members. | ||
* | ||
* Params: | ||
* Child = The base class to test. | ||
* Parent = The parent class to test. | ||
* | ||
* Returns: | ||
* Whether Child has all the members of Parent. | ||
*/ | ||
template hasSameMembers( Base, Parent ) | ||
{ | ||
static if( isExtendable!( Base, Parent ) ) | ||
{ | ||
enum hasSameMembers = { | ||
// If there are no members to check, return false. | ||
static if( [__traits( allMembers, Parent )].length == 0 ) | ||
{ | ||
return false; | ||
} | ||
else | ||
{ | ||
foreach( member; __traits( allMembers, Parent ) ) | ||
{ | ||
// Ignore some members. | ||
static if( !member.among( "this" ) ) | ||
{ | ||
// If Base has the member, check the type. | ||
static if( __traits( hasMember, Base, member ) ) | ||
{ | ||
static if( !is( | ||
typeof( __traits( getMember, Parent, member ) ) == | ||
typeof( __traits( getMember, Base, member ) ) | ||
) ) | ||
{ | ||
return false; | ||
} | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} (); | ||
} | ||
else | ||
{ | ||
enum hasSameMembers = false; | ||
} | ||
} | ||
/// | ||
unittest | ||
{ | ||
struct S1 | ||
{ | ||
@property int x() { return 42; } | ||
} | ||
|
||
struct S2 | ||
{ | ||
@property int x() { return 42; } | ||
} | ||
|
||
assert( hasSameMembers!( S1, S2 ) ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ public: | |
import quack.extends; | ||
import quack.aliasthis; | ||
import quack.mixins; | ||
import quack.membertest; |