From bb76e9388009ed0bb2512034f8cd48a7d19aabb3 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 4 Jun 2010 16:29:02 +0200 Subject: Next chunk of functionality --- odb/generator.cxx | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 123 insertions(+), 7 deletions(-) (limited to 'odb/generator.cxx') diff --git a/odb/generator.cxx b/odb/generator.cxx index 9e1da47..0d4e0c9 100644 --- a/odb/generator.cxx +++ b/odb/generator.cxx @@ -16,8 +16,12 @@ #include #include +#include +#include #include +#include + using namespace std; using namespace cutl; @@ -25,12 +29,11 @@ using semantics::path; namespace { - static char const cxx_header[] = + static char const file_header[] = "// This code was generated by CodeSynthesis ODB object persistence\n" "// compiler for C++.\n" "//\n\n"; - /* string make_guard (string const& file, context& ctx) { @@ -50,11 +53,10 @@ namespace if (isalpha (c1) && isalpha (c2) && islower (c1) && isupper (c2)) r += "_"; } - r += std::toupper (g[g.size () - 1]); + r += toupper (g[g.size () - 1]); return ctx.escape (r); } - */ void open (ifstream& ifs, string const& path) @@ -86,15 +88,51 @@ generate (options const& ops, semantics::unit& unit, path const& p) // C++ output. // + string hxx_name (base + ops.odb_file_suffix () + ops.hxx_suffix ()); + string ixx_name (base + ops.odb_file_suffix () + ops.ixx_suffix ()); string cxx_name (base + ops.odb_file_suffix () + ops.cxx_suffix ()); + string sql_name (base + ops.sql_suffix ()); + + path hxx_path (hxx_name); + path ixx_path (ixx_name); path cxx_path (cxx_name); + path sql_path (sql_name); if (!ops.output_dir ().empty ()) { path dir (ops.output_dir ()); + hxx_path = dir / hxx_path; + ixx_path = dir / ixx_path; cxx_path = dir / cxx_path; + sql_path = dir / sql_path; + } + + // + // + ofstream hxx (hxx_path.string ().c_str ()); + + if (!hxx.is_open ()) + { + cerr << "error: unable to open '" << hxx_path << "' in write mode" + << endl; + throw failed (); } + auto_rm.add (hxx_path); + + // + // + ofstream ixx (ixx_path.string ().c_str ()); + + if (!ixx.is_open ()) + { + cerr << "error: unable to open '" << ixx_path << "' in write mode" + << endl; + throw failed (); + } + + auto_rm.add (ixx_path); + // // ofstream cxx (cxx_path.string ().c_str ()); @@ -108,9 +146,29 @@ generate (options const& ops, semantics::unit& unit, path const& p) auto_rm.add (cxx_path); - // Print headers. // - cxx << cxx_header; + // + ofstream sql; + + if (ops.generate_schema ()) + { + sql.open (sql_path.string ().c_str (), ios_base::out); + + if (!sql.is_open ()) + { + cerr << "error: unable to open '" << sql_path << "' in write mode" + << endl; + throw failed (); + } + + auto_rm.add (sql_path); + } + + // Print C++ headers. + // + hxx << file_header; + ixx << file_header; + cxx << file_header; typedef compiler::ostream_filter cxx_filter; @@ -118,23 +176,81 @@ generate (options const& ops, semantics::unit& unit, path const& p) // bool br (ops.include_with_brackets ()); string ip (ops.include_prefix ()); + string gp (ops.guard_prefix ()); if (!ip.empty () && ip[ip.size () - 1] != '/') ip.append ("/"); + if (!gp.empty () && gp[gp.size () - 1] != '_') + gp.append ("_"); + + // HXX + // + { + cxx_filter filt (hxx); + context ctx (hxx, unit, ops); + + string guard (make_guard (gp + hxx_name, ctx)); + + hxx << "#ifndef " << guard << endl + << "#define " << guard << endl + << endl; + + hxx << "#include " << (br ? '<' : '"') << ip << file << + (br ? '>' : '"') << endl + << endl; + + generate_header (ctx); + + hxx << "#include " << (br ? '<' : '"') << ip << ixx_name << + (br ? '>' : '"') << endl + << endl; + + hxx << "#endif // " << guard << endl; + } + + // IXX + // + { + cxx_filter filt (ixx); + context ctx (ixx, unit, ops); + generate_inline (ctx); + } + // CXX // { cxx_filter filt (cxx); context ctx (cxx, unit, ops); - cxx << "#include " << (br ? '<' : '"') << ip << file << + cxx << "#include " << (br ? '<' : '"') << ip << hxx_name << (br ? '>' : '"') << endl << endl; generate_source (ctx); } + // SQL + // + if (ops.generate_schema ()) + { + context ctx (sql, unit, ops); + + switch (ops.database ()) + { + case database::mysql: + { + generate_mysql_schema (ctx); + break; + } + case database::tracer: + { + cerr << "error: the tracer database does not have schema" << endl; + throw failed (); + } + } + } + auto_rm.cancel (); } catch (const generation_failed&) -- cgit v1.1