Click here to Skip to main content
15,887,812 members
Articles / All Topics

Multi-Language Programming: More Complicated Function Calls Between Languages: 5

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
20 Oct 2011CPOL2 min read 15.5K  
Doing more complicated function calls between languages.

Components or libraries provide some functions. Other modules may access these functions through their interface. In some cases, the interface functions may be simple. The caller constructs input arguments, calls the function, and gets the return value. These type interfaces are easy to define and have no more restrictions. But in other cases, interface functions may be more complicated. Let’s use an example to explain this in detail.

Suppose that there is a C/C++ component. It provides a function to draw and fill a circle into memory pixel buffer. If the component uses a simple interface, it should take parameters as input arguments, such as line type, color, width, fill color, fill pattern, fill bitmap, etc. We define an interface object that contains this function in a common object environment. The function may be called by other languages, for example, Java.

270968/q8EqOmMQia9ilVbtCJZzeQ.jpg

In this case, there are some disadvantages. First, the interface function may have many input arguments, and for each call, these arguments should be input although their values do not change. Second, if a new parameter is added, all related functions must be changed. This will increase the burden of software maintenance.

In general, we often define two other objects: pen and brush objects. The draw function takes pen and brush objects as input arguments. The pen object has attributes color, type, and width, and the brush object has attributes color, pattern, image, etc. We also define an interface object that contains the draw function in the common object environment. As a result, there are three objects defined by C/C++ in the environment, which are pen, brush, and the object containing the draw function. For other languages using the function, for example Java, it should create instances of the three interface class objects. And the instances can be accessed by C/C++ and Java language based on the support of the environment.

270968/9aSlrxnVah9d9gnskJ8z5A.jpg

In this case, the function call is more complicated. The caller needs to create instances of objects defined in C/C++. The instances can be accessed by the two languages. They are used as input arguments of the draw function. Without the support of common object environment, it is difficult to realize it in this way, especially to support the call from applications programmed with multiple different languages.

It is clear that the C/C++ service can be called by any other language. Is it easy and concise? You can compare it with traditional methods using the JNI interface.

This article was originally posted at http://blog.yahoo.com/srplab/articles/76836

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --