aboutsummaryrefslogtreecommitdiff
path: root/libbpkg/package-name.hxx
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2018-05-23 00:36:15 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2018-05-24 17:56:03 +0300
commitb39e27a9f1ab04cb67ffad1d2c7e7ae089d5e8c4 (patch)
treed79f6d16d451f081af3a7014e3b629a3d85ca3fc /libbpkg/package-name.hxx
parente3c8b1c5273e20a6c20b5ba923cdcea919340950 (diff)
Add package_name class
Diffstat (limited to 'libbpkg/package-name.hxx')
-rw-r--r--libbpkg/package-name.hxx194
1 files changed, 194 insertions, 0 deletions
diff --git a/libbpkg/package-name.hxx b/libbpkg/package-name.hxx
new file mode 100644
index 0000000..6c7ec6c
--- /dev/null
+++ b/libbpkg/package-name.hxx
@@ -0,0 +1,194 @@
+// file : libbpkg/package-name.hxx -*- C++ -*-
+// copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
+// license : MIT; see accompanying LICENSE file
+
+#ifndef LIBBPKG_PACKAGE_NAME_HXX
+#define LIBBPKG_PACKAGE_NAME_HXX
+
+#include <string>
+#include <utility> // move()
+#include <ostream>
+
+#include <libbutl/utility.mxx> // casecmp()
+
+#include <libbpkg/export.hxx>
+#include <libbpkg/version.hxx>
+
+namespace bpkg
+{
+ class LIBBPKG_EXPORT package_name
+ {
+ public:
+ // Create package name from string verifying that it complied with the
+ // specification and throwing std::invalid_argument if that's not the
+ // case. Note that in this case the passed value is guaranteed to be
+ // unchanged.
+ //
+ explicit
+ package_name (const std::string& s): package_name (std::string (s)) {}
+
+ explicit
+ package_name (std::string&&);
+
+ // Create a special empty package name.
+ //
+ package_name () = default;
+
+ // Create an arbitrary string that can be used in contexts that expect
+ // a package name. For example, a package name pattern for use in ODB query
+ // expressions.
+ //
+ enum raw_string_type {raw_string};
+ package_name (std::string s, raw_string_type): value_ (std::move (s)) {}
+
+ bool
+ empty () const noexcept {return value_.empty ();}
+
+ const std::string&
+ string () const& noexcept {return value_;}
+
+ // Moves the underlying package name string out of the package name object.
+ // The object becomes empty. Usage: std::move (name).string ().
+ //
+ std::string
+ string () && {std::string r; r.swap (this->value_); return r;}
+
+ // Compare ignoring case. Note that a string is not checked to be a valid
+ // package name.
+ //
+ int compare (const package_name& n) const {return compare (n.value_);}
+ int compare (const std::string& n) const {return compare (n.c_str ());}
+ int compare (const char* n) const {return butl::casecmp (value_, n);}
+
+ private:
+ std::string value_;
+ };
+
+ inline bool
+ operator< (const package_name& x, const package_name& y)
+ {
+ return x.compare (y) < 0;
+ }
+
+ inline bool
+ operator> (const package_name& x, const package_name& y)
+ {
+ return x.compare (y) > 0;
+ }
+
+ inline bool
+ operator== (const package_name& x, const package_name& y)
+ {
+ return x.compare (y) == 0;
+ }
+
+ inline bool
+ operator<= (const package_name& x, const package_name& y)
+ {
+ return x.compare (y) <= 0;
+ }
+
+ inline bool
+ operator>= (const package_name& x, const package_name& y)
+ {
+ return x.compare (y) >= 0;
+ }
+
+ inline bool
+ operator!= (const package_name& x, const package_name& y)
+ {
+ return x.compare (y) != 0;
+ }
+
+ template <typename T>
+ inline auto
+ operator< (const package_name& x, const T& y)
+ {
+ return x.compare (y) < 0;
+ }
+
+ template <typename T>
+ inline auto
+ operator> (const package_name& x, const T& y)
+ {
+ return x.compare (y) > 0;
+ }
+
+ template <typename T>
+ inline auto
+ operator== (const package_name& x, const T& y)
+ {
+ return x.compare (y) == 0;
+ }
+
+ template <typename T>
+ inline auto
+ operator<= (const package_name& x, const T& y)
+ {
+ return x.compare (y) <= 0;
+ }
+
+ template <typename T>
+ inline auto
+ operator>= (const package_name& x, const T& y)
+ {
+ return x.compare (y) >= 0;
+ }
+
+ template <typename T>
+ inline auto
+ operator!= (const package_name& x, const T& y)
+ {
+ return x.compare (y) != 0;
+ }
+
+ template <typename T>
+ inline auto
+ operator< (const T& x, const package_name& y)
+ {
+ return y > x;
+ }
+
+ template <typename T>
+ inline auto
+ operator> (const T& x, const package_name& y)
+ {
+ return y < x;
+ }
+
+ template <typename T>
+ inline auto
+ operator== (const T& x, const package_name& y)
+ {
+ return y == x;
+ }
+
+ template <typename T>
+ inline auto
+ operator<= (const T& x, const package_name& y)
+ {
+ return y >= x;
+ }
+
+ template <typename T>
+ inline auto
+ operator>= (const T& x, const package_name& y)
+ {
+ return y <= x;
+ }
+
+ template <typename T>
+ inline auto
+ operator!= (const T& x, const package_name& y)
+ {
+ return y != x;
+ }
+
+ inline std::ostream&
+ operator<< (std::ostream& os, const package_name& v)
+ {
+ return os << v.string ();
+ }
+}
+
+#endif // LIBBPKG_PACKAGE_NAME_HXX