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
 * package: simple
 * python.package: simple.task
 * python.name: PyTask
 */
class Task {
public:
    /**
     * 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 05/12/2022-10:29.
 * Please do not change it manually.
 */

package com.examples.simple

import com.examples.cppbind.alias.*
import com.examples.cppbind.exceptionUtils.*
import com.examples.cppbind.exception_helpers.*

/**
 * Class holding task information.
 */
open class Task
internal constructor(obj: CppBindObject) : AutoCloseable {
    companion object {
        init {
            System.loadLibrary("wrapper_jni")
        }
        
        /**
         * Task Constructor.
         */
        protected fun construct_helper(title: String): Long {
            val id = jConstructor(title)
            return id
        }

        @JvmStatic
        private external fun jConstructor(title: String): Long
        const val cppbindCxxTypeName: String = "cppbind::example::Task"
    }
    
    protected var cppbindObj = obj
    private var refs: MutableList<Any> = mutableListOf()

    fun keepCppBindReference(ref: Any) {
        refs.add(ref)
    }
    
    open val id: Long
        get() {
            if (cppbindObj.id == 0L) {
                throw RuntimeException("Object is not allocated")
            }
            return cppbindObj.id
        }
    
    /**
     * Task Constructor.
     */
    constructor(title: String): this(CppBindObject(construct_helper(title), true)) {
    }
    
    /**
     * Get objects title.
     */
    val title: String
        get() {
            val result = jTitle(id)
            
            return result
        }
    
    /**
     * Get objects title.
     */
    fun setTitle(title: String): Unit {
        val result = jSettitle(id, title)
        
        return result
    }

    fun equals(t: Task): Boolean {
        val kotlintojdkt = t.id
        val result = jEquals(id, kotlintojdkt)
        
        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 jTitle(id: Long): String
    private external fun jSettitle(id: Long, title: String): Unit
    private external fun jEquals(id: Long, t: Long): Boolean
    private external fun jFinalize(id: Long): Unit
}

private external fun jGettypebyid(id: Long): String
"""
  ______ .______   .______   .______    __  .__   __.  _______  
 /      ||   _  \  |   _  \  |   _  \  |  | |  \ |  | |       \ 
|  ,----'|  |_)  | |  |_)  | |  |_)  | |  | |   \|  | |  .--.  |
|  |     |   ___/  |   ___/  |   _  <  |  | |  . `  | |  |  |  |
|  `----.|  |      |  |      |  |_)  | |  | |  |\   | |  '--'  |
 \______|| _|      | _|      |______/  |__| |__| \__| |_______/ 

This file is generated by cppbind on 05/12/2022-10:33.
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.bind_utils_pygen import *
from examples_lib.cppbind.metaclass_pygen import *


class PyTask(metaclass=CppBindMetaclass):
    """
    Class holding task information.
    Documentation generated from: `cxx/simple/task.hpp#L19
    <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/simple/task.hpp#L19>`_
    """
    
    @bind
    def __init__(self, title: str):
        """
        Task Constructor.
        Documentation generated from: `cxx/simple/task.hpp#L28
        <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/simple/task.hpp#L28>`_
        """
        pass
    
    @property
    @bind
    def title(self) -> str:
        """
        Get objects title.
        Documentation generated from: `cxx/simple/task.hpp#L37
        <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/simple/task.hpp#L37>`_
        """
        pass
    
    @bind
    def set_title(self, title: str) -> None:
        """
        Get objects title.
        Documentation generated from: `cxx/simple/task.hpp#L48
        <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/simple/task.hpp#L48>`_
        """
        pass

    @bind
    def equals(self, t: PyTask) -> bool:
        """
        Documentation generated from: `cxx/simple/task.hpp#L57
        <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/simple/task.hpp#L57>`_
        """
        pass
/**
 *   ______ .______   .______   .______    __  .__   __.  _______  
 *  /      ||   _  \  |   _  \  |   _  \  |  | |  \ |  | |       \ 
 * |  ,----'|  |_)  | |  |_)  | |  |_)  | |  | |   \|  | |  .--.  |
 * |  |     |   ___/  |   ___/  |   _  <  |  | |  . `  | |  |  |  |
 * |  `----.|  |      |  |      |  |_)  | |  | |  |\   | |  '--'  |
 *  \______|| _|      | _|      |______/  |__| |__| \__| |_______/ 
 * 
 * This file is generated by cppbind on 05/12/2022-10:26.
 * Please do not change it manually.
 */

import CWrapper
import Foundation

/**
 * Class holding task information.
 */
public class Task {

  public let cself: CppBindCObject
  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)
  }

  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 func equals(t: Task) -> Bool {

    let swifttosct = t.cself
    var cppbindErr = CppBindCObject()
    let result = _func_CppbindExample_Task_equals(cself, swifttosct, &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
  }

  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 05/12/2022-10:29.
 * Please do not change it manually.
 */

package com.examples.simple

import com.examples.cppbind.alias.*
import com.examples.cppbind.exceptionUtils.*
import com.examples.cppbind.exception_helpers.*

/**
 * Class holding project information.
 */
open class Project
internal constructor(obj: CppBindObject) : AutoCloseable {
    companion object {
        init {
            System.loadLibrary("wrapper_jni")
        }
        
        /**
         * Project constructor.
         */
        protected fun construct_helper(title: String): Long {
            val id = jConstructor(title)
            return id
        }

        @JvmStatic
        private external fun jConstructor(title: String): Long
        const val cppbindCxxTypeName: String = "cppbind::example::Project"
    }
    
    protected var cppbindObj = obj
    private var refs: MutableList<Any> = mutableListOf()

    fun keepCppBindReference(ref: Any) {
        refs.add(ref)
    }
    
    open val id: Long
        get() {
            if (cppbindObj.id == 0L) {
                throw RuntimeException("Object is not allocated")
            }
            return cppbindObj.id
        }
    
    /**
     * Project constructor.
     */
    constructor(title: String): this(CppBindObject(construct_helper(title), true)) {
    }
    
    /**
     * Get project´s title.
     */
    val title: String
        get() {
            val result = jTitle(id)
            
            return result
        }
    
    /**
     * Add a task to project.
     */
    fun addTask(task: Task): Unit {
        val kotlintojdktask = task.id
        val result = jAddtask(id, kotlintojdktask)
        
        return result
    }

    /**
     * Get project´s tasks.
     */
    fun tasks(): List<Task> {
        val result = jTasks(id)
        val jdktokotlinresult: MutableList<Task> = mutableListOf()
        for (value_result in result) {
            val jdktokotlinvalue_result = Task(CppBindObject(value_result))
            jdktokotlinresult.add(jdktokotlinvalue_result)
        }
        return jdktokotlinresult
    }

    fun equals(p: Project): Boolean {
        val kotlintojdkp = p.id
        val result = jEquals(id, kotlintojdkp)
        
        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 jTitle(id: Long): String
    private external fun jAddtask(id: Long, task: Long): Unit
    private external fun jTasks(id: Long): LongArray
    private external fun jEquals(id: Long, p: Long): Boolean
    private external fun jFinalize(id: Long): Unit
}

private external fun jGettypebyid(id: Long): String
"""
  ______ .______   .______   .______    __  .__   __.  _______  
 /      ||   _  \  |   _  \  |   _  \  |  | |  \ |  | |       \ 
|  ,----'|  |_)  | |  |_)  | |  |_)  | |  | |   \|  | |  .--.  |
|  |     |   ___/  |   ___/  |   _  <  |  | |  . `  | |  |  |  |
|  `----.|  |      |  |      |  |_)  | |  | |  |\   | |  '--'  |
 \______|| _|      | _|      |______/  |__| |__| \__| |_______/ 

This file is generated by cppbind on 05/12/2022-10:33.
Please do not change it manually.
"""
from __future__ import annotations

from typing import *

import examples.simple.project as pybind_project_pygen
import examples_lib.simple.task.task_pygen as task_task_pygen
from examples_lib.cppbind.bind_utils_pygen import *
from examples_lib.cppbind.metaclass_pygen import *


class Project(metaclass=CppBindMetaclass):
    """
    Class holding project information.
    Documentation generated from: `cxx/simple/project.hpp#L18
    <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/simple/project.hpp#L18>`_
    """
    
    @bind
    def __init__(self, title: str):
        """
        Project constructor.
        Documentation generated from: `cxx/simple/project.hpp#L26
        <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/simple/project.hpp#L26>`_
        """
        pass
    
    @property
    @bind
    def title(self) -> str:
        """
        Get project´s title.
        Documentation generated from: `cxx/simple/project.hpp#L34
        <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/simple/project.hpp#L34>`_
        """
        pass
    
    @bind
    def add_task(self, task: task_task_pygen.PyTask) -> None:
        """
        Add a task to project.
        Documentation generated from: `cxx/simple/project.hpp#L45
        <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/simple/project.hpp#L45>`_
        """
        pass

    @bind
    def tasks(self) -> List[task_task_pygen.PyTask]:
        """
        Get project´s tasks.
        Documentation generated from: `cxx/simple/project.hpp#L56
        <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/simple/project.hpp#L56>`_
        """
        pass

    @bind
    def equals(self, p: Project) -> bool:
        """
        Documentation generated from: `cxx/simple/project.hpp#L65
        <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/simple/project.hpp#L65>`_
        """
        pass
/**
 *   ______ .______   .______   .______    __  .__   __.  _______  
 *  /      ||   _  \  |   _  \  |   _  \  |  | |  \ |  | |       \ 
 * |  ,----'|  |_)  | |  |_)  | |  |_)  | |  | |   \|  | |  .--.  |
 * |  |     |   ___/  |   ___/  |   _  <  |  | |  . `  | |  |  |  |
 * |  `----.|  |      |  |      |  |_)  | |  | |  |\   | |  '--'  |
 *  \______|| _|      | _|      |______/  |__| |__| \__| |_______/ 
 * 
 * This file is generated by cppbind on 05/12/2022-10:26.
 * Please do not change it manually.
 */

import CWrapper
import Foundation

/**
 * Class holding project information.
 */
public class Project {

  public let cself: CppBindCObject
  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)
  }

  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 func equals(p: Project) -> Bool {

    let swifttoscp = p.cself
    var cppbindErr = CppBindCObject()
    let result = _func_CppbindExample_Project_equals(cself, swifttoscp, &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
  }

  class var cppbindCxxTypeName : String { return "cppbind::example::Project" }
}

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 05/12/2022-10:29.
 * Please do not change it manually.
 */

package com.examples.nested_types

import com.examples.cppbind.alias.*
import com.examples.cppbind.exceptionUtils.*
import com.examples.cppbind.exception_helpers.*

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 construct_helper(): Long {
            val id = jConstructor()
            return id
        }

        @JvmStatic
        private external fun jConstructor(): Long
        const val cppbindCxxTypeName: String = "cppbind::example::List"
    }
    
    protected var cppbindObj = obj
    private var refs: MutableList<Any> = mutableListOf()

    fun keepCppBindReference(ref: Any) {
        refs.add(ref)
    }
    
    open val id: Long
        get() {
            if (cppbindObj.id == 0L) {
                throw RuntimeException("Object is not allocated")
            }
            return cppbindObj.id
        }
    
    constructor(): this(CppBindObject(construct_helper(), true)) {
    }
    
    val back: List.Item
        get() {
            val result = jBack(id)
            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 construct_helper(v: List.Item.Value): Long {
                val kotlintojdkv = v.id
                val id = jConstructor(kotlintojdkv)
                return id
            }

            @JvmStatic
            private external fun jConstructor(v: Long): Long
            const val cppbindCxxTypeName: String = "cppbind::example::List::Item"
        }
        
        protected var cppbindObj = obj
        private var refs: MutableList<Any> = mutableListOf()

        fun keepCppBindReference(ref: Any) {
            refs.add(ref)
        }
        
        open val id: Long
            get() {
                if (cppbindObj.id == 0L) {
                    throw RuntimeException("Object is not allocated")
                }
                return cppbindObj.id
            }
        
        constructor(v: List.Item.Value): this(CppBindObject(construct_helper(v), true)) {
        }
        
        var value: List.Item.Value
            get() {
                val result = jValue(id)
                val jdktokotlinresult = List.Item.Value(CppBindObject(result))
                jdktokotlinresult.keepCppBindReference(this)
                return jdktokotlinresult
            }
            set(value) {
                val kotlintojdkvalue = value.id
                jSetvalue(id, kotlintojdkvalue)
            }
            
        
        open class Value
        internal constructor(obj: CppBindObject) : AutoCloseable {
            companion object {
                init {
                    System.loadLibrary("wrapper_jni")
                }
                
                protected fun construct_helper(_value: Int): Long {
                    val id = jConstructor(_value)
                    return id
                }

                @JvmStatic
                private external fun jConstructor(_value: Int): Long
                const val cppbindCxxTypeName: String = "cppbind::example::List::Item::Value"
            }
            
            protected var cppbindObj = obj
            private var refs: MutableList<Any> = mutableListOf()

            fun keepCppBindReference(ref: Any) {
                refs.add(ref)
            }
            
            open val id: Long
                get() {
                    if (cppbindObj.id == 0L) {
                        throw RuntimeException("Object is not allocated")
                    }
                    return cppbindObj.id
                }
            
            constructor(_value: Int): this(CppBindObject(construct_helper(_value), true)) {
            }
            
            var value: Int
                get() {
                    val result = jValue(id)
                    
                    return result
                }
                set(value) {
                    
                    jSetvalue(id, value)
                }
                

            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): Unit
            private external fun jFinalize(id: Long): Unit
        }

        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): Unit
        private external fun jFinalize(id: Long): Unit
    }

    fun push_back(item: List.Item): Unit {
        val kotlintojdkitem = item.id
        val result = jPush_back(id, kotlintojdkitem)
        
        return result
    }

    fun pop_back(): Unit {
        val result = jPop_back(id)
        
        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): Unit
    private external fun jPop_back(id: Long): Unit
    private external fun jBack(id: Long): Long
    private external fun jFinalize(id: Long): Unit
}

typealias Value = List.Item.Value


private external fun jGettypebyid(id: Long): String
"""
  ______ .______   .______   .______    __  .__   __.  _______  
 /      ||   _  \  |   _  \  |   _  \  |  | |  \ |  | |       \ 
|  ,----'|  |_)  | |  |_)  | |  |_)  | |  | |   \|  | |  .--.  |
|  |     |   ___/  |   ___/  |   _  <  |  | |  . `  | |  |  |  |
|  `----.|  |      |  |      |  |_)  | |  | |  |\   | |  '--'  |
 \______|| _|      | _|      |______/  |__| |__| \__| |_______/ 

This file is generated by cppbind on 05/12/2022-10:33.
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.bind_utils_pygen import *
from examples_lib.cppbind.metaclass_pygen import *


class List(metaclass=CppBindMetaclass):
    """
    An example with multi level nested type using header_code_fragment to create an alias in target languages.
    Documentation generated from: `cxx/nested_types/list.hpp#L23
    <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/nested_types/list.hpp#L23>`_
    """
    
    @bind
    def __init__(self):
        """
        Documentation generated from: `cxx/nested_types/list.hpp#L69
        <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/nested_types/list.hpp#L69>`_
        """
        pass
    
    @property
    @bind
    def back(self) -> List.Item:
        """
        Documentation generated from: `cxx/nested_types/list.hpp#L95
        <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/nested_types/list.hpp#L95>`_
        """
        pass
    

    class Item(metaclass=CppBindMetaclass):
        """
        Documentation generated from: `cxx/nested_types/list.hpp#L30
        <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/nested_types/list.hpp#L30>`_
        """
        
        @bind
        def __init__(self, v: List.Item.Value):
            """
            Documentation generated from: `cxx/nested_types/list.hpp#L61
            <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/nested_types/list.hpp#L61>`_
            """
            pass
        
        @property
        @bind
        def value(self) -> List.Item.Value:
            """
            Documentation generated from: `cxx/nested_types/list.hpp#L54
            <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/nested_types/list.hpp#L54>`_
            """
            pass

        @value.setter
        @bind
        def value(self, value: List.Item.Value):
            """
            Documentation generated from: `cxx/nested_types/list.hpp#L54
            <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/nested_types/list.hpp#L54>`_
            """
            pass
        

        class Value(metaclass=CppBindMetaclass):
            """
            Documentation generated from: `cxx/nested_types/list.hpp#L36
            <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/nested_types/list.hpp#L36>`_
            """
            
            @bind
            def __init__(self, _value: int):
                """
                Documentation generated from: `cxx/nested_types/list.hpp#L48
                <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/nested_types/list.hpp#L48>`_
                """
                pass
            
            @property
            @bind
            def value(self) -> int:
                """
                Documentation generated from: `cxx/nested_types/list.hpp#L41
                <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/nested_types/list.hpp#L41>`_
                """
                pass

            @value.setter
            @bind
            def value(self, value: int):
                """
                Documentation generated from: `cxx/nested_types/list.hpp#L41
                <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/nested_types/list.hpp#L41>`_
                """
                pass

    @bind
    def push_back(self, item: List.Item) -> None:
        """
        Documentation generated from: `cxx/nested_types/list.hpp#L76
        <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/nested_types/list.hpp#L76>`_
        """
        pass

    @bind
    def pop_back(self) -> None:
        """
        Documentation generated from: `cxx/nested_types/list.hpp#L85
        <https://github.com/PicsArt/cppbind/tree/master/examples/primitives/cxx/nested_types/list.hpp#L85>`_
        """
        pass
/**
 *   ______ .______   .______   .______    __  .__   __.  _______  
 *  /      ||   _  \  |   _  \  |   _  \  |  | |  \ |  | |       \ 
 * |  ,----'|  |_)  | |  |_)  | |  |_)  | |  | |   \|  | |  .--.  |
 * |  |     |   ___/  |   ___/  |   _  <  |  | |  . `  | |  |  |  |
 * |  `----.|  |      |  |      |  |_)  | |  | |  |\   | |  '--'  |
 *  \______|| _|      | _|      |______/  |__| |__| \__| |_______/ 
 * 
 * This file is generated by cppbind on 05/12/2022-10:26.
 * 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 {

  public let cself: CppBindCObject
  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)
  }

  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 {

    public let cself: CppBindCObject
    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)
    }

    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 {

      public let cself: CppBindCObject
      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)
      }

      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)
        }
      }

      class var cppbindCxxTypeName : String { return "cppbind::example::List::Item::Value" }
    }

    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")
      }
    }
  }

  class var cppbindCxxTypeName : String { return "cppbind::example::List" }
}

public typealias Value = List.Item.Value