September 22

On 17.09.23 17:05, Vitaliy Fadeev wrote:

>

Hi!
I want to change a method Draw on a custom object when the MouseIn event occurs.
This is known as "Change State" of the object: Init -> Hovered.

I want to change the state of an object by changing its class, like this:


this.__vptr = typeid(CLS).vtbl.ptr;

I have read the ABI and am confident in replacing __vptr as long as the classes contain the same fields and the same interfaces.

Example:

// O
//   to!state
// State_Init    : O
//   Draw
// State_Hovered : O
//   Draw
//
// o.to!State_Hovered
// o.to!State_Init

class O
{
   void to(CLS)()
   {
     // if (same fields && same interfaces && same instance size)
     this.__vptr =
       cast(immutable(void*)*)typeid(CLS).vtbl.ptr;
   }
}

State_Init : O
   void Draw() { /* ... */ }

State_Hovered : O
   void Draw() { /* ... */ }

when MouseIn:

   ...
   o.to!State_Hovered();
   ...

when MouseOut:

   ...
   o.to!State_Init();
   ...

It works! But I want to ask how to make this 100% the best of the best?
What should I consider before changing __vptr ?

You could model it oop style like this:
https://run.dlang.io/is/MJb5Fk
This solution might not be to your taste, as it involves interfaces, and classes and objects and garbage (all the news) ...

another option could be to model your own VTable in a struct like this:
https://run.dlang.io/is/3LTjP5

Kind regards,
Christian

September 22

On Friday, 22 September 2023 at 14:03:40 UTC, Vitaliy Fadeev wrote:

>

On Friday, 22 September 2023 at 12:53:28 UTC, Imperatorn wrote:

>

On Friday, 22 September 2023 at 03:33:08 UTC, Vitaliy Fadeev wrote:

>

[...]

What I mean is, why not use other language constructs like mixins or inheritance with some mapping for example?

Can you give an example?

You're basically just describing polymorphism. I can post an example tomorrow, it's midnight here now.

September 23

On Friday, 22 September 2023 at 19:50:17 UTC, Christian Köstlin wrote:

>

another option could be to model your own VTable in a struct like this:
https://run.dlang.io/is/3LTjP5

Kind regards,
Christian

Thank, Christian !
True nice tasty solution with VTable!
And further... the project is growing.

void Draw()
{
  DrawBG();
  DrawFG();
}

And we want use DrawBG() code from initial in other states, like Selected.

How to use some functions from initial via VTable ?

I see solution in classes and methods with override keyword.

VTable does the same thing as __vptr ?

September 23

On Friday, 22 September 2023 at 19:50:17 UTC, Christian Köstlin wrote:

>

On 17.09.23 17:05, Vitaliy Fadeev wrote:

>

Hi!

You could model it oop style like this:
https://run.dlang.io/is/MJb5Fk
This solution might not be to your taste, as it involves interfaces, and classes and objects and garbage (all the news) ...

another option could be to model your own VTable in a struct like this:
https://run.dlang.io/is/3LTjP5

Kind regards,
Christian

Behavior is beautiful code!

But it contains a second new when Chip is created. One new is possible?

Christian, really nice code!

Does __vptr do the same thing ?

September 23

On Friday, 22 September 2023 at 21:37:37 UTC, Imperatorn wrote:

>

On Friday, 22 September 2023 at 14:03:40 UTC, Vitaliy Fadeev wrote:

>

On Friday, 22 September 2023 at 12:53:28 UTC, Imperatorn wrote:
You're basically just describing polymorphism. I can post an example tomorrow, it's midnight here now.

Thank you. Of course! It's interesting to look at the solutions to choose the best one.

September 23

On 23.09.23 05:25, Vitaliy Fadeev wrote:

>

On Friday, 22 September 2023 at 19:50:17 UTC, Christian Köstlin wrote:

>

On 17.09.23 17:05, Vitaliy Fadeev wrote:

>

Hi!

You could model it oop style like this:
https://run.dlang.io/is/MJb5Fk
This solution might not be to your taste, as it involves interfaces, and classes and objects and garbage (all the news) ...

another option could be to model your own VTable in a struct like this:
https://run.dlang.io/is/3LTjP5

Kind regards,
Christian

Behavior is beautiful code!

But it contains a second new when Chip is created. One new is possible?

Christian, really nice code!
Here a solution with less news: https://run.dlang.io/is/iV1qVq.
It really depends on the program that you are doing if creating those news is a problem.

Kind regards,
Christian

September 23
On 23.09.23 05:11, Vitaliy Fadeev wrote:
> On Friday, 22 September 2023 at 19:50:17 UTC, Christian Köstlin wrote:
>> another option could be to model your own VTable in a struct like this:
>> https://run.dlang.io/is/3LTjP5
>>
>> Kind regards,
>> Christian
> 
> Thank, Christian !
> True nice tasty solution with ```VTable```!
> And further... the project is growing.
> ```
> void Draw()
> {
>    DrawBG();
>    DrawFG();
> }
> ```
> 
> And we want use ```DrawBG()``` code  from ```initial``` in other states, like ```Selected```.
> 
> How to use some functions from ```initial``` via ```VTable``` ?
> 
> I see solution in ```classes``` and methods with ```override``` keyword.
> 
> ```VTable``` does the same thing as ```__vptr``` ?

VTable is your structure .. it does exactly what you want it to do.
__vptr is the internal implementation of virtual methods in the dlang object model.
Line 20 and 21 in my example initialize the two `VTable`s Initial and Hovered. You can change VTable to contain two function pointers and initialize those as you like for the instances of the VTable structs.

e.g.
```d
struct DrawVTable
{
   void function(Chip, Renderer) background;
   void function(Chip, Renderer) foreground;
}

// define functions to draw the different fore and backgrounds
...
...

VTable initial = VTable(&drawInitialBackground, &drawInitialForeground);
VTable hovered = VTable(&drawHoveredBackground, &drawHoveredForeground);
VTable selected = VTable(&drawInitialBackground, &drawHoveredForegtround);

```

Kind regards,
Christian


1 2
Next ›   Last »