From 8f3d3956b1e837c726859eb8bbe19dad79c54a42 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Sat, 29 Apr 2017 23:55:46 +0300 Subject: Add hxx extension for headers and lib prefix for library dirs --- libbrep/.gitignore | 10 + libbrep/build.cxx | 48 +++++ libbrep/build.hxx | 166 +++++++++++++++ libbrep/build.xml | 67 ++++++ libbrep/buildfile | 50 +++++ libbrep/common.cxx | 10 + libbrep/common.hxx | 341 +++++++++++++++++++++++++++++++ libbrep/database-lock.cxx | 44 ++++ libbrep/database-lock.hxx | 43 ++++ libbrep/odb.sh | 40 ++++ libbrep/package-extra.sql | 130 ++++++++++++ libbrep/package-traits.cxx | 70 +++++++ libbrep/package-traits.hxx | 38 ++++ libbrep/package.cxx | 169 +++++++++++++++ libbrep/package.hxx | 500 +++++++++++++++++++++++++++++++++++++++++++++ libbrep/package.xml | 417 +++++++++++++++++++++++++++++++++++++ libbrep/types.hxx | 94 +++++++++ libbrep/utility.hxx | 34 +++ libbrep/version.hxx.in | 74 +++++++ libbrep/wrapper-traits.hxx | 63 ++++++ 20 files changed, 2408 insertions(+) create mode 100644 libbrep/.gitignore create mode 100644 libbrep/build.cxx create mode 100644 libbrep/build.hxx create mode 100644 libbrep/build.xml create mode 100644 libbrep/buildfile create mode 100644 libbrep/common.cxx create mode 100644 libbrep/common.hxx create mode 100644 libbrep/database-lock.cxx create mode 100644 libbrep/database-lock.hxx create mode 100755 libbrep/odb.sh create mode 100644 libbrep/package-extra.sql create mode 100644 libbrep/package-traits.cxx create mode 100644 libbrep/package-traits.hxx create mode 100644 libbrep/package.cxx create mode 100644 libbrep/package.hxx create mode 100644 libbrep/package.xml create mode 100644 libbrep/types.hxx create mode 100644 libbrep/utility.hxx create mode 100644 libbrep/version.hxx.in create mode 100644 libbrep/wrapper-traits.hxx (limited to 'libbrep') diff --git a/libbrep/.gitignore b/libbrep/.gitignore new file mode 100644 index 0000000..3f251f5 --- /dev/null +++ b/libbrep/.gitignore @@ -0,0 +1,10 @@ +common-odb.?xx + +package-odb.?xx +package.sql +package-extra.hxx + +build-odb.?xx +build.sql + +version.hxx diff --git a/libbrep/build.cxx b/libbrep/build.cxx new file mode 100644 index 0000000..710c0b2 --- /dev/null +++ b/libbrep/build.cxx @@ -0,0 +1,48 @@ +// file : libbrep/build.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include + +namespace brep +{ + // build_state + // + string + to_string (build_state s) + { + switch (s) + { + case build_state::untested: return "untested"; + case build_state::testing: return "testing"; + case build_state::tested: return "tested"; + } + + return string (); // Should never reach. + } + + build_state + to_build_state (const string& s) + { + if (s == "untested") return build_state::untested; + else if (s == "testing") return build_state::testing; + else if (s == "tested") return build_state::tested; + else throw invalid_argument ("invalid build state '" + s + "'"); + } + + // build + // + build:: + build (string pnm, version pvr, string cfg, string mnm, string msm) + : id (package_id (move (pnm), pvr), move (cfg)), + package_name (id.package.name), + package_version (move (pvr)), + configuration (id.configuration), + state (build_state::testing), + timestamp (timestamp_type::clock::now ()), + forced (false), + machine (move (mnm)), + machine_summary (move (msm)) + { + } +} diff --git a/libbrep/build.hxx b/libbrep/build.hxx new file mode 100644 index 0000000..d28f5ab --- /dev/null +++ b/libbrep/build.hxx @@ -0,0 +1,166 @@ +// file : libbrep/build.hxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef LIBBREP_BUILD_HXX +#define LIBBREP_BUILD_HXX + +#include + +#include +#include + +#include + +#include +#include + +#include // Must be included last (see assert). + +// Used by the data migration entries. +// +#define LIBBREP_BUILD_SCHEMA_VERSION_BASE 1 + +#pragma db model version(LIBBREP_BUILD_SCHEMA_VERSION_BASE, 1, open) + +// We have to keep these mappings at the global scope instead of inside +// the brep namespace because they need to be also effective in the +// bbot namespace from which we "borrow" types (and some of them use the mapped +// types). +// +#pragma db map type(bbot::result_status) as(std::string) \ + to(to_string (?)) \ + from(bbot::to_result_status (?)) + +namespace brep +{ + #pragma db value + struct build_id + { + package_id package; + string configuration; + + build_id () = default; + build_id (package_id p, string c) + : package (move (p)), configuration (move (c)) {} + }; + + inline bool + operator< (const build_id& x, const build_id& y) + { + return + x.package < y.package ? true : + y.package < x.package ? false : + x.configuration < y.configuration; + } + + // build_state + // + enum class build_state: std::uint8_t + { + untested, + testing, + tested + }; + + string + to_string (build_state); + + build_state + to_build_state (const string&); // May throw invalid_argument. + + inline ostream& + operator<< (ostream& os, build_state s) {return os << to_string (s);} + + #pragma db map type(build_state) as(string) \ + to(to_string (?)) \ + from(brep::to_build_state (?)) + + // result_status + // + using bbot::result_status; + + using optional_result_status = optional; + + #pragma db map type(optional_result_status) as(optional_string) \ + to((?) ? bbot::to_string (*(?)) : brep::optional_string ()) \ + from((?) \ + ? bbot::to_result_status (*(?)) \ + : brep::optional_result_status ()) + + // operation_results + // + using bbot::operation_result; + #pragma db value(operation_result) definition + + using bbot::operation_results; + + #pragma db object pointer(shared_ptr) session + class build + { + public: + using timestamp_type = brep::timestamp; + + // Create the build object with the testing state, non-existent status, + // the timestamp set to now and the forced flag set to false. + // + build (string name, version, + string configuration, + string machine, string machine_summary); + + build_id id; + + string& package_name; // Tracks id.package.name. + upstream_version package_version; // Original of id.package.version. + string& configuration; // Tracks id.configuration. + + build_state state; + + // Time of the last state change (the creation time initially). + // + timestamp_type timestamp; + + // True if the package rebuild has been forced. + // + bool forced; + + // Present only if the state is 'tested'. + // + optional status; + + // Present only if the state is 'testing' or 'tested'. + // + optional machine; + optional machine_summary; + + // Note that the logs are stored as std::string/TEXT which is Ok since + // they are UTF-8 and our database is UTF-8. + // + #pragma db section(results_section) + operation_results results; + + #pragma db load(lazy) update(always) + odb::section results_section; + + // Database mapping. + // + #pragma db member(id) id column("") + + #pragma db member(package_name) transient + #pragma db member(package_version) \ + set(this.package_version.init (this.id.package.version, (?))) + #pragma db member(configuration) transient + + #pragma db member(results) id_column("") value_column("") + + build (const build&) = delete; + build& operator= (const build&) = delete; + + private: + friend class odb::access; + build () + : package_name (id.package.name), configuration (id.configuration) {} + }; +} + +#endif // LIBBREP_BUILD_HXX diff --git a/libbrep/build.xml b/libbrep/build.xml new file mode 100644 index 0000000..8d9178a --- /dev/null +++ b/libbrep/build.xml @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
diff --git a/libbrep/buildfile b/libbrep/buildfile new file mode 100644 index 0000000..b4005ab --- /dev/null +++ b/libbrep/buildfile @@ -0,0 +1,50 @@ +# file : libbrep/buildfile +# copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +# license : MIT; see accompanying LICENSE file + +define sql: file +sql{*}: extension = sql +sql{*}: install = data/ + +import int_libs = libodb%lib{odb} +import int_libs += libodb-pgsql%lib{odb-pgsql} +import int_libs += libbutl%lib{butl} +import int_libs += libbpkg%lib{bpkg} +import int_libs += libbbot%lib{bbot} + +lib{brep}: \ +{hxx cxx}{ build } \ +{file }{ build.xml } \ +{hxx ixx cxx}{ build-odb } \ +{hxx cxx}{ common } \ +{hxx ixx cxx}{ common-odb } \ +{hxx cxx}{ package } \ +{file }{ package.xml } \ +{hxx ixx cxx}{ package-odb } \ +{hxx }{ package-extra } \ +{hxx cxx}{ package-traits } \ +{hxx cxx}{ database-lock } \ +{hxx }{ types } \ +{hxx }{ utility } \ +{hxx }{ version } \ +{hxx }{ wrapper-traits } \ + $int_libs \ +sql{build package package-extra} + +hxx{version}: in{version} $src_root/file{manifest} +hxx{version}: dist = true + +# For pre-releases use the complete version to make sure they cannot be used +# in place of another pre-release or the final version. +# +if $version.pre_release + lib{brep}: bin.lib.version = @"-$version.project_id" +else + lib{brep}: bin.lib.version = @"-$version.major.$version.minor" + +lib{brep}: cxx.export.poptions = "-I$out_root" "-I$src_root" +lib{brep}: cxx.export.libs = $int_libs + +# Install into the libbrep/ subdirectory of, say, /usr/include/. +# +install.include = $install.include/libbrep/ diff --git a/libbrep/common.cxx b/libbrep/common.cxx new file mode 100644 index 0000000..39fd0c7 --- /dev/null +++ b/libbrep/common.cxx @@ -0,0 +1,10 @@ +// file : libbrep/common.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include + +namespace brep +{ + const version wildcard_version (0, "0", nullopt, 0); +} diff --git a/libbrep/common.hxx b/libbrep/common.hxx new file mode 100644 index 0000000..bf60491 --- /dev/null +++ b/libbrep/common.hxx @@ -0,0 +1,341 @@ +// file : libbrep/common.hxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef LIBBREP_COMMON_HXX +#define LIBBREP_COMMON_HXX + +#include +#include +#include // static_assert + +#include +#include + +// The uint16_t value range is not fully covered by SMALLINT PostgreSQL type +// to which uint16_t is mapped by default. +// +#pragma db value(uint16_t) type("INTEGER") + +namespace brep +{ + // Use an image type to map bpkg::version to the database since there + // is no way to modify individual components directly. + // + #pragma db value + struct _version + { + uint16_t epoch; + string canonical_upstream; + string canonical_release; + uint16_t revision; + string upstream; + optional release; + }; +} + +#include + +namespace brep +{ + using optional_version = optional; + using _optional_version = optional<_version>; +} + +// Prevent assert() macro expansion in get/set expressions. This should +// appear after all #include directives since the assert() macro is +// redefined in each inclusion. +// +#ifdef ODB_COMPILER +# undef assert +# define assert assert +void assert (int); +#endif + +// We have to keep these mappings at the global scope instead of inside +// the brep namespace because they need to be also effective in the +// bpkg namespace from which we "borrow" types (and some of them use version). +// +#pragma db map type(bpkg::version) as(brep::_version) \ + to(brep::_version{(?).epoch, \ + (?).canonical_upstream, \ + (?).canonical_release, \ + (?).revision, \ + (?).upstream, \ + (?).release}) \ + from(bpkg::version ((?).epoch, \ + std::move ((?).upstream), \ + std::move ((?).release), \ + (?).revision)) + +#pragma db map type(brep::optional_version) as(brep::_optional_version) \ + to((?) \ + ? brep::_version{(?)->epoch, \ + (?)->canonical_upstream, \ + (?)->canonical_release, \ + (?)->revision, \ + (?)->upstream, \ + (?)->release} \ + : brep::_optional_version ()) \ + from((?) \ + ? bpkg::version ((?)->epoch, \ + std::move ((?)->upstream), \ + std::move ((?)->release), \ + (?)->revision) \ + : brep::optional_version ()) + +namespace brep +{ + // path + // + #pragma db map type(path) as(string) to((?).string ()) from(brep::path (?)) + + using optional_path = optional; + using optional_string = optional; + + #pragma db map type(optional_path) as(brep::optional_string) \ + to((?) ? (?)->string () : brep::optional_string ()) \ + from((?) ? brep::path (*(?)) : brep::optional_path ()) + + #pragma db map type(dir_path) as(string) \ + to((?).string ()) from(brep::dir_path (?)) + + // Ensure that timestamp can be represented in nonoseconds without loss of + // accuracy, so the following ODB mapping is adequate. + // + static_assert( + std::ratio_greater_equal::value, + "The following timestamp ODB mapping is invalid"); + + // As it pointed out in butl/timestamp we will overflow in year 2262, but + // by that time some larger basic type will be available for mapping. + // + #pragma db map type(timestamp) as(uint64_t) \ + to(std::chrono::duration_cast ( \ + (?).time_since_epoch ()).count ()) \ + from(brep::timestamp ( \ + std::chrono::duration_cast ( \ + std::chrono::nanoseconds (?)))) + + // version + // + using bpkg::version; + + #pragma db value + struct canonical_version + { + uint16_t epoch; + string canonical_upstream; + string canonical_release; + uint16_t revision; + + bool + empty () const noexcept + { + // Note that an empty canonical_upstream doesn't denote an empty + // canonical_version. Remeber, that canonical_upstream doesn't include + // rightmost digit-only zero components? So non-empty version("0") has + // an empty canonical_upstream. + // + return epoch == 0 && canonical_upstream.empty () && + canonical_release.empty () && revision == 0; + } + + // Change collation to ensure the proper comparison of the "absent" release + // with a specified one. + // + // The default collation for UTF8-encoded TEXT columns in PostgreSQL is + // UCA-compliant. This makes the statement 'a' < '~' to be false, which + // in turn makes the statement 2.1.alpha < 2.1 to be false as well. + // + // Unicode Collation Algorithm (UCA): http://unicode.org/reports/tr10/ + // + #pragma db member(canonical_release) options("COLLATE \"C\"") + }; + + #pragma db value transient + struct upstream_version: version + { + #pragma db member(upstream_) virtual(string) \ + get(this.upstream) \ + set(this = brep::version (0, std::move (?), std::string (), 0)) + + #pragma db member(release_) virtual(optional_string) \ + get(this.release) \ + set(this = brep::version ( \ + 0, std::move (this.upstream), std::move (?), 0)) + + upstream_version () = default; + upstream_version (version v): version (move (v)) {} + upstream_version& + operator= (version v) {version& b (*this); b = v; return *this;} + + void + init (const canonical_version& cv, const upstream_version& uv) + { + *this = version (cv.epoch, uv.upstream, uv.release, cv.revision); + assert (cv.canonical_upstream == canonical_upstream && + cv.canonical_release == canonical_release); + } + }; + + // Wildcard version. Satisfies any dependency constraint and is represented + // as 0+0 (which is also the "stub version"; since a real version is always + // greater than the stub version, we reuse it to signify a special case). + // + extern const version wildcard_version; + + #pragma db value + struct package_id + { + string name; + canonical_version version; + + package_id () = default; + package_id (string n, const brep::version& v) + : name (move (n)), + version { + v.epoch, v.canonical_upstream, v.canonical_release, v.revision} + { + } + }; + + // Version comparison operators. + // + // They allow comparing objects that have epoch, canonical_upstream, + // canonical_release, and revision data members. The idea is that this + // works for both query members of types version and canonical_version + // as well as for comparing canonical_version to version. + // + template + inline auto + compare_version_eq (const T1& x, const T2& y, bool revision) + -> decltype (x.epoch == y.epoch) + { + // Since we don't quite know what T1 and T2 are (and where the resulting + // expression will run), let's not push our luck with something like + // (!revision || x.revision == y.revision). + // + auto r (x.epoch == y.epoch && + x.canonical_upstream == y.canonical_upstream && + x.canonical_release == y.canonical_release); + + return revision + ? r && x.revision == y.revision + : r; + } + + template + inline auto + compare_version_ne (const T1& x, const T2& y, bool revision) + -> decltype (x.epoch == y.epoch) + { + auto r (x.epoch != y.epoch || + x.canonical_upstream != y.canonical_upstream || + x.canonical_release != y.canonical_release); + + return revision + ? r || x.revision != y.revision + : r; + } + + template + inline auto + compare_version_lt (const T1& x, const T2& y, bool revision) + -> decltype (x.epoch == y.epoch) + { + auto r ( + x.epoch < y.epoch || + (x.epoch == y.epoch && x.canonical_upstream < y.canonical_upstream) || + (x.epoch == y.epoch && x.canonical_upstream == y.canonical_upstream && + x.canonical_release < y.canonical_release)); + + return revision + ? r || + (x.epoch == y.epoch && x.canonical_upstream == y.canonical_upstream && + x.canonical_release == y.canonical_release && x.revision < y.revision) + : r; + } + + template + inline auto + compare_version_le (const T1& x, const T2& y, bool revision) + -> decltype (x.epoch == y.epoch) + { + auto r ( + x.epoch < y.epoch || + (x.epoch == y.epoch && x.canonical_upstream < y.canonical_upstream)); + + return revision + ? r || + (x.epoch == y.epoch && x.canonical_upstream == y.canonical_upstream && + x.canonical_release < y.canonical_release) || + (x.epoch == y.epoch && x.canonical_upstream == y.canonical_upstream && + x.canonical_release == y.canonical_release && x.revision <= y.revision) + : r || + (x.epoch == y.epoch && x.canonical_upstream == y.canonical_upstream && + x.canonical_release <= y.canonical_release); + } + + template + inline auto + compare_version_gt (const T1& x, const T2& y, bool revision) + -> decltype (x.epoch == y.epoch) + { + auto r ( + x.epoch > y.epoch || + (x.epoch == y.epoch && x.canonical_upstream > y.canonical_upstream) || + (x.epoch == y.epoch && x.canonical_upstream == y.canonical_upstream && + x.canonical_release > y.canonical_release)); + + return revision + ? r || + (x.epoch == y.epoch && x.canonical_upstream == y.canonical_upstream && + x.canonical_release == y.canonical_release && x.revision > y.revision) + : r; + } + + template + inline auto + compare_version_ge (const T1& x, const T2& y, bool revision) + -> decltype (x.epoch == y.epoch) + { + auto r ( + x.epoch > y.epoch || + (x.epoch == y.epoch && x.canonical_upstream > y.canonical_upstream)); + + return revision + ? r || + (x.epoch == y.epoch && x.canonical_upstream == y.canonical_upstream && + x.canonical_release > y.canonical_release) || + (x.epoch == y.epoch && x.canonical_upstream == y.canonical_upstream && + x.canonical_release == y.canonical_release && x.revision >= y.revision) + : r || + (x.epoch == y.epoch && x.canonical_upstream == y.canonical_upstream && + x.canonical_release >= y.canonical_release); + } + + template + inline auto + order_by_version_desc (const T& x) -> //decltype ("ORDER BY" + x.epoch) + decltype (x.epoch == 0) + { + return "ORDER BY" + + x.epoch + "DESC," + + x.canonical_upstream + "DESC," + + x.canonical_release + "DESC," + + x.revision + "DESC"; + } + + inline bool + operator< (const package_id& x, const package_id& y) + { + if (int r = x.name.compare (y.name)) + return r < 0; + + return compare_version_lt (x.version, y.version, true); + } +} + +#endif // LIBBREP_COMMON_HXX diff --git a/libbrep/database-lock.cxx b/libbrep/database-lock.cxx new file mode 100644 index 0000000..c69c240 --- /dev/null +++ b/libbrep/database-lock.cxx @@ -0,0 +1,44 @@ +// file : libbrep/database-lock.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include + +#include +#include +#include + +namespace brep +{ + using namespace odb::pgsql; + + database_lock:: + database_lock (database& db) + { + // Before locking the table make sure it exists. + // + { + transaction t (db.begin ()); + db.execute ("CREATE TABLE IF NOT EXISTS database_mutex ()"); + t.commit (); + } + + connection_ = db.connection (); + + // Don't make current. Will be rolled back in destructor. + // + transaction_.reset (new transaction (connection_->begin (), false)); + + try + { + connection_->execute ("LOCK TABLE database_mutex NOWAIT"); + } + catch (const database_exception& e) + { + if (e.sqlstate () == "55P03") // The table is already locked. + throw database_locked (); + + throw; + } + } +} diff --git a/libbrep/database-lock.hxx b/libbrep/database-lock.hxx new file mode 100644 index 0000000..60d57a4 --- /dev/null +++ b/libbrep/database-lock.hxx @@ -0,0 +1,43 @@ +// file : libbrep/database-lock.hxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef LIBBREP_DATABASE_LOCK_HXX +#define LIBBREP_DATABASE_LOCK_HXX + +#include // database, transaction +#include + +#include +#include + +namespace brep +{ + struct database_locked: std::exception + { + virtual char const* + what () const throw () {return "database locked";} + }; + + // Try to "lock" the PostgreSQL database in the constructor and release the + // lock in the destructor. Throw database_locked if the database is already + // locked by someone else. May also throw odb::pgsql::database_exception. + // + // This mechanism is used by the brep loader and schema migration tool to + // make sure they don't step on each others toes. + // + // Note: movable but not copyable. + // + class database_lock + { + public: + explicit + database_lock (odb::pgsql::database&); + + private: + odb::pgsql::connection_ptr connection_; + unique_ptr transaction_; + }; +} + +#endif // LIBBREP_DATABASE_LOCK_HXX diff --git a/libbrep/odb.sh b/libbrep/odb.sh new file mode 100755 index 0000000..b794a2d --- /dev/null +++ b/libbrep/odb.sh @@ -0,0 +1,40 @@ +#! /usr/bin/env bash + +trap 'exit 1' ERR + +odb=odb +lib="\ +-I$HOME/work/odb/libodb-pgsql-default \ +-I$HOME/work/odb/libodb-pgsql \ +-I$HOME/work/odb/libodb-default \ +-I$HOME/work/odb/libodb" + +$odb $lib -d pgsql --std c++11 --generate-query \ + --odb-epilogue '#include ' \ + --hxx-prologue '#include ' \ + -DLIBODB_BUILD2 -DLIBODB_PGSQL_BUILD2 \ + -I .. -I ../../libbbot -I ../../libbpkg -I ../../libbutl \ + --hxx-suffix ".hxx" --include-with-brackets \ + --include-prefix libbrep --guard-prefix LIBBREP \ + common.hxx + +$odb $lib -d pgsql --std c++11 --generate-query --generate-schema \ + --schema-format sql --schema-format embedded --schema-name package \ + --odb-epilogue '#include ' \ + --hxx-prologue '#include ' \ + --generate-prepared -DLIBODB_BUILD2 -DLIBODB_PGSQL_BUILD2 \ + -I .. -I ../../libbbot -I ../../libbpkg -I ../../libbutl \ + --hxx-suffix ".hxx" --include-with-brackets \ + --include-prefix libbrep --guard-prefix LIBBREP \ + package.hxx + +xxd -i package-extra.hxx + +$odb $lib -d pgsql --std c++11 --generate-query --generate-schema \ + --schema-format sql --schema-format embedded --schema-name build \ + --odb-epilogue '#include ' \ + --generate-prepared -DLIBODB_BUILD2 -DLIBODB_PGSQL_BUILD2 \ + -I .. -I ../../libbbot -I ../../libbpkg -I ../../libbutl \ + --hxx-suffix ".hxx" --include-with-brackets \ + --include-prefix libbrep --guard-prefix LIBBREP \ + build.hxx diff --git a/libbrep/package-extra.sql b/libbrep/package-extra.sql new file mode 100644 index 0000000..823c3af --- /dev/null +++ b/libbrep/package-extra.sql @@ -0,0 +1,130 @@ +-- This file should be parsable by the brep-migrate utility. To decrease the +-- parser complexity, the following restrictions are placed: +-- +-- * comments must start with -- at the beginning of the line (ignoring +-- leading spaces) +-- * only CREATE and DROP statements for FUNCTION and TYPE +-- * function bodies must be defined using $$-quoted strings +-- * strings other then function bodies must be quoted with ' or " +-- * statements must end with ";\n" +-- + +-- There is no need to drop to_tsvector() explicitly, as we can rely on "DROP +-- TYPE IF EXISTS weighted_text CASCADE" statement below, which will drop all +-- objects that depend on this type. Moreover this DROP FUNCTION statement will +-- fail for old versions of PostgreSQL (experienced for 9.2.14) with error: +-- type "weighted_text" does not exist. +-- +-- DROP FUNCTION IF EXISTS to_tsvector(IN document weighted_text); +-- +DROP FUNCTION IF EXISTS search_packages(IN query tsquery, INOUT name TEXT); +DROP FUNCTION IF EXISTS search_latest_packages(IN query tsquery); +DROP FUNCTION IF EXISTS latest_package(INOUT name TEXT); +DROP FUNCTION IF EXISTS latest_packages(); + +DROP TYPE IF EXISTS weighted_text CASCADE; +CREATE TYPE weighted_text AS (a TEXT, b TEXT, c TEXT, d TEXT); + +-- Return the latest versions of internal packages as a set of package rows. +-- +CREATE FUNCTION +latest_packages() +RETURNS SETOF package AS $$ + SELECT p1.* + FROM package p1 LEFT JOIN package p2 ON ( + p1.internal_repository IS NOT NULL AND p1.name = p2.name AND + p2.internal_repository IS NOT NULL AND + (p1.version_epoch < p2.version_epoch OR + p1.version_epoch = p2.version_epoch AND + (p1.version_canonical_upstream < p2.version_canonical_upstream OR + p1.version_canonical_upstream = p2.version_canonical_upstream AND + (p1.version_canonical_release < p2.version_canonical_release OR + p1.version_canonical_release = p2.version_canonical_release AND + p1.version_revision < p2.version_revision)))) + WHERE + p1.internal_repository IS NOT NULL AND p2.name IS NULL; +$$ LANGUAGE SQL STABLE; + +-- Find the latest version of an internal package having the specified name. +-- Return a single row containing the package id, empty row set if the package +-- not found. +-- +CREATE FUNCTION +latest_package(INOUT name TEXT, + OUT version_epoch INTEGER, + OUT version_canonical_upstream TEXT, + OUT version_canonical_release TEXT, + OUT version_revision INTEGER) +RETURNS SETOF record AS $$ + SELECT name, version_epoch, version_canonical_upstream, + version_canonical_release, version_revision + FROM latest_packages() + WHERE name = latest_package.name; +$$ LANGUAGE SQL STABLE; + +-- Search for the latest version of an internal packages matching the specified +-- search query. Return a set of rows containing the package id and search +-- rank. If query is NULL, then match all packages and return 0 rank for +-- all rows. +-- +CREATE FUNCTION +search_latest_packages(IN query tsquery, + OUT name TEXT, + OUT version_epoch INTEGER, + OUT version_canonical_upstream TEXT, + OUT version_canonical_release TEXT, + OUT version_revision INTEGER, + OUT rank real) +RETURNS SETOF record AS $$ + SELECT name, version_epoch, version_canonical_upstream, + version_canonical_release, version_revision, + CASE + WHEN query IS NULL THEN 0 +-- Weight mapping: D C B A + ELSE ts_rank_cd('{0.05, 0.2, 0.9, 1.0}', search_index, query) + END AS rank + FROM latest_packages() + WHERE query IS NULL OR search_index @@ query; +$$ LANGUAGE SQL STABLE; + +-- Search for packages matching the search query and having the specified name. +-- Return a set of rows containing the package id and search rank. If query +-- is NULL, then match all packages and return 0 rank for all rows. +-- +CREATE FUNCTION +search_packages(IN query tsquery, + INOUT name TEXT, + OUT version_epoch INTEGER, + OUT version_canonical_upstream TEXT, + OUT version_canonical_release TEXT, + OUT version_revision INTEGER, + OUT rank real) +RETURNS SETOF record AS $$ + SELECT name, version_epoch, version_canonical_upstream, + version_canonical_release, version_revision, + CASE + WHEN query IS NULL THEN 0 +-- Weight mapping: D C B A + ELSE ts_rank_cd('{0.05, 0.2, 0.9, 1.0}', search_index, query) + END AS rank + FROM package + WHERE + internal_repository IS NOT NULL AND name = search_packages.name AND + (query IS NULL OR search_index @@ query); +$$ LANGUAGE SQL STABLE; + +-- Parse weighted_text to tsvector. +-- +CREATE FUNCTION +to_tsvector(IN document weighted_text) +RETURNS tsvector AS $$ + SELECT + CASE + WHEN document IS NULL THEN NULL + ELSE + setweight(to_tsvector(document.a), 'A') || + setweight(to_tsvector(document.b), 'B') || + setweight(to_tsvector(document.c), 'C') || + setweight(to_tsvector(document.d), 'D') + END +$$ LANGUAGE SQL IMMUTABLE; diff --git a/libbrep/package-traits.cxx b/libbrep/package-traits.cxx new file mode 100644 index 0000000..a03cbac --- /dev/null +++ b/libbrep/package-traits.cxx @@ -0,0 +1,70 @@ +// file : libbrep/package-traits.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include + +#include +#include +#include +#include // memcpy + +#include + +using namespace std; + +namespace odb +{ + namespace pgsql + { + static inline void + to_pg_string (ostream& os, const string& s) + { + os << '"'; + + for (auto c: s) + { + if (c == '\\' || c == '"') + os << '\\'; + + os << c; + } + + os << '"'; + } + + // Convert C++ weighted_text struct to PostgreSQL weighted_text + // composite type. + // + void value_traits:: + set_image (details::buffer& b, + size_t& n, + bool& is_null, + const value_type& v) + { + is_null = v.a.empty () && v.b.empty () && v.c.empty () && v.d.empty (); + + if (!is_null) + { + ostringstream o; + o << "("; + to_pg_string (o, v.a); + o << ","; + to_pg_string (o, v.b); + o << ","; + to_pg_string (o, v.c); + o << ","; + to_pg_string (o, v.d); + o << ")"; + + const string& s (o.str ()); + n = s.size (); + + if (n > b.capacity ()) + b.capacity (n); + + memcpy (b.data (), s.c_str (), n); + } + } + } +} diff --git a/libbrep/package-traits.hxx b/libbrep/package-traits.hxx new file mode 100644 index 0000000..b626ea8 --- /dev/null +++ b/libbrep/package-traits.hxx @@ -0,0 +1,38 @@ +// file : brep/package-traits -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef BREP_PACKAGE_TRAITS +#define BREP_PACKAGE_TRAITS + +#include // size_t + +#include + +#include // weighted_text + +namespace odb +{ + namespace pgsql + { + template <> + class value_traits + { + public: + typedef brep::weighted_text value_type; + typedef value_type query_type; + typedef details::buffer image_type; + + static void + set_value (value_type&, const details::buffer&, std::size_t, bool) {} + + static void + set_image (details::buffer&, + std::size_t& n, + bool& is_null, + const value_type&); + }; + } +} + +#endif // BREP_PACKAGE_TRAITS diff --git a/libbrep/package.cxx b/libbrep/package.cxx new file mode 100644 index 0000000..20be387 --- /dev/null +++ b/libbrep/package.cxx @@ -0,0 +1,169 @@ +// file : libbrep/package.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include + +#include + +#include + +using namespace std; +using namespace odb::core; + +namespace brep +{ + // dependency + // + string dependency:: + name () const + { + return package.object_id ().name; + } + + ostream& + operator<< (ostream& o, const dependency& d) + { + o << d.name (); + + if (d.constraint) + o << ' ' << *d.constraint; + + return o; + } + + bool + operator== (const dependency& x, const dependency& y) + { + return x.name () == y.name () && x.constraint == y.constraint; + } + + bool + operator!= (const dependency& x, const dependency& y) + { + return !(x == y); + } + + // package + // + package:: + package (string nm, + version_type vr, + priority_type pr, + string sm, + license_alternatives_type la, + strings tg, + optional ds, + string ch, + url_type ur, + optional pu, + email_type em, + optional pe, + optional be, + dependencies_type dp, + requirements_type rq, + optional lc, + optional sh, + shared_ptr rp) + : id (move (nm), vr), + version (move (vr)), + priority (move (pr)), + summary (move (sm)), + license_alternatives (move (la)), + tags (move (tg)), + description (move (ds)), + changes (move (ch)), + url (move (ur)), + package_url (move (pu)), + email (move (em)), + package_email (move (pe)), + build_email (move (be)), + dependencies (move (dp)), + requirements (move (rq)), + internal_repository (move (rp)), + location (move (lc)), + sha256sum (move (sh)) + { + assert (internal_repository->internal); + } + + package:: + package (string nm, + version_type vr, + shared_ptr rp) + : id (move (nm), vr), + version (move (vr)) + { + assert (!rp->internal); + other_repositories.emplace_back (move (rp)); + } + + weighted_text package:: + search_text () const + { + if (!internal ()) + return weighted_text (); + + // Derive keywords from the basic package information: name, + // version. + // + //@@ What about 'stable' from cppget.org/stable? Add path of + // the repository to keywords? Or is it too "polluting" and + // we will handle it in some other way (e.g., by allowing + // the user to specify repo location in the drop-down box)? + // Probably drop-box would be better as also tells what are + // the available internal repositories. + // + string k (id.name + " " + version.string () + " " + version.string (true)); + + // Add tags to keywords. + // + for (const auto& t: tags) + k += " " + t; + + // Add licenses to keywords. + // + for (const auto& la: license_alternatives) + { + for (const auto& l: la) + { + k += " " + l; + + // If license is say LGPLv2 then LGPL is also a keyword. + // + size_t n (l.size ()); + if (n > 2 && l[n - 2] == 'v' && l[n - 1] >= '0' && l[n - 1] <= '9') + k += " " + string (l, 0, n - 2); + } + } + + return {move (k), summary, description ? *description : "", changes}; + } + + // repository + // + repository:: + repository (repository_location l, + string d, + repository_location h, + optional c, + uint16_t r) + : name (l.canonical_name ()), + location (move (l)), + display_name (move (d)), + priority (r), + cache_location (move (h)), + certificate (move (c)), + internal (true) + { + } + + repository:: + repository (repository_location l) + : name (l.canonical_name ()), + location (move (l)), + priority (0), + internal (false) + { + } +} diff --git a/libbrep/package.hxx b/libbrep/package.hxx new file mode 100644 index 0000000..8164639 --- /dev/null +++ b/libbrep/package.hxx @@ -0,0 +1,500 @@ +// file : libbrep/package.hxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef LIBBREP_PACKAGE_HXX +#define LIBBREP_PACKAGE_HXX + +#include +#include + +#include +#include + +#include +#include + +#include // Must be included last (see assert). + +// Used by the data migration entries. +// +#define LIBBREP_PACKAGE_SCHEMA_VERSION_BASE 4 + +#pragma db model version(LIBBREP_PACKAGE_SCHEMA_VERSION_BASE, 4, open) + +namespace brep +{ + // @@ Might make sense to put some heavy members (e.g., description, + // containers) into a separate section. + // + // @@ Not sure there is a benefit in making tags a full-blown container + // (i.e., a separate table). Maybe provide a mapping of vector + // to TEXT as a comma-separated list. + // + + // Forward declarations. + // + class repository; + class package; + + // priority + // + using bpkg::priority; + + #pragma db value(priority) definition + #pragma db member(priority::value) column("") + + // url + // + using bpkg::url; + + #pragma db value(url) definition + #pragma db member(url::value) virtual(string) before access(this) column("") + + // email + // + using bpkg::email; + + #pragma db value(email) definition + #pragma db member(email::value) virtual(string) before access(this) column("") + + // licenses + // + using bpkg::licenses; + using license_alternatives = vector; + + #pragma db value(licenses) definition + + // dependencies + // + using bpkg::dependency_constraint; + + #pragma db value(dependency_constraint) definition + + // Notes: + // + // 1. Will the package be always resolvable? What if it is in + // another repository (i.e., a "chained" third-party repo). + // The question is then whether we will load such "third- + // party packages" (i.e., packages that are not in our + // repository). If the answer is yes, then we can have + // a pointer here. If the answer is no, then we can't. + // Also, if the answer is yes, we probably don't need to + // load as much information as for "our own" packages. We + // also shouldn't be showing them in search results, etc. + // I think all we need is to know which repository this + // package comes from so that we can tell the user. How are + // we going to capture this? Poly hierarchy of packages? + // + // 2. I believe we don't need to use a weak pointer here since + // there should be no package dependency cycles (and therefore + // ownership cycles). + // + // 3. Actually there can be dependency cycle as dependency referes not to + // just a package but a specific version, so for the same pair of + // packages dependency for different versions can have an opposite + // directions. The possible solution is instead of a package we point + // to the earliest version that satisfies the constraint. But this + // approach requires to ensure no cycles exist before instantiating + // package objects which in presense of "foreign" packages can be + // tricky. Can stick to just a package name until get some clarity on + // "foreign" package resolution. + // + // 4. As we left just the package class the dependency resolution come to + // finding the best version matching package object. The question is + // if to resolve dependencies on the loading phase or in the WEB interface + // when required. The arguments in favour of doing that during loading + // phase are: + // + // - WEB interface get offloaded from a possibly expensive queries + // which otherwise have to be executed multiple times for the same + // dependency no matter the result would be the same. + // + // - No need to complicate persisted object model with repository + // relations otherwise required just for dependency resolution. + // + + #pragma db value + struct dependency + { + using package_type = brep::package; + + lazy_shared_ptr package; + optional constraint; + + // Prerequisite package name. + // + string + name () const; + + // Database mapping. + // + #pragma db member(package) column("") not_null + #pragma db member(constraint) column("") + }; + + ostream& + operator<< (ostream&, const dependency&); + + bool + operator== (const dependency&, const dependency&); + + bool + operator!= (const dependency&, const dependency&); + + #pragma db value + class dependency_alternatives: public vector + { + public: + bool conditional; + bool buildtime; + string comment; + + dependency_alternatives () = default; + dependency_alternatives (bool d, bool b, string c) + : conditional (d), buildtime (b), comment (move (c)) {} + }; + + using dependencies = vector; + + // requirements + // + using bpkg::requirement_alternatives; + using requirements = vector; + + #pragma db value(requirement_alternatives) definition + + // repository_location + // + using bpkg::repository_location; + + #pragma db map type(repository_location) as(string) \ + to((?).string ()) from(brep::repository_location (?)) + + #pragma db value + class certificate + { + public: + string fingerprint; // SHA256 fingerprint. + string name; // CN component of Subject. + string organization; // O component of Subject. + string email; // email: in Subject Alternative Name. + string pem; // PEM representation. + }; + + #pragma db object pointer(shared_ptr) session + class repository + { + public: + using email_type = brep::email; + using certificate_type = brep::certificate; + + // Create internal repository. + // + repository (repository_location, + string display_name, + repository_location cache_location, + optional, + uint16_t priority); + + // Create external repository. + // + explicit + repository (repository_location); + + string name; // Object id (canonical name). + repository_location location; + string display_name; + + // The order in the internal repositories configuration file, starting from + // 1. 0 for external repositories. + // + uint16_t priority; + + optional url; + + // Present only for internal repositories. + // + optional email; + optional summary; + optional description; + + // Location of the repository local cache. Non empty for internal + // repositories and external ones with a filesystem path location. + // + repository_location cache_location; + + // Present only for internal signed repositories. + // + optional certificate; + + // Initialized with timestamp_nonexistent by default. + // + timestamp packages_timestamp; + + // Initialized with timestamp_nonexistent by default. + // + timestamp repositories_timestamp; + + bool internal; + vector> complements; + vector> prerequisites; + + // Database mapping. + // + #pragma db member(name) id + + #pragma db member(location) \ + set(this.location = std::move (?); \ + assert (this.name == this.location.canonical_name ())) + + #pragma db member(complements) id_column("repository") \ + value_column("complement") value_not_null + + #pragma db member(prerequisites) id_column("repository") \ + value_column("prerequisite") value_not_null + + private: + friend class odb::access; + repository () = default; + }; + + // The 'to' expression calls the PostgreSQL to_tsvector(weighted_text) + // function overload (package-extra.sql). Since we are only interested + // in "write-only" members of this type, make the 'from' expression + // always return empty string (we still have to work the placeholder + // in to keep overprotective ODB happy). + // + #pragma db map type("tsvector") as("TEXT") \ + to("to_tsvector((?)::weighted_text)") from("COALESCE('',(?))") + + // C++ type for weighted PostgreSQL tsvector. + // + #pragma db value type("tsvector") + struct weighted_text + { + string a; + string b; + string c; + string d; + }; + + #pragma db object pointer(shared_ptr) session + class package + { + public: + using repository_type = brep::repository; + using version_type = brep::version; + using priority_type = brep::priority; + using license_alternatives_type = brep::license_alternatives; + using url_type = brep::url; + using email_type = brep::email; + using dependencies_type = brep::dependencies; + using requirements_type = brep::requirements; + + // Create internal package object. + // + package (string name, + version_type, + priority_type, + string summary, + license_alternatives_type, + strings tags, + optional description, + string changes, + url_type, + optional package_url, + email_type, + optional package_email, + optional build_email, + dependencies_type, + requirements_type, + optional location, + optional sha256sum, + shared_ptr); + + // Create external package object. + // + // External repository packages can appear on the WEB interface only in + // dependency list in the form of a link to the corresponding WEB page. + // The only package information required to compose such a link is the + // package name, version, and repository location. + // + package (string name, version_type, shared_ptr); + + bool + internal () const noexcept {return internal_repository != nullptr;} + + // Manifest data. + // + package_id id; + upstream_version version; + priority_type priority; + string summary; + license_alternatives_type license_alternatives; + strings tags; + optional description; + string changes; + url_type url; + optional package_url; + email_type email; + optional package_email; + optional build_email; + dependencies_type dependencies; + requirements_type requirements; + lazy_shared_ptr internal_repository; + + // Path to the package file. Present only for internal packages. + // + optional location; + + // Present only for internal packages. + // + optional sha256sum; + + vector> other_repositories; + + // Database mapping. + // + #pragma db member(id) id column("") + #pragma db member(version) set(this.version.init (this.id.version, (?))) + + // license + // + using _license_key = odb::nested_key; + using _licenses_type = std::map<_license_key, string>; + + #pragma db value(_license_key) + #pragma db member(_license_key::outer) column("alternative_index") + #pragma db member(_license_key::inner) column("index") + + #pragma db member(license_alternatives) id_column("") value_column("") + #pragma db member(licenses) \ + virtual(_licenses_type) \ + after(license_alternatives) \ + get(odb::nested_get (this.license_alternatives)) \ + set(odb::nested_set (this.license_alternatives, std::move (?))) \ + id_column("") key_column("") value_column("license") + + // tags + // + #pragma db member(tags) id_column("") value_column("tag") + + // dependencies + // + using _dependency_key = odb::nested_key; + using _dependency_alternatives_type = + std::map<_dependency_key, dependency>; + + #pragma db value(_dependency_key) + #pragma db member(_dependency_key::outer) column("dependency_index") + #pragma db member(_dependency_key::inner) column("index") + + #pragma db member(dependencies) id_column("") value_column("") + #pragma db member(dependency_alternatives) \ + virtual(_dependency_alternatives_type) \ + after(dependencies) \ + get(odb::nested_get (this.dependencies)) \ + set(odb::nested_set (this.dependencies, std::move (?))) \ + id_column("") key_column("") value_column("dep_") + + // requirements + // + using _requirement_key = odb::nested_key; + using _requirement_alternatives_type = + std::map<_requirement_key, string>; + + #pragma db value(_requirement_key) + #pragma db member(_requirement_key::outer) column("requirement_index") + #pragma db member(_requirement_key::inner) column("index") + + #pragma db member(requirements) id_column("") value_column("") + #pragma db member(requirement_alternatives) \ + virtual(_requirement_alternatives_type) \ + after(requirements) \ + get(odb::nested_get (this.requirements)) \ + set(odb::nested_set (this.requirements, std::move (?))) \ + id_column("") key_column("") value_column("id") + + // other_repositories + // + #pragma db member(other_repositories) \ + id_column("") value_column("repository") value_not_null + + // search_index + // + #pragma db member(search_index) virtual(weighted_text) null \ + access(search_text) + + #pragma db index method("GIN") member(search_index) + + private: + friend class odb::access; + package () = default; + + // Save keywords, summary, description, and changes to weighted_text + // a, b, c, d members, respectively. So a word found in keywords will + // have a higher weight than if it's found in the summary. + // + weighted_text + search_text () const; + + // Noop as search_index is a write-only member. + // + void + search_text (const weighted_text&) {} + }; + + // Package search query matching rank. + // + #pragma db view query("/*CALL*/ SELECT * FROM search_latest_packages(?)") + struct latest_package_search_rank + { + package_id id; + double rank; + }; + + #pragma db view \ + query("/*CALL*/ SELECT count(*) FROM search_latest_packages(?)") + struct latest_package_count + { + size_t result; + + operator size_t () const {return result;} + }; + + #pragma db view query("/*CALL*/ SELECT * FROM search_packages(?)") + struct package_search_rank + { + package_id id; + double rank; + }; + + #pragma db view query("/*CALL*/ SELECT count(*) FROM search_packages(?)") + struct package_count + { + size_t result; + + operator size_t () const {return result;} + }; + + #pragma db view query("/*CALL*/ SELECT * FROM latest_package(?)") + struct latest_package + { + package_id id; + }; + + #pragma db view object(package) + struct package_version + { + package_id id; + upstream_version version; + + // Database mapping. + // + #pragma db member(id) column("") + #pragma db member(version) set(this.version.init (this.id.version, (?))) + }; +} + +#endif // LIBBREP_PACKAGE_HXX diff --git a/libbrep/package.xml b/libbrep/package.xml new file mode 100644 index 0000000..657c2fc --- /dev/null +++ b/libbrep/package.xml @@ -0,0 +1,417 @@ + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
diff --git a/libbrep/types.hxx b/libbrep/types.hxx new file mode 100644 index 0000000..a0001a7 --- /dev/null +++ b/libbrep/types.hxx @@ -0,0 +1,94 @@ +// file : libbrep/types.hxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef LIBBREP_TYPES_HXX +#define LIBBREP_TYPES_HXX + +#include +#include +#include // unique_ptr, shared_ptr +#include // pair +#include // size_t, nullptr_t +#include // uint{8,16,32,64}_t +#include +#include +#include // function, reference_wrapper + +#include // ios_base::failure +#include // exception +#include // logic_error, invalid_argument, runtime_error +#include + +#include + +#include +#include +#include +#include + +namespace brep +{ + // Commonly-used types. + // + using std::uint8_t; + using std::uint16_t; + using std::uint32_t; + using std::uint64_t; + + using std::size_t; + using std::nullptr_t; + + using std::pair; + using std::string; + using std::function; + using std::reference_wrapper; + + using std::unique_ptr; + using std::shared_ptr; + using std::weak_ptr; + + using std::vector; + + using strings = vector; + using cstrings = vector; + + using std::istream; + using std::ostream; + + // Exceptions. While is included, there is no using for + // std::exception -- use qualified. + // + using std::logic_error; + using std::invalid_argument; + using std::runtime_error; + using std::system_error; + using io_error = std::ios_base::failure; + + // + // + using butl::optional; + using butl::nullopt; + + // ODB smart pointers. + // + using odb::lazy_shared_ptr; + using odb::lazy_weak_ptr; + + // + // + using butl::path; + using butl::dir_path; + using butl::basic_path; + using butl::invalid_path; + + using paths = std::vector; + using dir_paths = std::vector; + + // + // + using butl::timestamp; + using butl::timestamp_nonexistent; +} + +#endif // LIBBREP_TYPES_HXX diff --git a/libbrep/utility.hxx b/libbrep/utility.hxx new file mode 100644 index 0000000..f5948dd --- /dev/null +++ b/libbrep/utility.hxx @@ -0,0 +1,34 @@ +// file : libbrep/utility.hxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef LIBBREP_UTILITY_HXX +#define LIBBREP_UTILITY_HXX + +#include // make_shared() +#include // to_string() +#include // move(), forward(), declval(), make_pair() +#include // assert() +#include // make_move_iterator() + +#include // reverse_iterate(), operator<<(ostream, exception) + +namespace brep +{ + using std::move; + using std::forward; + using std::declval; + + using std::make_pair; + using std::make_shared; + using std::make_move_iterator; + using std::to_string; + + // + // + using butl::reverse_iterate; +} + +#include + +#endif // LIBBREP_UTILITY_HXX diff --git a/libbrep/version.hxx.in b/libbrep/version.hxx.in new file mode 100644 index 0000000..e1b60a2 --- /dev/null +++ b/libbrep/version.hxx.in @@ -0,0 +1,74 @@ +// file : libbrep/version.hxx.in -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef BREP_VERSION // Note: using the version macro itself. + +// Note: using build2 standard versioning scheme. The numeric version format +// is AAABBBCCCDDDE where: +// +// AAA - major version number +// BBB - minor version number +// CCC - bugfix version number +// DDD - alpha / beta (DDD + 500) version number +// E - final (0) / snapshot (1) +// +// When DDDE is not 0, 1 is subtracted from AAABBBCCC. For example: +// +// Version AAABBBCCCDDDE +// +// 0.1.0 0000010000000 +// 0.1.2 0000010010000 +// 1.2.3 0010020030000 +// 2.2.0-a.1 0020019990010 +// 3.0.0-b.2 0029999995020 +// 2.2.0-a.1.z 0020019990011 +// +#define BREP_VERSION $brep.version.project_number$ULL +#define BREP_VERSION_STR "$brep.version.project$" +#define BREP_VERSION_ID "$brep.version.project_id$" + +#define BREP_VERSION_MAJOR $brep.version.major$ +#define BREP_VERSION_MINOR $brep.version.minor$ +#define BREP_VERSION_PATCH $brep.version.patch$ + +#define BREP_PRE_RELEASE $brep.version.pre_release$ + +#define BREP_SNAPSHOT $brep.version.snapshot_sn$ULL +#define BREP_SNAPSHOT_ID "$brep.version.snapshot_id$" + +#include + +$libbutl.check(LIBBUTL_VERSION, LIBBUTL_SNAPSHOT)$ + +#include + +$libbpkg.check(LIBBPKG_VERSION, LIBBPKG_SNAPSHOT)$ + +#include + +$libbbot.check(LIBBBOT_VERSION, LIBBBOT_SNAPSHOT)$ + +#include + +$libodb.check(LIBODB_VERSION, LIBODB_SNAPSHOT)$ + +#include + +$libodb-pgsql.check(LIBODB_PGSQL_VERSION, LIBODB_PGSQL_SNAPSHOT)$ + +// @@ Not really the correct place for the check since we don't use +// it here. +// +/* +#include + +$libstudxml.check(LIBSTUDXML_VERSION, LIBSTUDXML_SNAPSHOT)$ +*/ + +// For now these are the same. +// +#define LIBBREP_VERSION BREP_VERSION +#define LIBBREP_VERSION_STR BREP_VERSION_STR + +#endif // BREP_VERSION diff --git a/libbrep/wrapper-traits.hxx b/libbrep/wrapper-traits.hxx new file mode 100644 index 0000000..ac039e3 --- /dev/null +++ b/libbrep/wrapper-traits.hxx @@ -0,0 +1,63 @@ +// file : libbrep/wrapper-traits.hxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef LIBBREP_WRAPPER_TRAITS_HXX +#define LIBBREP_WRAPPER_TRAITS_HXX + +#include + +#include + +#include + +namespace odb +{ + template + class wrapper_traits> + { + public: + typedef T wrapped_type; + typedef butl::optional wrapper_type; + + // T can be const. + // + typedef + typename odb::details::meta::remove_const::result + unrestricted_wrapped_type; + + static const bool null_handler = true; + static const bool null_default = true; + + static bool + get_null (const wrapper_type& o) + { + return !o; + } + + static void + set_null (wrapper_type& o) + { + o = wrapper_type (); + } + + static const wrapped_type& + get_ref (const wrapper_type& o) + { + return *o; + } + + static unrestricted_wrapped_type& + set_ref (wrapper_type& o) + { + if (!o) + o = unrestricted_wrapped_type (); + + return const_cast (*o); + } + }; +} + +#include + +#endif // LIBBREP_WRAPPER_TRAITS_HXX -- cgit v1.1