2009-06-21 00:09:28 +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.
|
|
|
|
|
|
|
|
#ifndef _CTOCPP_H
|
|
|
|
#define _CTOCPP_H
|
|
|
|
|
2009-10-02 19:59:38 +02:00
|
|
|
#include "include/cef.h"
|
|
|
|
#include "include/cef_capi.h"
|
|
|
|
#include "libcef_dll/cef_logging.h"
|
2009-06-21 00:09:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
// 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.
|
2009-05-28 02:31:21 +02:00
|
|
|
template <class ClassName, class BaseName, class StructName>
|
|
|
|
class CefCToCpp : public CefThreadSafeBase<BaseName>
|
2009-03-05 02:10:06 +01:00
|
|
|
{
|
2009-06-21 00:09:28 +02:00
|
|
|
public:
|
|
|
|
// Use this method to create a wrapper class instance for a structure
|
|
|
|
// received from the other side.
|
|
|
|
static CefRefPtr<BaseName> Wrap(StructName* s)
|
|
|
|
{
|
|
|
|
// Wrap their structure with the CefCToCpp object.
|
2009-05-28 02:31:21 +02:00
|
|
|
ClassName* wrapper = new ClassName(s);
|
|
|
|
// Put the wrapper object in a smart pointer.
|
2009-06-21 00:09:28 +02:00
|
|
|
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.
|
|
|
|
static StructName* Unwrap(CefRefPtr<BaseName> c)
|
|
|
|
{
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefCToCpp(StructName* str)
|
|
|
|
: struct_(str)
|
|
|
|
{
|
|
|
|
DCHECK(str);
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
CefAtomicIncrement(&DebugObjCt);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
virtual ~CefCToCpp()
|
|
|
|
{
|
|
|
|
#ifdef _DEBUG
|
|
|
|
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.
|
2009-03-05 02:10:06 +01:00
|
|
|
virtual int AddRef()
|
|
|
|
{
|
|
|
|
UnderlyingAddRef();
|
2009-05-28 02:31:21 +02:00
|
|
|
return CefThreadSafeBase<BaseName>::AddRef();
|
2009-03-05 02:10:06 +01:00
|
|
|
}
|
2009-06-21 00:09:28 +02:00
|
|
|
virtual int Release()
|
|
|
|
{
|
2009-03-05 02:10:06 +01:00
|
|
|
UnderlyingRelease();
|
2009-06-21 00:09:28 +02:00
|
|
|
return CefThreadSafeBase<BaseName>::Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increment/decrement reference counts on only the underlying class.
|
|
|
|
int UnderlyingAddRef()
|
|
|
|
{
|
2009-03-05 02:10:06 +01:00
|
|
|
if(!struct_->base.add_ref)
|
|
|
|
return 0;
|
2009-06-21 00:09:28 +02:00
|
|
|
return struct_->base.add_ref(&struct_->base);
|
|
|
|
}
|
|
|
|
int UnderlyingRelease()
|
2009-05-28 02:31:21 +02:00
|
|
|
{
|
2009-06-21 00:09:28 +02:00
|
|
|
if(!struct_->base.release)
|
|
|
|
return 0;
|
|
|
|
return struct_->base.release(&struct_->base);
|
2009-05-28 02:31:21 +02:00
|
|
|
}
|
2009-06-21 00:09:28 +02:00
|
|
|
int UnderlyingGetRefCt()
|
|
|
|
{
|
|
|
|
if(!struct_->base.get_refct)
|
2009-05-28 02:31:21 +02:00
|
|
|
return 0;
|
2009-06-21 00:09:28 +02:00
|
|
|
return struct_->base.get_refct(&struct_->base);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
// Simple tracking of allocated objects.
|
|
|
|
static long DebugObjCt;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
protected:
|
|
|
|
StructName* struct_;
|
2009-05-28 02:31:21 +02:00
|
|
|
};
|
2009-06-21 00:09:28 +02:00
|
|
|
|
|
|
|
#endif // _CTOCPP_H
|