Files
scylladb/cql3/column_identifier.hh
Kefu Chai f5b05cf981 treewide: use defaulted operator!=() and operator==()
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
2023-04-27 10:24:46 +03:00

127 lines
3.1 KiB
C++

/*
* Copyright (C) 2015-present ScyllaDB
*
* Modified by ScyllaDB
*/
/*
* SPDX-License-Identifier: (AGPL-3.0-or-later and Apache-2.0)
*/
#pragma once
#include "schema/schema.hh"
#include <algorithm>
#include <functional>
#include <iosfwd>
namespace cql3 {
class column_identifier_raw;
/**
* Represents an identifer for a CQL column definition.
* TODO : should support light-weight mode without text representation for when not interned
*/
class column_identifier final {
public:
bytes bytes_;
private:
sstring _text;
public:
// less comparator sorting by text
struct text_comparator {
bool operator()(const column_identifier& c1, const column_identifier& c2) const;
};
column_identifier(sstring raw_text, bool keep_case);
column_identifier(bytes bytes_, data_type type);
column_identifier(bytes bytes_, sstring text);
bool operator==(const column_identifier& other) const;
const sstring& text() const;
const bytes& name() const;
sstring to_string() const;
sstring to_cql_string() const;
friend std::ostream& operator<<(std::ostream& out, const column_identifier& i) {
return out << i._text;
}
#if 0
public ColumnIdentifier clone(AbstractAllocator allocator)
{
return new ColumnIdentifier(allocator.clone(bytes), text);
}
#endif
using raw = column_identifier_raw;
};
/**
* Because Thrift-created tables may have a non-text comparator, we cannot determine the proper 'key' until
* we know the comparator. ColumnIdentifier.Raw is a placeholder that can be converted to a real ColumnIdentifier
* once the comparator is known with prepare(). This should only be used with identifiers that are actual
* column names. See CASSANDRA-8178 for more background.
*/
class column_identifier_raw final {
private:
const sstring _raw_text;
sstring _text;
public:
column_identifier_raw(sstring raw_text, bool keep_case);
// for selectable::with_expression::raw:
::shared_ptr<column_identifier> prepare(const schema& s) const;
::shared_ptr<column_identifier> prepare_column_identifier(const schema& s) const;
// for selectable::with_expression::raw:
bool processes_selection() const;
bool operator==(const column_identifier_raw& other) const;
virtual sstring to_string() const;
sstring to_cql_string() const;
friend std::hash<column_identifier_raw>;
friend std::ostream& operator<<(std::ostream& out, const column_identifier_raw& id);
};
static inline
const column_definition* get_column_definition(const schema& schema, const column_identifier& id) {
return schema.get_column_definition(id.bytes_);
}
static inline
::shared_ptr<column_identifier> to_identifier(const column_definition& def) {
return def.column_specification->name;
}
}
namespace std {
template<>
struct hash<cql3::column_identifier> {
size_t operator()(const cql3::column_identifier& i) const {
return std::hash<bytes>()(i.bytes_);
}
};
template<>
struct hash<cql3::column_identifier_raw> {
size_t operator()(const cql3::column_identifier::raw& r) const {
return std::hash<sstring>()(r._text);
}
};
}