blob: e71b4fc5931460a8f4c8d4459a5a88014a85e35a [file] [log] [blame]
Mark D. Roth70db6632017-11-27 14:53:26 -08001/*
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
27namespace grpc_core {
28
29class 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. Rothabadc6c2017-11-28 08:24:10 -080042 // Allow Delete() to access destructor.
43 template<typename T>
44 friend void Delete(T*);
45
Mark D. Roth70db6632017-11-27 14:53:26 -080046 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
59#endif // GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_H