diff options
author | dec05eba <dec05eba@protonmail.com> | 2020-10-19 19:36:10 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-10-19 19:36:10 +0200 |
commit | 41fe990530c546b4cac7e000b40481f87fb33305 (patch) | |
tree | a93fa803b04e5e7efeeacb6d302f68a240188321 /src/plugins | |
parent | 878beb16ad10d1e1e44d51f3841d44a6836bb4e9 (diff) |
shared_ptr<RoomData> -> unique_ptr<RoomData> and naked pointers
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/Matrix.cpp | 72 |
1 files changed, 39 insertions, 33 deletions
diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index bfdd496..35f5302 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -139,19 +139,23 @@ namespace QuickMedia { void Matrix::get_room_join_updates(Rooms &new_rooms) { std::lock_guard<std::mutex> lock(room_data_mutex); - new_rooms.insert(new_rooms.end(), rooms.begin() + room_list_read_index, rooms.end()); size_t num_new_rooms = rooms.size() - room_list_read_index; + size_t new_rooms_prev_size = new_rooms.size(); + new_rooms.resize(new_rooms_prev_size + num_new_rooms); + for(size_t i = new_rooms_prev_size; i < new_rooms.size(); ++i) { + new_rooms[i] = rooms[room_list_read_index + i].get(); + } room_list_read_index += num_new_rooms; } - PluginResult Matrix::get_all_synced_room_messages(std::shared_ptr<RoomData> room, Messages &messages) { + PluginResult Matrix::get_all_synced_room_messages(RoomData *room, Messages &messages) { room->acquire_room_lock(); messages = room->get_messages_thread_unsafe(); room->release_room_lock(); return PluginResult::OK; } - PluginResult Matrix::get_previous_room_messages(std::shared_ptr<RoomData> room, Messages &messages) { + PluginResult Matrix::get_previous_room_messages(RoomData *room, Messages &messages) { room->acquire_room_lock(); size_t num_messages_before = room->get_messages_thread_unsafe().size(); room->release_room_lock(); @@ -189,18 +193,19 @@ namespace QuickMedia { std::string room_id_str = room_id.GetString(); - auto room = get_room_by_id(room_id_str); + RoomData *room = get_room_by_id(room_id_str); if(!room) { - room = std::make_shared<RoomData>(); - room->id = room_id_str; - add_room(room); + auto new_room = std::make_unique<RoomData>(); + new_room->id = room_id_str; + room = new_room.get(); + add_room(std::move(new_room)); } const rapidjson::Value &state_json = GetMember(it.value, "state"); if(state_json.IsObject()) { const rapidjson::Value &events_json = GetMember(state_json, "events"); - events_add_user_info(events_json, room.get()); - events_set_room_name(events_json, room.get()); + events_add_user_info(events_json, room); + events_set_room_name(events_json, room); } const rapidjson::Value &timeline_json = GetMember(it.value, "timeline"); @@ -222,15 +227,15 @@ namespace QuickMedia { } const rapidjson::Value &events_json = GetMember(timeline_json, "events"); - events_add_user_info(events_json, room.get()); + events_add_user_info(events_json, room); events_add_messages(events_json, room, MessageDirection::AFTER, &room_messages, has_unread_notifications); - events_set_room_name(events_json, room.get()); + events_set_room_name(events_json, room); } const rapidjson::Value &ephemeral_json = GetMember(it.value, "ephemeral"); if(ephemeral_json.IsObject()) { const rapidjson::Value &events_json = GetMember(ephemeral_json, "events"); - events_add_user_read_markers(events_json, room.get()); + events_add_user_read_markers(events_json, room); } } @@ -443,7 +448,7 @@ namespace QuickMedia { return false; } - void Matrix::events_add_messages(const rapidjson::Value &events_json, std::shared_ptr<RoomData> &room_data, MessageDirection message_dir, RoomSyncMessages *room_messages, bool has_unread_notifications) { + void Matrix::events_add_messages(const rapidjson::Value &events_json, RoomData *room_data, MessageDirection message_dir, RoomSyncMessages *room_messages, bool has_unread_notifications) { if(!events_json.IsArray()) return; @@ -451,7 +456,7 @@ namespace QuickMedia { auto me = get_me(room_data); for(const rapidjson::Value &event_item_json : events_json.GetArray()) { - std::shared_ptr<Message> new_message = parse_message_event(event_item_json, room_data.get()); + std::shared_ptr<Message> new_message = parse_message_event(event_item_json, room_data); if(!new_message) continue; @@ -731,7 +736,7 @@ namespace QuickMedia { } } - PluginResult Matrix::get_previous_room_messages(std::shared_ptr<RoomData> &room_data) { + PluginResult Matrix::get_previous_room_messages(RoomData *room_data) { std::string from = room_data->prev_batch; if(from.empty()) { fprintf(stderr, "Info: missing previous batch for room: %s, using /sync next batch\n", room_data->id.c_str()); @@ -766,8 +771,8 @@ namespace QuickMedia { return PluginResult::ERR; const rapidjson::Value &state_json = GetMember(json_root, "state"); - events_add_user_info(state_json, room_data.get()); - events_set_room_name(state_json, room_data.get()); + events_add_user_info(state_json, room_data); + events_set_room_name(state_json, room_data); const rapidjson::Value &chunk_json = GetMember(json_root, "chunk"); events_add_messages(chunk_json, room_data, MessageDirection::BEFORE, nullptr, false); @@ -818,7 +823,7 @@ namespace QuickMedia { return "m.file"; } - PluginResult Matrix::post_message(std::shared_ptr<RoomData> room, const std::string &body, const std::optional<UploadInfo> &file_info, const std::optional<UploadInfo> &thumbnail_info) { + PluginResult Matrix::post_message(RoomData *room, const std::string &body, const std::optional<UploadInfo> &file_info, const std::optional<UploadInfo> &thumbnail_info) { char random_characters[18]; if(!generate_random_characters(random_characters, sizeof(random_characters))) return PluginResult::ERR; @@ -990,11 +995,11 @@ namespace QuickMedia { } // TODO: Support greentext - PluginResult Matrix::post_reply(std::shared_ptr<RoomData> room, const std::string &body, void *relates_to) { + PluginResult Matrix::post_reply(RoomData *room, const std::string &body, void *relates_to) { // TODO: Store shared_ptr<Message> instead of raw pointer... Message *relates_to_message_raw = (Message*)relates_to; std::shared_ptr<Message> relates_to_message_shared = room->get_message_by_id(relates_to_message_raw->event_id); - std::shared_ptr<Message> relates_to_message_original = get_edited_message_original_message(room.get(), relates_to_message_shared); + std::shared_ptr<Message> relates_to_message_original = get_edited_message_original_message(room, relates_to_message_shared); if(!relates_to_message_original) { fprintf(stderr, "Failed to get the original message for message with event id: %s\n", relates_to_message_raw->event_id.c_str()); return PluginResult::ERR; @@ -1051,10 +1056,10 @@ namespace QuickMedia { return PluginResult::OK; } - PluginResult Matrix::post_edit(std::shared_ptr<RoomData> room, const std::string &body, void *relates_to) { + PluginResult Matrix::post_edit(RoomData *room, const std::string &body, void *relates_to) { Message *relates_to_message_raw = (Message*)relates_to; std::shared_ptr<Message> relates_to_message_shared = room->get_message_by_id(relates_to_message_raw->event_id); - std::shared_ptr<Message> relates_to_message_original = get_edited_message_original_message(room.get(), relates_to_message_shared); + std::shared_ptr<Message> relates_to_message_original = get_edited_message_original_message(room, relates_to_message_shared); if(!relates_to_message_original) { fprintf(stderr, "Failed to get the original message for message with event id: %s\n", relates_to_message_raw->event_id.c_str()); return PluginResult::ERR; @@ -1205,7 +1210,7 @@ namespace QuickMedia { return filename; } - PluginResult Matrix::post_file(std::shared_ptr<RoomData> room, const std::string &filepath, std::string &err_msg) { + PluginResult Matrix::post_file(RoomData *room, const std::string &filepath, std::string &err_msg) { UploadInfo file_info; UploadInfo thumbnail_info; PluginResult upload_file_result = upload_file(room, filepath, file_info, thumbnail_info, err_msg); @@ -1221,7 +1226,7 @@ namespace QuickMedia { return post_message(room, filename, file_info_opt, thumbnail_info_opt); } - PluginResult Matrix::upload_file(std::shared_ptr<RoomData> room, const std::string &filepath, UploadInfo &file_info, UploadInfo &thumbnail_info, std::string &err_msg) { + PluginResult Matrix::upload_file(RoomData *room, const std::string &filepath, UploadInfo &file_info, UploadInfo &thumbnail_info, std::string &err_msg) { FileAnalyzer file_analyzer; if(!file_analyzer.load_file(filepath.c_str())) { err_msg = "Failed to load " + filepath; @@ -1400,6 +1405,7 @@ namespace QuickMedia { // Make sure all fields are reset here! rooms.clear(); + room_list_read_index = 0; room_data_by_id.clear(); user_id.clear(); username.clear(); @@ -1411,7 +1417,7 @@ namespace QuickMedia { return PluginResult::OK; } - PluginResult Matrix::delete_message(std::shared_ptr<RoomData> room, void *message, std::string &err_msg){ + PluginResult Matrix::delete_message(RoomData *room, void *message, std::string &err_msg){ char random_characters[18]; if(!generate_random_characters(random_characters, sizeof(random_characters))) return PluginResult::ERR; @@ -1517,7 +1523,7 @@ namespace QuickMedia { return PluginResult::OK; } - PluginResult Matrix::on_start_typing(std::shared_ptr<RoomData> room) { + PluginResult Matrix::on_start_typing(RoomData *room) { rapidjson::Document request_data(rapidjson::kObjectType); request_data.AddMember("typing", true, request_data.GetAllocator()); request_data.AddMember("timeout", 30000, request_data.GetAllocator()); // 30 sec timeout @@ -1540,7 +1546,7 @@ namespace QuickMedia { return PluginResult::OK; } - PluginResult Matrix::on_stop_typing(std::shared_ptr<RoomData> room) { + PluginResult Matrix::on_stop_typing(RoomData *room) { rapidjson::Document request_data(rapidjson::kObjectType); request_data.AddMember("typing", false, request_data.GetAllocator()); @@ -1562,7 +1568,7 @@ namespace QuickMedia { return PluginResult::OK; } - PluginResult Matrix::set_read_marker(std::shared_ptr<RoomData> room, const Message *message) { + PluginResult Matrix::set_read_marker(RoomData *room, const Message *message) { rapidjson::Document request_data(rapidjson::kObjectType); request_data.AddMember("m.fully_read", rapidjson::StringRef(message->event_id.c_str()), request_data.GetAllocator()); request_data.AddMember("m.read", rapidjson::StringRef(message->event_id.c_str()), request_data.GetAllocator()); @@ -1628,22 +1634,22 @@ namespace QuickMedia { return PluginResult::OK; } - std::shared_ptr<UserInfo> Matrix::get_me(std::shared_ptr<RoomData> room) { + std::shared_ptr<UserInfo> Matrix::get_me(RoomData *room) { return room->get_user_by_id(user_id); } - std::shared_ptr<RoomData> Matrix::get_room_by_id(const std::string &id) { + RoomData* Matrix::get_room_by_id(const std::string &id) { std::lock_guard<std::mutex> lock(room_data_mutex); auto room_it = room_data_by_id.find(id); if(room_it == room_data_by_id.end()) return nullptr; - return rooms[room_it->second]; + return rooms[room_it->second].get(); } - void Matrix::add_room(std::shared_ptr<RoomData> room) { + void Matrix::add_room(std::unique_ptr<RoomData> room) { std::lock_guard<std::mutex> lock(room_data_mutex); room_data_by_id.insert(std::make_pair(room->id, rooms.size())); - rooms.push_back(room); + rooms.push_back(std::move(room)); } DownloadResult Matrix::download_json(rapidjson::Document &result, const std::string &url, std::vector<CommandArg> additional_args, bool use_browser_useragent, std::string *err_msg) const { |