Mark D. Roth | 70db663 | 2017-11-27 14:53:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2017 gRPC authors. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #ifndef GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_H |
| 20 | #define GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_H |
| 21 | |
| 22 | #include <grpc/support/sync.h> |
| 23 | |
| 24 | #include "src/core/lib/debug/trace.h" |
| 25 | #include "src/core/lib/support/debug_location.h" |
| 26 | |
| 27 | namespace grpc_core { |
| 28 | |
| 29 | class ReferenceCounted { |
| 30 | public: |
| 31 | void Ref(); |
| 32 | void Ref(const DebugLocation& location, const char* reason); |
| 33 | |
| 34 | bool Unref(); |
| 35 | bool Unref(const DebugLocation& location, const char* reason); |
| 36 | |
| 37 | // Not copyable nor movable. |
| 38 | ReferenceCounted(const ReferenceCounted&) = delete; |
| 39 | ReferenceCounted& operator=(const ReferenceCounted&) = delete; |
| 40 | |
| 41 | protected: |
Mark D. Roth | abadc6c | 2017-11-28 08:24:10 -0800 | [diff] [blame] | 42 | // Allow Delete() to access destructor. |
Mark D. Roth | d3984c3 | 2017-11-28 09:13:27 -0800 | [diff] [blame^] | 43 | template <typename T> |
Mark D. Roth | abadc6c | 2017-11-28 08:24:10 -0800 | [diff] [blame] | 44 | friend void Delete(T*); |
| 45 | |
Mark D. Roth | 70db663 | 2017-11-27 14:53:26 -0800 | [diff] [blame] | 46 | explicit ReferenceCounted(TraceFlag* trace_flag) : trace_flag_(trace_flag) { |
| 47 | gpr_ref_init(&refs_, 1); |
| 48 | } |
| 49 | |
| 50 | virtual ~ReferenceCounted() {} |
| 51 | |
| 52 | private: |
| 53 | TraceFlag* trace_flag_; |
| 54 | gpr_refcount refs_; |
| 55 | }; |
| 56 | |
| 57 | } // namespace grpc_core |
| 58 | |
Mark D. Roth | 3228489 | 2017-11-28 09:12:16 -0800 | [diff] [blame] | 59 | #endif /* GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_H */ |