Just plain old C++ RAII (or C#
using declaration) implementation for TypeScipt via
Suppose you have a local resource handle that you must manually release in the same scope.
With this plugin the cleanup is done automatically for you.
;;using new AutoVarResource;doStuffWithResourceresource;↓ ↓ ↓ ↓ ↓ ↓;tryfinally
is added as
"macros" item in
"plugins" array of your babel config file.
Afterwards just install this package as dev dependency and use it in your code:
npm i -D using.macro
The way you import this macro doesn't matter (as long as TypeScript eats it):
There are only two options for invoking this macro:
- Near variable declaration
var/let/const varId = using(expresssion);
- As a single expression statement
using(expression);so that a variable will be created automatically to release it for you.
By default when you do
.delete() method called on your handles.
If you want to change this behaviour to e.g. call
free() function on your handle,
you can wrap this macro with your own via
// using-ptr.macro.ts # .macro extension matters! See babel-plugin-macros docs;;;// Pass this noop function type to createMacro<>() for TypeScript support.;
and use it in your code this way:
;;// stuff↓ ↓ ↓ ↓ ↓ ↓;tryfinally
create[Function/Method]CallFactory() are simply two helper
functions. You can actually pass your own destructor codegen function instead:
; // default import is not supported by babel!createUsingMacro
'babel-plugin-macros' doesn't natively support TypeScript macros
in runtime, so you need to build your custom
and put in the same directory as
This macro was originally created for simplifying the usage of
'embind' C++ object