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 23 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
64fabb2
Initial pass at adding support for a templated emplacement new functi…
M-Herr Sep 20, 2024
6aa4ef6
Merge remote-tracking branch 'origin/master'
M-Herr Sep 20, 2024
61c2fdb
Updated Makefile.common to include /build in include paths
M-Herr Sep 20, 2024
1e5d1a8
Moved tmm_alloc_args into inludde/trick. File never changes so it wa…
M-Herr Sep 21, 2024
59537e4
Moved tmm_alloc_args to it's own file. Updates to convert_swig to
M-Herr Sep 25, 2024
cc30885
Was leaning to heavily on perl for writing wrappers. ICG now writes …
M-Herr Sep 27, 2024
36b2640
Finishing touches on PrintConstructors class. Moved back to generatin…
M-Herr Sep 28, 2024
95230b0
Merge branch 'nasa:master' into master
M-Herr Sep 28, 2024
43253a2
Updated unit test sim
M-Herr Sep 29, 2024
91caad9
Extended unit test to include allocatin gin the input processor
M-Herr Sep 29, 2024
0542f0f
Wrapped JSON include in a flag used to enable/disable siwg interface …
M-Herr Sep 30, 2024
9ec72b9
Fixed typo in test_sims.yml
M-Herr Oct 1, 2024
48914fd
Changed getReturnType to getType since we're in a constructor
M-Herr Oct 1, 2024
daba529
Minor update to test
M-Herr Oct 1, 2024
a89a594
Merge remote-tracking branch 'upstream/master'
M-Herr Oct 1, 2024
19b06f1
Updated includes
M-Herr Oct 2, 2024
8f64831
Added alternative to generating TrickTypeToString structures.
M-Herr Oct 10, 2024
61f179e
Documentation updates
M-Herr Oct 10, 2024
bf25bbe
Update swig_class_typedef.i
M-Herr Oct 16, 2024
8679e8c
Fixed indentation in interface file
M-Herr Oct 16, 2024
80ff429
Changed convert swig to ignore private constructors.
M-Herr Oct 16, 2024
cb1491d
Merge remote-tracking branch 'upstream/master'
M-Herr Oct 17, 2024
f81d1c7
Merge remote-tracking branch 'upstream/master'
M-Herr Jan 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions include/trick/swig/swig_class_typedef.i
Original file line number Diff line number Diff line change
Expand Up @@ -232,23 +232,34 @@
The difference in the 2 blocks is that the first one handles constructor(s) with
arguments, the second one handles classes that have only a single default constructor.
*/

%feature("shadow") TYPE::TYPE %{
def __init__(self, *args, **kwargs):
import _sim_services
this = $action(*args)
try: self.this.append(this)
except: self.this = this
using_alloc = False
try:
this = TYPE.alloc(*args)
using_alloc = True
except:
this = $action(*args)
try:
this.append(this)
except:
self.this = this
if 'TMMName' in kwargs:
this.own(0)
self.this.own(0)
if not using_alloc:
this.own(0)
self.this.own(0)
#This should still be valid
isThisInMM = _sim_services.get_alloc_info_at(this)
if isThisInMM:
_sim_services.set_alloc_name_at(this, kwargs['TMMName'])
else:
_sim_services.TMM_declare_ext_var(this, _sim_services.TRICK_STRUCTURED, "TYPE", 0, kwargs['TMMName'], 0, None)
alloc_info = _sim_services.get_alloc_info_at(this)
alloc_info.stcl = _sim_services.TRICK_LOCAL
alloc_info.alloc_type = _sim_services.TRICK_ALLOC_NEW
if not self.using_alloc:
alloc_info = _sim_services.get_alloc_info_at(this)
alloc_info.stcl = _sim_services.TRICK_LOCAL
alloc_info.alloc_type = _sim_services.TRICK_ALLOC_NEW
%}

%feature("shadow") TYPE::TYPE() %{
Expand All @@ -269,5 +280,7 @@
alloc_info.alloc_type = _sim_services.TRICK_ALLOC_NEW
%}



%enddef

105 changes: 105 additions & 0 deletions include/trick/tmm_alloc_args.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*************************************************************************
PURPOSE: (Trick TMM Alloc W/ Args)
ICG: (No)
LIBRARY DEPENDENCY:
(
()
)
**************************************************************************/
#ifndef SWIG
#ifndef __ALLOC_WITH_ARGS_HH__
#define __ALLOC_WITH_ARGS_HH__

#include "trick/trick_type_traits.hh"

/**
* @brief Allocates and constructs an object of type `T` using Trick's Memory Manager.
* @tparam T Type to be allocated.
* @tparam Args parameter pack for forwarding arguments to placement new.
* @param args Parameter pack for forwarding arguments to the constructor of type `T`.
*
* @details Wrapper around Trick's `void* MemoryManager::declare_var( const char* declaration)`.
* It allocates memory for an object of type `T` and constructs it using placement new.
* Users can pass constructor arguments, which will be perfectly forward to `T`'s constructor.
*
* @return pointer to type T that was just allocated.
*
* @note This uses SFINAE to determine which overload should be selected.
* Where `Trick::has_getname<T>::value` evaluates to `false`, this overload is not selected.
* When it evalutes to `true`, this overload is used and the return type is `T*`.
*
* @example
* @code
* //Assuming:
* //1. TrickTypeToString has a specialization for TestClass.
* //2. TestClass has a default constructor.
* //3. TestClass has a constructor that takes two arguments.
* TestClass* test_class = tmm_alloc_args<TestClass>(arg_1, arg_2);
* @endcode
*/
template<typename T, typename ...Args>
typename std::enable_if<Trick::has_getname<T>::value, T*>::type
tmm_alloc_args(Args&&... args)
{
void* new_alloc = trick_MM->declare_var(TrickTypeToString<T>::getName().c_str());
return new (new_alloc) T(std::forward<Args>(args)...);
}

/**
* @brief Constructs an object of type `T` using placement new.
* @tparam T Type to be constructed.
* @tparam Args parameter pack for forwarding arguments to placement new.
* @param address Address of memory in which T will be constructed.
* @param args Parameter pack for forwarding arguments to the constructor of type `T`.
*
* @details Generalization of placement new for any type for which a specialization of
* TrickTypeToString exists.
*
* @return T Pointer to address.
*
* @note This uses SFINAE to determine which overload should be selected.
* Where `Trick::has_getname<T>::value` evaluates to `false`, this overload is not selected.
* When it evalutes to `true`, this overload is used and the return type is `T*`.
*
* @example
* @code
* //Assuming:
* //1. TrickTypeToString has a specialization for TestClass.
* //2. TestClass has a default constructor.
* //3. TestClass has a constructor that takes two arguments.
*
* TestClass* test_class = trick_MM->declare_var("TestClass");
* TestClass* test_class = tmm_alloc_args<TestClass>(test_class, arg_1, arg_2);
* // or
* TestClass* test_class = new TestClass();
* TestClass* test_class = tmm_alloc_args<TestClass>(test_class, arg_1, arg_2);
* @endcode
*/
template<typename T, typename ...Args>
typename std::enable_if<Trick::has_getname<T>::value, T*>::type
tmm_alloc_args(T* address, Args&&... args)
{
return new (address) T(std::forward<Args>(args)...);
}

/**
* @tparam T Type to be allocated.
* @tparam Args parameter pack for forward arguments to placement new.
* @details Version of tmm_alloc_args that will be compiled if TrickTypeToString has no
* member function 'getName()'. Static assert will end compilation and print a useful
* error message.
* @return Technically a nullptr, but this will never compile.
*/
template<typename T, typename ...Args>
typename std::enable_if<!Trick::has_getname<T>::value, T*>::type
tmm_alloc_args(Args&&... args)
{
//Compilation will always fail here.
static_assert(Trick::always_false<T>::value,
"You've attempted to call tmm_alloc_args using a type(T) that does not have an implemented specialization for TrickTypeToString.");

return nullptr;
}

#endif
#endif
47 changes: 47 additions & 0 deletions include/trick/trick_type_to_string.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*************************************************************************
PURPOSE: (Trick TMM Alloc W/ Args)
ICG: (No)
LIBRARY DEPENDENCY:
(
(trick_type_to_string.cc)
)
**************************************************************************/

#ifndef __TRICK_TYPE_TO_STRING_HH__
#define __TRICK_TYPE_TO_STRING_HH__

#ifdef TRICK_HAVE_ABI
#include <cxxabi.h>
#endif

#include <string>

#ifdef TRICK_HAVE_ABI
template<typename T>
struct TrickTypeToString {
inline const std::string getName() {
const std::typeinfo& type_info = typeid(T);
int status = 0;
char* demangeled_name = abi::__cxa_demangle(ti.name(), nullptr, nullptr, &status);
if(status == 0)
{
return std::string(demangled_name);
}

return std::string("";)
}
};
#else
/**
* @tparam T: Type to return string representation of via getName() function.
* @details: Basic version of TrickTypeToString with no getName() function.
* ICG generates specializations after parsing user header files and writes
* them to build/trick/trick_type_to_string_ext.hh
*/

template<typename T>
struct TrickTypeToString { };
#endif


#endif //__TRICK_TYPE_TO_STRING_HH__
78 changes: 78 additions & 0 deletions include/trick/trick_type_traits.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*************************************************************************
PURPOSE: (Trick Type Traits)
ICG: (No)
LIBRARY DEPENDENCY:
(
()
)
**************************************************************************/

#ifndef SWIG
#ifndef __TMM_ALLOC_ARGS_HH__
#define __TMM_ALLOC_ARGS_HH__

#include <utility>
#include <type_traits>
#include "trick_types.hh"

#include "trick/MemoryManager.hh"

#if defined (__GNUC__)
#define TRICK_HAVE_ABI
#elif
#if __has_include(<cxxabi.h>)
#define TRICK_HAVE_ABI
#endif
#endif

namespace Trick {


// Helper struct to always yield false
template<typename T>
struct always_false : std::false_type {};

//C++11 work around to get a void_t type
template<typename... Ts>
struct make_void {
typedef void type;
};

template<typename... Ts>
using void_t = typename make_void<Ts...>::type;

/**
* @brief Primary template for `has_getname`. Defaults to std::false_type.
*
* @tparam T The type for which we care if `TrickTypeToString` has a specialization.
*
* Default implementation of `has_getname`.
*
* @note In the case where `TrickTypeToString` has no member funciton `getName()`,
* this template is selected.
*/
template<typename T, typename = void_t<>>
struct has_getname : std::false_type {};

/**
* @brief Trait to check if `TrickTypeToString` has a `getName()` member function.
*
* @tparam T The type for which we care if `TrickTypeToString` has a specialization.
*
* Specialization of `has_getname` for types where `TrickTypeToString<T>` has `getName()` method.
*
* @note This uses SFINAE to detect the presence of the method `getName()` in `TrickTypeToString<T>`. If the method
* doesn't exist this specialization is discarded, and the primary template is used instead.
*
*
*/
template<typename T>
struct has_getname<T, void_t<decltype(std::declval<TrickTypeToString<T>>().getName())>> : std::true_type {};


}


#endif //__TMM_ALLOC_ARGS_HH__

#endif
20 changes: 20 additions & 0 deletions include/trick/trick_types.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*************************************************************************
PURPOSE: (Trick TMM Alloc W/ Args)
ICG: (No)
LIBRARY DEPENDENCY:
(
()
)
**************************************************************************/
#ifndef __TRICK_TYPES_HH__
#define __TRICK_TYPES_HH__

#include "trick_type_to_string.hh"
#ifndef TRICK_HAVE_ABI
/*
This file is generated when ICG runs and should be located
in the sims build/trick directory
*/
#include "trick/trick_type_to_string_ext.hh"
#endif
#endif
Loading
Loading