From cb1c7730cf5065c708eaea3d151fb9c8682cfce5 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sun, 17 Aug 2014 09:05:29 -0600 Subject: [PATCH] Add the ability to look up user defined typenames Closes #124 --- CMakeLists.txt | 4 ++++ .../chaiscript/language/chaiscript_engine.hpp | 12 ++++++++++ unittests/type_name_test.cpp | 22 +++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 unittests/type_name_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fbc5925..f6183c1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -275,6 +275,10 @@ if(BUILD_TESTING) target_link_libraries(type_info_test ${LIBS}) add_test(NAME Type_Info_Test COMMAND type_info_test) + add_executable(type_name_test unittests/type_name_test.cpp) + target_link_libraries(type_name_test ${LIBS}) + add_test(NAME Type_Name_Test COMMAND type_name_test) + add_executable(eval_catch_exception_test unittests/eval_catch_exception_test.cpp) target_link_libraries(eval_catch_exception_test ${LIBS}) add_test(NAME Eval_Catch_Exception_Test COMMAND eval_catch_exception_test) diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index 336e03a..81b6cb8 100644 --- a/include/chaiscript/language/chaiscript_engine.hpp +++ b/include/chaiscript/language/chaiscript_engine.hpp @@ -503,6 +503,18 @@ namespace chaiscript return ss.str(); } + std::string get_type_name(const Type_Info &ti) const + { + return m_engine.get_type_name(ti); + } + + template + std::string get_type_name() const + { + return get_type_name(user_type()); + } + + /// \brief Loads and parses a file. If the file is already, it is not reloaded /// The use paths specified at ChaiScript construction time are searched for the /// requested file. diff --git a/unittests/type_name_test.cpp b/unittests/type_name_test.cpp new file mode 100644 index 0000000..e837303 --- /dev/null +++ b/unittests/type_name_test.cpp @@ -0,0 +1,22 @@ +// Tests to make sure that the order in which function dispatches occur is correct + +#include +#include + +class MyClass +{ +}; + +int main() +{ + chaiscript::ChaiScript chai; + auto type = chaiscript::user_type(); + chai.add(type, "MyClass"); + + if (chai.get_type_name(type) == "MyClass" && chai.get_type_name() == "MyClass") + { + return EXIT_SUCCESS; + } else { + return EXIT_FAILURE; + } +}