Mirror reflection library - Lagoon run-time layer 0.5.13
|
This example shows the usage of the SOCI based automatically generated polymorphic factory to create instances of the test person class.
This example requires access to a local postgresql database named 'test' with a table named 'person' with (more or less) the following definition (containing some data):
CREATE TABLE person ( first_name TEXT NOT NULL, middle_name TEXT. family_name TEXT NOT NULL, birth_date DATE, weight FLOAT, height FLOAT );
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/soci_factory.hpp> #include <soci-postgresql.h> #include <iostream> #include <stdexcept> #include "./person.hpp" int main(void) { try { using namespace lagoon; using namespace soci; session test_db(postgresql, "dbname=test"); rowset<row> dataset(test_db.prepare << "SELECT * FROM person"); auto r = dataset.begin(), e = dataset.end(); int i = 0; soci_factory_builder builder; soci_fact_data data(r); auto meta_person = reflected_class<test::person>(); auto person_factory = meta_person->make_factory( builder, raw_ptr(&data) ); // make a tetrahedron object factory while(r != e) { raw_ptr ppers = person_factory->new_(); test::person& pers = *raw_cast<test::person*>(ppers); std::cout << mirror::stream::to_json::from( pers, [&i](std::ostream& out) { out << "person_" << i; } ) << std::endl; meta_person->delete_(ppers); ++r; ++i; } } catch(std::exception const& error) { std::cerr << "Error: " << error.what() << std::endl; } // return 0; } /* Example of output: | "person_0": { | "first_name": "Johnny", | "middle_name": "B", | "family_name": "Goode", | "birth_date": { | "tm_sec": 0, | "tm_min": 0, | "tm_hour": 0, | "tm_mday": 1, | "tm_mon": 0, | "tm_year": 10, | "tm_wday": 6, | "tm_yday": 0, | "tm_isdst": 0 | }, | "weight": 80.1, | "height": 180 | } */