2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
2018-04-24 18:21:47 +02:00
|
|
|
|
2019-02-12 21:48:16 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
|
2018-06-25 23:10:53 +02:00
|
|
|
#include <tuple>
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
2018-04-24 18:21:47 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class DcId {
|
|
|
|
public:
|
|
|
|
DcId() = default;
|
|
|
|
|
2023-10-23 14:40:57 +02:00
|
|
|
static constexpr int32 MAX_RAW_DC_ID = 1000;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
static bool is_valid(int32 dc_id) {
|
2023-10-23 14:40:57 +02:00
|
|
|
return 1 <= dc_id && dc_id <= MAX_RAW_DC_ID;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
static DcId main() {
|
|
|
|
return DcId{MainDc, false};
|
|
|
|
}
|
|
|
|
static DcId invalid() {
|
|
|
|
return DcId{Invalid, false};
|
|
|
|
}
|
|
|
|
static DcId internal(int32 id) {
|
|
|
|
CHECK(is_valid(id));
|
|
|
|
return DcId{id, false};
|
|
|
|
}
|
|
|
|
static DcId external(int32 id) {
|
|
|
|
CHECK(is_valid(id));
|
|
|
|
return DcId{id, true};
|
|
|
|
}
|
|
|
|
static DcId empty() {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
static DcId from_value(int32 value) {
|
|
|
|
return DcId{value, false};
|
|
|
|
}
|
2019-06-08 11:26:35 +02:00
|
|
|
static DcId create(int32 dc_id_value) {
|
|
|
|
if (DcId::is_valid(dc_id_value)) {
|
2019-06-10 19:00:18 +02:00
|
|
|
return DcId(dc_id_value, false);
|
2019-06-08 11:26:35 +02:00
|
|
|
} else {
|
|
|
|
return DcId::invalid();
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
bool is_empty() const {
|
|
|
|
return !is_valid();
|
|
|
|
}
|
|
|
|
bool is_main() const {
|
|
|
|
return dc_id_ == MainDc;
|
|
|
|
}
|
|
|
|
int32 get_raw_id() const {
|
|
|
|
CHECK(is_exact());
|
|
|
|
return dc_id_;
|
|
|
|
}
|
|
|
|
int32 get_value() const {
|
|
|
|
return dc_id_;
|
|
|
|
}
|
|
|
|
bool is_internal() const {
|
|
|
|
return !is_external();
|
|
|
|
}
|
|
|
|
bool is_external() const {
|
|
|
|
return is_external_;
|
|
|
|
}
|
|
|
|
bool is_exact() const {
|
|
|
|
return dc_id_ > 0;
|
|
|
|
}
|
|
|
|
bool operator==(DcId other) const {
|
|
|
|
return dc_id_ == other.dc_id_ && is_external_ == other.is_external_;
|
|
|
|
}
|
|
|
|
bool operator<(DcId other) const {
|
2018-06-25 23:10:53 +02:00
|
|
|
return std::tie(dc_id_, is_external_) < std::tie(other.dc_id_, other.is_external_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
bool operator!=(DcId other) const {
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-04-19 15:08:30 +02:00
|
|
|
enum : int32 { Empty = 0, MainDc = -1, Invalid = -2 };
|
2018-12-31 20:04:05 +01:00
|
|
|
int32 dc_id_{Empty};
|
|
|
|
bool is_external_{false};
|
|
|
|
|
|
|
|
DcId(int32 dc_id, bool is_external) : dc_id_(dc_id), is_external_(is_external) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_valid() const {
|
|
|
|
return is_exact() || is_main();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline StringBuilder &operator<<(StringBuilder &sb, const DcId &dc_id) {
|
|
|
|
sb << "DcId{";
|
2019-05-09 04:01:45 +02:00
|
|
|
if (dc_id == DcId::invalid()) {
|
|
|
|
sb << "invalid";
|
|
|
|
} else if (dc_id == DcId()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
sb << "empty";
|
2019-05-09 04:01:45 +02:00
|
|
|
} else if (dc_id.is_empty()) {
|
|
|
|
sb << "is_empty";
|
2018-12-31 20:04:05 +01:00
|
|
|
} else if (dc_id.is_main()) {
|
|
|
|
sb << "main";
|
|
|
|
} else {
|
|
|
|
sb << dc_id.get_raw_id();
|
|
|
|
if (dc_id.is_external()) {
|
|
|
|
sb << " external";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sb << "}";
|
|
|
|
return sb;
|
|
|
|
}
|
|
|
|
|
2018-07-18 03:30:29 +02:00
|
|
|
} // namespace td
|