Mirror reflection library - Lagoon run-time layer 0.5.13
|
This example shows the usage of the XML-parsing automatically generated polymorphic factory to create instances of the test tetrahedron class.
Copyright 2008-2010 Matus Chochlik. 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)
// // We want to use Lagoon's polymorphic factories #define LAGOON_MT_WITH_MAKE_FACTORY 1 // // We don't need to traverse through namespace members. // Using this CT switch can greatly improve compile times // and the resulting executable size if namespace member // traversal is not needed #define LAGOON_NO_NAMESPACE_MEMBERS 1 #include <mirror/mirror.hpp> #include <mirror/stream.hpp> #include <lagoon/lagoon.hpp> #include <lagoon/utils/rapidxml_factory.hpp> #include <iostream> #include <stdexcept> #include "./tetrahedron.hpp" int main(void) { try { using namespace lagoon; rapidxml_factory_builder builder; rapidxml_factory_input in; auto data = in.data(); // auto meta_th = reflected_class<test::tetrahedron>(); auto th_factory = meta_th->make_factory( builder, raw_ptr(&data) ); // const char xml[] = "<?xml version='1.0' encoding='utf-8' ?>\n" "<data>\n" " <t1>\n" " <base>\n" " <a x='1' y='0' z='0'/>\n" " <b x='0' y='1' z='0'/>\n" " <c x='0' y='0' z='0'/>\n" " </base>\n" " <apex x='0' y='0' z='6'/>\n" " </t1>\n" " <t2>\n" " <base>\n" " <a x='1' y='0' z='0'/>\n" " <b x='0' y='2' z='0'/>\n" " <c w='0'/>\n" " </base>\n" " <apex x='0' y='0' z='6'/>\n" " </t2>\n" " <t3>\n" " <base>\n" " <a x='2' y='0' z='0'/>\n" " <b x='0' y='2' z='0'/>\n" " <c/>\n" " </base>\n" " <apex x='0' y='0' z='6'/>\n" " </t3>\n" " <t4>\n" " <a x='2' y='0' z='0'/>\n" " <b x='0' y='2' z='0'/>\n" " <c/>\n" " <d x='0' y='0' z='12'/>\n" " </t4>\n" "</data>\n"; // rapidxml::xml_document<> doc; doc.parse<rapidxml::parse_non_destructive>((char*)xml); rapidxml::xml_node<>* root = doc.first_node("data"); rapidxml::xml_node<>* src = root->first_node(); // while(src != nullptr) { in.set(src); // use the factory to construct a tetrahedron and calculate // its volume and base area raw_ptr pt = th_factory->new_(); test::tetrahedron& t = *raw_cast<test::tetrahedron*>(pt); // ... and print them out std::cout << "the volume is " << t.volume() << " " << std::flush; std::cout << "the area of the base is " << t.base.area() << std::endl; meta_th->delete_(pt); src = src->next_sibling(); } } catch(std::exception const& error) { std::cerr << "Error: " << error.what() << std::endl; } // return 0; } /* Example of output: the volume is 1 the area of the base is 0.5 the volume is 2 the area of the base is 1 the volume is 4 the area of the base is 2 the volume is 8 the area of the base is 2 */