/* * Boost.Reflection / extension integration example * * (C) Copyright Mariano G. Consoni 2008 * Distributed under the Boost Software License, Version 1.0. (See * accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) * * See http://www.boost.org/ for latest version. */ #include #include #include #include #include #include #include #include int main(void) { using boost::reflections::instance; using boost::reflections::instance_constructor; using boost::reflections::reflection; // Create a mapping of reflections to strings that // will be populated inside the shared library. std::map reflection_map; // Load the shared library using Boost.Extension boost::extensions::shared_library lib ("libcar_lib.extension"); lib.open(); if (!lib.is_open()) return 2; if (!lib.get &>("extension_export_car")) { return 3; } // Call an exported function to populate // reflection_map lib.get &> ("extension_export_car")(reflection_map); if (reflection_map.size() != size_t(2) || reflection_map.find("suv") == reflection_map.end() || reflection_map.find("compact") == reflection_map.end()) { std::cout << "Could not load reflections!"; return 1; } // Pull out two reflections that were named "suv" and // "compact" respectively. reflection & first_reflection = reflection_map["suv"]; reflection & second_reflection = reflection_map["compact"]; // Use the get_constructor function to find a constructor // that takes one argument, a const char*. instance_constructor first_constructor = first_reflection.get_constructor(); // Use the constructor retrieved to create an instance. // Warning! instances should only be used with functions // and constructors generated by a single reflection object. instance first_instance = first_constructor("First Instance"); // Get a function to call on this instance. boost::reflections::function first_function = first_reflection.get_function("get_type"); std::cout << "First reflection: " << first_function(first_instance) << std::endl; // Repeat the steps for the second reflection. instance_constructor second_constructor = second_reflection.get_constructor(); instance second_instance = second_constructor("Second Instance"); boost::reflections::function second_function = second_reflection.get_function("get_type"); std::cout << "Second reflection: " << second_function(second_instance) << std::endl; }