Commit ff2b2ced authored by Chris Hamilton's avatar Chris Hamilton Committed by Commit Bot

[PM] Add ability to safely downcast from Graph to GraphImpl.

This will allow us to use a single observer interface rather than having
public and private versions.

BUG=910288

Change-Id: Icd6ace3d79a03de28e947765edd65ec21618e85f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1635637Reviewed-by: default avatarSigurður Ásgeirsson <siggi@chromium.org>
Commit-Queue: Chris Hamilton <chrisha@chromium.org>
Auto-Submit: Chris Hamilton <chrisha@chromium.org>
Cr-Commit-Position: refs/heads/master@{#664728}
parent ef518cfd
...@@ -23,6 +23,13 @@ class UkmEntryBuilder; ...@@ -23,6 +23,13 @@ class UkmEntryBuilder;
namespace performance_manager { namespace performance_manager {
namespace {
// A unique type ID for this implementation.
const uintptr_t kGraphImplType = reinterpret_cast<uintptr_t>(&kGraphImplType);
} // namespace
GraphImpl::GraphImpl() { GraphImpl::GraphImpl() {
DETACH_FROM_SEQUENCE(sequence_checker_); DETACH_FROM_SEQUENCE(sequence_checker_);
} }
...@@ -44,6 +51,20 @@ GraphImpl::~GraphImpl() { ...@@ -44,6 +51,20 @@ GraphImpl::~GraphImpl() {
DCHECK(nodes_.empty()); DCHECK(nodes_.empty());
} }
uintptr_t GraphImpl::GetImplType() const {
return kGraphImplType;
}
const void* GraphImpl::GetImpl() const {
return this;
}
// static
GraphImpl* GraphImpl::FromGraph(const Graph* graph) {
CHECK_EQ(kGraphImplType, graph->GetImplType());
return reinterpret_cast<GraphImpl*>(const_cast<void*>(graph->GetImpl()));
}
void GraphImpl::RegisterObserver(GraphObserver* observer) { void GraphImpl::RegisterObserver(GraphObserver* observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
observer->SetGraph(this); observer->SetGraph(this);
......
...@@ -50,6 +50,14 @@ class GraphImpl : public Graph { ...@@ -50,6 +50,14 @@ class GraphImpl : public Graph {
GraphImpl(); GraphImpl();
~GraphImpl() override; ~GraphImpl() override;
// Graph implementation:
uintptr_t GetImplType() const override;
const void* GetImpl() const override;
// Helper function for safely downcasting to the implementation. This also
// casts away constness. This will CHECK on an invalid cast.
static GraphImpl* FromGraph(const Graph* graph);
void set_ukm_recorder(ukm::UkmRecorder* ukm_recorder) { void set_ukm_recorder(ukm::UkmRecorder* ukm_recorder) {
ukm_recorder_ = ukm_recorder; ukm_recorder_ = ukm_recorder;
} }
......
...@@ -15,6 +15,12 @@ ...@@ -15,6 +15,12 @@
namespace performance_manager { namespace performance_manager {
TEST(GraphImplTest, SafeCasting) {
GraphImpl graph_impl;
const Graph* graph = &graph_impl;
EXPECT_EQ(&graph_impl, GraphImpl::FromGraph(graph));
}
TEST(GraphImplTest, FindOrCreateSystemNode) { TEST(GraphImplTest, FindOrCreateSystemNode) {
GraphImpl graph; GraphImpl graph;
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef CHROME_BROWSER_PERFORMANCE_MANAGER_PUBLIC_GRAPH_GRAPH_H_ #ifndef CHROME_BROWSER_PERFORMANCE_MANAGER_PUBLIC_GRAPH_GRAPH_H_
#define CHROME_BROWSER_PERFORMANCE_MANAGER_PUBLIC_GRAPH_GRAPH_H_ #define CHROME_BROWSER_PERFORMANCE_MANAGER_PUBLIC_GRAPH_GRAPH_H_
#include <cstdint>
#include "base/macros.h" #include "base/macros.h"
namespace performance_manager { namespace performance_manager {
...@@ -17,6 +19,12 @@ class Graph { ...@@ -17,6 +19,12 @@ class Graph {
Graph(); Graph();
virtual ~Graph(); virtual ~Graph();
// The following functions are implementation detail and should not need to be
// used by external clients. They provide the ability to safely downcast to
// the underlying implementation.
virtual uintptr_t GetImplType() const = 0;
virtual const void* GetImpl() const = 0;
private: private:
DISALLOW_COPY_AND_ASSIGN(Graph); DISALLOW_COPY_AND_ASSIGN(Graph);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment