Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Chris Hamilton | 77da59d | 2019-06-04 15:59:32 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Sigurdur Asgeirsson | 51d9d24 | 2019-10-07 20:38:34 | [diff] [blame] | 5 | #ifndef COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_GRAPH_NODE_H_ |
| 6 | #define COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_GRAPH_NODE_H_ |
Chris Hamilton | 77da59d | 2019-06-04 15:59:32 | [diff] [blame] | 7 | |
| 8 | #include <cstdint> |
| 9 | |
Patrick Monette | 0564af7c | 2024-06-05 16:19:48 | [diff] [blame] | 10 | #include "base/check_op.h" |
Chris Hamilton | f86f634 | 2021-03-30 16:42:34 | [diff] [blame] | 11 | #include "components/performance_manager/public/graph/node_state.h" |
Patrick Monette | 0564af7c | 2024-06-05 16:19:48 | [diff] [blame] | 12 | #include "components/performance_manager/public/graph/node_type.h" |
Chris Hamilton | 77da59d | 2019-06-04 15:59:32 | [diff] [blame] | 13 | |
| 14 | namespace performance_manager { |
| 15 | |
| 16 | class Graph; |
| 17 | |
| 18 | // Interface that all nodes must implement. |
Chris Hamilton | 77da59d | 2019-06-04 15:59:32 | [diff] [blame] | 19 | class Node { |
| 20 | public: |
| 21 | Node(); |
Peter Boström | 09c0182 | 2021-09-20 22:43:27 | [diff] [blame] | 22 | |
| 23 | Node(const Node&) = delete; |
| 24 | Node& operator=(const Node&) = delete; |
| 25 | |
Chris Hamilton | 77da59d | 2019-06-04 15:59:32 | [diff] [blame] | 26 | virtual ~Node(); |
| 27 | |
Patrick Monette | 0564af7c | 2024-06-05 16:19:48 | [diff] [blame] | 28 | // Returns the type of this node. |
| 29 | virtual NodeTypeEnum GetNodeType() const = 0; |
| 30 | |
Chris Hamilton | 77da59d | 2019-06-04 15:59:32 | [diff] [blame] | 31 | // Returns the graph to which this node belongs. |
| 32 | virtual Graph* GetGraph() const = 0; |
| 33 | |
| 34 | // The following functions are implementation detail and should not need to be |
| 35 | // used by external clients. They provide the ability to safely downcast to |
| 36 | // the underlying implementation. |
| 37 | virtual uintptr_t GetImplType() const = 0; |
| 38 | virtual const void* GetImpl() const = 0; |
Chris Hamilton | 77da59d | 2019-06-04 15:59:32 | [diff] [blame] | 39 | }; |
| 40 | |
Patrick Monette | 0564af7c | 2024-06-05 16:19:48 | [diff] [blame] | 41 | template <class PublicNodeClass> |
| 42 | class TypedNode : public Node { |
| 43 | public: |
| 44 | TypedNode() = default; |
| 45 | ~TypedNode() override = default; |
| 46 | |
| 47 | NodeTypeEnum GetNodeType() const override { return PublicNodeClass::Type(); } |
| 48 | |
| 49 | // Helper function for casting from the generic Node type to its underlying |
| 50 | // public node type. This CHECKs that the cast is valid. |
| 51 | static const PublicNodeClass* FromNode(const Node* node) { |
| 52 | CHECK_EQ(node->GetNodeType(), PublicNodeClass::Type()); |
| 53 | return reinterpret_cast<const PublicNodeClass*>(node); |
| 54 | } |
| 55 | }; |
| 56 | |
Chris Hamilton | 77da59d | 2019-06-04 15:59:32 | [diff] [blame] | 57 | } // namespace performance_manager |
| 58 | |
Sigurdur Asgeirsson | 51d9d24 | 2019-10-07 20:38:34 | [diff] [blame] | 59 | #endif // COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_GRAPH_NODE_H_ |