From 253414f29b9c1d3e0dc06c807b79591fe6daad85 Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Fri, 3 Mar 2017 00:43:54 -0800 Subject: [PATCH] fixes --- command_line.cc | 4 ++-- ipc.cc | 14 +++++++++++--- ipc.h | 35 +++++++++++------------------------ 3 files changed, 24 insertions(+), 29 deletions(-) diff --git a/command_line.cc b/command_line.cc index fba35fa4..f0b694d4 100644 --- a/command_line.cc +++ b/command_line.cc @@ -96,7 +96,7 @@ indexer.exe --index-file /work2/chrome/src/chrome/foo.cc */ -struct IpcMessage_IsAlive : public IpcMessage { +struct IpcMessage_IsAlive : public BaseIpcMessage { static IpcMessageId id; }; @@ -307,7 +307,7 @@ void main2() { } int main(int argc, char** argv) { - IpcMessage_IsAlive _; + IpcRegistry::instance()->RegisterAllocator(); //main2(); //return 0; diff --git a/ipc.cc b/ipc.cc index 4bab034e..bf39dea9 100644 --- a/ipc.cc +++ b/ipc.cc @@ -24,7 +24,12 @@ void JsonMessage::SetPayload(size_t payload_size, const char* payload) { memcpy(payload_dest, payload, payload_size); } -BaseIpcMessage::BaseIpcMessage(BaseIpcMessage::DoNotDeriveDirectly) {} +BaseIpcMessage::BaseIpcMessage() { + assert(!runtime_id_.empty() && "Message is not registered using IpcRegistry::RegisterAllocator"); + + runtime_id = runtime_id_; + hashed_runtime_id = hashed_runtime_id_; +} BaseIpcMessage::~BaseIpcMessage() {} @@ -32,8 +37,11 @@ void BaseIpcMessage::Serialize(Writer& writer) {} void BaseIpcMessage::Deserialize(Reader& reader) {} +IpcMessageId BaseIpcMessage::runtime_id_; -IpcRegistry IpcRegistry::Instance; +int BaseIpcMessage::hashed_runtime_id_ = -1; + +IpcRegistry* IpcRegistry::instance_ = nullptr; std::unique_ptr IpcRegistry::Allocate(int id) { return std::unique_ptr((*allocators)[id]()); @@ -108,7 +116,7 @@ std::vector> IpcDirectionalChannel::TakeMessages char* message = local_block; while (remaining_bytes > 0) { - std::unique_ptr base_message = IpcRegistry::Instance.Allocate(as_message(message)->message_id); + std::unique_ptr base_message = IpcRegistry::instance()->Allocate(as_message(message)->message_id); rapidjson::Document document; document.Parse(as_message(message)->payload(), as_message(message)->payload_size); diff --git a/ipc.h b/ipc.h index 36bd7ba6..b9279660 100644 --- a/ipc.h +++ b/ipc.h @@ -32,24 +32,24 @@ struct JsonMessage { using IpcMessageId = std::string; struct BaseIpcMessage { + BaseIpcMessage(); virtual ~BaseIpcMessage(); virtual void Serialize(Writer& writer); virtual void Deserialize(Reader& reader); IpcMessageId runtime_id; - int hashed_runtime_id; + int hashed_runtime_id = -1; + + // Populated by IpcRegistry::RegisterAllocator. + static IpcMessageId runtime_id_; + static int hashed_runtime_id_; /* private: template friend struct IpcMessage; */ - - enum class DoNotDeriveDirectly { - DeriveFromIpcMessageInstead - }; - BaseIpcMessage(DoNotDeriveDirectly); }; struct IpcRegistry { @@ -66,7 +66,8 @@ struct IpcRegistry { std::unique_ptr Allocate(int id); static IpcRegistry* instance() { - // TODO: Remove static magic. Just call register explicitly. + if (!instance_) + instance_ = new IpcRegistry(); return instance_; } static IpcRegistry* instance_; @@ -90,26 +91,12 @@ int IpcRegistry::RegisterAllocator() { return new T(); }; + T::runtime_id_ = id; + T::hashed_runtime_id_ = hash; + return hash; } -template -struct IpcMessage : public BaseIpcMessage { - IpcMessage(); - - static int hashed_id_; -}; - -template -int IpcMessage::hashed_id_ = IpcRegistry::Instance.RegisterAllocator(); - -template -IpcMessage::IpcMessage() - : BaseIpcMessage(DoNotDeriveDirectly::DeriveFromIpcMessageInstead) { - runtime_id = TChild::id; - hashed_runtime_id = hashed_id_; -} -