Thursday, June 30, 2022

Script Binding for C++ - Technologies

                                Image source https://tech.blueyonder.com/python-calling-c++/

SWIG

SWIG is an open-source utility that can be used to create bindings for C or C++ interfaces in a variety of high-level languages.


Supported Scripting Languages 

  • Perl
  • PHP
  • Python
  • Tcl
  • Ruby

Supported Non-Scripting Languages 

  • C#
  • Common Lisp
  • Java
  • Lua
  • Modula-3
  • OCAML
  • Octave
  • R

                                    source https://www.sobyte.net/post/2021-11/swig-python/

Python-Sip

  • SIP is a tool that lets you create C and C++ bindings for Python. It was originally created for the
  • Python-Sip tool can also be used to create bindings for any C++ API.
  • SIP works in a very similar fashion to SWIG, although it does not support the range of languages
  • that SWIG does. SIP supports much of the C/C++ syntax for its interface specification files and uses a similar syntax for its commands as SWIG  

Boost Python

  • Boost Python (also written as boost::python or Boost.Python) is a C++ library that lets C++ APIs interoperate with Python.
  • Boost Python allows to  create bindings programmatically in C++ code and then link the bindings against the Python and Boost Python libraries. 
  • This produces a dynamic library that can be imported directly into Python.
  • Boost Python includes support for the following capabilities and features in terms of wrapping C++ APIs:
    • C++ references 
    • C++ pointers
    • Translation of C++ exceptions to Python
    • C++ default arguments and Python keyword arguments
    • Manipulating Python objects in C++
    • Exporting C++ iterators as Python iterators
    • Python documentation strings
    • Globally registered type coercions
  • Example 

COM Automation

  • Component Object Model (COM) is a binary interface standard that allows objects to interact with each other via inter-process communication. 
  • COM objects specify well-defined interfaces that allow software components to be reused and linked together to build end-user applications. 
  • The technology was developed by Microsoft in 1993 and is still used today, predominantly on the Windows platform, although Microsoft now encourages the use of .NET and SOAP.
  • A COM object is identified by a Universally Unique ID (UUID) and exposes its functionality via interfaces that are also identified by UUIDs.
  • Supported languages  
    • Visual Basic 
    • JScript
    • Perl
    • Python
    • Ruby
    • Microsoft .NET .
  • The object model for the interface being exposed is described using an interface description language
  • (IDL).
  • IDL is a language-neutral description of a software component’s interface, normally
  • stored in a file with an .idl extension. 
  • This IDL description can then be translated into various forms using the MIDL.EXE compiler on Windows. 
  • The generated files include the proxy DLL code for the COM object and a type library that describes the object model. The following sample shows an example of the Microsoft IDL syntax:

CORBA

  • The Common Object Request Broker Architecture (CORBA) is an industry standard to allow software components to communicate with each other independent of their location and vendor. In this regard, it is very similar to COM: both technologies solve the problem of communication between objects from different sources and both make use of a language-neutral IDL format to describe each object’s interface.
  • CORBA is cross-platform with several open source implementations and provides strong support for UNIX platforms. It was defined by the Object Management Group in 1991 (the same group that manages the UML modeling language). 
  • CORBA offers a wide range of language bindings
    • Python
    • Perl
    • Ruby
    • Smalltalk
    • JavaScript
    • Tcl
    • CORBA Scripting Language (IDL script).
  • It also supports interfaces with multiple inheritance versus COM’s single inheritance.
  • In terms of scripting, CORBA doesn’t require a specific automation interface as COM does.
  • All CORBA objects are scriptable by default via the Dynamic Invocation Interface, which lets scripting languages determine the object’s interface dynamically. As an example of accessing CORBA objects from a scripting language

References




Script Binding for C++ - Introduction


                                                            source https://www.swig.org 


SCRIPT BINDINGS

Scripting will allow C++ API to be accessed from a scripting language, such as Python, Ruby, Lua, Tcl, or Perl. 

Extending


In extending client can use the library or API as a module in the client's own script

eg expat and md5 modules in the Python standard library

are implemented in C, not Python.

Embedding

In Embedding Client C++ application embeds a scripting language inside it, the script bindings are used to call the core functionality of the library or API

eg Adobe Director multimedia authoring platform, which embeds the
Lingo scripting language.

Why scripting?

  • Cross-platform
    • Scripting languages execute in ASCII source code or platform-independent byte code
    • Scripting languages can work on different platforms without modification 
    • Disadvantage - Scripting code will have to be distributed in source form
  • Faster development
    • In c++ any changes done to code has to compile and link
    • In the case of a large c++ module or API, this can be time-consuming
    • in scripting language there is no compile and link stage, therefore edit the code and just run it This allows you to prototype and test new changes quickly.
  • Write less code
    • Scripting languages don’t require explicit memory management, they tend to have a much larger standard library available than C++’s STL, and often take care of complex concepts such as reference counting behind the scenes.
  • Support for expert users
    • Adding a scripting language to an end-user application can allow advanced users to customize the functionality of the application by writing macros to perform repetitive tasks or tasks that are not exposed through the GUI.
  • Extensibility
    • A scripting interface can be used to let users add entirely new functionality to the application through plugin interfaces. users have the power to solve their own problems.
    • For example, the Firefox Web browser allows new extensions to be created using JavaScript as its embedded scripting language.
  • Testability 
    • Scripting languages can have automated tests using that language
    • QA engineers will not write C++ code. but QA can write scripting languages
  • Expressiveness.  
    • Flexibility and ease of use of a programming language could impact the kinds of solutions that you can envision. That’s because you don’t have to be distracted by low-level issues such as memory management or statically typed data representations.









LeetCode C++ Cheat Sheet June

🎯 Core Patterns & Representative Questions 1. Arrays & Hashing Two Sum – hash map → O(n) Contains Duplicate , Product of A...