Const-correct unique_ptr.

GitOrigin-RevId: 938cf4b0261cfea123066ae1fc4197754da11e68
This commit is contained in:
levlam 2018-09-27 19:51:45 +03:00
parent c3417b5f94
commit 6ac930139b
9 changed files with 35 additions and 22 deletions

View File

@ -25,7 +25,7 @@ class ConfigShared {
Callback(const Callback &) = delete;
Callback &operator=(const Callback &) = delete;
virtual ~Callback() = default;
virtual void on_option_updated(const string &name, const string &value) = 0;
virtual void on_option_updated(const string &name, const string &value) const = 0;
};
ConfigShared(BinlogPmcPtr config_pmc, unique_ptr<Callback> callback);

View File

@ -91,7 +91,7 @@ const FormattedText &Game::get_message_text() const {
return text_;
}
tl_object_ptr<td_api::game> Game::get_game_object(const Td *td) const {
tl_object_ptr<td_api::game> Game::get_game_object(Td *td) const {
return make_tl_object<td_api::game>(
id_, short_name_, title_, get_formatted_text_object(text_), description_,
get_photo_object(td->file_manager_.get(), &photo_),

View File

@ -59,7 +59,7 @@ class Game {
const FormattedText &get_message_text() const;
tl_object_ptr<td_api::game> get_game_object(const Td *td) const;
tl_object_ptr<td_api::game> get_game_object(Td *td) const;
tl_object_ptr<telegram_api::inputMediaGame> get_input_media_game(const Td *td) const;

View File

@ -12755,7 +12755,7 @@ Status MessagesManager::delete_dialog_reply_markup(DialogId dialog_id, MessageId
return Status::OK();
}
const Message *message = get_message_force(d, message_id);
Message *message = get_message_force(d, message_id);
CHECK(message != nullptr);
CHECK(message->reply_markup != nullptr);
@ -14140,7 +14140,7 @@ tl_object_ptr<td_api::messages> MessagesManager::get_dialog_history(DialogId dia
if (*p == nullptr) {
// there is no gap if from_message_id is less than first message in the dialog
if (left_tries == 0 && d->messages != nullptr && offset < 0) {
Message *cur = d->messages.get();
const Message *cur = d->messages.get();
while (cur->left != nullptr) {
cur = cur->left.get();
}

View File

@ -1585,7 +1585,7 @@ class MessagesManager : public Actor {
unique_ptr<Message> left;
unique_ptr<Message> right;
int32 last_access_date = 0;
mutable int32 last_access_date = 0;
uint64 send_message_logevent_id = 0;

View File

@ -4053,7 +4053,7 @@ Status Td::init(DbKey key) {
VLOG(td_init) << "Create ConfigManager and ConfigShared";
class ConfigSharedCallback : public ConfigShared::Callback {
public:
void on_option_updated(const string &name, const string &value) override {
void on_option_updated(const string &name, const string &value) const override {
send_closure(G()->td(), &Td::on_config_option_updated, name);
}
};

View File

@ -88,17 +88,18 @@ class FileDownloadGenerateActor : public FileGenerateActor {
}
void on_download_ok() {
send_lambda(G()->file_manager(), [file_type = file_type_, file_id = file_id_, callback = std::move(callback_)] {
auto file_view = G()->td().get_actor_unsafe()->file_manager_->get_file_view(file_id);
if (file_view.has_local_location()) {
auto location = file_view.local_location();
location.file_type_ = file_type;
callback->on_ok(location);
} else {
LOG(ERROR) << "Expected to have local location";
callback->on_error(Status::Error(500, "Unknown"));
}
});
send_lambda(G()->file_manager(),
[file_type = file_type_, file_id = file_id_, callback = std::move(callback_)]() mutable {
auto file_view = G()->td().get_actor_unsafe()->file_manager_->get_file_view(file_id);
if (file_view.has_local_location()) {
auto location = file_view.local_location();
location.file_type_ = file_type;
callback->on_ok(location);
} else {
LOG(ERROR) << "Expected to have local location";
callback->on_error(Status::Error(500, "Unknown"));
}
});
stop();
}
void on_download_error(Status error) {

View File

@ -175,6 +175,7 @@ template <class T>
bool operator!=(const unique_ptr<T> &p, std::nullptr_t) {
return static_cast<bool>(p);
}
} // namespace tl
template <class Type>
using tl_object_ptr = tl::unique_ptr<Type>;

View File

@ -12,8 +12,10 @@
namespace td {
// const-correct and compiler-friendly (g++ RAM and CPU usage 10 times less than for std::unique_ptr)
// replacement for std::unique_ptr
template <class T>
class unique_ptr {
class unique_ptr final {
public:
using pointer = T *;
using element_type = T;
@ -52,13 +54,22 @@ class unique_ptr {
ptr_ = nullptr;
return res;
}
T *get() const noexcept {
T *get() noexcept {
return ptr_;
}
T *operator->() const noexcept {
const T *get() const noexcept {
return ptr_;
}
T &operator*() const noexcept {
T *operator->() noexcept {
return ptr_;
}
const T *operator->() const noexcept {
return ptr_;
}
T &operator*() noexcept {
return *ptr_;
}
const T &operator*() const noexcept {
return *ptr_;
}
explicit operator bool() const noexcept {