2
0
mirror of https://github.com/boostorg/fiber.git synced 2026-02-20 14:42:21 +00:00
Files
fiber/libs/reflection/examples/extension/extension.cpp
Oliver Kowalke 39ec793737 initial checkin
2011-02-09 18:41:35 +01:00

89 lines
2.9 KiB
C++

/*
* 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 <string>
#include <iostream>
#include <boost/extension/factory_map.hpp>
#include <boost/extension/shared_library.hpp>
#include <boost/extension/convenience.hpp>
#include <boost/reflection/reflection.hpp>
#include <boost/reflection/reflection.hpp>
#include <boost/function.hpp>
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<std::string, reflection> 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<void, std::map<std::string,
reflection> &>("extension_export_car")) {
return 3;
}
// Call an exported function to populate
// reflection_map
lib.get<void, std::map<std::string, reflection> &>
("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<const char *> first_constructor =
first_reflection.get_constructor<const char *>();
// 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<const char *> first_function =
first_reflection.get_function<const char *>("get_type");
std::cout << "First reflection: " << first_function(first_instance)
<< std::endl;
// Repeat the steps for the second reflection.
instance_constructor<const char *> second_constructor =
second_reflection.get_constructor<const char *>();
instance second_instance =
second_constructor("Second Instance");
boost::reflections::function<const char *> second_function =
second_reflection.get_function<const char *>("get_type");
std::cout << "Second reflection: " << second_function(second_instance)
<< std::endl;
}