kyy
2025-07-02 07558e32634314eec359ec8437d97bdc5def64f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright © 2021 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
 
#ifndef CEFSHARP_CORE_CEFREFCOUNTEDMANAGED_H
#define CEFSHARP_CORE_CEFREFCOUNTEDMANAGED_H
#pragma once
 
#include <winnt.h>
 
#include "include\base\cef_macros.h"
 
class CefRefCountManaged
{
public:
    CefRefCountManaged() : ref_count_(0)
    {
    }
 
    ///
    // Increment the reference count.
    ///
    void AddRef() const
    {
        InterlockedIncrement(&ref_count_);
    }
 
    ///
    // Decrement the reference count. Returns true if the reference count is 0.
    ///
    bool Release() const
    {
        LONG res = InterlockedDecrement(&ref_count_);
 
        return res == 0;
    }
 
    ///
    // Returns true if the reference count is 1.
    ///
    bool HasOneRef() const
    {
        LONG res = InterlockedCompareExchange(&ref_count_, 0, 0);
 
        return res == 1;
    }
 
    ///
    // Returns true if the reference count is at least 1.
    ///
    bool HasAtLeastOneRef() const
    {
        LONG res = InterlockedCompareExchange(&ref_count_, 0, 0);
 
        return res > 0;
    }
 
private:
    mutable volatile LONG ref_count_;
    DISALLOW_COPY_AND_ASSIGN(CefRefCountManaged);
};
 
 
///
// Macro that provides a reference counting implementation for classes extending
// CefBase.
///
#define IMPLEMENT_REFCOUNTINGM(ClassName)                            \
 public:                                                             \
  void AddRef() const override { ref_count_.AddRef(); }              \
  bool Release() const override {                                    \
    if (ref_count_.Release()) {                                      \
      delete static_cast<const ClassName*>(this);                    \
      return true;                                                   \
    }                                                                \
    return false;                                                    \
  }                                                                  \
  bool HasOneRef() const override { return ref_count_.HasOneRef(); } \
  bool HasAtLeastOneRef() const override {                           \
    return ref_count_.HasAtLeastOneRef();                            \
  }                                                                  \
                                                                     \
 private:                                                            \
  CefRefCountManaged ref_count_
 
#endif  // CEFSHARP_CORE_CEFREFCOUNTEDMANAGED_H