From 640f548e7c75ae8fcedab7d67e9cc004cf9dbf4b Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sat, 10 Mar 2018 15:40:27 -0800 Subject: [PATCH] Use std::index_sequence and std::make_unique --- src/clang_complete.cc | 4 ++-- src/clang_translation_unit.cc | 2 +- src/command_line.cc | 11 ++++----- src/diagnostics_engine.cc | 2 +- src/file_consumer.cc | 2 +- src/iindexer.cc | 8 +++---- src/import_pipeline.cc | 9 ++++---- src/include_complete.cc | 2 +- src/lsp.h | 2 +- src/messages/initialize.cc | 2 ++ src/queue_manager.cc | 18 +++++---------- src/queue_manager.h | 17 +++++++------- src/semantic_highlight_symbol_cache.cc | 2 +- src/serializer.cc | 4 ++-- src/task.cc | 4 ++-- src/threaded_queue.h | 32 ++++++-------------------- src/utils.h | 8 ------- src/working_files.cc | 2 +- 18 files changed, 50 insertions(+), 81 deletions(-) diff --git a/src/clang_complete.cc b/src/clang_complete.cc index fbb3b136..0eec9a71 100644 --- a/src/clang_complete.cc +++ b/src/clang_complete.cc @@ -689,7 +689,7 @@ void ClangCompleteManager::CodeComplete( const lsRequestId& id, const lsTextDocumentPositionParams& completion_location, const OnComplete& on_complete) { - completion_request_.PushBack(MakeUnique( + completion_request_.PushBack(std::make_unique( id, completion_location.textDocument, completion_location.position, on_complete, false)); } @@ -698,7 +698,7 @@ void ClangCompleteManager::DiagnosticsUpdate( const lsRequestId& id, const lsTextDocumentIdentifier& document) { completion_request_.PushBack( - MakeUnique(id, document, true)); + std::make_unique(id, document, true)); } void ClangCompleteManager::NotifyView(const std::string& filename) { diff --git a/src/clang_translation_unit.cc b/src/clang_translation_unit.cc index 350df886..52639c98 100644 --- a/src/clang_translation_unit.cc +++ b/src/clang_translation_unit.cc @@ -98,7 +98,7 @@ std::unique_ptr ClangTranslationUnit::Create( switch (error_code) { case CXError_Success: - return MakeUnique(cx_tu); + return std::make_unique(cx_tu); case CXError_Failure: LOG_S(ERROR) << "libclang generic failure for " << filepath << ". " << make_msg(); diff --git a/src/command_line.cc b/src/command_line.cc index 5e58227a..717ba3af 100644 --- a/src/command_line.cc +++ b/src/command_line.cc @@ -203,9 +203,9 @@ void RunQueryDbThread(const std::string& bin_name, }); IncludeComplete include_complete(config, &project); - auto global_code_complete_cache = MakeUnique(); - auto non_global_code_complete_cache = MakeUnique(); - auto signature_cache = MakeUnique(); + auto global_code_complete_cache = std::make_unique(); + auto non_global_code_complete_cache = std::make_unique(); + auto signature_cache = std::make_unique(); ImportManager import_manager; ImportPipelineStatus import_pipeline_status; TimestampManager timestamp_manager; @@ -423,8 +423,7 @@ int main(int argc, char** argv) { loguru::init(argc, argv); MultiQueueWaiter querydb_waiter, indexer_waiter, stdout_waiter; - QueueManager::CreateInstance(&querydb_waiter, &indexer_waiter, - &stdout_waiter); + QueueManager::Init(&querydb_waiter, &indexer_waiter, &stdout_waiter); PlatformInit(); IndexInit(); @@ -489,7 +488,7 @@ int main(int argc, char** argv) { } // std::cerr << "Running language server" << std::endl; - auto config = MakeUnique(); + auto config = std::make_unique(); LanguageServerMain(argv[0], config.get(), &querydb_waiter, &indexer_waiter, &stdout_waiter); } diff --git a/src/diagnostics_engine.cc b/src/diagnostics_engine.cc index 199a1aae..8d66325f 100644 --- a/src/diagnostics_engine.cc +++ b/src/diagnostics_engine.cc @@ -6,7 +6,7 @@ void DiagnosticsEngine::Init(Config* config) { frequencyMs_ = config->diagnostics.frequencyMs; - match_ = MakeUnique(config->diagnostics.whitelist, + match_ = std::make_unique(config->diagnostics.whitelist, config->diagnostics.blacklist); } diff --git a/src/file_consumer.cc b/src/file_consumer.cc index 779895e2..13042d79 100644 --- a/src/file_consumer.cc +++ b/src/file_consumer.cc @@ -84,7 +84,7 @@ IndexFile* FileConsumer::TryConsumeFile(CXFile file, // Build IndexFile instance. *is_first_ownership = true; - local_[file_id] = MakeUnique(file_name, *contents); + local_[file_id] = std::make_unique(file_name, *contents); return local_[file_id].get(); } diff --git a/src/iindexer.cc b/src/iindexer.cc index befed64d..e0758410 100644 --- a/src/iindexer.cc +++ b/src/iindexer.cc @@ -30,15 +30,15 @@ struct ClangIndexer : IIndexer { struct TestIndexer : IIndexer { static std::unique_ptr FromEntries( const std::vector& entries) { - auto result = MakeUnique(); + auto result = std::make_unique(); for (const TestEntry& entry : entries) { std::vector> indexes; if (entry.num_indexes > 0) - indexes.push_back(MakeUnique(entry.path, "")); + indexes.push_back(std::make_unique(entry.path, "")); for (int i = 1; i < entry.num_indexes; ++i) { - indexes.push_back(MakeUnique( + indexes.push_back(std::make_unique( entry.path + "_extra_" + std::to_string(i) + ".h", "")); } @@ -82,7 +82,7 @@ IIndexer::TestEntry::TestEntry(const std::string& path, int num_indexes) // static std::unique_ptr IIndexer::MakeClangIndexer() { - return MakeUnique(); + return std::make_unique(); } // static diff --git a/src/import_pipeline.cc b/src/import_pipeline.cc index f02fbb8a..dc84f642 100644 --- a/src/import_pipeline.cc +++ b/src/import_pipeline.cc @@ -677,8 +677,8 @@ void QueryDb_DoIdMap(QueueManager* queue, if (!file) return nullptr; - auto id_map = MakeUnique(db, file->id_cache); - return MakeUnique(std::move(file), + auto id_map = std::make_unique(db, file->id_cache); + return std::make_unique(std::move(file), std::move(id_map)); }; response.current = make_map(std::move(request->current)); @@ -763,8 +763,7 @@ bool QueryDb_ImportMain(Config* config, TEST_SUITE("ImportPipeline") { struct Fixture { Fixture() { - QueueManager::CreateInstance(&querydb_waiter, &indexer_waiter, - &stdout_waiter); + QueueManager::Init(&querydb_waiter, &indexer_waiter, &stdout_waiter); queue = QueueManager::instance(); cache_manager = ICacheManager::MakeFake({}); @@ -810,7 +809,7 @@ TEST_SUITE("ImportPipeline") { const std::vector& new_args = {}) { std::unique_ptr opt_previous_index; if (!old_args.empty()) { - opt_previous_index = MakeUnique("---.cc", ""); + opt_previous_index = std::make_unique("---.cc", ""); opt_previous_index->args = old_args; } optional from; diff --git a/src/include_complete.cc b/src/include_complete.cc index c6116853..9f4476c0 100644 --- a/src/include_complete.cc +++ b/src/include_complete.cc @@ -112,7 +112,7 @@ void IncludeComplete::Rescan() { if (!match_ && (!config_->completion.includeWhitelist.empty() || !config_->completion.includeBlacklist.empty())) - match_ = MakeUnique(config_->completion.includeWhitelist, + match_ = std::make_unique(config_->completion.includeWhitelist, config_->completion.includeBlacklist); is_scanning = true; diff --git a/src/lsp.h b/src/lsp.h index feca5923..b44024ce 100644 --- a/src/lsp.h +++ b/src/lsp.h @@ -47,7 +47,7 @@ struct MessageRegistryRegister { std::string method_name = IpcIdToString(T::kIpcId); MessageRegistry::instance()->allocators[method_name] = [](Reader& visitor, std::unique_ptr* message) { - *message = MakeUnique(); + *message = std::make_unique(); // Reflect may throw and *message will be partially deserialized. Reflect(visitor, static_cast(**message)); }; diff --git a/src/messages/initialize.cc b/src/messages/initialize.cc index 2b8453af..e7b822f8 100644 --- a/src/messages/initialize.cc +++ b/src/messages/initialize.cc @@ -9,12 +9,14 @@ #include "semantic_highlight_symbol_cache.h" #include "serializers/json.h" #include "timer.h" +#include "work_thread.h" #include "working_files.h" #include #include #include +#include // TODO Cleanup global variables extern std::string g_init_options; diff --git a/src/queue_manager.cc b/src/queue_manager.cc index 60d6e52f..c3a24cfd 100644 --- a/src/queue_manager.cc +++ b/src/queue_manager.cc @@ -52,20 +52,14 @@ Index_OnIndexed::Index_OnIndexed(IndexUpdate&& update, PerformanceImportFile perf) : update(std::move(update)), perf(perf) {} -QueueManager* QueueManager::instance_ = nullptr; +std::unique_ptr QueueManager::instance_; // static -QueueManager* QueueManager::instance() { - return instance_; -} - -// static -void QueueManager::CreateInstance(MultiQueueWaiter* querydb_waiter, - MultiQueueWaiter* indexer_waiter, - MultiQueueWaiter* stdout_waiter) { - if (instance_) - delete instance_; - instance_ = new QueueManager(querydb_waiter, indexer_waiter, stdout_waiter); +void QueueManager::Init(MultiQueueWaiter* querydb_waiter, + MultiQueueWaiter* indexer_waiter, + MultiQueueWaiter* stdout_waiter) { + instance_ = std::unique_ptr( + new QueueManager(querydb_waiter, indexer_waiter, stdout_waiter)); } // static diff --git a/src/queue_manager.h b/src/queue_manager.h index 67ea61ee..46312d54 100644 --- a/src/queue_manager.h +++ b/src/queue_manager.h @@ -78,16 +78,19 @@ struct Index_OnIndexed { Index_OnIndexed(IndexUpdate&& update, PerformanceImportFile perf); }; -struct QueueManager { - static QueueManager* instance(); - static void CreateInstance(MultiQueueWaiter* querydb_waiter, - MultiQueueWaiter* indexer_waiter, - MultiQueueWaiter* stdout_waiter); +class QueueManager { + static std::unique_ptr instance_; + +public: + static QueueManager* instance() { return instance_.get(); } + static void Init(MultiQueueWaiter* querydb_waiter, + MultiQueueWaiter* indexer_waiter, + MultiQueueWaiter* stdout_waiter); static void WriteStdout(IpcId id, lsBaseOutMessage& response); bool HasWork(); - // Runs on stdout thread. + // Messages received by "stdout" thread. ThreadedQueue for_stdout; // Runs on querydb thread. @@ -107,6 +110,4 @@ struct QueueManager { explicit QueueManager(MultiQueueWaiter* querydb_waiter, MultiQueueWaiter* indexer_waiter, MultiQueueWaiter* stdout_waiter); - - static QueueManager* instance_; }; diff --git a/src/semantic_highlight_symbol_cache.cc b/src/semantic_highlight_symbol_cache.cc index f3a55790..5992b5d4 100644 --- a/src/semantic_highlight_symbol_cache.cc +++ b/src/semantic_highlight_symbol_cache.cc @@ -61,7 +61,7 @@ SemanticHighlightSymbolCache::SemanticHighlightSymbolCache() : cache_(kCacheSize) {} void SemanticHighlightSymbolCache::Init(Config* config) { - match_ = MakeUnique(config->highlight.whitelist, + match_ = std::make_unique(config->highlight.whitelist, config->highlight.blacklist); } diff --git a/src/serializer.cc b/src/serializer.cc index b03ce1eb..07db20de 100644 --- a/src/serializer.cc +++ b/src/serializer.cc @@ -383,7 +383,7 @@ std::unique_ptr Deserialize( if (reader.HasParseError()) return nullptr; - file = MakeUnique(path, file_content); + file = std::make_unique(path, file_content); JsonReader json_reader{&reader}; try { Reflect(json_reader, *file); @@ -405,7 +405,7 @@ std::unique_ptr Deserialize( memcpy(upk.buffer(), serialized_index_content.data(), serialized_index_content.size()); upk.buffer_consumed(serialized_index_content.size()); - file = MakeUnique(path, file_content); + file = std::make_unique(path, file_content); MessagePackReader reader(&upk); Reflect(reader, major); Reflect(reader, minor); diff --git a/src/task.cc b/src/task.cc index 81413314..b26f7b60 100644 --- a/src/task.cc +++ b/src/task.cc @@ -6,8 +6,8 @@ #include TaskManager::TaskManager() { - pending_tasks_[TaskThread::Indexer] = MakeUnique(); - pending_tasks_[TaskThread::QueryDb] = MakeUnique(); + pending_tasks_[TaskThread::Indexer] = std::make_unique(); + pending_tasks_[TaskThread::QueryDb] = std::make_unique(); } void TaskManager::Post(TaskThread thread, const TTask& task) { diff --git a/src/threaded_queue.h b/src/threaded_queue.h index 907e8da7..7b8ffe7e 100644 --- a/src/threaded_queue.h +++ b/src/threaded_queue.h @@ -1,7 +1,6 @@ #pragma once #include "utils.h" -#include "work_thread.h" #include @@ -11,6 +10,7 @@ #include #include #include +#include // TODO: cleanup includes. @@ -19,24 +19,6 @@ struct BaseThreadQueue { virtual ~BaseThreadQueue() = default; }; -// TODO Remove after migration to C++14 -namespace { - -template -struct index_sequence {}; - -template -struct make_index_sequence { - using type = typename make_index_sequence::type; -}; - -template -struct make_index_sequence<0, Is...> { - using type = index_sequence; -}; - -} // namespace - // std::lock accepts two or more arguments. We define an overload for one // argument. namespace std { @@ -51,20 +33,20 @@ struct MultiQueueLock { MultiQueueLock(Queue... lockable) : tuple_{lockable...} { lock(); } ~MultiQueueLock() { unlock(); } void lock() { - lock_impl(typename make_index_sequence::type{}); + lock_impl(typename std::make_index_sequence{}); } void unlock() { - unlock_impl(typename make_index_sequence::type{}); + unlock_impl(typename std::make_index_sequence{}); } private: template - void lock_impl(index_sequence) { + void lock_impl(std::index_sequence) { std::lock(std::get(tuple_)->mutex_...); } template - void unlock_impl(index_sequence) { + void unlock_impl(std::index_sequence) { (void)std::initializer_list{ (std::get(tuple_)->mutex_.unlock(), 0)...}; } @@ -96,9 +78,9 @@ template struct ThreadedQueue : public BaseThreadQueue { public: ThreadedQueue() : total_count_(0) { - owned_waiter_ = MakeUnique(); + owned_waiter_ = std::make_unique(); waiter_ = owned_waiter_.get(); - owned_waiter1_ = MakeUnique(); + owned_waiter1_ = std::make_unique(); waiter1_ = owned_waiter1_.get(); } diff --git a/src/utils.h b/src/utils.h index 204d3467..a602c760 100644 --- a/src/utils.h +++ b/src/utils.h @@ -111,14 +111,6 @@ struct TextReplacer { void WriteToFile(const std::string& filename, const std::string& content); -// note: this implementation does not disable this overload for array types -// See -// http://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique#Possible_Implementatiog -template -std::unique_ptr MakeUnique(Args&&... args) { - return std::unique_ptr(new T(std::forward(args)...)); -} - template void AddRange(std::vector* dest, const std::vector& to_add) { dest->insert(dest->end(), to_add.begin(), to_add.end()); diff --git a/src/working_files.cc b/src/working_files.cc index 2b2afea4..64c091c3 100644 --- a/src/working_files.cc +++ b/src/working_files.cc @@ -478,7 +478,7 @@ WorkingFile* WorkingFiles::OnOpen(const lsTextDocumentItem& open) { return file; } - files.push_back(MakeUnique(filename, content)); + files.push_back(std::make_unique(filename, content)); return files[files.size() - 1].get(); }