Commit 3e817fcb authored by Jacob Potter's avatar Jacob Potter

Prevent copying of Handle objects

parent 143fb84c
......@@ -16,18 +16,18 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
@interface TXSSortItems ()
@property (nonatomic, readonly) ::djinni::DbxCppWrapperCache<::textsort::SortItems>::Handle cppRef;
- (id)initWithCpp:(const std::shared_ptr<::textsort::SortItems>&)cppRef;
@end
@implementation TXSSortItems
@implementation TXSSortItems {
::djinni::DbxCppWrapperCache<::textsort::SortItems>::Handle _cppRefHandle;
}
- (id)initWithCpp:(const std::shared_ptr<::textsort::SortItems>&)cppRef
{
if (self = [super init]) {
_cppRef.assign(cppRef);
_cppRefHandle.assign(cppRef);
}
return self;
}
......@@ -35,8 +35,8 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
- (void)sort:(TXSSortOrder)order
items:(nonnull TXSItemList *)items {
try {
_cppRef.get()->sort(::djinni::Enum<::textsort::sort_order, TXSSortOrder>::toCpp(order),
::djinni_generated::ItemList::toCpp(items));
_cppRefHandle.get()->sort(::djinni::Enum<::textsort::sort_order, TXSSortOrder>::toCpp(order),
::djinni_generated::ItemList::toCpp(items));
} DJINNI_TRANSLATE_EXCEPTIONS()
}
......@@ -47,8 +47,6 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
} DJINNI_TRANSLATE_EXCEPTIONS()
}
@end
namespace djinni_generated {
auto SortItems::toCpp(ObjcType objc) -> CppType
......@@ -56,7 +54,7 @@ auto SortItems::toCpp(ObjcType objc) -> CppType
if (!objc) {
return nullptr;
}
return objc.cppRef.get();
return objc->_cppRefHandle.get();
}
auto SortItems::fromCpp(const CppType& cpp) -> ObjcType
......@@ -70,3 +68,5 @@ auto SortItems::fromCpp(const CppType& cpp) -> ObjcType
}
} // namespace djinni_generated
@end
......@@ -128,18 +128,18 @@ class ObjcppGenerator(spec: Spec) extends Generator(spec) {
else
w.wl(s"@interface $objcSelf ()")
w.wl
w.wl(s"@property (nonatomic, readonly) ::djinni::DbxCppWrapperCache<$cppSelf>::Handle cppRef;")
w.wl
w.wl(s"- (id)initWithCpp:(const std::shared_ptr<$cppSelf>&)cppRef;")
w.wl
w.wl("@end")
w.wl
w.wl(s"@implementation $objcSelf")
w.wl(s"@implementation $objcSelf {")
w.wl(s" ::djinni::DbxCppWrapperCache<$cppSelf>::Handle _cppRefHandle;")
w.wl("}")
w.wl
w.wl(s"- (id)initWithCpp:(const std::shared_ptr<$cppSelf>&)cppRef")
w.braced {
w.w("if (self = [super init])").braced {
w.wl("_cppRef.assign(cppRef);")
w.wl("_cppRefHandle.assign(cppRef);")
}
w.wl("return self;")
}
......@@ -149,15 +149,13 @@ class ObjcppGenerator(spec: Spec) extends Generator(spec) {
w.braced {
w.w("try").bracedEnd(" DJINNI_TRANSLATE_EXCEPTIONS()") {
val ret = m.ret.fold("")(_ => "auto r = ")
val call = ret + (if (!m.static) "_cppRef.get()->" else cppSelf + "::") + idCpp.method(m.ident) + "("
val call = ret + (if (!m.static) "_cppRefHandle.get()->" else cppSelf + "::") + idCpp.method(m.ident) + "("
writeAlignedCall(w, call, m.params, ")", p => objcppMarshal.toCpp(p.ty, idObjc.local(p.ident.name)))
w.wl(";")
m.ret.fold()(r => w.wl(s"return ${objcppMarshal.fromCpp(r, "r")};"))
}
}
}
w.wl
w.wl("@end")
}
if (i.ext.objc) {
......@@ -198,14 +196,14 @@ class ObjcppGenerator(spec: Spec) extends Generator(spec) {
if (i.ext.cpp && !i.ext.objc) {
// C++ only. In this case we generate a class instead of a protocol, so
// we don't have to do any casting at all, just access cppRef directly.
w.wl(s"return objc.cppRef.get();")
w.wl(s"return objc->_cppRefHandle.get();")
} else {
// ObjC only, or ObjC and C++.
val objcExtSelf = objcppMarshal.helperClass("objc_proxy")
if (i.ext.cpp) {
// If it could be implemented in C++, we might have to unwrap a proxy object.
w.w(s"if ([(id)objc isKindOfClass:[$objcSelf class]])").braced {
w.wl(s"return (($objcSelf*)objc).cppRef.get();")
w.wl(s"return (($objcSelf*)objc)->_cppRefHandle.get();")
}
}
w.wl(s"return ::djinni::DbxObjcWrapperCache<$objcExtSelf>::getInstance()->get(objc);")
......@@ -237,6 +235,11 @@ class ObjcppGenerator(spec: Spec) extends Generator(spec) {
}
}
})
if (i.ext.cpp) {
w.wl
w.wl("@end")
}
})
}
......
......@@ -63,6 +63,8 @@ public:
class Handle {
public:
Handle() = default;
Handle(const Handle &) = delete;
Handle & operator=(const Handle &) = delete;
~Handle() {
if (_ptr) {
_cache->remove(_ptr);
......
......@@ -55,7 +55,9 @@ public:
class Handle {
public:
Handle(id obj) : _obj(obj) { };
Handle(id obj) : _obj(obj) { }
Handle(const Handle &) = delete;
Handle & operator=(const Handle &) = delete;
~Handle() {
if (_obj) {
_cache->remove(_obj);
......
......@@ -13,30 +13,28 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
@interface DBConstantsInterface ()
@property (nonatomic, readonly) ::djinni::DbxCppWrapperCache<::ConstantsInterface>::Handle cppRef;
- (id)initWithCpp:(const std::shared_ptr<::ConstantsInterface>&)cppRef;
@end
@implementation DBConstantsInterface
@implementation DBConstantsInterface {
::djinni::DbxCppWrapperCache<::ConstantsInterface>::Handle _cppRefHandle;
}
- (id)initWithCpp:(const std::shared_ptr<::ConstantsInterface>&)cppRef
{
if (self = [super init]) {
_cppRef.assign(cppRef);
_cppRefHandle.assign(cppRef);
}
return self;
}
- (void)dummy {
try {
_cppRef.get()->dummy();
_cppRefHandle.get()->dummy();
} DJINNI_TRANSLATE_EXCEPTIONS()
}
@end
namespace djinni_generated {
auto ConstantsInterface::toCpp(ObjcType objc) -> CppType
......@@ -44,7 +42,7 @@ auto ConstantsInterface::toCpp(ObjcType objc) -> CppType
if (!objc) {
return nullptr;
}
return objc.cppRef.get();
return objc->_cppRefHandle.get();
}
auto ConstantsInterface::fromCpp(const CppType& cpp) -> ObjcType
......@@ -58,3 +56,5 @@ auto ConstantsInterface::fromCpp(const CppType& cpp) -> ObjcType
}
} // namespace djinni_generated
@end
......@@ -14,25 +14,25 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
@interface DBCppException ()
@property (nonatomic, readonly) ::djinni::DbxCppWrapperCache<::CppException>::Handle cppRef;
- (id)initWithCpp:(const std::shared_ptr<::CppException>&)cppRef;
@end
@implementation DBCppException
@implementation DBCppException {
::djinni::DbxCppWrapperCache<::CppException>::Handle _cppRefHandle;
}
- (id)initWithCpp:(const std::shared_ptr<::CppException>&)cppRef
{
if (self = [super init]) {
_cppRef.assign(cppRef);
_cppRefHandle.assign(cppRef);
}
return self;
}
- (int32_t)throwAnException {
try {
auto r = _cppRef.get()->throw_an_exception();
auto r = _cppRefHandle.get()->throw_an_exception();
return ::djinni::I32::fromCpp(r);
} DJINNI_TRANSLATE_EXCEPTIONS()
}
......@@ -44,8 +44,6 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
} DJINNI_TRANSLATE_EXCEPTIONS()
}
@end
namespace djinni_generated {
auto CppException::toCpp(ObjcType objc) -> CppType
......@@ -53,7 +51,7 @@ auto CppException::toCpp(ObjcType objc) -> CppType
if (!objc) {
return nullptr;
}
return objc.cppRef.get();
return objc->_cppRefHandle.get();
}
auto CppException::fromCpp(const CppType& cpp) -> ObjcType
......@@ -67,3 +65,5 @@ auto CppException::fromCpp(const CppType& cpp) -> ObjcType
}
} // namespace djinni_generated
@end
......@@ -14,31 +14,29 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
@interface DBExternInterface1 ()
@property (nonatomic, readonly) ::djinni::DbxCppWrapperCache<::ExternInterface1>::Handle cppRef;
- (id)initWithCpp:(const std::shared_ptr<::ExternInterface1>&)cppRef;
@end
@implementation DBExternInterface1
@implementation DBExternInterface1 {
::djinni::DbxCppWrapperCache<::ExternInterface1>::Handle _cppRefHandle;
}
- (id)initWithCpp:(const std::shared_ptr<::ExternInterface1>&)cppRef
{
if (self = [super init]) {
_cppRef.assign(cppRef);
_cppRefHandle.assign(cppRef);
}
return self;
}
- (nonnull DBClientReturnedRecord *)foo:(nullable id<DBClientInterface>)i {
try {
auto r = _cppRef.get()->foo(::djinni_generated::ClientInterface::toCpp(i));
auto r = _cppRefHandle.get()->foo(::djinni_generated::ClientInterface::toCpp(i));
return ::djinni_generated::ClientReturnedRecord::fromCpp(r);
} DJINNI_TRANSLATE_EXCEPTIONS()
}
@end
namespace djinni_generated {
auto ExternInterface1::toCpp(ObjcType objc) -> CppType
......@@ -46,7 +44,7 @@ auto ExternInterface1::toCpp(ObjcType objc) -> CppType
if (!objc) {
return nullptr;
}
return objc.cppRef.get();
return objc->_cppRefHandle.get();
}
auto ExternInterface1::fromCpp(const CppType& cpp) -> ObjcType
......@@ -60,3 +58,5 @@ auto ExternInterface1::fromCpp(const CppType& cpp) -> ObjcType
}
} // namespace djinni_generated
@end
......@@ -14,18 +14,18 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
@interface DBTestDuration ()
@property (nonatomic, readonly) ::djinni::DbxCppWrapperCache<::TestDuration>::Handle cppRef;
- (id)initWithCpp:(const std::shared_ptr<::TestDuration>&)cppRef;
@end
@implementation DBTestDuration
@implementation DBTestDuration {
::djinni::DbxCppWrapperCache<::TestDuration>::Handle _cppRefHandle;
}
- (id)initWithCpp:(const std::shared_ptr<::TestDuration>&)cppRef
{
if (self = [super init]) {
_cppRef.assign(cppRef);
_cppRefHandle.assign(cppRef);
}
return self;
}
......@@ -170,8 +170,6 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
} DJINNI_TRANSLATE_EXCEPTIONS()
}
@end
namespace djinni_generated {
auto TestDuration::toCpp(ObjcType objc) -> CppType
......@@ -179,7 +177,7 @@ auto TestDuration::toCpp(ObjcType objc) -> CppType
if (!objc) {
return nullptr;
}
return objc.cppRef.get();
return objc->_cppRefHandle.get();
}
auto TestDuration::fromCpp(const CppType& cpp) -> ObjcType
......@@ -193,3 +191,5 @@ auto TestDuration::fromCpp(const CppType& cpp) -> ObjcType
}
} // namespace djinni_generated
@end
......@@ -20,18 +20,18 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
@interface DBTestHelpers ()
@property (nonatomic, readonly) ::djinni::DbxCppWrapperCache<::TestHelpers>::Handle cppRef;
- (id)initWithCpp:(const std::shared_ptr<::TestHelpers>&)cppRef;
@end
@implementation DBTestHelpers
@implementation DBTestHelpers {
::djinni::DbxCppWrapperCache<::TestHelpers>::Handle _cppRefHandle;
}
- (id)initWithCpp:(const std::shared_ptr<::TestHelpers>&)cppRef
{
if (self = [super init]) {
_cppRef.assign(cppRef);
_cppRefHandle.assign(cppRef);
}
return self;
}
......@@ -200,8 +200,6 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
} DJINNI_TRANSLATE_EXCEPTIONS()
}
@end
namespace djinni_generated {
auto TestHelpers::toCpp(ObjcType objc) -> CppType
......@@ -209,7 +207,7 @@ auto TestHelpers::toCpp(ObjcType objc) -> CppType
if (!objc) {
return nullptr;
}
return objc.cppRef.get();
return objc->_cppRefHandle.get();
}
auto TestHelpers::fromCpp(const CppType& cpp) -> ObjcType
......@@ -223,3 +221,5 @@ auto TestHelpers::fromCpp(const CppType& cpp) -> ObjcType
}
} // namespace djinni_generated
@end
......@@ -14,31 +14,29 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
@interface DBUserTokenCppProxy : NSObject<DBUserToken>
@property (nonatomic, readonly) ::djinni::DbxCppWrapperCache<::UserToken>::Handle cppRef;
- (id)initWithCpp:(const std::shared_ptr<::UserToken>&)cppRef;
@end
@implementation DBUserTokenCppProxy
@implementation DBUserTokenCppProxy {
::djinni::DbxCppWrapperCache<::UserToken>::Handle _cppRefHandle;
}
- (id)initWithCpp:(const std::shared_ptr<::UserToken>&)cppRef
{
if (self = [super init]) {
_cppRef.assign(cppRef);
_cppRefHandle.assign(cppRef);
}
return self;
}
- (nonnull NSString *)whoami {
try {
auto r = _cppRef.get()->whoami();
auto r = _cppRefHandle.get()->whoami();
return ::djinni::String::fromCpp(r);
} DJINNI_TRANSLATE_EXCEPTIONS()
}
@end
namespace djinni_generated {
class UserToken::ObjcProxy final
......@@ -66,7 +64,7 @@ auto UserToken::toCpp(ObjcType objc) -> CppType
return nullptr;
}
if ([(id)objc isKindOfClass:[DBUserTokenCppProxy class]]) {
return ((DBUserTokenCppProxy*)objc).cppRef.get();
return ((DBUserTokenCppProxy*)objc)->_cppRefHandle.get();
}
return ::djinni::DbxObjcWrapperCache<ObjcProxy>::getInstance()->get(objc);
}
......@@ -85,3 +83,5 @@ auto UserToken::fromCpp(const CppType& cpp) -> ObjcType
}
} // namespace djinni_generated
@end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment