xref: /aosp_15_r20/external/cronet/net/http/http_version.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2006-2008 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_HTTP_HTTP_VERSION_H_
6 #define NET_HTTP_HTTP_VERSION_H_
7 
8 #include <stdint.h>
9 
10 namespace net {
11 
12 // Wrapper for an HTTP (major,minor) version pair.
13 // This type is final as the type is copy-constructable and assignable and so
14 // there is a risk of slicing if it was subclassed.
15 class HttpVersion final {
16  public:
17   // Default constructor (major=0, minor=0).
HttpVersion()18   constexpr HttpVersion() : value_(0) {}
19 
20   // Build from unsigned major/minor pair.
HttpVersion(uint16_t major,uint16_t minor)21   constexpr HttpVersion(uint16_t major, uint16_t minor)
22       : value_(static_cast<uint32_t>(major << 16) | minor) {}
23 
24   constexpr HttpVersion(const HttpVersion& rhs) = default;
25   constexpr HttpVersion& operator=(const HttpVersion& rhs) = default;
26 
27   // Major version number.
major_value()28   constexpr uint16_t major_value() const { return value_ >> 16; }
29 
30   // Minor version number.
minor_value()31   constexpr uint16_t minor_value() const { return value_ & 0xffff; }
32 
33   // Overloaded operators:
34 
35   constexpr bool operator==(const HttpVersion& v) const {
36     return value_ == v.value_;
37   }
38   constexpr bool operator!=(const HttpVersion& v) const {
39     return value_ != v.value_;
40   }
41   constexpr bool operator>(const HttpVersion& v) const {
42     return value_ > v.value_;
43   }
44   constexpr bool operator>=(const HttpVersion& v) const {
45     return value_ >= v.value_;
46   }
47   constexpr bool operator<(const HttpVersion& v) const {
48     return value_ < v.value_;
49   }
50   constexpr bool operator<=(const HttpVersion& v) const {
51     return value_ <= v.value_;
52   }
53 
54  private:
55   uint32_t value_;  // Packed as <major>:<minor>
56 };
57 
58 }  // namespace net
59 
60 #endif  // NET_HTTP_HTTP_VERSION_H_
61