Arbiter
Dependency manager library that supports decentralization
Version.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifndef __cplusplus
4 #error "This file must be compiled as C++."
5 #endif
6 
7 #include <arbiter/Version.h>
8 
9 #include "Optional.h"
10 #include "Types.h"
11 #include "Value.h"
12 
13 #include <functional>
14 #include <string>
15 #include <vector>
16 
17 struct ArbiterSemanticVersion final : public Arbiter::Base
18 {
19  public:
20  unsigned _major;
21  unsigned _minor;
22  unsigned _patch;
23 
24  Arbiter::Optional<std::string> _prereleaseVersion;
25  Arbiter::Optional<std::string> _buildMetadata;
26 
27  ArbiterSemanticVersion (unsigned major, unsigned minor, unsigned patch, Arbiter::Optional<std::string> prereleaseVersion = Arbiter::Optional<std::string>(), Arbiter::Optional<std::string> buildMetadata = Arbiter::Optional<std::string>())
28  : _major(major)
29  , _minor(minor)
30  , _patch(patch)
31  , _prereleaseVersion(prereleaseVersion)
32  , _buildMetadata(buildMetadata)
33  {}
34 
35  /**
36  * Attempts to parse a well-formed semantic version from a string.
37  */
38  // TODO: Add error reporting
39  static Arbiter::Optional<ArbiterSemanticVersion> fromString (const std::string &versionString);
40 
41  std::unique_ptr<Arbiter::Base> clone () const override;
42  std::ostream &describe (std::ostream &os) const override;
43  bool operator== (const Arbiter::Base &other) const override;
44 
45  bool operator< (const ArbiterSemanticVersion &other) const noexcept;
46 
47  bool operator> (const ArbiterSemanticVersion &other) const noexcept
48  {
49  return other < *this;
50  }
51 
53  {
54  return !(*this < other);
55  }
56 
58  {
59  return other >= *this;
60  }
61 };
62 
63 struct ArbiterSelectedVersion final : public Arbiter::Base
64 {
65  public:
66  using Metadata = Arbiter::SharedUserValue<ArbiterSelectedVersion>;
67 
68  Arbiter::Optional<ArbiterSemanticVersion> _semanticVersion;
69  Metadata _metadata;
70 
71  ArbiterSelectedVersion (Arbiter::Optional<ArbiterSemanticVersion> semanticVersion, Metadata metadata)
72  : _semanticVersion(std::move(semanticVersion))
74  {}
75 
76  std::unique_ptr<Arbiter::Base> clone () const override;
77  std::ostream &describe (std::ostream &os) const override;
78  bool operator== (const Arbiter::Base &other) const override;
79 
80  bool operator< (const ArbiterSelectedVersion &other) const;
81 
82  bool operator> (const ArbiterSelectedVersion &other) const
83  {
84  return other < *this;
85  }
86 
87  bool operator<= (const ArbiterSelectedVersion &other) const
88  {
89  return !(other < *this);
90  }
91 
92  bool operator>= (const ArbiterSelectedVersion &other) const
93  {
94  return !(*this < other);
95  }
96 };
97 
98 struct ArbiterSelectedVersionList final : public Arbiter::Base
99 {
100  public:
101  std::vector<ArbiterSelectedVersion> _versions;
102 
103  ArbiterSelectedVersionList () = default;
104 
105  explicit ArbiterSelectedVersionList (std::vector<ArbiterSelectedVersion> versions)
106  : _versions(std::move(versions))
107  {}
108 
109  std::unique_ptr<Arbiter::Base> clone () const override;
110  std::ostream &describe (std::ostream &os) const override;
111  bool operator== (const Arbiter::Base &other) const override;
112 };
113 
114 namespace std {
115 
116 template<>
117 struct hash<ArbiterSemanticVersion> final
118 {
119  public:
120  size_t operator() (const ArbiterSemanticVersion &version) const;
121 };
122 
123 template<>
124 struct hash<ArbiterSelectedVersion> final
125 {
126  public:
127  size_t operator() (const ArbiterSelectedVersion &version) const;
128 };
129 
130 } // namespace std
bool operator>=(const ArbiterSelectedVersion &other) const
Definition: Version.h:92
Metadata _metadata
Definition: Version.h:69
static Arbiter::Optional< ArbiterSemanticVersion > fromString(const std::string &versionString)
Attempts to parse a well-formed semantic version from a string.
auto makeIteratorRange(const Collection &collection)
Creates an IteratorRange encompassing the entirety of the given read-only collection.
Definition: Iterator.h:45
std::ostream & describe(std::ostream &os) const override
Arbiter::Optional< std::string > _buildMetadata
Definition: Version.h:25
bool operator>(const ArbiterSelectedVersion &other) const
Definition: Version.h:82
Arbiter::Optional< ArbiterSemanticVersion > _semanticVersion
Definition: Version.h:68
bool operator>(const ArbiterSemanticVersion &other) const noexcept
Definition: Version.h:47
std::vector< ArbiterSelectedVersion > _versions
Definition: Version.h:101
bool operator==(const Arbiter::Base &other) const override
ArbiterSelectedVersionList()=default
std::ostream & describe(std::ostream &os) const override
bool operator>=(const ArbiterSemanticVersion &other) const noexcept
Definition: Version.h:52
std::unique_ptr< Arbiter::Base > clone() const override
std::ostream & describe(std::ostream &os) const override
std::unique_ptr< Arbiter::Base > clone() const override
size_t operator()(const ArbiterSemanticVersion &version) const
std::unique_ptr< Arbiter::Base > clone() const override
ArbiterSemanticVersion(unsigned major, unsigned minor, unsigned patch, Arbiter::Optional< std::string > prereleaseVersion=Arbiter::Optional< std::string >(), Arbiter::Optional< std::string > buildMetadata=Arbiter::Optional< std::string >())
Definition: Version.h:27
size_t operator()(const ArbiterSelectedVersion &version) const
ArbiterSelectedVersionList(std::vector< ArbiterSelectedVersion > versions)
Definition: Version.h:105
Arbiter::Optional< std::string > _prereleaseVersion
Definition: Version.h:24
ArbiterSelectedVersion(Arbiter::Optional< ArbiterSemanticVersion > semanticVersion, Metadata metadata)
Definition: Version.h:71
bool operator==(const Arbiter::Base &other) const override
virtual std::unique_ptr< Base > clone() const =0
bool operator==(const Arbiter::Base &other) const override
size_t hash() const
Definition: Value.h:96