#ifndef CEF_LIBCEF_DLL_CPPTOC_CPPTOC_REF_COUNTED_H_
#define CEF_LIBCEF_DLL_CPPTOC_CPPTOC_REF_COUNTED_H_
#pragma once
#include "include/base/cef_logging.h"
#include "include/capi/cef_base_capi.h"
#include "include/cef_base.h"
#include "libcef_dll/wrapper_types.h"
template <class ClassName, class BaseName, class StructName>
class CefCppToCRefCounted : public CefBaseRefCounted {
public:
CefCppToCRefCounted(const CefCppToCRefCounted&) = delete;
CefCppToCRefCounted& operator=(const CefCppToCRefCounted&) = delete;
static StructName* Wrap(CefRefPtr<BaseName> c) {
if (!c.get())
return nullptr;
ClassName* wrapper = new ClassName();
wrapper->wrapper_struct_.object_ = c.get();
wrapper->AddRef();
return wrapper->GetStruct();
}
static CefRefPtr<BaseName> Unwrap(StructName* s) {
if (!s)
return nullptr;
WrapperStruct* wrapperStruct = GetWrapperStruct(s);
if (wrapperStruct->type_ != kWrapperType)
return UnwrapDerived(wrapperStruct->type_, s);
CefRefPtr<BaseName> objectPtr(wrapperStruct->object_);
wrapperStruct->wrapper_->Release();
return objectPtr;
}
static CefRefPtr<BaseName> Get(StructName* s) {
DCHECK(s);
WrapperStruct* wrapperStruct = GetWrapperStruct(s);
DCHECK_EQ(kWrapperType, wrapperStruct->type_);
return wrapperStruct->object_;
}
StructName* GetStruct() { return &wrapper_struct_.struct_; }
void AddRef() const {
UnderlyingAddRef();
ref_count_.AddRef();
}
bool Release() const {
UnderlyingRelease();
if (ref_count_.Release()) {
delete this;
return true;
}
return false;
}
bool HasOneRef() const { return UnderlyingHasOneRef(); }
bool HasAtLeastOneRef() const { return UnderlyingHasAtLeastOneRef(); }
protected:
CefCppToCRefCounted() {
wrapper_struct_.type_ = kWrapperType;
wrapper_struct_.wrapper_ = this;
memset(GetStruct(), 0, sizeof(StructName));
cef_base_ref_counted_t* base =
reinterpret_cast<cef_base_ref_counted_t*>(GetStruct());
base->size = sizeof(StructName);
base->add_ref = struct_add_ref;
base->release = struct_release;
base->has_one_ref = struct_has_one_ref;
base->has_at_least_one_ref = struct_has_at_least_one_ref;
}
virtual ~CefCppToCRefCounted() = default;
private:
struct WrapperStruct {
CefWrapperType type_;
BaseName* object_;
CefCppToCRefCounted<ClassName, BaseName, StructName>* wrapper_;
StructName struct_;
};
static WrapperStruct* GetWrapperStruct(StructName* s) {
return reinterpret_cast<WrapperStruct*>(
reinterpret_cast<char*>(s) -
(sizeof(WrapperStruct) - sizeof(StructName)));
}
static CefRefPtr<BaseName> UnwrapDerived(CefWrapperType type, StructName* s);
void UnderlyingAddRef() const { wrapper_struct_.object_->AddRef(); }
void UnderlyingRelease() const { wrapper_struct_.object_->Release(); }
bool UnderlyingHasOneRef() const {
return wrapper_struct_.object_->HasOneRef();
}
bool UnderlyingHasAtLeastOneRef() const {
return wrapper_struct_.object_->HasAtLeastOneRef();
}
static void CEF_CALLBACK struct_add_ref(cef_base_ref_counted_t* base) {
DCHECK(base);
if (!base)
return;
WrapperStruct* wrapperStruct =
GetWrapperStruct(reinterpret_cast<StructName*>(base));
DCHECK_EQ(kWrapperType, wrapperStruct->type_);
wrapperStruct->wrapper_->AddRef();
}
static int CEF_CALLBACK struct_release(cef_base_ref_counted_t* base) {
DCHECK(base);
if (!base)
return 0;
WrapperStruct* wrapperStruct =
GetWrapperStruct(reinterpret_cast<StructName*>(base));
DCHECK_EQ(kWrapperType, wrapperStruct->type_);
return wrapperStruct->wrapper_->Release();
}
static int CEF_CALLBACK struct_has_one_ref(cef_base_ref_counted_t* base) {
DCHECK(base);
if (!base)
return 0;
WrapperStruct* wrapperStruct =
GetWrapperStruct(reinterpret_cast<StructName*>(base));
DCHECK_EQ(kWrapperType, wrapperStruct->type_);
return wrapperStruct->wrapper_->HasOneRef();
}
static int CEF_CALLBACK
struct_has_at_least_one_ref(cef_base_ref_counted_t* base) {
DCHECK(base);
if (!base)
return 0;
WrapperStruct* wrapperStruct =
GetWrapperStruct(reinterpret_cast<StructName*>(base));
DCHECK_EQ(kWrapperType, wrapperStruct->type_);
return wrapperStruct->wrapper_->HasAtLeastOneRef();
}
WrapperStruct wrapper_struct_;
CefRefCount ref_count_;
static CefWrapperType kWrapperType;
};
#endif