2010-10-03 23:04:50 +02:00
|
|
|
// Copyright (c) 2009 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#ifndef CEF_LIBCEF_DLL_CTOCPP_CTOCPP_H_
|
|
|
|
#define CEF_LIBCEF_DLL_CTOCPP_CTOCPP_H_
|
|
|
|
#pragma once
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-12-23 18:36:30 +01:00
|
|
|
#include "include/cef_base.h"
|
|
|
|
#include "include/capi/cef_base_capi.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
#include "libcef_dll/cef_logging.h"
|
|
|
|
|
|
|
|
|
|
|
|
// Wrap a C structure with a C++ class. This is used when the implementation
|
|
|
|
// exists on the other side of the DLL boundary but will have methods called on
|
|
|
|
// this side of the DLL boundary.
|
|
|
|
template <class ClassName, class BaseName, class StructName>
|
2012-01-10 00:46:23 +01:00
|
|
|
class CefCToCpp : public BaseName {
|
|
|
|
public:
|
2010-10-03 23:04:50 +02:00
|
|
|
// Use this method to create a wrapper class instance for a structure
|
|
|
|
// received from the other side.
|
2012-01-10 00:46:23 +01:00
|
|
|
static CefRefPtr<BaseName> Wrap(StructName* s) {
|
2011-10-14 14:40:40 +02:00
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// Wrap their structure with the CefCToCpp object.
|
|
|
|
ClassName* wrapper = new ClassName(s);
|
|
|
|
// Put the wrapper object in a smart pointer.
|
|
|
|
CefRefPtr<BaseName> wrapperPtr(wrapper);
|
|
|
|
// Release the reference that was added to the CefCppToC wrapper object on
|
|
|
|
// the other side before their structure was passed to us.
|
|
|
|
wrapper->UnderlyingRelease();
|
|
|
|
// Return the smart pointer.
|
|
|
|
return wrapperPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use this method to retrieve the underlying structure from a wrapper class
|
|
|
|
// instance for return back to the other side.
|
2012-01-10 00:46:23 +01:00
|
|
|
static StructName* Unwrap(CefRefPtr<BaseName> c) {
|
2011-12-08 02:38:30 +01:00
|
|
|
if (!c.get())
|
|
|
|
return NULL;
|
2011-10-14 14:40:40 +02:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// Cast the object to our wrapper class type.
|
|
|
|
ClassName* wrapper = static_cast<ClassName*>(c.get());
|
|
|
|
// Add a reference to the CefCppToC wrapper object on the other side that
|
|
|
|
// will be released once the structure is received.
|
|
|
|
wrapper->UnderlyingAddRef();
|
|
|
|
// Return their original structure.
|
|
|
|
return wrapper->GetStruct();
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
explicit CefCToCpp(StructName* str)
|
|
|
|
: struct_(str) {
|
2010-10-03 23:04:50 +02:00
|
|
|
DCHECK(str);
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
#ifndef NDEBUG
|
2010-10-03 23:04:50 +02:00
|
|
|
CefAtomicIncrement(&DebugObjCt);
|
|
|
|
#endif
|
|
|
|
}
|
2012-01-10 00:46:23 +01:00
|
|
|
virtual ~CefCToCpp() {
|
2011-05-20 16:42:25 +02:00
|
|
|
#ifndef NDEBUG
|
2010-10-03 23:04:50 +02:00
|
|
|
CefAtomicDecrement(&DebugObjCt);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// If returning the structure across the DLL boundary you should call
|
|
|
|
// UnderlyingAddRef() on this wrapping CefCToCpp object. On the other side of
|
|
|
|
// the DLL boundary, call Release() on the CefCppToC object.
|
|
|
|
StructName* GetStruct() { return struct_; }
|
|
|
|
|
|
|
|
// CefBase methods increment/decrement reference counts on both this object
|
|
|
|
// and the underlying wrapped structure.
|
2012-01-10 00:46:23 +01:00
|
|
|
int AddRef() {
|
2010-10-03 23:04:50 +02:00
|
|
|
UnderlyingAddRef();
|
2011-05-20 16:42:25 +02:00
|
|
|
return refct_.AddRef();
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
2012-01-10 00:46:23 +01:00
|
|
|
int Release() {
|
2010-10-03 23:04:50 +02:00
|
|
|
UnderlyingRelease();
|
2011-05-20 16:42:25 +02:00
|
|
|
int retval = refct_.Release();
|
|
|
|
if (retval == 0)
|
|
|
|
delete this;
|
|
|
|
return retval;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
2011-05-20 16:42:25 +02:00
|
|
|
int GetRefCt() { return refct_.GetRefCt(); }
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
// Increment/decrement reference counts on only the underlying class.
|
2012-01-10 00:46:23 +01:00
|
|
|
int UnderlyingAddRef() {
|
|
|
|
if (!struct_->base.add_ref)
|
2010-10-03 23:04:50 +02:00
|
|
|
return 0;
|
|
|
|
return struct_->base.add_ref(&struct_->base);
|
|
|
|
}
|
2012-01-10 00:46:23 +01:00
|
|
|
int UnderlyingRelease() {
|
|
|
|
if (!struct_->base.release)
|
2010-10-03 23:04:50 +02:00
|
|
|
return 0;
|
|
|
|
return struct_->base.release(&struct_->base);
|
|
|
|
}
|
2012-01-10 00:46:23 +01:00
|
|
|
int UnderlyingGetRefCt() {
|
|
|
|
if (!struct_->base.get_refct)
|
2010-10-03 23:04:50 +02:00
|
|
|
return 0;
|
|
|
|
return struct_->base.get_refct(&struct_->base);
|
|
|
|
}
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
#ifndef NDEBUG
|
2010-10-03 23:04:50 +02:00
|
|
|
// Simple tracking of allocated objects.
|
2012-01-10 00:46:23 +01:00
|
|
|
static long DebugObjCt; // NOLINT(runtime/int)
|
2010-10-03 23:04:50 +02:00
|
|
|
#endif
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
protected:
|
2011-05-20 16:42:25 +02:00
|
|
|
CefRefCount refct_;
|
2010-10-03 23:04:50 +02:00
|
|
|
StructName* struct_;
|
|
|
|
};
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#endif // CEF_LIBCEF_DLL_CTOCPP_CTOCPP_H_
|