cpp_import_class_test.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2016 Klemens Morgenstern
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. // For more information, see http://www.boost.org
  7. #include <boost/predef.h>
  8. #if (__cplusplus >= 201402L) || (BOOST_COMP_MSVC >= BOOST_VERSION_NUMBER(14,0,0))
  9. #include "../example/b2_workarounds.hpp"
  10. #include <iostream>
  11. using namespace std;
  12. #include <boost/dll/smart_library.hpp>
  13. #include <boost/dll/import_mangled.hpp>
  14. #include <boost/dll/import_class.hpp>
  15. #include <boost/core/lightweight_test.hpp>
  16. #include <boost/filesystem.hpp>
  17. #include <boost/variant.hpp>
  18. #include <boost/function.hpp>
  19. #define L cout << __LINE__ << endl;
  20. int main(int argc, char* argv[])
  21. {
  22. using namespace boost::dll;
  23. using namespace boost::dll::experimental;
  24. boost::dll::fs::path pt = b2_workarounds::first_lib_from_argv(argc, argv);
  25. BOOST_TEST(!pt.empty());
  26. std::cout << "Library: " << pt << std::endl;
  27. smart_library sm(pt);
  28. auto static_val = import_mangled<int>(sm, "some_space::some_class::value");
  29. std::cout << "--------------------- Entry Points ------------------------\n" << std::endl;
  30. for (auto &s : sm.symbol_storage().get_storage())
  31. std::cout << s.demangled << std::endl;
  32. std::cout << "-----------------------------------------------------------\n\n" << std::endl;
  33. auto sp_variable = import_mangled<double>(sm, "some_space::variable");
  34. auto unscoped_var = import_mangled<int>(sm, "unscoped_var");
  35. std::size_t type_size = *import_mangled<std::size_t>(sm, "some_space::size_of_some_class");
  36. {
  37. #if defined(BOOST_MSVC) || defined(BOOST_MSVC_FULL_VER)
  38. class override_class{};
  39. auto cl = import_class<override_class, int>(sm, "some_space::some_class", type_size, 42);
  40. #else
  41. auto cl = import_class<class override_class, int>(sm, "some_space::some_class", type_size, 42);
  42. #endif
  43. BOOST_TEST(!cl.is_copy_assignable());
  44. BOOST_TEST(!cl.is_copy_constructible());
  45. BOOST_TEST( cl.is_move_assignable());
  46. BOOST_TEST( cl.is_move_constructible());
  47. BOOST_TEST(*static_val == 42);
  48. auto i = cl.call<const override_class, int()>("get")();
  49. BOOST_TEST(i == 456);
  50. cl.call<void(int)>("set")(42);
  51. i = 0;
  52. i = cl.call<const override_class, int()>("get")();
  53. BOOST_TEST(i == 42);
  54. auto func = import_mangled<
  55. override_class, double(double, double), int(int, int),
  56. volatile override_class, int(int, int),
  57. const volatile override_class, double(double, double)>(sm, "func");
  58. BOOST_TEST((cl->*func)(3.,2.) == 6.);
  59. BOOST_TEST((cl->*func)(1 ,2 ) == 3 );
  60. auto fun2 = cl.import<double(double, double), int(int, int)>("func");
  61. BOOST_TEST((cl->*fun2)(3.,2.) == 6.);
  62. BOOST_TEST((cl->*fun2)(5 ,2 ) == 7 );
  63. //test if it binds.
  64. boost::function<int(override_class* const, int, int)> mem_fn_obj = func;
  65. const std::type_info & ti = cl.get_type_info();
  66. std::string imp_name = boost::core::demangle(ti.name());
  67. #if defined(BOOST_MSVC) || defined(BOOST_MSVC_FULL_VER)
  68. std::string exp_name = "struct some_space::some_class";
  69. #else
  70. std::string exp_name = "some_space::some_class";
  71. #endif
  72. BOOST_TEST(imp_name == exp_name);
  73. }
  74. BOOST_TEST(*static_val == 0);
  75. return boost::report_errors();
  76. }
  77. #else
  78. int main() {return 0;}
  79. #endif