-
Notifications
You must be signed in to change notification settings - Fork 205
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
RAII - V2 #680
base: master
Are you sure you want to change the base?
RAII - V2 #680
Conversation
…II implementation in 'terralib.lua'
As the previous attempt, I really love this. but I can still find remains of |
Thanks. Indeed, there are two additional methods not explained in the documentation. These methods are auto-generated by terralibext.t:
These methods are not used within the compiler. They are however useful in libraries when, for instance, you want to implement a custom I'll add some documentation on these methods soon. You can find non-trivial use cases in the following repo where we are developing a standard library and a linear algebra library. The following files are most interesting in this respect:
We can't provide documentation right now and all functionality is undergoing heavy development. Nevertheless, it may be of interest. |
I'm trying to remember how we solved the issue of dangling references, e.g.:
Would be compiled as:
The
This would effectively force us to always copy, which is correct but perhaps inefficient. I'm pretty sure we already had this discussion and you had a solution to that, so it would be good to document that solution, whatever it is. |
The destructor is only called for objects that are not returned. Here is an example for a managed variable of type
This example is also in the documentation. |
The following case is not yet implemented: When a managed variable is passed by value then a copy should be made using the copy constructor and a deferred destructor call should be scheduled:
I've added it as an issue on my fork and I will handle it soon. We can still discuss the default behavior of |
I've closed #659 [(https://github.com//pull/659)] and reopened it here to put forward the refactored design with a clean git commit history.
I'll summarize the pull request and the revised design:
Objective: The point of the contribution is to add methods to enable RAII in order to implement smart containers and smart pointers, like
std::string
,std::vector
andstd::unique_ptr
,std::shared_ptr
,boost:offset_ptr
in C++.The design does not introduce any breaking changes. No new keywords are introduced. Heap resources are acquired and released using the regular C stdlib functions such malloc and free, leaving memory allocation in the hands of the programmer. The idea here is that allocators are implemented in libraries.
New metamethods: The following metamethods are implemented:
__init(self : &A)
__dtor(self : &A)
__copy(from : &A, to : &B)
If implemented, these methods are inserted judiciously during the type checking phase, implemented in terralib.lua. All these metamethods can be implemented as macro's or as terra functions.
Documenation about the design choices and the implementation can be found here