Moved tmm_alloc_args into inludde/trick. File never changes so it was 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.

This commit is contained in:
Mark Herring 2024-09-21 14:49:53 -05:00
parent 61c2fdbed3
commit 1e5d1a8cc4
13 changed files with 186 additions and 89 deletions

View File

@ -0,0 +1,7 @@
#ifndef __TRICK_TYPE_TO_STRING_HH__
#define __TRICK_TYPE_TO_STRING_HH__
template<typename T>
struct TrickTypeToString { };
#endif //__TRICK_TYPE_TO_STRING_HH__

View File

@ -0,0 +1,71 @@
/*************************************************************************
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"
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;
//Helper struct so we can check if a type has a function "has_getname" defaults to false
template<typename T, typename = void_t<>>
struct has_getname : std::false_type {};
template<typename T>
struct has_getname<T, void_t<decltype(std::declval<TrickTypeToString<T>>().getName())>> : std::true_type {};
/*
In the case that a TrickTypeToString<T> for some type T exist, then this function is valid and will be compiled.
*/
template<typename T, typename ...Args>
typename std::enable_if<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)...);
}
/*
In the case that a TrickTypeToString<T> for some type T does NOT exist, then this function is valid and will be compiled.
*/
template<typename T, typename ...Args>
typename std::enable_if<!has_getname<T>::value, T*>::type
tmm_alloc_args(Args&&... args)
{
static_assert(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 //__TMM_ALLOC_ARGS_HH__
#endif

View File

@ -0,0 +1,12 @@
#ifndef __TRICK_TYPES_HH__
#define __TRICK_TYPES_HH__
#include "trick_type_to_string.hh"
/*
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

View File

@ -3,9 +3,19 @@ from trick.unit_test import *
print(f"alloc_test.atwargs = {alloc_test}")
#TRICK_EXPECT_EQ(alloc_test.atwargs.some_int, 0)
#TRICK_EXPECT_NEAR(alloc_test.atwargs.some_double, 0, 1e-6)
def main():
trick_utest.unit_tests.enable()
trick_utest.unit_tests.set_file_name( os.getenv("TRICK_HOME") + "/trick_test/SIM_tmm_alloc_args.xml" )
trick_utest.unit_tests.set_test_name( "TMMAllocWithArgsTest" )
trick.add_read(4.0, """TRICK_EXPECT_EQ(alloc_test.atwargs.some_int, 0)""")
trick.add_read(4.0, """TRICK_EXPECT_NEAR(alloc_test.atwargs.some_double, 0, 1e-6)""")
trick.stop(5.0)
if __name__ == "__main__":
main()

View File

@ -0,0 +1,31 @@
import math
from trick.unit_test import *
print(f"alloc_test.atwargs = {alloc_test}")
def test():
test_suite = "AllocatingUserDefinedTypes"
test_case = "type_AllocTestWithArguments"
trick.add_read(4.0, """TRICK_EXPECT_EQ(alloc_test.atwargs.some_int, 0, test_case, test_suite)""")
trick.add_read(4.0, """TRICK_EXPECT_NEAR(alloc_test.atwargs.some_double, 0, 1e-6, test_case, test_suite)""")
def main():
trick_utest.unit_tests.enable()
trick_utest.unit_tests.set_file_name( os.getenv("TRICK_HOME") + "/trick_test/SIM_tmm_alloc_args.xml" )
trick_utest.unit_tests.set_test_name( "TMMAllocWithArgsTest" )
trick.add_read(5.0, """test()""")
trick.stop(5.0)
if __name__ == "__main__":
main()

View File

@ -7,7 +7,7 @@ PURPOSE:
#include "sim_objects/default_trick_sys.sm"
##include "alloc_with_args.hh"
##include "trick/tmm_alloc_args.hh"
##include "trick/trick_type_traits.hh"
class AllocTestSimObject : public Trick::SimObject {
@ -23,8 +23,8 @@ class AllocTestSimObject : public Trick::SimObject {
void init_alloc()
{
std::cout << "Entered init_alloc()\n";
std::cout << TrickTypeToString<AllocTestWithArguments>::getName() << "\n";
atwargs = tmm_alloc_args<AllocTestWithArguments>(0, 1.0);
//std::cout << TrickTypeToString<AllocTestWithArguments>::getName() << "\n";
atwargs = Trick::tmm_alloc_args<AllocTestWithArguments>(0, 1.0);
std::cout << "Called tmm_alloc_args: " << atwargs << "\n";
}
};

View File

@ -653,6 +653,7 @@ void PrintAttributes::markHeaderAsVisited(const std::string& header) {
void PrintAttributes::writeTrickTypeToStructHeader()
{
_mkdir("build/trick");
std::set<std::string> class_names_to_print;
for(auto& pair : processed_classes)
@ -679,7 +680,7 @@ void PrintAttributes::writeTrickTypeToStructHeader()
}
std::ofstream out_file;
out_file.open("build/trick/trick_type_to_string.hh");
out_file.open("build/trick/trick_type_to_string_ext.hh");
out_file << "/********************************* TRICK HEADER *******************************\n";
out_file << "PURPOSE: ( Map types that trick knows about to strings using tempalte specialization )\n";
@ -690,6 +691,7 @@ void PrintAttributes::writeTrickTypeToStructHeader()
out_file << "\n";
out_file << "#pragma once";
out_file << "\n\n";
out_file << "#include \"trick/trick_type_to_string.hh\"\n";
out_file << "#include <string>\n";
out_file << "//Forward declarations\n";
@ -700,11 +702,6 @@ void PrintAttributes::writeTrickTypeToStructHeader()
out_file << "\n\n";
out_file << "template<typename T>\n";
out_file << "struct TrickTypeToString { static std::string getName(); };\n";
out_file << "\n\n";
for (auto class_name : class_names_to_print)
{
out_file << "template<>\n";
@ -723,73 +720,13 @@ void PrintAttributes::writeTrickTypeToStructHeader()
}
void PrintAttributes::writeTemplateAllocHeader()
{
std::string build_trick = "build/trick/";
_mkdir(build_trick.c_str());
std::ofstream out_file;
out_file.open("build/trick/tmm_alloc_args.hh");
out_file << "#ifndef SWIG\n";
out_file << "/********************************* TRICK HEADER *******************************\n";
out_file << "PURPOSE: ( Simulate balls contacting boundaries. )\n";
out_file << "LIBRARY DEPENDENCY:\n";
out_file << "((trick/tmm_alloc_args.o))\n";
out_file << "*******************************************************************************/\n";
out_file << "#ifndef __TMM_ALLOC_ARGS_HH__\n";
out_file << "#define __TMM_ALLOC_ARGS_HH__\n";
out_file << "\n\n";
out_file << "#include <utility>\n\n";
out_file << "#include \"trick/trick_type_to_string.hh\"\n";
out_file << "#include <type_traits>\n\n";
out_file << "//trick includes\n";
out_file << "#include \"trick/MemoryManager.hh\"\n\n";
out_file << "template<typename... Ts>\n";
out_file << "struct make_void {\n";
out_file << " typedef void type;\n";
out_file << "};\n\n";
out_file << "template<typename... Ts>\n";
out_file << "using void_t = typename make_void<Ts...>::type;\n\n";
out_file << "template<typename T, typename = void_t<>>\n";
out_file << "struct has_getname : std::false_type {};\n";
out_file << "\n";
out_file << "template<typename T>\n";
out_file << "struct has_getname<T, void_t<decltype(std::declval<TrickTypeToString<T>>().getName())>> : std::true_type {};\n\n";
out_file << "template<typename T, typename ...Args>\n";
out_file << "typename std::enable_if<has_getname<T>::value, T*>::type\n";
out_file << "tmm_alloc_args(Args&&... args)\n";
out_file << "{\n";
out_file << " void* new_alloc = trick_MM->declare_var(TrickTypeToString<T>::getName().c_str());\n";
out_file << " return new (new_alloc) T(std::forward<Args>(args)...);\n";
out_file << "}\n\n";
out_file << "template<typename T, typename ...Args>\n";
out_file << "typename std::enable_if<!has_getname<T>::value, T*>::type\n";
out_file << "tmm_alloc_args(Args&&... args)\n";
out_file << "{\n";
out_file << " static_assert(true, \"You've attempted to call tmm_alloc_args using a type(T) that does not have an implemented template specialization.\");\n";
out_file << " return nullptr;\n";
out_file << "}\n\n";
out_file << "#endif //__TMM_ALLOC_ARGS_HH__\n";
out_file << "#endif // SWIG\n";
out_file.close();
out_file.open("build/trick/tmm_alloc_args.cc");
out_file << "#include \"trick/tmm_alloc_args.hh\"";
out_file.close();
}
void PrintAttributes::addTypedefClass(std::string typedef_class)
{
typedef_classes.insert(typedef_class);
}
void PrintAttributes::setUseTMMAllocArgs(bool use_tmm_alloc_args)
{
this->use_tmm_alloc_args = use_tmm_alloc_args;
}

View File

@ -68,12 +68,11 @@ class PrintAttributes {
void markHeaderAsVisited(const std::string& header);
void writeTemplateAllocHeader();
void writeTrickTypeToStructHeader();
void addTypedefClass(std::string typedef_class);
void setUseTMMAllocArgs(bool use_tmm_alloc_args);
protected:
const bool verboseBuild = (getenv("TRICK_VERBOSE_BUILD") || getenv("VERBOSE"));
@ -109,6 +108,9 @@ class PrintAttributes {
/** We are processing sim_services */
bool sim_services_flag ;
/** Control if files related to tmm_alloc_args are generated */
bool use_tmm_alloc_args ;
/** We are specifying an output directory for all files */
std::string output_dir ;

View File

@ -38,9 +38,14 @@ void PrintFileContents10::printIOHeader(std::ostream & ostream , std::string hea
<< "#include \"trick/parameter_types.h\"\n"
<< "#include \"trick/ClassSizeCheck.hh\"\n"
<< "#include \"trick/UnitsMap.hh\"\n"
<< "#include \"trick/checkpoint_stl.hh\"\n"
<< "#include \"trick/tmm_alloc_args.hh\"\n"
<< "#include \"" << header_file_name << "\"\n"
<< "#include \"trick/checkpoint_stl.hh\"\n";
if(use_tmm_alloc_args)
{
ostream << "#include \"trick/tmm_alloc_args.hh\"\n";
}
ostream << "#include \"" << header_file_name << "\"\n"
<< "\n" ;
}
@ -357,8 +362,11 @@ void PrintFileContents10::printClass( std::ostream & ostream , ClassValues * cv
print_io_src_delete(ostream, cv) ;
print_close_extern_c(ostream) ;
print_units_map(ostream, cv) ;
if(use_tmm_alloc_args)
{
printTemplateConstructorWrapper(ostream, cv) ;
}
}
void PrintFileContents10::printEnum( std::ostream & ostream , EnumValues * ev ) {
print_enum_attr(ostream, ev) ;

View File

@ -87,3 +87,7 @@ std::vector<FieldDescription*> PrintFileContentsBase::getPrintableFields(ClassVa
return results;
}
void PrintFileContentsBase::setUseTMMAllocArgs(bool use_tmm_alloc_args)
{
this->use_tmm_alloc_args = use_tmm_alloc_args;
}

View File

@ -8,6 +8,7 @@
#include <map>
#include <set>
class ConstructValues ;
class ClassValues ;
class EnumValues ;
@ -42,6 +43,7 @@ class PrintFileContentsBase {
virtual void printEnumMap(std::ostream & ostream, EnumValues * ev) ;
virtual void printEnumMapFooter(std::ostream & ostream) ;
virtual void setUseTMMAllocArgs(bool use_tmm_alloc_args);
/* gets a vector of fields that can be printed */
std::vector<FieldDescription*> getPrintableFields(ClassValues& classValues, unsigned int ioMask = 0xFFFFFFF);
@ -58,6 +60,8 @@ class PrintFileContentsBase {
/* internal function determines if a particular field is printable */
bool isPrintable(ClassValues * c , FieldDescription *fdes , unsigned int ioMask = 0xFFFFFFF) ;
/* Flag to control printing output related to tmm_alloc_args*/
bool use_tmm_alloc_args ;
} ;
#endif

View File

@ -52,6 +52,7 @@ llvm::cl::list<std::string> input_file_names(llvm::cl::Positional, llvm::cl::des
llvm::cl::list<std::string> sink(llvm::cl::Sink, llvm::cl::ZeroOrMore);
llvm::cl::list<std::string> pre_compiled_headers("include", llvm::cl::Prefix, llvm::cl::desc("pre-compiled headers"), llvm::cl::value_desc("pre_compiled_headers"));
llvm::cl::opt<bool> use_tmm_alloc_args("use_tmm_alloc_args", llvm::cl::desc("use_tmm_alloc_args"), llvm::cl::init(false), llvm::cl::ZeroOrMore) ;
llvm::cl::opt<bool> global_compat15("c", llvm::cl::desc("Print the offsetof calculations in attributes")) ;
llvm::cl::opt<llvm::cl::boolOrDefault> print_trick_icg("print-TRICK-ICG", llvm::cl::desc("Print warnings where TRICK_ICG may cause io_src inconsistencies")) ;
llvm::cl::alias compat15_alias ("compat15" , llvm::cl::desc("Alias for -c") , llvm::cl::aliasopt(global_compat15)) ;
@ -340,9 +341,19 @@ int main(int argc, char * argv[]) {
// Print the list of headers that have the ICG:(No) comment
printAttributes.printICGNoFiles();
printAttributes.writeTemplateAllocHeader();
if(use_tmm_alloc_args || !sim_services_flag)
{
std::cout << "Printing tmm_alloc_arg related files.\n";
printAttributes.setUseTMMAllocArgs(true);
printAttributes.writeTrickTypeToStructHeader();
}
else
{
std::cout << "NOT PRINTING THEM\n";
printAttributes.setUseTMMAllocArgs(false);
}
if (icgDiagConsumer->error_in_user_code) {
std::cout << color(ERROR, "Trick build was terminated due to error in user code!") << std::endl;