SWI-Prolog / packages-cpp

The SWI-Prolog C++ interface
30 stars 14 forks source link

Add support for "blob" atoms #15

Closed kamahen closed 1 year ago

kamahen commented 2 years ago

Currently, the only way to store an arbitrary vaule is using the PL_put_pointer/PL_get_pointer interface. This leads to weird stuff:

?- make_my_object(X, 666), my_object_i(X, Y), free_my_object(X).
X = 95261619877024,
Y = 666.

?- my_object_i(95261619877024, Y).
Y = -753257696.

which also shows that this is dangerous because the deleted object can still be accessed (but returning an incorrect value).

The implementation of PlBlob should be an abstract class that requires defining the "release" and "compare" methods, with a default implementation of "write".

@jan - please assign this to me.

JanWielemaker commented 2 years ago

I think an abstraction of blobs used as pointers is very useful. In C++ it can probably be done quite easily. In C it is a bit harder, though the ffi pack contains some abstractions.

kamahen commented 2 years ago

I wasn't thinking of providing a "smart pointer" for blobs, just a light wrapper around PL_blob_t and related things like PL_blob_data. Smart pointers would require me trying to remember how to write C++ templates, and smart pointers can be fairly tricky to get right. I'll take a look at pack(ffi), but from a quick look at it, it seems to be more functionality than I want to provide right now.

JanWielemaker commented 2 years ago

Yes, pack(ffi) does too much. Something that would create a named blob and has virtual methods to deal with the blob hook functions in a nice C++ way would be great.

jan commented 2 years ago

Keep up the great work, guys! Please don't hesitate wo tag me (@jan) if you have any further questions.

kamahen commented 1 year ago

Commit 7c351d5334292e09022cadaf82d9a6c00c2c846d added a PlBlob class that can be subclassed, for easy creation of blobs using C++.