1 /*
2   Copyright (c) DataStax, Inc.
3 
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7 
8   http://www.apache.org/licenses/LICENSE-2.0
9 
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15 */
16 
17 #ifndef DATASTAX_INTERNAL_PROTOCOL_HPP
18 #define DATASTAX_INTERNAL_PROTOCOL_HPP
19 
20 #include "cassandra.h"
21 #include "constants.hpp"
22 #include "string.hpp"
23 
24 namespace datastax { namespace internal { namespace core {
25 
26 /**
27  * A type that represents the protocol version for Cassandra/DSE.
28  */
29 class ProtocolVersion {
30 public:
31   /**
32    * Constructs an invalid (uninitialized) protocol version.
33    */
34   ProtocolVersion();
35 
36   /**
37    * Constructs a protocol version from a value. Use `is_valid()` to check if
38    * the value is valid.
39    *
40    * @see is_valid()
41    *
42    * @param value The value to use for the protocol version.
43    */
44   ProtocolVersion(int value);
45 
46 public:
47   /**
48    * Returns the lowest supported protocol version.
49    *
50    * @return The lowest protocol version.
51    */
52   static ProtocolVersion lowest_supported();
53 
54   /**
55    * Returns the highest supported protocol version.
56    *
57    * @param is_dse If true the highest DSE protocol version is returned; otherwise the highest
58    * Apache Cassandra version is returned.
59    * @return The highest protocol version.
60    */
61   static ProtocolVersion highest_supported(bool is_dse = true);
62 
63   /**
64    * Returns the newest supported beta protocol version.
65    *
66    * @return The newest beta protocol version.
67    */
68   static ProtocolVersion newest_beta();
69 
70 public:
71   /**
72    * Get the raw value for the protocol version.
73    *
74    * @return The protocol version value.
75    */
76   int value() const;
77 
78   /**
79    * Check to see if the protocol version's value is valid. Beta versions
80    * are valid but will return false. Use `is_beta()` for beta versions.
81    *
82    * @see is_beta()
83    *
84    * @return true if valid, otherwise false.
85    */
86   bool is_valid() const;
87 
88   /**
89    * Check to see if the protocol version's value is DSE.
90    *
91    * @return true if DSE, otherwise false;
92    */
93   bool is_dse() const;
94 
95   /**
96    * Check to see if the protocol version is a beta version.
97    *
98    * @return true if a beta version, otherwise false.
99    */
100   bool is_beta() const;
101 
102   /**
103    * Returns the string representation for the protocol version.
104    *
105    * @return A protocol version string.
106    */
107   String to_string() const;
108 
109   /**
110    * Attempt to lower the protocol version.
111    *
112    * @return The previous protocol version if valid; otherwise an invalid protocol object (Use:
113    * is_valid())
114    */
115   ProtocolVersion previous() const;
116 
117 public:
118   /**
119    * Check to see if the set keyspace operation is supported by the current
120    * protocol version.
121    *
122    * @return true if supported, otherwise false.
123    */
124   bool supports_set_keyspace() const;
125 
126   /**
127    * Check to see if result metadata IDs are supported by the current protocol
128    * version.
129    *
130    * @return true if supported, otherwise false.
131    */
132   bool supports_result_metadata_id() const;
133 
134 public:
operator <(ProtocolVersion version) const135   bool operator<(ProtocolVersion version) const { return value_ < version.value_; }
operator >(ProtocolVersion version) const136   bool operator>(ProtocolVersion version) const { return value_ > version.value_; }
operator <=(ProtocolVersion version) const137   bool operator<=(ProtocolVersion version) const { return value_ <= version.value_; }
operator >=(ProtocolVersion version) const138   bool operator>=(ProtocolVersion version) const { return value_ >= version.value_; }
operator ==(ProtocolVersion version) const139   bool operator==(ProtocolVersion version) const { return value_ == version.value_; }
operator !=(ProtocolVersion version) const140   bool operator!=(ProtocolVersion version) const { return value_ != version.value_; }
141 
142 private:
143   int value_;
144 };
145 
146 }}} // namespace datastax::internal::core
147 
148 #endif
149