Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic casting functions should be available in TypeInfo_Class #17318

Open
dlangBugzillaToGithub opened this issue Dec 9, 2015 · 2 comments
Labels

Comments

@dlangBugzillaToGithub
Copy link

Ketmar Dark reported this on 2015-12-09T07:17:45Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=15427

CC List

  • Ketmar Dark

Description

it's sometimes useful to emulate dynamic casting in runtime having only TypeInfo_Class at hands. now one has to rely on undocumented (and unexported) `_d_dynamic_cast()` and friends, writing something like this:


// get druntime cast function
private extern(C) void* _d_dynamic_cast (Object o, ClassInfo c);

bool isCastable (Object O, TypeInfo_Class ti) {
  return (_d_dynamic_cast(o, ti) !is null);
}


it would be fine to introduce and use a documented API for that, like this:

ti.dynamicCast(O)
ti.isBaseOf(O)


i.e. we should add these methods to TypeInfo_Class. also, the same methods should be added to TypeInfo_Interface.
@dlangBugzillaToGithub
Copy link
Author

ketmar commented on 2015-12-09T07:19:24Z

p.s. `ti.dynamicCast(O)` should still return `void*`. not a big deal, it's very internal API anyway.

@dlangBugzillaToGithub
Copy link
Author

ketmar commented on 2015-12-09T08:01:13Z

sample patch:

diff --git a/src/object.d b/src/object.d
index 097fc5a..6eb9163 100644
--- a/src/object.d
+++ b/src/object.d
@@ -21,6 +21,8 @@ private
 {
     extern (C) Object _d_newclass(const TypeInfo_Class ci);
     extern (C) void rt_finalize(void *data, bool det=true);
+    extern (C) void* _d_dynamic_cast(Object o, ClassInfo c);
+    extern (C) int _d_isbaseof(ClassInfo oc, ClassInfo c);
 }
 
 // NOTE: For some reason, this declaration method doesn't work
@@ -860,6 +862,17 @@ unittest
  */
 class TypeInfo_Class : TypeInfo
 {
+    final void* dynamicCast (Object o)
+    {
+        return (o !is null ? _d_dynamic_cast(o, this) : null);
+    }
+
+    // is this typeinfo base of `o`?
+    final bool baseOf (Object o)
+    {
+        return (o !is null ? (_d_isbaseof(typeid(o), this) != 0) : false);
+    }
+
     override string toString() const { return info.name; }
 
     override bool opEquals(Object o)

@thewilsonator thewilsonator added Severity:Enhancement Druntime Specific to druntime and removed enhancement labels Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants