/* Boost.Extension / adaptable_factory example * * (C) Copyright Jeremy Pack 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 using namespace boost::extensions; using namespace boost::reflections; // This function would need to use some of the facilities // from Boost.TypeTraits in order to handle references, etc. // well. template void InputParameter(parameter_map& params, const std::string& name) { T value; std::cin >> value; std::cout << "Input value: " << value << " of type: " << typeid(T).name() << std::endl; params.insert(std::make_pair(name, new parameter(value))); } class Operation { public: Operation(double result) : result(result) { } double result; }; class Multiplier : public Operation { public: Multiplier(double first_value, double second_value) : Operation(first_value * second_value) { } }; int main() { std::map input_functions; input_functions[typeid(float)] = &InputParameter; input_functions[typeid(double)] = &InputParameter; input_functions[typeid(std::string)] = &InputParameter; adaptable_factory op_factory; op_factory.set("First Double", "Second Double"); parameter_map params; std::vector > missing = op_factory.get_missing_params(params); for (std::vector >::iterator it = missing.begin(); it != missing.end(); ++it) { std::cout << "Finding type to input..." << std::endl; std::map::iterator func = input_functions.find(it->first); if (func == input_functions.end()) { std::cout << "Could not find all needed parameters." << std::endl; return 1; } std::cout << "Please input a value for the variable <" << it->second << "> of type <" << it->first.type().name() << ">:" << std::endl; (*func->second)(params, it->second); std::cout << "Input complete for parameter." << std::endl; } boost::scoped_ptr op(op_factory.create(params)); if (op.get()) { std::cout << "The result is: " << op->result << std::endl; } else { std::cout << "Creation failed!" << std::endl; } }