www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - Interfaces and Dynamic Casts library for betterC classes

Hello betterC people. I have done this little project for anyone 
wanting to use classes in betterC without losing interface 
implementations for them.

The library simply defines `CppExtend` and `CppInterface` as 
objects which you must extend. It has a simple allocator `New!T` 
and deallocator `Destroy!T`.

It works by defining and redefining functions based on inputs. 
The classes implementations are private inside the class, it 
stores each implementation with a reference to base class to call 
the base class implementation of the functions, so, you can get a 
standalone interface like we have in pure D today.

Simple example code to stoke your curiosity:
```d
extern(C++)
{
	///Interface
	abstract class Printable
	{
		void print();
	}
	///Interface
	abstract class Stringificable
	{
		extern(D) string toString2();
	}


	///New class implementing Printable and Stringificable classes
	class Test : CppInterface!(Test, Printable, Stringificable)
	{
		void print()
		{
			import core.stdc.stdio;
			printf("toString on print function: %s\n", toString2.ptr);
		}
		extern(D) string toString2()
		{
			return __traits(identifier, Test);
		}
	}
}

extern(C) void main()
{
     Test t = New!Test;
     t.print();
     Printable p = t.getInterface!Printable;
     p.print();
}
```

I have tested it under DLL boundaries, it is working correctly. I 
have worked on this project as a future intention of checking 
into how I'm going to make my game engine betterC compatible.

Requires dmd-fe 2.101 or later

https://code.dlang.org/packages/betterc-interface
Jan 02 2023