mirror of
https://github.com/scylladb/scylladb.git
synced 2026-05-01 13:45:53 +00:00
in C++20, compiler generate operator!=() if the corresponding operator==() is already defined, the language now understands that the comparison is symmetric in the new standard. fortunately, our operator!=() is always equivalent to `! operator==()`, this matches the behavior of the default generated operator!=(). so, in this change, all `operator!=` are removed. in addition to the defaulted operator!=, C++20 also brings to us the defaulted operator==() -- it is able to generated the operator==() if the member-wise lexicographical comparison. under some circumstances, this is exactly what we need. so, in this change, if the operator==() is also implemented as a lexicographical comparison of all memeber variables of the class/struct in question, it is implemented using the default generated one by removing its body and mark the function as `default`. moreover, if the class happen to have other comparison operators which are implemented using lexicographical comparison, the default generated `operator<=>` is used in place of the defaulted `operator==`. sometimes, we fail to mark the operator== with the `const` specifier, in this change, to fulfil the need of C++ standard, and to be more correct, the `const` specifier is added. also, to generate the defaulted operator==, the operand should be `const class_name&`, but it is not always the case, in the class of `version`, we use `version` as the parameter type, to fulfill the need of the C++ standard, the parameter type is changed to `const version&` instead. this does not change the semantic of the comparison operator. and is a more idiomatic way to pass non-trivial struct as function parameters. please note, because in C++20, both operator= and operator<=> are symmetric, some of the operators in `multiprecision` are removed. they are the symmetric form of the another variant. if they were not removed, compiler would, for instance, find ambiguous overloaded operator '=='. this change is a cleanup to modernize the code base with C++20 features. Signed-off-by: Kefu Chai <kefu.chai@scylladb.com> Closes #13687
112 lines
3.0 KiB
C++
112 lines
3.0 KiB
C++
/*
|
|
* Copyright (C) 2015-present ScyllaDB
|
|
*/
|
|
|
|
/*
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
#include "cql3/column_identifier.hh"
|
|
#include "exceptions/exceptions.hh"
|
|
#include "cql3/util.hh"
|
|
#include "cql3/query_options.hh"
|
|
|
|
#include <regex>
|
|
|
|
namespace cql3 {
|
|
|
|
column_identifier::column_identifier(sstring raw_text, bool keep_case) {
|
|
_text = std::move(raw_text);
|
|
if (!keep_case) {
|
|
std::transform(_text.begin(), _text.end(), _text.begin(), ::tolower);
|
|
}
|
|
bytes_ = to_bytes(_text);
|
|
}
|
|
|
|
column_identifier::column_identifier(bytes bytes_, data_type type)
|
|
: bytes_(std::move(bytes_))
|
|
, _text(type->get_string(this->bytes_))
|
|
{ }
|
|
|
|
column_identifier::column_identifier(bytes bytes_, sstring text)
|
|
: bytes_(std::move(bytes_))
|
|
, _text(std::move(text))
|
|
{ }
|
|
|
|
bool column_identifier::operator==(const column_identifier& other) const {
|
|
return bytes_ == other.bytes_;
|
|
}
|
|
|
|
const sstring& column_identifier::text() const {
|
|
return _text;
|
|
}
|
|
|
|
const bytes& column_identifier::name() const {
|
|
return bytes_;
|
|
}
|
|
|
|
sstring column_identifier::to_string() const {
|
|
return _text;
|
|
}
|
|
|
|
sstring column_identifier::to_cql_string() const {
|
|
return util::maybe_quote(_text);
|
|
}
|
|
|
|
sstring column_identifier_raw::to_cql_string() const {
|
|
return util::maybe_quote(_text);
|
|
}
|
|
|
|
column_identifier_raw::column_identifier_raw(sstring raw_text, bool keep_case)
|
|
: _raw_text{raw_text}
|
|
, _text{raw_text}
|
|
{
|
|
if (!keep_case) {
|
|
std::transform(_text.begin(), _text.end(), _text.begin(), ::tolower);
|
|
}
|
|
}
|
|
|
|
::shared_ptr<column_identifier> column_identifier_raw::prepare(const schema& s) const {
|
|
return prepare_column_identifier(s);
|
|
}
|
|
|
|
::shared_ptr<column_identifier>
|
|
column_identifier_raw::prepare_column_identifier(const schema& schema) const {
|
|
if (schema.regular_column_name_type() == utf8_type) {
|
|
return ::make_shared<column_identifier>(_text, true);
|
|
}
|
|
|
|
// We have a Thrift-created table with a non-text comparator. We need to parse column names with the comparator
|
|
// to get the correct ByteBuffer representation. However, this doesn't apply to key aliases, so we need to
|
|
// make a special check for those and treat them normally. See CASSANDRA-8178.
|
|
auto text_bytes = to_bytes(_text);
|
|
auto def = schema.get_column_definition(text_bytes);
|
|
if (def) {
|
|
return ::make_shared<column_identifier>(std::move(text_bytes), _text);
|
|
}
|
|
|
|
return ::make_shared<column_identifier>(schema.regular_column_name_type()->from_string(_raw_text), _text);
|
|
}
|
|
|
|
bool column_identifier_raw::processes_selection() const {
|
|
return false;
|
|
}
|
|
|
|
bool column_identifier_raw::operator==(const column_identifier_raw& other) const {
|
|
return _text == other._text;
|
|
}
|
|
|
|
sstring column_identifier_raw::to_string() const {
|
|
return _text;
|
|
}
|
|
|
|
std::ostream& operator<<(std::ostream& out, const column_identifier_raw& id) {
|
|
return out << id._text;
|
|
}
|
|
|
|
}
|
|
|
|
bool cql3::column_identifier::text_comparator::operator()(const cql3::column_identifier& c1, const cql3::column_identifier& c2) const {
|
|
return c1.text() < c2.text();
|
|
}
|