mangled.qbk 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. [/
  2. Copyright 2016 Klemens D. Morgenstern
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. /]
  6. [template mangled_ref[path] '''<ulink url="https://github.com/apolukhin/Boost.DLL/blob/develop/example/'''[path]'''">example/'''[path]'''</ulink>''']
  7. [def __to_top [link boost_dll.mangled_import Back to the Top]]
  8. [section Mangled Import]
  9. This section describes the experimental feature of allowing the import of mangled symbols from an dll. While this feature is unique to this library and looks quite promising, it is not throroughly tested and thus not considered stable.
  10. As a short example we can import the following functions quite easily:
  11. ```
  12. //library.dll
  13. namespace foo {
  14. int bar(int);
  15. double bar(double);
  16. }
  17. ```
  18. And the import looks like this:
  19. ```
  20. auto f1 = import_mangled<int(int)>("library.dll", "foo::bar");
  21. auto f2 = import_mangled<double(double)>("library.dll", "foo::bar");
  22. cout << f1(42) << endl;
  23. cout << f2(3.2) << endl;
  24. ```
  25. [section Support & Requirements]
  26. Currently, the Itanium ABI and the MSVC ABI are implemented. The MSVC ABI requires boost.spirit.x3 support, allowing only the usage of MSVC 2015. The Itanium API requires C++11.
  27. * Gcc
  28. * Clang
  29. * MSVC 2015
  30. * Intel C++
  31. The Itanium API does not import the return type of functions, nor the type of global variables.
  32. [endsect]
  33. [section Mangled Import Example]
  34. The core of the mangled import is the [classref smart_library] class. It can import functions and variables in their mangled form; to do this, the smart_library reads the entire outline of the library and demangles every entry point in it. That also means, that this class should only be constructed once.
  35. In order to import all the methods in the following library, we will use the [classref smart_library] .
  36. The first thing to do when creating your own plugins is define the plugin interface. There is an example
  37. of an abstract class that will be our plugin API:
  38. [import ../example/mangled/my_cpp_plugin.hpp]
  39. [cppplug]
  40. Alright, now we have the definition for the plugin, so we use it in the following full-fleshed example. Mind that there is a more convenient solution to import member-functions which will be discussed later on. This example shows however what the [classref smart_lib] provides as features.
  41. [import ../example/mangled/smart_lib.cpp]
  42. At first we setup the smart library. Mind that the alias class is needed to provide a type-alias for the my_plugin.
  43. [smart_lib_setup]
  44. In order to create the class, we will need to allocate memory. That of course means, that we need to know the size; unfortunately it is not exported into the dll, so we added the static size function for export. Static are used as plain functions.
  45. So we import it, call it and allocate memory.
  46. [smart_lib_size]
  47. Now, we have the memory size and a reference with our alias type. In order to use it, we need to register the type as an alias. That will allow the smart library to resolve the type name.
  48. [smart_lib_type_alias]
  49. In order to use the class, we of course need to initialize it, i.e. call the constructor. The Itanium ABI may also implement an allocating constructor. That is why a constructor may have two functions; since we already have allocated the memory we use the standard constructor version, of the constructor from string. So we select the constructor by passing the signature.
  50. [smart_lib_ctor]
  51. So since the class is now initialized, we can call the name method. If the function is const and/or volatile the type parameter passed as type must have the same qualifiers.
  52. [smart_lib_name]
  53. Overloaded functions can only be imported separately.
  54. [smart_lib_calculate]
  55. Import of static variable is done like with plain variable.
  56. [smart_lib_var]
  57. Since we are finished, we call the destructor of the class.
  58. [smart_lib_dtor]
  59. __to_top
  60. [endsect]
  61. [section Class Import]
  62. Now it is demonstrated, how mangled and methods may be imported. This is however a rather versatile way, so an easier interface is provided, which also allows access to the type_info of an object.
  63. We will take the same class and import the same methods, but do it with the import features.
  64. [import ../example/mangled/import_class.cpp]
  65. We put the library into a shared_pointer, because every import will hold such a pointer to it. That is, we do not want to copy it.
  66. [import_class_setup]
  67. Similar to the previous example, we need the size of the class.
  68. [import_class_size]
  69. On a side note, we can also import variable easily with that function.
  70. [import_class_value]
  71. We do the forward declaration on the first call, and invoke the constructor directly. This is quite simple and allows to invoke the constructor directly. The destructor will be invoked automatically.
  72. [import_class_ctor]
  73. Invoking a function will still require to import it first.
  74. [import_class_name]
  75. For overloaded functions, we can import them as groups, which will give us an object containing the overloads.
  76. [import_class_calc]
  77. Additionally, we can access the typeinfo like this.
  78. [import_class_typeinfo]
  79. __to_top
  80. [endsect]
  81. [section Overloading qualifiers]
  82. Not handled in the example was the question, of how it is handled if the qualification differs for an overloaded function. This can be done, by passing the class again with another qualification - a function signature will always pick the last one provided.
  83. If we have this in our plugin:
  84. ```
  85. struct plugin
  86. {
  87. void f(int);
  88. void f(double);
  89. void f(int) const;
  90. void f() const;
  91. void f() volatile;
  92. void f(int) volatile;
  93. void f(double); const volatile;
  94. };
  95. ```
  96. we can import them all at once, with the following command:
  97. ```
  98. auto f = import_class<
  99. alias, f(int), f(double), //not qualified
  100. const alias, f(int), f(), //const
  101. volatile alias, f(), f(int), //volatile
  102. const volatile alias, f(double)//const volatile
  103. >(lib, "f");
  104. ```
  105. [endsect]
  106. [endsect]