Skip to content
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

Added passing arguments to constrcutors when allocating with Trick Memory Manager. #1790

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from

Conversation

M-Herr
Copy link

@M-Herr M-Herr commented Sep 28, 2024

Goals

  1. Let users call constructors that take arguments when allocating with the trick memory manager.
  2. Make this available in both C++ and Python

New Things

  1. Variadic template function (tmm_alloc_args) that wraps a memory allocation and subsequent placement new call .

From a users perspective:
Foo* foo = tmm_alloc_args<Foo>()

The allocation functions takes the template parameters <typename T, typename ...Args> where Args is a parameter pack. We can use this to wrap any constructor call, as long as we can a string representation of the type T to pass to the trick memory manager.

The struct TrickTypeToString let's us get a string representation of the type through template specialization. ICG will generate specializations for all types it knows about (except anything in Trick, std, or er7 namespaces).

  1. ICG Parses Constructors
    I've added FunctionVisitor and FunctionDescription classes (modeled after FieldVisitor and FieldDescription). The visitor handles pulling some information out of clang::Decl::CXXConstructor and the description holds it. This info is used by ICG to generate two files: trick_type_to_string_ext.hh and constructors.json. As mentioned above, the TrickTypeToString structures are used to convert types to strings. The constructor information in constructors.json is used by convert_swig to generated addition functions in swig interface files.
[
{
    "className": "AllocTestSimObject",
    "qualifiedClassName": "AllocTestSimObject",
    "constructors": [
    {
      "accessSpecifier": "public",
      "parameters": [
      ],
      "isDefault": true,
      "isCopy": false,
      "isMove": false
    }
    ]
}
]

If the constructor takes arguments, it'll have an array of type/name pairs.

  1. SWIG Interface files
    convert_swig has been updated with a new function process_constructor_json that uses the json data written by ICG to add some functionality. Functions are added to the class using swigs %extend functionality. One alloc function is generated for every constructor overload that exists.

This is an example of the %extend functionality from test/SIM_tmm_alloc_args.

%extend AllocTestWithArguments {
    static AllocTestWithArguments* alloc()  {
        return tmm_alloc_args<AllocTestWithArguments>();
    }
    static AllocTestWithArguments* alloc(int in_int, double in_double)  {
        return tmm_alloc_args<AllocTestWithArguments>(in_int, in_double);
    }
    static AllocTestWithArguments* alloc(int * in_int, double * in_double, std::string & a_name)  {
        return tmm_alloc_args<AllocTestWithArguments>(in_int, in_double, a_name);
    }
    static AllocTestWithArguments* alloc(int * arg_0, double & arg_1, std::string arg_2)  {
        return tmm_alloc_args<AllocTestWithArguments>(arg_0, arg_1, arg_2);
    }
}

SWIG will make those functions available in python for us, at this point a user can allocate using tmm_alloc_args from python.

var = trick.AllocTestWithArguments.alloc(1, 5.0)

To make it even more seamless I've modified the one of the __init__ overrides in swig_class_typedef.i to call alloc using python's unpacking operator(*). So now you can allocate using trick.AllocTestWithArguments(1, 5.0).

I've also added flags to ICG and convert swig so that all of the above features have to be explicitly enabled via:

TRICK_ICGFLAGS += -use_tmm_alloc_args
TRICK_CONVERT_SWIG_FLAGS += --tmm

This is still a work in progress, but if anyone spots any issues please let me know.

M-Herr and others added 16 commits September 19, 2024 23:38
…on. Added FunctionVisitor class to ICG to handle parsing constructors with arguments.
… unecessary to write it out from ICG. Created trick_types.hh to hold includes for the non-specialized TrickTypeToString<T> and the specialzied TrickTypeToString<T> created by ICG.
generate alloc functions in the swig interface files for classes.
These alloc functions wrap the tmm_alloc_args call so swig doesn't have
to think about variadic templates.
… json (for now) file that has important constructor info. Less regex, less room for error.
…g static alloc functions for the swig interface files. These get called by overwriting the __init__ functions.
@coveralls
Copy link

coveralls commented Oct 2, 2024

Coverage Status

coverage: 55.893% (+0.06%) from 55.834%
when pulling cb1491d on M-Herr:master
into 7bc6ab7 on nasa:master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants