Classes/Structs¶
Let's generate a binding code for the following custom type.
/**
* Class holding task information.
* \internal
* internal comment should not go to wrappers
* __API__
* action: gen_class
* shared_ref: False
* swift.package: simple
* python.package: simple.task
* python.name: PyTask
*/
class Task {
public:
Task(){};
/**
* Task Constructor.
* __API__
* action: gen_constructor
* throws: no_throw
*
*/
Task(const std::string& title) : _title(title) {};
/**
* Get objects title.
* __API__
* action: gen_getter
* throws: no_throw
*/
std::string& title() {
return _title;
};
/**
* Get objects title.
* __API__
* action: gen_method
* throws: no_throw
*/
void setTitle(const std::string& title) {
_title = title;
};
/**
* __API__
* action: gen_method
* throws: no_throw
*/
bool equals(Task* t) const {
return this == t;
}
protected:
std::string _title;
};
In the code above, we are exporting all public members and constructors.
Note
The title method is marked as a getter, a read-only property for the target language.
Note
By default, CppBind generates System.loadLibrary("wrapper_jni")
for
each Kotlin class. To disable this behavior define
c_wrapper_lib_name with null value: c_wrapper_lib_name: null or
c_wrapper_lib_name:.
Usage examples:
val task = Task("My Task")
assert(task.title == "My Task")
task = Task("My Task")
assert task.title == "My Task"
let task = Task(title: "My Task")
assert(task.title == "My Task")
Generated code for the target languages
"""
______ .______ .______ .______ __ .__ __. _______
/ || _ \ | _ \ | _ \ | | | \ | | | \
| ,----'| |_) | | |_) | | |_) | | | | \| | | .--. |
| | | ___/ | ___/ | _ < | | | . ` | | | | |
| `----.| | | | | |_) | | | | |\ | | '--' |
\______|| _| | _| |______/ |__| |__| \__| |_______/
This file is generated by cppbind on 08/11/2022-07:34.
Please do not change it manually.
"""
from __future__ import annotations
from typing import *
import examples.simple.task.task as pybind_task_pygen
from examples_lib.cppbind.cppbind_metaclass_pygen import *
from examples_lib.cppbind.cppbind_utils_pygen import *
class PyTask(metaclass=CppBindMetaclass):
"""
Class holding task information.
"""
@bind
def __init__(self, title: str):
"""
Task Constructor.
"""
pass
@property
@bind
def title(self) -> str:
"""
Get objects title.
"""
pass
@bind
def set_title(self, title: str) -> None:
"""
Get objects title.
"""
pass
@bind
def __eq__(self, t: PyTask) -> bool:
pass
@bind
def __repr__(self) -> str:
"""
CppBind generated __repr__ method returning underlying C++ object type and id.
"""
pass
/**
* ______ .______ .______ .______ __ .__ __. _______
* / || _ \ | _ \ | _ \ | | | \ | | | \
* | ,----'| |_) | | |_) | | |_) | | | | \| | | .--. |
* | | | ___/ | ___/ | _ < | | | . ` | | | | |
* | `----.| | | | | |_) | | | | |\ | | '--' |
* \______|| _| | _| |______/ |__| |__| \__| |_______/
*
* This file is generated by cppbind on 08/14/2022-10:58.
* Please do not change it manually.
*/
import CWrapper
import Foundation
/// Class holding task information.
public class Task: Equatable {
/// An internal property to keep a reference to the original C++ object.
/// It is intended to be used by the generated code.
public let cself: CppBindCObject
/// An internal property to keep track whether Swift is responsible for deallocating the underlying C++ object or not.
/// It is intended to be used by the generated code.
public let owner: Bool
private var refs: [Any]
/// internal main initializer
internal required init(_ _cself: CppBindCObject, _ _owner: Bool = false) {
self.cself = _cself
self.owner = _owner
self.refs = []
}
deinit {
release_CppbindExample_Task(cself, owner)
}
/// An internal method to bind the lifetimes of the current and another object.
/// It is intended to be used by the generated code.
public func keepCppBindReference(_ object: Any) {
self.refs.append(object)
}
/// Task Constructor.
public convenience init(title: String) {
let swifttosctitle = strdup(title)!
var cppbindErr = CppBindCObject()
self.init(create_CppbindExample_Task(swifttosctitle, &cppbindErr), true)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
}
/// Get objects title.
public var title: String {
var cppbindErr = CppBindCObject()
let result = _prop_get_CppbindExample_Task_title(cself, &cppbindErr)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
let sctoswiftresult = String(cString: result)
defer {
result.deallocate()
}
return sctoswiftresult
}
/// Get objects title.
public func setTitle(title: String) -> Void {
let swifttosctitle = strdup(title)!
var cppbindErr = CppBindCObject()
_func_CppbindExample_Task_setTitle(cself, swifttosctitle, &cppbindErr)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
}
public static func == (lhs: Task, rhs: Task) -> Bool {
var cppbindErr = CppBindCObject()
let result = _func_CppbindExample_Task_equals(lhs.cself, rhs.cself, &cppbindErr)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
return result
}
/// An internal property to keep an information about the underlying C++ object type.
/// It is intended to be used by the generated code.
class var cppbindCxxTypeName : String { return "cppbind::example::Task" }
}
Let's assume we have another class using the one above.
/**
* Class holding project information.
* __API__
* action: gen_class
* shared_ref: False
* package: simple
*/
class Project {
public:
/**
* Project constructor.
* __API__
* action: gen_constructor
* throws: no_throw
*/
Project(const std::string& title) : _title(title) {};
/**
* Get project´s title.
* __API__
* action: gen_getter
* throws: no_throw
*/
std::string& title() {
return _title;
};
/**
* Add a task to project.
* __API__
* action: gen_method
* throws: no_throw
*/
void addTask(Task* task) {
_tasks.push_back(task);
}
/**
* Get project´s tasks.
* __API__
* action: gen_method
* throws: no_throw
* return_value_policy: reference
*/
const std::vector<Task*>& tasks() const {
return _tasks;
}
/**
* __API__
* action: gen_method
* throws: no_throw
*/
bool equals(Project* p) const {
return this == p;
}
private:
std::vector<Task*> _tasks;
std::string _title;
};
And the usage example:
val title = "My Project"
val prj = Project(title)
assert(prj.title == title)
val task1 = Task("My Task")
prj.addTask(task1)
assert(prj.tasks().size == 1)
assert(prj.tasks()[0].title == task1.title)
title = 'My Project'
prj = Project(title=title)
assert prj.title == title
task1 = Task('First Task')
prj.add_task(task1)
task2 = Task('Second Task')
prj.add_task(task2)
assert prj.tasks() == [task1, task2]
let title = "My Project"
let prj = Project(title: title)
assert(prj.title == title)
let task1 = Task(title: "My Task")
prj.addTask(task: task1)
assert(prj.tasks().count == 1)
assert(prj.tasks()[0].title == task1.title)
Here are the generated bindings
/**
* ______ .______ .______ .______ __ .__ __. _______
* / || _ \ | _ \ | _ \ | | | \ | | | \
* | ,----'| |_) | | |_) | | |_) | | | | \| | | .--. |
* | | | ___/ | ___/ | _ < | | | . ` | | | | |
* | `----.| | | | | |_) | | | | |\ | | '--' |
* \______|| _| | _| |______/ |__| |__| \__| |_______/
*
* This file is generated by cppbind on 11/10/2022-06:59.
* Please do not change it manually.
*/
package com.examples.simple
import com.examples.cppbind.*
import com.examples.cppbind.exceptions.*
import com.examples.simple.task.Task
/**
* Class holding project information.
*/
open class Project
internal constructor(obj: CppBindObject) : AutoCloseable {
companion object {
init {
System.loadLibrary("wrapper_jni")
}
/**
* Project constructor.
*/
protected fun constructHelper(title: String): Long {
val id = jConstructor(title)
return id
}
@JvmStatic
private external fun jConstructor(title: String, vararg extraObjs: Any?): Long
/**
* An internal property to keep an information about the underlying C++ object type.
* It is intended to be used by the generated code.
*/
const val cppbindCxxTypeName: String = "cppbind::example::Project"
}
protected var cppbindObj = obj
private var refs: MutableList<Any> = mutableListOf()
/**
* An internal method to bind the lifetimes of the current and another object.
* It is intended to be used by the generated code.
*/
fun keepCppBindReference(ref: Any) {
refs.add(ref)
}
/**
* An internal getter to get the id of an object.
* It is intended to be used by the generated code.
*/
open val cppbindObjId: Long
get() {
if (cppbindObj.id == 0L) {
throw RuntimeException("Object is not allocated")
}
return cppbindObj.id
}
/**
* An internal property returning underlying C++ object id.
* It is intended to be used by the generated code.
*/
internal val cxxId: Long by lazy {
jGetcxxid(cppbindObj.id)
}
/**
* An internal property returning underlying C++ type name.
* It is intended to be used by the generated code.
*/
internal val cxxTypeName: String by lazy {
jGettypebyid(cppbindObj.id)
}
/**
* Project constructor.
*/
constructor(title: String): this(CppBindObject(constructHelper(title), true)) {
}
/**
* Get project´s title.
*/
val title: String
get() {
val result = jTitle(cppbindObjId)
return result
}
/**
* Add a task to project.
*/
fun addTask(task: Task): Unit {
val kotlintojdktask = task.cppbindObjId
val result = jAddtask(cppbindObjId, kotlintojdktask, task)
return result
}
/**
* Get project´s tasks.
*/
fun tasks(): List<Task> {
val result = jTasks(cppbindObjId)
val jdktokotlinresult: MutableList<Task> = mutableListOf()
for (valueResult in result) {
val jdktokotlinvalueResult = Task(CppBindObject(valueResult))
jdktokotlinresult.add(jdktokotlinvalueResult)
}
return jdktokotlinresult
}
override fun equals(other: Any?): Boolean {
other as Project
return jEquals(cppbindObjId, other.cppbindObjId, other)
}
/**
* CppBind generated hashCode method returning the hash of underlying C++ object id.
*/
override fun hashCode(): Int {
return cxxId.hashCode()
}
/**
* CppBind generated toString method returning underlying C++ object type and id.
*/
override fun toString(): String {
return "<0x$cxxId: $cxxTypeName>"
}
override fun close() {
if (cppbindObj.owner && cppbindObj.id != 0L) {
jFinalize(cppbindObj.id)
cppbindObj.id = 0L
}
}
/**
* Finalize and deletes the object
*/
protected fun finalize() {
close()
}
///// External wrapper functions ////////////
private external fun jTitle(id: Long): String
private external fun jAddtask(id: Long, task: Long, vararg extraObjs: Any?): Unit
private external fun jTasks(id: Long): LongArray
private external fun jEquals(id: Long, p: Long, vararg extraObjs: Any?): Boolean
private external fun jFinalize(id: Long): Unit
private external fun jGetcxxid(id: Long): Long
}
private external fun jGettypebyid(id: Long): String
"""
______ .______ .______ .______ __ .__ __. _______
/ || _ \ | _ \ | _ \ | | | \ | | | \
| ,----'| |_) | | |_) | | |_) | | | | \| | | .--. |
| | | ___/ | ___/ | _ < | | | . ` | | | | |
| `----.| | | | | |_) | | | | |\ | | '--' |
\______|| _| | _| |______/ |__| |__| \__| |_______/
This file is generated by cppbind on 11/24/2022-07:17.
Please do not change it manually.
"""
from __future__ import annotations
from typing import *
import examples.simple.project as pybind_project_pygen
from examples_lib.cppbind.cppbind_metaclass_pygen import *
from examples_lib.cppbind.cppbind_utils_pygen import *
if TYPE_CHECKING:
import examples_lib.simple.task.task_pygen as task_task_pygen
class Project(metaclass=CppBindMetaclass):
"""
Class holding project information.
"""
@bind
def __init__(self, title: str):
"""
Project constructor.
"""
pass
@property
@bind
def title(self) -> str:
"""
Get project´s title.
"""
pass
@bind
def add_task(self, task: task_task_pygen.PyTask) -> None:
"""
Add a task to project.
"""
pass
@bind
def tasks(self) -> List[task_task_pygen.PyTask]:
"""
Get project´s tasks.
"""
pass
@bind
def __eq__(self, p: Project) -> bool:
pass
@bind
def __repr__(self) -> str:
"""
CppBind generated __repr__ method returning underlying C++ object type and id.
"""
pass
/**
* ______ .______ .______ .______ __ .__ __. _______
* / || _ \ | _ \ | _ \ | | | \ | | | \
* | ,----'| |_) | | |_) | | |_) | | | | \| | | .--. |
* | | | ___/ | ___/ | _ < | | | . ` | | | | |
* | `----.| | | | | |_) | | | | |\ | | '--' |
* \______|| _| | _| |______/ |__| |__| \__| |_______/
*
* This file is generated by cppbind on 08/14/2022-10:58.
* Please do not change it manually.
*/
import CWrapper
import Foundation
/// Class holding project information.
public class Project: Equatable {
/// An internal property to keep a reference to the original C++ object.
/// It is intended to be used by the generated code.
public let cself: CppBindCObject
/// An internal property to keep track whether Swift is responsible for deallocating the underlying C++ object or not.
/// It is intended to be used by the generated code.
public let owner: Bool
private var refs: [Any]
/// internal main initializer
internal required init(_ _cself: CppBindCObject, _ _owner: Bool = false) {
self.cself = _cself
self.owner = _owner
self.refs = []
}
deinit {
release_CppbindExample_Project(cself, owner)
}
/// An internal method to bind the lifetimes of the current and another object.
/// It is intended to be used by the generated code.
public func keepCppBindReference(_ object: Any) {
self.refs.append(object)
}
/// Project constructor.
public convenience init(title: String) {
let swifttosctitle = strdup(title)!
var cppbindErr = CppBindCObject()
self.init(create_CppbindExample_Project(swifttosctitle, &cppbindErr), true)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
}
/// Get project´s title.
public var title: String {
var cppbindErr = CppBindCObject()
let result = _prop_get_CppbindExample_Project_title(cself, &cppbindErr)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
let sctoswiftresult = String(cString: result)
defer {
result.deallocate()
}
return sctoswiftresult
}
/// Add a task to project.
public func addTask(task: Task) -> Void {
let swifttosctask = task.cself
var cppbindErr = CppBindCObject()
_func_CppbindExample_Project_addTask(cself, swifttosctask, &cppbindErr)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
}
/// Get project´s tasks.
public func tasks() -> Array<Task> {
var cppbindErr = CppBindCObject()
let result = _func_CppbindExample_Project_tasks(cself, &cppbindErr)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
let _resultBuffer = UnsafeBufferPointer<CppBindCObject>(
start: result.data.assumingMemoryBound(to: CppBindCObject.self),
count: Int(result.size))
var sctoswiftresult: [Task] = []
defer {
_resultBuffer.deallocate()
}
for i in 0..<Int(result.size) {
let resultValue = _resultBuffer[i]
var sctoswiftresultValue: Task
sctoswiftresultValue = Task(resultValue)
sctoswiftresult.append(sctoswiftresultValue)
}
return sctoswiftresult
}
public static func == (lhs: Project, rhs: Project) -> Bool {
var cppbindErr = CppBindCObject()
let result = _func_CppbindExample_Project_equals(lhs.cself, rhs.cself, &cppbindErr)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
return result
}
/// An internal property to keep an information about the underlying C++ object type.
/// It is intended to be used by the generated code.
class var cppbindCxxTypeName : String { return "cppbind::example::Project" }
}
Factory method as a constructor¶
CppBind allows generating a constructor from a factory method by just annotating it with gen_constructor. Here's an example:
/**
* __API__
* action: gen_class
* shared_ref: True
* package: factories
*/
class School {
public:
/**
* __API__
* action: gen_constructor
* throws: no_throw
* keep_alive: [1]
*/
static std::shared_ptr<School> create(const std::vector<Teacher*>& teachers);
/**
* __API__
* action: gen_constructor
* throws: no_throw
*/
static std::shared_ptr<School> create();
/**
* __API__
* action: gen_method
* throws: no_throw
* keep_alive: [1]
*/
void addTeacher(Teacher* teacher);
/**
* __API__
* action: gen_getter
* throws: no_throw
*/
const std::vector<Teacher*>& teachers();
private:
School() {}
School(const School&) = default;
private:
std::vector<Teacher*> _teachers;
};
Here we have two overloaded factory methods marked with gen_constructor. CppBind generates two constructors corresponding to them.
Here's a small usage example:
val school1 = School()
val teacher = Teacher()
school1.addTeacher(teacher)
assert(school1.teachers.size == 1)
val school2 = School(listOf(teacher))
assert(school2.teachers.size == 1)
school1 = School()
teacher = Teacher()
school1.add_teacher(teacher)
assert len(school1.teachers) == 1
school2 = School([teacher])
assert len(school2.teachers) == 1
let school1 = School()
let teacher = Teacher()
school1.addTeacher(teacher: teacher)
assert(school1.teachers.count == 1)
let school2 = School(teachers: [teacher])
assert(school2.teachers.count == 1)
The generated bindings
/**
* ______ .______ .______ .______ __ .__ __. _______
* / || _ \ | _ \ | _ \ | | | \ | | | \
* | ,----'| |_) | | |_) | | |_) | | | | \| | | .--. |
* | | | ___/ | ___/ | _ < | | | . ` | | | | |
* | `----.| | | | | |_) | | | | |\ | | '--' |
* \______|| _| | _| |______/ |__| |__| \__| |_______/
*
* This file is generated by cppbind on 08/18/2022-11:02.
* Please do not change it manually.
*/
package com.examples.factories
import com.examples.cppbind.*
import com.examples.cppbind.exceptions.*
import com.examples.forward_decl.teacher.Teacher
open class School
internal constructor(obj: CppBindObject) : AutoCloseable {
companion object {
init {
System.loadLibrary("wrapper_jni")
}
protected fun constructHelper(teachers: List<Teacher>): Long {
val kotlintojdkteachers = LongArray(teachers.size)
var indexTeachers = 0
for (valueTeachers in teachers) {
val kotlintojdkvalueTeachers = valueTeachers.cppbindObjId
kotlintojdkteachers[indexTeachers] = kotlintojdkvalueTeachers
++indexTeachers
}
val id = jCreate(kotlintojdkteachers, teachers)
return id
}
@JvmStatic
private external fun jCreate(teachers: LongArray, vararg extraObjs: Any?): Long
protected fun constructHelper(): Long {
val id = jCreate1()
return id
}
@JvmStatic
private external fun jCreate1(): Long
/**
* An internal property to keep an information about the underlying C++ object type.
* It is intended to be used by the generated code.
*/
const val cppbindCxxTypeName: String = "cppbind::example::School"
}
protected var cppbindObj = obj
private var refs: MutableList<Any> = mutableListOf()
/**
* An internal method to bind the lifetimes of the current and another object.
* It is intended to be used by the generated code.
*/
fun keepCppBindReference(ref: Any) {
refs.add(ref)
}
/**
* An internal getter to get the id of an object.
* It is intended to be used by the generated code.
*/
open val cppbindObjId: Long
get() {
if (cppbindObj.id == 0L) {
throw RuntimeException("Object is not allocated")
}
return cppbindObj.id
}
/**
* An internal property returning underlying C++ object id.
* It is intended to be used by the generated code.
*/
internal val cxxId: Long by lazy {
jGetcxxid(cppbindObj.id)
}
/**
* An internal property returning underlying C++ type name.
* It is intended to be used by the generated code.
*/
internal val cxxTypeName: String by lazy {
jGettypebyid(cppbindObj.id)
}
constructor(teachers: List<Teacher>): this(CppBindObject(constructHelper(teachers), true)) {
this.keepCppBindReference(teachers)
}
constructor(): this(CppBindObject(constructHelper(), true)) {
}
val teachers: List<Teacher>
get() {
val result = jTeachers(cppbindObjId)
val jdktokotlinresult: MutableList<Teacher> = mutableListOf()
for (valueResult in result) {
val jdktokotlinvalueResult = Teacher(CppBindObject(valueResult))
jdktokotlinresult.add(jdktokotlinvalueResult)
}
for (valuejdktokotlinresult in jdktokotlinresult) {
valuejdktokotlinresult.keepCppBindReference(this)
}
return jdktokotlinresult
}
fun addTeacher(teacher: Teacher): Unit {
val kotlintojdkteacher = teacher.cppbindObjId
val result = jAddteacher(cppbindObjId, kotlintojdkteacher, teacher)
this.keepCppBindReference(teacher)
return result
}
/**
* CppBind generated hashCode method returning the hash of underlying C++ object id.
*/
override fun hashCode(): Int {
return cxxId.hashCode()
}
/**
* CppBind generated equals method comparing the underlying C++ object ids.
*/
override fun equals(other: Any?): Boolean {
other as School
return cxxId == other.cxxId
}
/**
* CppBind generated toString method returning underlying C++ object type and id.
*/
override fun toString(): String {
return "<0x$cxxId: $cxxTypeName>"
}
override fun close() {
if (cppbindObj.owner && cppbindObj.id != 0L) {
jFinalize(cppbindObj.id)
cppbindObj.id = 0L
}
}
/**
* Finalize and deletes the object
*/
protected fun finalize() {
close()
}
///// External wrapper functions ////////////
private external fun jAddteacher(id: Long, teacher: Long, vararg extraObjs: Any?): Unit
private external fun jTeachers(id: Long): LongArray
private external fun jFinalize(id: Long): Unit
private external fun jGetcxxid(id: Long): Long
}
private external fun jGettypebyid(id: Long): String
"""
______ .______ .______ .______ __ .__ __. _______
/ || _ \ | _ \ | _ \ | | | \ | | | \
| ,----'| |_) | | |_) | | |_) | | | | \| | | .--. |
| | | ___/ | ___/ | _ < | | | . ` | | | | |
| `----.| | | | | |_) | | | | |\ | | '--' |
\______|| _| | _| |______/ |__| |__| \__| |_______/
This file is generated by cppbind on 11/24/2022-07:17.
Please do not change it manually.
"""
from __future__ import annotations
from typing import *
import examples.factories.school as pybind_school_pygen
from examples_lib.cppbind.cppbind_metaclass_pygen import *
from examples_lib.cppbind.cppbind_utils_pygen import *
if TYPE_CHECKING:
import examples_lib.forward_decl.teacher_pygen as forward_decl_teacher_pygen
class School(metaclass=CppBindMetaclass):
@bind
def __init__(self, teachers: List[forward_decl_teacher_pygen.Teacher]):
pass
@bind
def __init__(self):
pass
@property
@bind
def teachers(self) -> List[forward_decl_teacher_pygen.Teacher]:
pass
@bind
def add_teacher(self, teacher: forward_decl_teacher_pygen.Teacher) -> None:
pass
@bind
def __repr__(self) -> str:
"""
CppBind generated __repr__ method returning underlying C++ object type and id.
"""
pass
/**
* ______ .______ .______ .______ __ .__ __. _______
* / || _ \ | _ \ | _ \ | | | \ | | | \
* | ,----'| |_) | | |_) | | |_) | | | | \| | | .--. |
* | | | ___/ | ___/ | _ < | | | . ` | | | | |
* | `----.| | | | | |_) | | | | |\ | | '--' |
* \______|| _| | _| |______/ |__| |__| \__| |_______/
*
* This file is generated by cppbind on 08/14/2022-10:58.
* Please do not change it manually.
*/
import CWrapper
import Foundation
public class School {
/// An internal property to keep a reference to the original C++ object.
/// It is intended to be used by the generated code.
public let cself: CppBindCObject
/// An internal property to keep track whether Swift is responsible for deallocating the underlying C++ object or not.
/// It is intended to be used by the generated code.
public let owner: Bool
private var refs: [Any]
/// internal main initializer
internal required init(_ _cself: CppBindCObject, _ _owner: Bool = false) {
self.cself = _cself
self.owner = _owner
self.refs = []
}
deinit {
release_CppbindExample_School(cself, owner)
}
/// An internal method to bind the lifetimes of the current and another object.
/// It is intended to be used by the generated code.
public func keepCppBindReference(_ object: Any) {
self.refs.append(object)
}
public convenience init(teachers: Array<Teacher>) {
let _swifttoscteachersData = UnsafeMutablePointer<CppBindCObject>.allocate(capacity: teachers.count)
var swifttoscteachers = CppBindCDataArray()
swifttoscteachers.data = UnsafeMutableRawPointer(_swifttoscteachersData)
swifttoscteachers.size = Int64(teachers.count)
for i in 0..<teachers.count {
let teachersVal = teachers[i]
let swifttoscteachersVal = teachersVal.cself
_swifttoscteachersData[i] = swifttoscteachersVal
}
var cppbindErr = CppBindCObject()
self.init(func_CppbindExample_School_create(swifttoscteachers, &cppbindErr), true)
self.keepCppBindReference(teachers)
swifttoscteachers.data.deallocate()
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
}
public convenience init() {
var cppbindErr = CppBindCObject()
self.init(func_CppbindExample_School_create1(&cppbindErr), true)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
}
public var teachers: Array<Teacher> {
var cppbindErr = CppBindCObject()
let result = _prop_get_CppbindExample_School_teachers(cself, &cppbindErr)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
let _resultBuffer = UnsafeBufferPointer<CppBindCObject>(
start: result.data.assumingMemoryBound(to: CppBindCObject.self),
count: Int(result.size))
var sctoswiftresult: [Teacher] = []
defer {
_resultBuffer.deallocate()
}
for i in 0..<Int(result.size) {
let resultValue = _resultBuffer[i]
var sctoswiftresultValue: Teacher
sctoswiftresultValue = Teacher(resultValue)
sctoswiftresult.append(sctoswiftresultValue)
}
for valuesctoswiftresult in sctoswiftresult {
valuesctoswiftresult.keepCppBindReference(self)
}
return sctoswiftresult
}
public func addTeacher(teacher: Teacher) -> Void {
let swifttoscteacher = teacher.cself
var cppbindErr = CppBindCObject()
_func_CppbindExample_School_addTeacher(cself, swifttoscteacher, &cppbindErr)
self.keepCppBindReference(teacher)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
}
/// An internal property to keep an information about the underlying C++ object type.
/// It is intended to be used by the generated code.
class var cppbindCxxTypeName : String { return "cppbind::example::School" }
}
Special methods¶
Many languages have a base class from which other classes are derived.
This class contains methods like hash
, equals
, toString
, etc. For
the languages which have this base class, CppBind generates
implementations for hash
, equals
, toString
. This is done to make
sure the objects which are wrapping the same C++ instance are equal and
have the same value for hash
and toString
.
The generated methods are implemented in the following way:
- hash - returns the hash value of underlying C++ object address.
- equals - returns whether the underlying C++ objects are identical or not.
- toString - returns a string containing type and address of the underlying C++ object.
Users can define their own implementations by adding hash
, equals
and toString
to C++ classes. The names of these methods can be
configured with hash_method, equals_method and
tostring_method variables. More on variables can be found
here.
Let's see an example:
/**
* Class with custom hash, equals and toString methods.
* __API__
* action: gen_class
* package: hashing
*/
class Object1 {
public:
/**
* __API__
* action: gen_constructor
* throws: no_throw
*/
Object1(int value=0): _value(value) {}
/**
* __API__
* action: gen_getter
* throws: no_throw
*/
int value() const {
return _value;
}
/**
* Hash function returning dummy value for testing purposes.
* __API__
* action: gen_method
* throws: no_throw
*/
virtual size_t hash() const {
return std::hash<int>{}(_value);
}
/**
* Equals function returning dummy value for testing purposes.
* __API__
* action: gen_method
* throws: no_throw
*/
virtual bool equals(const Object1* other) const {
return _value == other->value();
}
/**
* ToString function returning dummy value for testing purposes.
* __API__
* action: gen_method
* throws: no_throw
*/
virtual std::string toString() const {
return "Object1 " + std::to_string(_value);
}
virtual ~Object1() = default;
private:
int _value;
};
/**
* Class without any special functions.
* For Kotlin CppBind will generate the default ones based on underlying C++ object is and type name.
* For Swift and Python it won't generate them.
* Instead for Python will generate a __repr__ returning a string containing C++ object id and type name.
* __API__
* action: gen_class
* package: hashing
*/
struct Object2 {
/**
* __API__
* action: gen_constructor
* throws: no_throw
*/
Object2() {}
virtual ~Object2() = default;
};
In the above example the first class: Object1
, contains custom
definitions for hash
, equals
and toString
which means CppBind
generates appropriate methods for all target languages, and if it's
required also implements appropriate interfaces. Particularly for Swift
the generated class conforms Hashable
, Equatable
and
CustomStringConvertible
.
In case of the second example, i.e. Object2
, CppBind generates the
default implementations for Kotlin.
For Python CppBind does not generate the default ones, as pybind already
does that. Instead CppBind generates a __repr__
method which returns a
string containing C++ object address and type information. This can be
used for debugging purposes. Users can have their own __repr__
by just
annotating the appropriate method with python.name: __repr__
.
For Swift the default implementations won't be generated, as by default, classes in Swift are neither hashable nor equatable.
Note
It is not allowed to define hash
, equals
and toString
for
interfaces. If they are defined then CppBind raises an error.
Note
If hash
is defined but equals
is not defined then CppBind raises an
error for Swift. All Hashable
types are also Equatable
in Swift.
Now let's see some usage examples:
// custom hashCode, equals and toString
val obj11 = Object1(1)
val obj12 = Object1(1)
val obj13 = Object1(2)
assert(obj11.toString() == "Object1 1")
assert(obj12.toString() == "Object1 1")
assert(obj13.toString() == "Object1 2")
// obj11 and obj12 have the same value so they are equal and have the same hash
assert(obj11 == obj12)
assert(obj11.hashCode() == obj12.hashCode())
val map1: HashMap<Object1, String> = hashMapOf(
obj11 to "Object 11",
obj13 to "Object 13"
)
assert(map1.size == 2)
assert(map1[obj11] == "Object 11")
assert(map1[obj13] == "Object 13")
// default __hash__, __eq__ and __str__
val obj21 = Object2()
val obj22 = Object2()
assert(obj21 != obj22)
val map2: HashMap<Object2, String> = hashMapOf(
obj21 to "Object 21",
obj22 to "Object 22"
)
assert(map2[obj21] != map2[obj22])
// underlying C++ object type name used in toString
assert(obj21.toString().endsWith("cppbind::example::Object2>"))
# custom __hash__, __eq__ and __str__
obj11 = Object1(1)
obj12 = Object1(1)
obj13 = Object1(2)
assert str(obj11) == 'Object1 1'
assert str(obj12) == 'Object1 1'
assert str(obj13) == 'Object1 2'
# obj11 and obj12 have the same value so they are equal and have the same hash
assert obj11 == obj12
assert hash(obj11) == hash(obj12)
d1 = {
obj11: 'Object 11',
obj13: 'Object 13',
}
assert len(d1) == 2
assert d1[obj11] == 'Object 11'
assert d1[obj13] == 'Object 13'
# default __hash__, __eq__ and __str__
obj21 = Object2()
obj22 = Object2()
assert obj21 != obj22
d2 = {
obj21: 'Object 31',
obj22: 'Object 32',
}
assert d2[obj21] != d2[obj22]
# CppBind generated __repr__ returning underlying C++ object type name and id
assert repr(obj21).endswith('cppbind::example::Object2>')
let obj11 = Object1(value: 1)
let obj12 = Object1(value: 1)
let obj13 = Object1(value: 2)
assert(String(describing: obj11) == "Object1 1")
assert(String(describing: obj12) == "Object1 1")
assert(String(describing: obj13) == "Object1 2")
/// obj11 and obj12 have the same value so they are equal and have the same hash
assert(obj11 == obj12)
var hasher = Hasher()
hasher.combine(obj11)
var hashVal1 = hasher.finalize()
hasher = Hasher()
hasher.combine(obj12)
var hashVal2 = hasher.finalize()
assert(hashVal1 == hashVal2)
let d1: [Object1 : String] = [
obj11: "Object 11",
obj13: "Object 13"
]
assert(d1.count == 2)
assert(d1[obj11] == "Object 11")
assert(d1[obj13] == "Object 13")
The generated bindings
/**
* ______ .______ .______ .______ __ .__ __. _______
* / || _ \ | _ \ | _ \ | | | \ | | | \
* | ,----'| |_) | | |_) | | |_) | | | | \| | | .--. |
* | | | ___/ | ___/ | _ < | | | . ` | | | | |
* | `----.| | | | | |_) | | | | |\ | | '--' |
* \______|| _| | _| |______/ |__| |__| \__| |_______/
*
* This file is generated by cppbind on 10/18/2022-09:00.
* Please do not change it manually.
*/
package com.examples.hashing
import com.examples.cppbind.*
import com.examples.cppbind.exceptions.*
/**
* Class with custom hash, equals and toString methods.
*/
open class Object1
internal constructor(obj: CppBindObject) : AutoCloseable {
companion object {
init {
System.loadLibrary("wrapper_jni")
}
protected fun constructHelper(value: Int): Long {
val id = jConstructor(value)
return id
}
@JvmStatic
private external fun jConstructor(value: Int, vararg extraObjs: Any?): Long
/**
* An internal property to keep an information about the underlying C++ object type.
* It is intended to be used by the generated code.
*/
const val cppbindCxxTypeName: String = "cppbind::example::Object1"
/**
* An internal method to create a Kotlin object from a C++ object.
* It is intended to be used by the generated code.
*/
public fun cppbindConstructObject(id: Long, owner: Boolean = false): Object1 {
val idType = jGettypebyid(id)
val obj : Object1
when (idType) {
ExtendedObject1.cppbindCxxTypeName -> obj = ExtendedObject1(CppBindObject(id, owner))
else -> obj = Object1(CppBindObject(id, owner))
}
return obj
}
}
protected var cppbindObj = obj
private var refs: MutableList<Any> = mutableListOf()
/**
* An internal method to bind the lifetimes of the current and another object.
* It is intended to be used by the generated code.
*/
fun keepCppBindReference(ref: Any) {
refs.add(ref)
}
/**
* An internal getter to get the id of an object.
* It is intended to be used by the generated code.
*/
open val cppbindObjId: Long
get() {
if (cppbindObj.id == 0L) {
throw RuntimeException("Object is not allocated")
}
return cppbindObj.id
}
/**
* An internal property returning underlying C++ object id.
* It is intended to be used by the generated code.
*/
internal val cxxId: Long by lazy {
jGetcxxid(cppbindObj.id)
}
/**
* An internal property returning underlying C++ type name.
* It is intended to be used by the generated code.
*/
internal val cxxTypeName: String by lazy {
jGettypebyid(cppbindObj.id)
}
constructor(value: Int = 0): this(CppBindObject(constructHelper(value), true)) {
}
val value: Int
get() {
val result = jValue(cppbindObjId)
return result
}
/**
* Hash function returning dummy value for testing purposes.
*/
override fun hashCode(): Int {
return jHash(cppbindObjId).hashCode()
}
/**
* Equals function returning dummy value for testing purposes.
*/
override fun equals(other: Any?): Boolean {
other as Object1
return jEquals(cppbindObjId, other.cppbindObjId, other)
}
/**
* ToString function returning dummy value for testing purposes.
*/
override fun toString(): String {
return jTostring(cppbindObjId)
}
override fun close() {
if (cppbindObj.owner && cppbindObj.id != 0L) {
jFinalize(cppbindObj.id)
cppbindObj.id = 0L
}
}
/**
* Finalize and deletes the object
*/
protected fun finalize() {
close()
}
///// External wrapper functions ////////////
private external fun jValue(id: Long): Int
private external fun jHash(id: Long): Long
private external fun jEquals(id: Long, other: Long, vararg extraObjs: Any?): Boolean
private external fun jTostring(id: Long): String
private external fun jFinalize(id: Long): Unit
private external fun jGetcxxid(id: Long): Long
}
/**
* Class without any special functions.
* For Kotlin CppBind will generate the default ones based on underlying C++ object is and type name.
* For Swift and Python it won't generate them.
* Instead for Python will generate a __repr__ returning a string containing C++ object id and type name.
*/
open class Object2
internal constructor(obj: CppBindObject) : AutoCloseable {
companion object {
init {
System.loadLibrary("wrapper_jni")
}
protected fun constructHelper(): Long {
val id = jConstructor()
return id
}
@JvmStatic
private external fun jConstructor(): Long
/**
* An internal property to keep an information about the underlying C++ object type.
* It is intended to be used by the generated code.
*/
const val cppbindCxxTypeName: String = "cppbind::example::Object2"
/**
* An internal method to create a Kotlin object from a C++ object.
* It is intended to be used by the generated code.
*/
public fun cppbindConstructObject(id: Long, owner: Boolean = false): Object2 {
val idType = jGettypebyid(id)
val obj : Object2
when (idType) {
ExtendedObject2.cppbindCxxTypeName -> obj = ExtendedObject2(CppBindObject(id, owner))
else -> obj = Object2(CppBindObject(id, owner))
}
return obj
}
}
protected var cppbindObj = obj
private var refs: MutableList<Any> = mutableListOf()
/**
* An internal method to bind the lifetimes of the current and another object.
* It is intended to be used by the generated code.
*/
fun keepCppBindReference(ref: Any) {
refs.add(ref)
}
/**
* An internal getter to get the id of an object.
* It is intended to be used by the generated code.
*/
open val cppbindObjId: Long
get() {
if (cppbindObj.id == 0L) {
throw RuntimeException("Object is not allocated")
}
return cppbindObj.id
}
/**
* An internal property returning underlying C++ object id.
* It is intended to be used by the generated code.
*/
internal val cxxId: Long by lazy {
jGetcxxid(cppbindObj.id)
}
/**
* An internal property returning underlying C++ type name.
* It is intended to be used by the generated code.
*/
internal val cxxTypeName: String by lazy {
jGettypebyid(cppbindObj.id)
}
constructor(): this(CppBindObject(constructHelper(), true)) {
}
/**
* CppBind generated hashCode method returning the hash of underlying C++ object id.
*/
override fun hashCode(): Int {
return cxxId.hashCode()
}
/**
* CppBind generated equals method comparing the underlying C++ object ids.
*/
override fun equals(other: Any?): Boolean {
other as Object2
return cxxId == other.cxxId
}
/**
* CppBind generated toString method returning underlying C++ object type and id.
*/
override fun toString(): String {
return "<0x$cxxId: $cxxTypeName>"
}
override fun close() {
if (cppbindObj.owner && cppbindObj.id != 0L) {
jFinalize(cppbindObj.id)
cppbindObj.id = 0L
}
}
/**
* Finalize and deletes the object
*/
protected fun finalize() {
close()
}
///// External wrapper functions ////////////
private external fun jFinalize(id: Long): Unit
private external fun jGetcxxid(id: Long): Long
}
/**
* Class with custom hash, equals and toString methods.
*/
open class Object3
internal constructor(obj: CppBindObject) : AutoCloseable {
companion object {
init {
System.loadLibrary("wrapper_jni")
}
protected fun constructHelper(value: Int): Long {
val id = jConstructor(value)
return id
}
@JvmStatic
private external fun jConstructor(value: Int, vararg extraObjs: Any?): Long
/**
* An internal property to keep an information about the underlying C++ object type.
* It is intended to be used by the generated code.
*/
const val cppbindCxxTypeName: String = "cppbind::example::Object3"
/**
* An internal method to create a Kotlin object from a C++ object.
* It is intended to be used by the generated code.
*/
public fun cppbindConstructObject(id: Long, owner: Boolean = false): Object3 {
val idType = jGettypebyid(id)
val obj : Object3
when (idType) {
ExtendedObject3.cppbindCxxTypeName -> obj = ExtendedObject3(CppBindObject(id, owner))
else -> obj = Object3(CppBindObject(id, owner))
}
return obj
}
}
protected var cppbindObj = obj
private var refs: MutableList<Any> = mutableListOf()
/**
* An internal method to bind the lifetimes of the current and another object.
* It is intended to be used by the generated code.
*/
fun keepCppBindReference(ref: Any) {
refs.add(ref)
}
/**
* An internal getter to get the id of an object.
* It is intended to be used by the generated code.
*/
open val cppbindObjId: Long
get() {
if (cppbindObj.id == 0L) {
throw RuntimeException("Object is not allocated")
}
return cppbindObj.id
}
/**
* An internal property returning underlying C++ object id.
* It is intended to be used by the generated code.
*/
internal val cxxId: Long by lazy {
jGetcxxid(cppbindObj.id)
}
/**
* An internal property returning underlying C++ type name.
* It is intended to be used by the generated code.
*/
internal val cxxTypeName: String by lazy {
jGettypebyid(cppbindObj.id)
}
constructor(value: Int = 0): this(CppBindObject(constructHelper(value), true)) {
}
val value: Int
get() {
val result = jValue(cppbindObjId)
return result
}
/**
* Hash function returning dummy value for testing purposes.
*/
override fun hashCode(): Int {
return jHash(cppbindObjId).hashCode()
}
/**
* Equals based on value property for testing purposes.
*/
override fun equals(other: Any?): Boolean {
other as Object3
return jEquals(cppbindObjId, other.cppbindObjId, other)
}
/**
* ToString based on value property for testing purposes.
*/
override fun toString(): String {
return jTostring(cppbindObjId)
}
override fun close() {
if (cppbindObj.owner && cppbindObj.id != 0L) {
jFinalize(cppbindObj.id)
cppbindObj.id = 0L
}
}
/**
* Finalize and deletes the object
*/
protected fun finalize() {
close()
}
///// External wrapper functions ////////////
private external fun jValue(id: Long): Int
private external fun jHash(id: Long): Long
private external fun jEquals(id: Long, other: Long, vararg extraObjs: Any?): Boolean
private external fun jTostring(id: Long): String
private external fun jFinalize(id: Long): Unit
private external fun jGetcxxid(id: Long): Long
}
/**
* Class without any special functions.
* For Kotlin CppBind will generate the default ones based on underlying C++ object is and type name.
* For Swift and Python it won't generate them.
* Instead for Python will generate a __repr__ returning a string containing C++ object id and type name.
*/
open class Object4
internal constructor(obj: CppBindObject) : AutoCloseable {
companion object {
init {
System.loadLibrary("wrapper_jni")
}
protected fun constructHelper(): Long {
val id = jConstructor()
return id
}
@JvmStatic
private external fun jConstructor(): Long
/**
* An internal property to keep an information about the underlying C++ object type.
* It is intended to be used by the generated code.
*/
const val cppbindCxxTypeName: String = "cppbind::example::Object4"
/**
* An internal method to create a Kotlin object from a C++ object.
* It is intended to be used by the generated code.
*/
public fun cppbindConstructObject(id: Long, owner: Boolean = false): Object4 {
val idType = jGettypebyid(id)
val obj : Object4
when (idType) {
ExtendedObject4.cppbindCxxTypeName -> obj = ExtendedObject4(CppBindObject(id, owner))
else -> obj = Object4(CppBindObject(id, owner))
}
return obj
}
}
protected var cppbindObj = obj
private var refs: MutableList<Any> = mutableListOf()
/**
* An internal method to bind the lifetimes of the current and another object.
* It is intended to be used by the generated code.
*/
fun keepCppBindReference(ref: Any) {
refs.add(ref)
}
/**
* An internal getter to get the id of an object.
* It is intended to be used by the generated code.
*/
open val cppbindObjId: Long
get() {
if (cppbindObj.id == 0L) {
throw RuntimeException("Object is not allocated")
}
return cppbindObj.id
}
/**
* An internal property returning underlying C++ object id.
* It is intended to be used by the generated code.
*/
internal val cxxId: Long by lazy {
jGetcxxid(cppbindObj.id)
}
/**
* An internal property returning underlying C++ type name.
* It is intended to be used by the generated code.
*/
internal val cxxTypeName: String by lazy {
jGettypebyid(cppbindObj.id)
}
constructor(): this(CppBindObject(constructHelper(), true)) {
}
/**
* CppBind generated hashCode method returning the hash of underlying C++ object id.
*/
override fun hashCode(): Int {
return cxxId.hashCode()
}
/**
* CppBind generated equals method comparing the underlying C++ object ids.
*/
override fun equals(other: Any?): Boolean {
other as Object4
return cxxId == other.cxxId
}
/**
* CppBind generated toString method returning underlying C++ object type and id.
*/
override fun toString(): String {
return "<0x$cxxId: $cxxTypeName>"
}
override fun close() {
if (cppbindObj.owner && cppbindObj.id != 0L) {
jFinalize(cppbindObj.id)
cppbindObj.id = 0L
}
}
/**
* Finalize and deletes the object
*/
protected fun finalize() {
close()
}
///// External wrapper functions ////////////
private external fun jFinalize(id: Long): Unit
private external fun jGetcxxid(id: Long): Long
}
open class ExtendedObject1
internal constructor(obj: CppBindObject) : Object1(obj) {
companion object {
protected fun constructHelper(value: Int): Long {
val id = jConstructor(value)
return id
}
@JvmStatic
private external fun jConstructor(value: Int, vararg extraObjs: Any?): Long
/**
* An internal property to keep an information about the underlying C++ object type.
* It is intended to be used by the generated code.
*/
const val cppbindCxxTypeName: String = "cppbind::example::ExtendedObject1"
}
constructor(value: Int = 0): this(CppBindObject(constructHelper(value), true)) {
}
/**
* Hash function returning dummy value for testing purposes.
*/
override fun hashCode(): Int {
return jHash(cppbindObjId).hashCode()
}
///// External wrapper functions ////////////
private external fun jHash(id: Long): Long
}
open class ExtendedObject2
internal constructor(obj: CppBindObject) : Object2(obj) {
companion object {
protected fun constructHelper(): Long {
val id = jConstructor()
return id
}
@JvmStatic
private external fun jConstructor(): Long
/**
* An internal property to keep an information about the underlying C++ object type.
* It is intended to be used by the generated code.
*/
const val cppbindCxxTypeName: String = "cppbind::example::ExtendedObject2"
}
constructor(): this(CppBindObject(constructHelper(), true)) {
}
}
open class ExtendedObject3
internal constructor(obj: CppBindObject) : Object3(obj) {
companion object {
protected fun constructHelper(value: Int): Long {
val id = jConstructor(value)
return id
}
@JvmStatic
private external fun jConstructor(value: Int, vararg extraObjs: Any?): Long
/**
* An internal property to keep an information about the underlying C++ object type.
* It is intended to be used by the generated code.
*/
const val cppbindCxxTypeName: String = "cppbind::example::ExtendedObject3"
}
constructor(value: Int = 0): this(CppBindObject(constructHelper(value), true)) {
}
}
open class ExtendedObject4
internal constructor(obj: CppBindObject) : Object4(obj) {
companion object {
protected fun constructHelper(): Long {
val id = jConstructor()
return id
}
@JvmStatic
private external fun jConstructor(): Long
/**
* An internal property to keep an information about the underlying C++ object type.
* It is intended to be used by the generated code.
*/
const val cppbindCxxTypeName: String = "cppbind::example::ExtendedObject4"
}
constructor(): this(CppBindObject(constructHelper(), true)) {
}
}
private external fun jGettypebyid(id: Long): String
"""
______ .______ .______ .______ __ .__ __. _______
/ || _ \ | _ \ | _ \ | | | \ | | | \
| ,----'| |_) | | |_) | | |_) | | | | \| | | .--. |
| | | ___/ | ___/ | _ < | | | . ` | | | | |
| `----.| | | | | |_) | | | | |\ | | '--' |
\______|| _| | _| |______/ |__| |__| \__| |_______/
This file is generated by cppbind on 08/11/2022-07:34.
Please do not change it manually.
"""
from __future__ import annotations
from typing import *
import examples.hashing.objects as pybind_objects_pygen
from examples_lib.cppbind.cppbind_metaclass_pygen import *
from examples_lib.cppbind.cppbind_utils_pygen import *
class Object1(metaclass=CppBindMetaclass):
"""
Class with custom hash, equals and toString methods.
"""
@bind
def __init__(self, value: int = 0):
pass
@property
@bind
def value(self) -> int:
pass
@bind
def __hash__(self) -> int:
"""
Hash function returning dummy value for testing purposes.
"""
pass
@bind
def __eq__(self, other: Object1) -> bool:
"""
Equals function returning dummy value for testing purposes.
"""
pass
@bind
def __str__(self) -> str:
"""
ToString function returning dummy value for testing purposes.
"""
pass
@bind
def __repr__(self) -> str:
"""
CppBind generated __repr__ method returning underlying C++ object type and id.
"""
pass
class Object2(metaclass=CppBindMetaclass):
"""
Class without any special functions.
For Kotlin CppBind will generate the default ones based on underlying C++ object is and type name.
For Swift and Python it won't generate them.
Instead for Python will generate a __repr__ returning a string containing C++ object id and type name.
"""
@bind
def __init__(self):
pass
@bind
def __repr__(self) -> str:
"""
CppBind generated __repr__ method returning underlying C++ object type and id.
"""
pass
class Object3(metaclass=CppBindMetaclass):
"""
Class with custom hash, equals and toString methods.
"""
@bind
def __init__(self, value: int = 0):
pass
@property
@bind
def value(self) -> int:
pass
@bind
def __hash__(self) -> int:
"""
Hash function returning dummy value for testing purposes.
"""
pass
@bind
def __eq__(self, other: Object3) -> bool:
"""
Equals based on value property for testing purposes.
"""
pass
@bind
def __str__(self) -> str:
"""
ToString based on value property for testing purposes.
"""
pass
@bind
def __repr__(self) -> str:
"""
CppBind generated __repr__ method returning underlying C++ object type and id.
"""
pass
class Object4(metaclass=CppBindMetaclass):
"""
Class without any special functions.
For Kotlin CppBind will generate the default ones based on underlying C++ object is and type name.
For Swift and Python it won't generate them.
Instead for Python will generate a __repr__ returning a string containing C++ object id and type name.
"""
@bind
def __init__(self):
pass
@bind
def __repr__(self) -> str:
"""
CppBind generated __repr__ method returning underlying C++ object type and id.
"""
pass
class ExtendedObject1(Object1, metaclass=CppBindMetaclass):
@bind
def __init__(self, value: int = 0):
pass
@bind
def __hash__(self) -> int:
"""
Hash function returning dummy value for testing purposes.
"""
pass
class ExtendedObject2(Object2, metaclass=CppBindMetaclass):
@bind
def __init__(self):
pass
class ExtendedObject3(Object3, metaclass=CppBindMetaclass):
@bind
def __init__(self, value: int = 0):
pass
class ExtendedObject4(Object4, metaclass=CppBindMetaclass):
@bind
def __init__(self):
pass
/**
* ______ .______ .______ .______ __ .__ __. _______
* / || _ \ | _ \ | _ \ | | | \ | | | \
* | ,----'| |_) | | |_) | | |_) | | | | \| | | .--. |
* | | | ___/ | ___/ | _ < | | | . ` | | | | |
* | `----.| | | | | |_) | | | | |\ | | '--' |
* \______|| _| | _| |______/ |__| |__| \__| |_______/
*
* This file is generated by cppbind on 10/18/2022-08:51.
* Please do not change it manually.
*/
import CWrapper
import Foundation
/// Class with custom hash, equals and toString methods.
public class Object1: Hashable, CustomStringConvertible {
/// An internal property to keep a reference to the original C++ object.
/// It is intended to be used by the generated code.
public let cself: CppBindCObject
/// An internal property to keep track whether Swift is responsible for deallocating the underlying C++ object or not.
/// It is intended to be used by the generated code.
public let owner: Bool
private var refs: [Any]
/// internal main initializer
internal required init(_ _cself: CppBindCObject, _ _owner: Bool = false) {
self.cself = _cself
self.owner = _owner
self.refs = []
}
deinit {
release_CppbindExample_Object1(cself, owner)
}
/// An internal method to bind the lifetimes of the current and another object.
/// It is intended to be used by the generated code.
public func keepCppBindReference(_ object: Any) {
self.refs.append(object)
}
/// internal dummy initializer to prevent automatic initializer inheritance
internal init(_cself: CppBindCObject, _self: Object1) {
fatalError("A dummy internal initializer should not be called.")
}
public convenience init(value: Int = 0) {
let swifttoscvalue = CInt(value)
var cppbindErr = CppBindCObject()
self.init(create_CppbindExample_Object1(swifttoscvalue, &cppbindErr), true)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
}
public var value: Int {
var cppbindErr = CppBindCObject()
let result = _prop_get_CppbindExample_Object1_value(cself, &cppbindErr)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
let sctoswiftresult = Int(result)
return sctoswiftresult
}
/**
* Hash function returning dummy value for testing purposes.
*/
public func hash(into hasher: inout Hasher) {
var cppbindErr = CppBindCObject()
let result = _func_CppbindExample_Object1_hash(cself, &cppbindErr)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
hasher.combine(result)
}
/**
* Equals function returning dummy value for testing purposes.
*/
public static func == (lhs: Object1, rhs: Object1) -> Bool {
var cppbindErr = CppBindCObject()
let result = _func_CppbindExample_Object1_equals(lhs.cself, rhs.cself, &cppbindErr)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
return result
}
/**
* ToString function returning dummy value for testing purposes.
*/
public var description: String {
var cppbindErr = CppBindCObject()
let result = _func_CppbindExample_Object1_toString(cself, &cppbindErr)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
let sctoswiftresult = String(cString: result)
defer {
result.deallocate()
}
return sctoswiftresult
}
/// An internal property to keep an information about the underlying C++ object type.
/// It is intended to be used by the generated code.
class var cppbindCxxTypeName : String { return "cppbind::example::Object1" }
/// An internal method to create a Swift object from a C++ object.
/// It is intended to be used by the generated code.
class func cppbindConstructObject(_ cppbindObj: CppBindCObject, _ owner: Bool = false) -> Object1 {
let typeName = String(cString: cppbindObj.type)
var obj : Object1
switch(typeName) {
case(ExtendedObject1.cppbindCxxTypeName):
obj = ExtendedObject1(cppbindObj, owner)
default:
obj = Object1(cppbindObj, owner)
}
return obj
}
}
/// Class without any special functions.
/// For Kotlin CppBind will generate the default ones based on underlying C++ object is and type name.
/// For Swift and Python it won't generate them.
/// Instead for Python will generate a __repr__ returning a string containing C++ object id and type name.
public class Object2 {
/// An internal property to keep a reference to the original C++ object.
/// It is intended to be used by the generated code.
public let cself: CppBindCObject
/// An internal property to keep track whether Swift is responsible for deallocating the underlying C++ object or not.
/// It is intended to be used by the generated code.
public let owner: Bool
private var refs: [Any]
/// internal main initializer
internal required init(_ _cself: CppBindCObject, _ _owner: Bool = false) {
self.cself = _cself
self.owner = _owner
self.refs = []
}
deinit {
release_CppbindExample_Object2(cself, owner)
}
/// An internal method to bind the lifetimes of the current and another object.
/// It is intended to be used by the generated code.
public func keepCppBindReference(_ object: Any) {
self.refs.append(object)
}
/// internal dummy initializer to prevent automatic initializer inheritance
internal init(_cself: CppBindCObject, _self: Object2) {
fatalError("A dummy internal initializer should not be called.")
}
public convenience init() {
var cppbindErr = CppBindCObject()
self.init(create_CppbindExample_Object2(&cppbindErr), true)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
}
/// An internal property to keep an information about the underlying C++ object type.
/// It is intended to be used by the generated code.
class var cppbindCxxTypeName : String { return "cppbind::example::Object2" }
/// An internal method to create a Swift object from a C++ object.
/// It is intended to be used by the generated code.
class func cppbindConstructObject(_ cppbindObj: CppBindCObject, _ owner: Bool = false) -> Object2 {
let typeName = String(cString: cppbindObj.type)
var obj : Object2
switch(typeName) {
case(ExtendedObject2.cppbindCxxTypeName):
obj = ExtendedObject2(cppbindObj, owner)
default:
obj = Object2(cppbindObj, owner)
}
return obj
}
}
/// Class with custom hash, equals and toString methods.
public class Object3: Hashable, CustomStringConvertible {
/// An internal property to keep a reference to the original C++ object.
/// It is intended to be used by the generated code.
public let cself: CppBindCObject
/// An internal property to keep track whether Swift is responsible for deallocating the underlying C++ object or not.
/// It is intended to be used by the generated code.
public let owner: Bool
private var refs: [Any]
/// internal main initializer
internal required init(_ _cself: CppBindCObject, _ _owner: Bool = false) {
self.cself = _cself
self.owner = _owner
self.refs = []
}
deinit {
release_CppbindExample_Object3(cself, owner)
}
/// An internal method to bind the lifetimes of the current and another object.
/// It is intended to be used by the generated code.
public func keepCppBindReference(_ object: Any) {
self.refs.append(object)
}
/// internal dummy initializer to prevent automatic initializer inheritance
internal init(_cself: CppBindCObject, _self: Object3) {
fatalError("A dummy internal initializer should not be called.")
}
public convenience init(value: Int = 0) {
let swifttoscvalue = CInt(value)
var cppbindErr = CppBindCObject()
self.init(create_CppbindExample_Object3(swifttoscvalue, &cppbindErr), true)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
}
public var value: Int {
var cppbindErr = CppBindCObject()
let result = _prop_get_CppbindExample_Object3_value(cself, &cppbindErr)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
let sctoswiftresult = Int(result)
return sctoswiftresult
}
/**
* Hash function returning dummy value for testing purposes.
*/
public func hash(into hasher: inout Hasher) {
var cppbindErr = CppBindCObject()
let result = _func_CppbindExample_Object3_hash(cself, &cppbindErr)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
hasher.combine(result)
}
/**
* Equals based on value property for testing purposes.
*/
public static func == (lhs: Object3, rhs: Object3) -> Bool {
var cppbindErr = CppBindCObject()
let result = _func_CppbindExample_Object3_equals(lhs.cself, rhs.cself, &cppbindErr)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
return result
}
/**
* ToString based on value property for testing purposes.
*/
public var description: String {
var cppbindErr = CppBindCObject()
let result = _func_CppbindExample_Object3_toString(cself, &cppbindErr)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
let sctoswiftresult = String(cString: result)
defer {
result.deallocate()
}
return sctoswiftresult
}
/// An internal property to keep an information about the underlying C++ object type.
/// It is intended to be used by the generated code.
class var cppbindCxxTypeName : String { return "cppbind::example::Object3" }
/// An internal method to create a Swift object from a C++ object.
/// It is intended to be used by the generated code.
class func cppbindConstructObject(_ cppbindObj: CppBindCObject, _ owner: Bool = false) -> Object3 {
let typeName = String(cString: cppbindObj.type)
var obj : Object3
switch(typeName) {
case(ExtendedObject3.cppbindCxxTypeName):
obj = ExtendedObject3(cppbindObj, owner)
default:
obj = Object3(cppbindObj, owner)
}
return obj
}
}
/// Class without any special functions.
/// For Kotlin CppBind will generate the default ones based on underlying C++ object is and type name.
/// For Swift and Python it won't generate them.
/// Instead for Python will generate a __repr__ returning a string containing C++ object id and type name.
public class Object4 {
/// An internal property to keep a reference to the original C++ object.
/// It is intended to be used by the generated code.
public let cself: CppBindCObject
/// An internal property to keep track whether Swift is responsible for deallocating the underlying C++ object or not.
/// It is intended to be used by the generated code.
public let owner: Bool
private var refs: [Any]
/// internal main initializer
internal required init(_ _cself: CppBindCObject, _ _owner: Bool = false) {
self.cself = _cself
self.owner = _owner
self.refs = []
}
deinit {
release_CppbindExample_Object4(cself, owner)
}
/// An internal method to bind the lifetimes of the current and another object.
/// It is intended to be used by the generated code.
public func keepCppBindReference(_ object: Any) {
self.refs.append(object)
}
/// internal dummy initializer to prevent automatic initializer inheritance
internal init(_cself: CppBindCObject, _self: Object4) {
fatalError("A dummy internal initializer should not be called.")
}
public convenience init() {
var cppbindErr = CppBindCObject()
self.init(create_CppbindExample_Object4(&cppbindErr), true)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
}
/// An internal property to keep an information about the underlying C++ object type.
/// It is intended to be used by the generated code.
class var cppbindCxxTypeName : String { return "cppbind::example::Object4" }
/// An internal method to create a Swift object from a C++ object.
/// It is intended to be used by the generated code.
class func cppbindConstructObject(_ cppbindObj: CppBindCObject, _ owner: Bool = false) -> Object4 {
let typeName = String(cString: cppbindObj.type)
var obj : Object4
switch(typeName) {
case(ExtendedObject4.cppbindCxxTypeName):
obj = ExtendedObject4(cppbindObj, owner)
default:
obj = Object4(cppbindObj, owner)
}
return obj
}
}
public class ExtendedObject1: Object1 {
/// internal main initializer
internal required init(_ _cself: CppBindCObject, _ _owner: Bool = false) {
super.init(_cself, _owner)
}
public convenience init(value: Int = 0) {
let swifttoscvalue = CInt(value)
var cppbindErr = CppBindCObject()
self.init(create_CppbindExample_ExtendedObject1(swifttoscvalue, &cppbindErr), true)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
}
/**
* Hash function returning dummy value for testing purposes.
*/
public override func hash(into hasher: inout Hasher) {
var cppbindErr = CppBindCObject()
let result = _func_CppbindExample_ExtendedObject1_hash(cself, &cppbindErr)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
hasher.combine(result)
}
/// An internal property to keep an information about the underlying C++ object type.
/// It is intended to be used by the generated code.
override class var cppbindCxxTypeName : String { return "cppbind::example::ExtendedObject1" }
}
public class ExtendedObject2: Object2 {
/// internal main initializer
internal required init(_ _cself: CppBindCObject, _ _owner: Bool = false) {
super.init(_cself, _owner)
}
public convenience init() {
var cppbindErr = CppBindCObject()
self.init(create_CppbindExample_ExtendedObject2(&cppbindErr), true)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
}
/// An internal property to keep an information about the underlying C++ object type.
/// It is intended to be used by the generated code.
override class var cppbindCxxTypeName : String { return "cppbind::example::ExtendedObject2" }
}
public class ExtendedObject3: Object3 {
/// internal main initializer
internal required init(_ _cself: CppBindCObject, _ _owner: Bool = false) {
super.init(_cself, _owner)
}
public convenience init(value: Int = 0) {
let swifttoscvalue = CInt(value)
var cppbindErr = CppBindCObject()
self.init(create_CppbindExample_ExtendedObject3(swifttoscvalue, &cppbindErr), true)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
}
/// An internal property to keep an information about the underlying C++ object type.
/// It is intended to be used by the generated code.
override class var cppbindCxxTypeName : String { return "cppbind::example::ExtendedObject3" }
}
public class ExtendedObject4: Object4 {
/// internal main initializer
internal required init(_ _cself: CppBindCObject, _ _owner: Bool = false) {
super.init(_cself, _owner)
}
public convenience init() {
var cppbindErr = CppBindCObject()
self.init(create_CppbindExample_ExtendedObject4(&cppbindErr), true)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
}
/// An internal property to keep an information about the underlying C++ object type.
/// It is intended to be used by the generated code.
override class var cppbindCxxTypeName : String { return "cppbind::example::ExtendedObject4" }
}
Nested Types¶
Let's now generate bindings for nested types. Here's a small example:
/**
* An example with multi level nested type using header_code_fragment to create an alias in target languages.
* __API__
* action: gen_class
* package: nested_types
* kotlin.header_code_fragment: |
* typealias Item = List.Item
* kotlin.footer_code_fragment: |
* typealias Value = List.Item.Value
* swift.header_code_fragment: |
* public typealias Item = List.Item
* swift.footer_code_fragment: |
* public typealias Value = List.Item.Value
*/
struct List {
/**
* __API__
* action: gen_class
* package: nested_types
*/
struct Item {
/**
* __API__
* action: gen_class
* package: nested_types
*/
struct Value {
/**
* __API__
* action: gen_property_setter
*/
int value;
/**
* __API__
* action: gen_constructor
* throws: no_throw
*/
Value(int _value) : value(_value){};
};
/**
* __API__
* action: gen_property_setter
*/
Value* value;
/**
* __API__
* action: gen_constructor
* throws: no_throw
*/
Item(Value* v) : value(v) {};
};
/**
* __API__
* action: gen_constructor
* throws: no_throw
*/
List() {};
/**
* __API__
* action: gen_method
* throws: no_throw
*/
void push_back(Item* item) {
items.push_back(item);
};
/**
* __API__
* action: gen_method
* throws: no_throw
*/
void pop_back() {
items.pop_back();
};
/**
* __API__
* action: gen_getter
* throws: no_throw
* return_value_policy: reference
*/
Item* back() {
return items.back();
};
private:
std::list<Item*> items;
};
Note
Here we used header_code_fragment and footer_code_fragment variables to create nested types.
Some usage examples:
val value1 = Value(1)
val value2 = List.Item.Value(3)
val item1 = Item(value1)
val item2 = List.Item(value2)
val list = List()
list.push_back(item1)
assert(list.back.value.value == value1.value)
list.push_back(item2)
assert(list.back.value.value == value2.value)
list.pop_back()
assert(list.back.value.value == value1.value)
val listWrapper = ListWrapper()
listWrapper.push_back(item1)
assert(list.back.value.value == value1.value)
value1 = List.Item.Value(1)
value2 = List.Item.Value(3)
item1 = List.Item(value1)
item2 = List.Item(value2)
l = List()
l.push_back(item1)
assert l.back.value.value == value1.value
l.push_back(item2)
assert l.back.value.value == value2.value
l.pop_back()
assert l.back.value.value == value1.value
list_wrapper = ListWrapper()
list_wrapper.push_back(item1)
assert(list_wrapper.back.value.value == value1.value)
let value1 = List.Item.Value(value: 1)
let value2 = Value(value: 3)
let item1 = List.Item(v: value1)
let item2 = Item(v: value2)
let list = List()
list.push_back(item: item1)
assert(list.back.value.value == value1.value)
list.push_back(item: item2)
assert(list.back.value.value == value2.value)
list.pop_back()
assert(list.back.value.value == value1.value)
let listWrapper = ListWrapper()
listWrapper.push_back(item: item1)
assert(listWrapper.back.value.value == item1.value.value)
Generated bindings
/**
* ______ .______ .______ .______ __ .__ __. _______
* / || _ \ | _ \ | _ \ | | | \ | | | \
* | ,----'| |_) | | |_) | | |_) | | | | \| | | .--. |
* | | | ___/ | ___/ | _ < | | | . ` | | | | |
* | `----.| | | | | |_) | | | | |\ | | '--' |
* \______|| _| | _| |______/ |__| |__| \__| |_______/
*
* This file is generated by cppbind on 08/18/2022-11:02.
* Please do not change it manually.
*/
package com.examples.nested_types
import com.examples.cppbind.*
import com.examples.cppbind.exceptions.*
typealias Item = List.Item
/**
* An example with multi level nested type using header_code_fragment to create an alias in target languages.
*/
open class List
internal constructor(obj: CppBindObject) : AutoCloseable {
companion object {
init {
System.loadLibrary("wrapper_jni")
}
protected fun constructHelper(): Long {
val id = jConstructor()
return id
}
@JvmStatic
private external fun jConstructor(): Long
/**
* An internal property to keep an information about the underlying C++ object type.
* It is intended to be used by the generated code.
*/
const val cppbindCxxTypeName: String = "cppbind::example::List"
}
protected var cppbindObj = obj
private var refs: MutableList<Any> = mutableListOf()
/**
* An internal method to bind the lifetimes of the current and another object.
* It is intended to be used by the generated code.
*/
fun keepCppBindReference(ref: Any) {
refs.add(ref)
}
/**
* An internal getter to get the id of an object.
* It is intended to be used by the generated code.
*/
open val cppbindObjId: Long
get() {
if (cppbindObj.id == 0L) {
throw RuntimeException("Object is not allocated")
}
return cppbindObj.id
}
/**
* An internal property returning underlying C++ object id.
* It is intended to be used by the generated code.
*/
internal val cxxId: Long by lazy {
jGetcxxid(cppbindObj.id)
}
/**
* An internal property returning underlying C++ type name.
* It is intended to be used by the generated code.
*/
internal val cxxTypeName: String by lazy {
jGettypebyid(cppbindObj.id)
}
constructor(): this(CppBindObject(constructHelper(), true)) {
}
val back: List.Item
get() {
val result = jBack(cppbindObjId)
val jdktokotlinresult = List.Item(CppBindObject(result))
return jdktokotlinresult
}
open class Item
internal constructor(obj: CppBindObject) : AutoCloseable {
companion object {
init {
System.loadLibrary("wrapper_jni")
}
protected fun constructHelper(v: List.Item.Value): Long {
val kotlintojdkv = v.cppbindObjId
val id = jConstructor(kotlintojdkv, v)
return id
}
@JvmStatic
private external fun jConstructor(v: Long, vararg extraObjs: Any?): Long
/**
* An internal property to keep an information about the underlying C++ object type.
* It is intended to be used by the generated code.
*/
const val cppbindCxxTypeName: String = "cppbind::example::List::Item"
}
protected var cppbindObj = obj
private var refs: MutableList<Any> = mutableListOf()
/**
* An internal method to bind the lifetimes of the current and another object.
* It is intended to be used by the generated code.
*/
fun keepCppBindReference(ref: Any) {
refs.add(ref)
}
/**
* An internal getter to get the id of an object.
* It is intended to be used by the generated code.
*/
open val cppbindObjId: Long
get() {
if (cppbindObj.id == 0L) {
throw RuntimeException("Object is not allocated")
}
return cppbindObj.id
}
/**
* An internal property returning underlying C++ object id.
* It is intended to be used by the generated code.
*/
internal val cxxId: Long by lazy {
jGetcxxid(cppbindObj.id)
}
/**
* An internal property returning underlying C++ type name.
* It is intended to be used by the generated code.
*/
internal val cxxTypeName: String by lazy {
jGettypebyid(cppbindObj.id)
}
constructor(v: List.Item.Value): this(CppBindObject(constructHelper(v), true)) {
}
var value: List.Item.Value
get() {
val result = jValue(cppbindObjId)
val jdktokotlinresult = List.Item.Value(CppBindObject(result))
jdktokotlinresult.keepCppBindReference(this)
return jdktokotlinresult
}
set(value) {
val kotlintojdkvalue = value.cppbindObjId
jSetvalue(cppbindObjId, kotlintojdkvalue, value)
}
open class Value
internal constructor(obj: CppBindObject) : AutoCloseable {
companion object {
init {
System.loadLibrary("wrapper_jni")
}
protected fun constructHelper(_value: Int): Long {
val id = jConstructor(_value)
return id
}
@JvmStatic
private external fun jConstructor(_value: Int, vararg extraObjs: Any?): Long
/**
* An internal property to keep an information about the underlying C++ object type.
* It is intended to be used by the generated code.
*/
const val cppbindCxxTypeName: String = "cppbind::example::List::Item::Value"
}
protected var cppbindObj = obj
private var refs: MutableList<Any> = mutableListOf()
/**
* An internal method to bind the lifetimes of the current and another object.
* It is intended to be used by the generated code.
*/
fun keepCppBindReference(ref: Any) {
refs.add(ref)
}
/**
* An internal getter to get the id of an object.
* It is intended to be used by the generated code.
*/
open val cppbindObjId: Long
get() {
if (cppbindObj.id == 0L) {
throw RuntimeException("Object is not allocated")
}
return cppbindObj.id
}
/**
* An internal property returning underlying C++ object id.
* It is intended to be used by the generated code.
*/
internal val cxxId: Long by lazy {
jGetcxxid(cppbindObj.id)
}
/**
* An internal property returning underlying C++ type name.
* It is intended to be used by the generated code.
*/
internal val cxxTypeName: String by lazy {
jGettypebyid(cppbindObj.id)
}
constructor(_value: Int): this(CppBindObject(constructHelper(_value), true)) {
}
var value: Int
get() {
val result = jValue(cppbindObjId)
return result
}
set(value) {
jSetvalue(cppbindObjId, value, value)
}
/**
* CppBind generated hashCode method returning the hash of underlying C++ object id.
*/
override fun hashCode(): Int {
return cxxId.hashCode()
}
/**
* CppBind generated equals method comparing the underlying C++ object ids.
*/
override fun equals(other: Any?): Boolean {
other as Value
return cxxId == other.cxxId
}
/**
* CppBind generated toString method returning underlying C++ object type and id.
*/
override fun toString(): String {
return "<0x$cxxId: $cxxTypeName>"
}
override fun close() {
if (cppbindObj.owner && cppbindObj.id != 0L) {
jFinalize(cppbindObj.id)
cppbindObj.id = 0L
}
}
/**
* Finalize and deletes the object
*/
protected fun finalize() {
close()
}
///// External wrapper functions ////////////
private external fun jValue(id: Long): Int
private external fun jSetvalue(id: Long, value: Int, valueObj: Any?): Unit
private external fun jFinalize(id: Long): Unit
private external fun jGetcxxid(id: Long): Long
}
override fun close() {
if (cppbindObj.owner && cppbindObj.id != 0L) {
jFinalize(cppbindObj.id)
cppbindObj.id = 0L
}
}
/**
* Finalize and deletes the object
*/
protected fun finalize() {
close()
}
///// External wrapper functions ////////////
private external fun jValue(id: Long): Long
private external fun jSetvalue(id: Long, value: Long, valueObj: Any?): Unit
private external fun jFinalize(id: Long): Unit
private external fun jGetcxxid(id: Long): Long
}
fun push_back(item: List.Item): Unit {
val kotlintojdkitem = item.cppbindObjId
val result = jPush_back(cppbindObjId, kotlintojdkitem, item)
return result
}
fun pop_back(): Unit {
val result = jPop_back(cppbindObjId)
return result
}
override fun close() {
if (cppbindObj.owner && cppbindObj.id != 0L) {
jFinalize(cppbindObj.id)
cppbindObj.id = 0L
}
}
/**
* Finalize and deletes the object
*/
protected fun finalize() {
close()
}
///// External wrapper functions ////////////
private external fun jPush_back(id: Long, item: Long, vararg extraObjs: Any?): Unit
private external fun jPop_back(id: Long): Unit
private external fun jBack(id: Long): Long
private external fun jFinalize(id: Long): Unit
private external fun jGetcxxid(id: Long): Long
}
typealias Value = List.Item.Value
private external fun jGettypebyid(id: Long): String
"""
______ .______ .______ .______ __ .__ __. _______
/ || _ \ | _ \ | _ \ | | | \ | | | \
| ,----'| |_) | | |_) | | |_) | | | | \| | | .--. |
| | | ___/ | ___/ | _ < | | | . ` | | | | |
| `----.| | | | | |_) | | | | |\ | | '--' |
\______|| _| | _| |______/ |__| |__| \__| |_______/
This file is generated by cppbind on 08/11/2022-07:34.
Please do not change it manually.
"""
from __future__ import annotations
from typing import *
import examples.nested_types.list as pybind_list_pygen
from examples_lib.cppbind.cppbind_metaclass_pygen import *
from examples_lib.cppbind.cppbind_utils_pygen import *
class List(metaclass=CppBindMetaclass):
"""
An example with multi level nested type using header_code_fragment to create an alias in target languages.
"""
@bind
def __init__(self):
pass
@property
@bind
def back(self) -> List.Item:
pass
class Item(metaclass=CppBindMetaclass):
@bind
def __init__(self, v: List.Item.Value):
pass
@property
@bind
def value(self) -> List.Item.Value:
pass
@value.setter
@bind
def value(self, value: List.Item.Value):
pass
class Value(metaclass=CppBindMetaclass):
@bind
def __init__(self, _value: int):
pass
@property
@bind
def value(self) -> int:
pass
@value.setter
@bind
def value(self, value: int):
pass
@bind
def __repr__(self) -> str:
"""
CppBind generated __repr__ method returning underlying C++ object type and id.
"""
pass
@bind
def push_back(self, item: List.Item) -> None:
pass
@bind
def pop_back(self) -> None:
pass
/**
* ______ .______ .______ .______ __ .__ __. _______
* / || _ \ | _ \ | _ \ | | | \ | | | \
* | ,----'| |_) | | |_) | | |_) | | | | \| | | .--. |
* | | | ___/ | ___/ | _ < | | | . ` | | | | |
* | `----.| | | | | |_) | | | | |\ | | '--' |
* \______|| _| | _| |______/ |__| |__| \__| |_______/
*
* This file is generated by cppbind on 08/14/2022-10:58.
* Please do not change it manually.
*/
import CWrapper
import Foundation
public typealias Item = List.Item
/// An example with multi level nested type using header_code_fragment to create an alias in target languages.
public class List {
/// An internal property to keep a reference to the original C++ object.
/// It is intended to be used by the generated code.
public let cself: CppBindCObject
/// An internal property to keep track whether Swift is responsible for deallocating the underlying C++ object or not.
/// It is intended to be used by the generated code.
public let owner: Bool
private var refs: [Any]
/// internal main initializer
internal required init(_ _cself: CppBindCObject, _ _owner: Bool = false) {
self.cself = _cself
self.owner = _owner
self.refs = []
}
deinit {
release_CppbindExample_List(cself, owner)
}
/// An internal method to bind the lifetimes of the current and another object.
/// It is intended to be used by the generated code.
public func keepCppBindReference(_ object: Any) {
self.refs.append(object)
}
public convenience init() {
var cppbindErr = CppBindCObject()
self.init(create_CppbindExample_List(&cppbindErr), true)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
}
public var back: List.Item {
var cppbindErr = CppBindCObject()
let result = _prop_get_CppbindExample_List_back(cself, &cppbindErr)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
var sctoswiftresult: List.Item
sctoswiftresult = List.Item(result)
return sctoswiftresult
}
public class Item {
/// An internal property to keep a reference to the original C++ object.
/// It is intended to be used by the generated code.
public let cself: CppBindCObject
/// An internal property to keep track whether Swift is responsible for deallocating the underlying C++ object or not.
/// It is intended to be used by the generated code.
public let owner: Bool
private var refs: [Any]
/// internal main initializer
internal required init(_ _cself: CppBindCObject, _ _owner: Bool = false) {
self.cself = _cself
self.owner = _owner
self.refs = []
}
deinit {
release_List_Item(cself, owner)
}
/// An internal method to bind the lifetimes of the current and another object.
/// It is intended to be used by the generated code.
public func keepCppBindReference(_ object: Any) {
self.refs.append(object)
}
public convenience init(v: List.Item.Value) {
let swifttoscv = v.cself
var cppbindErr = CppBindCObject()
self.init(create_List_Item(swifttoscv, &cppbindErr), true)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
}
public var value: List.Item.Value {
get {
let result = _prop_get_List_Item_value(cself)
var sctoswiftresult: List.Item.Value
sctoswiftresult = List.Item.Value(result)
sctoswiftresult.keepCppBindReference(self)
return sctoswiftresult
}
set(value) {
let swifttoscvalue = value.cself
_prop_set_List_Item_value(cself, swifttoscvalue)
}
}
public class Value {
/// An internal property to keep a reference to the original C++ object.
/// It is intended to be used by the generated code.
public let cself: CppBindCObject
/// An internal property to keep track whether Swift is responsible for deallocating the underlying C++ object or not.
/// It is intended to be used by the generated code.
public let owner: Bool
private var refs: [Any]
/// internal main initializer
internal required init(_ _cself: CppBindCObject, _ _owner: Bool = false) {
self.cself = _cself
self.owner = _owner
self.refs = []
}
deinit {
release_List_Item_Value(cself, owner)
}
/// An internal method to bind the lifetimes of the current and another object.
/// It is intended to be used by the generated code.
public func keepCppBindReference(_ object: Any) {
self.refs.append(object)
}
public convenience init(value: Int) {
let swifttoscvalue = CInt(value)
var cppbindErr = CppBindCObject()
self.init(create_List_Item_Value(swifttoscvalue, &cppbindErr), true)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
}
public var value: Int {
get {
let result = _prop_get_List_Item_Value_value(cself)
let sctoswiftresult = Int(result)
return sctoswiftresult
}
set(value) {
let swifttoscvalue = CInt(value)
_prop_set_List_Item_Value_value(cself, swifttoscvalue)
}
}
/// An internal property to keep an information about the underlying C++ object type.
/// It is intended to be used by the generated code.
class var cppbindCxxTypeName : String { return "cppbind::example::List::Item::Value" }
}
/// An internal property to keep an information about the underlying C++ object type.
/// It is intended to be used by the generated code.
class var cppbindCxxTypeName : String { return "cppbind::example::List::Item" }
}
public func push_back(item: List.Item) -> Void {
let swifttoscitem = item.cself
var cppbindErr = CppBindCObject()
_func_CppbindExample_List_push_back(cself, swifttoscitem, &cppbindErr)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
}
public func pop_back() -> Void {
var cppbindErr = CppBindCObject()
_func_CppbindExample_List_pop_back(cself, &cppbindErr)
if cppbindErr.type != nil {
let errorType = String(cString: cppbindErr.type!)
switch errorType {
case ("std::exception"):
let excObj = StdException(cppbindErr, true)
ExceptionHandler.handleUncaughtException(excObj.what())
default:
cppbindErr.type.deallocate()
ExceptionHandler.handleUncaughtException("Uncaught Exception")
}
}
}
/// An internal property to keep an information about the underlying C++ object type.
/// It is intended to be used by the generated code.
class var cppbindCxxTypeName : String { return "cppbind::example::List" }
}
public typealias Value = List.Item.Value