cpp_import_test.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 <boost/dll/smart_library.hpp>
  11. #include <boost/dll/import_mangled.hpp>
  12. #include <boost/core/lightweight_test.hpp>
  13. #include <boost/filesystem.hpp>
  14. #include <boost/variant.hpp>
  15. #include <boost/function.hpp>
  16. #include <iostream>
  17. struct override_class
  18. {
  19. int arr[32];
  20. };
  21. int main(int argc, char* argv[])
  22. {
  23. using namespace boost::dll;
  24. using namespace boost::dll::experimental;
  25. boost::dll::fs::path pt = b2_workarounds::first_lib_from_argv(argc, argv);
  26. BOOST_TEST(!pt.empty());
  27. std::cout << "Library: " << pt << std::endl;
  28. smart_library sm(pt);
  29. auto sp_variable = import_mangled<double>(sm, "some_space::variable");
  30. auto unscoped_var = import_mangled<int>(sm, "unscoped_var");
  31. auto ovl = import_mangled<void(int), void(double)>(sm, "overloaded");
  32. ovl(12);
  33. BOOST_TEST(*unscoped_var == 12);
  34. ovl(5.0);
  35. BOOST_TEST(*sp_variable == 5.0);
  36. boost::function<void(int)> f_test = ovl;//test if it binds
  37. f_test(-2);
  38. BOOST_TEST(*unscoped_var == -2);
  39. sm.add_type_alias<override_class>("some_space::some_class");
  40. auto func = import_mangled<
  41. override_class, double(double, double), int(int, int),
  42. volatile override_class, int(int, int),
  43. const volatile override_class, double(double, double)>(sm, "func");
  44. override_class override_class_varible{};
  45. override_class *ov = &override_class_varible;
  46. volatile override_class *ovv = ov;
  47. const volatile override_class *ovcv = ov;
  48. BOOST_TEST(func(ov, 3.,2.) == 6.);
  49. BOOST_TEST(func(ov, 1,2 ) == 3 );
  50. BOOST_TEST(func(ovv, 10,2) == 8 );
  51. BOOST_TEST(func(ovcv, 9,2) == 4.5 );
  52. return boost::report_errors();
  53. }
  54. #else
  55. int main() {return 0;}
  56. #endif