Kakadu and Java. David Taubman, UNSW June 3, 2003

Similar documents
Lecture Notes on Garbage Collection

Chapter 1 Getting Started

Wrapping a complex C++ library for Eiffel. FINAL REPORT July 1 st, 2005

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

Cpt S 122 Data Structures. Introduction to C++ Part II

Motivation was to facilitate development of systems software, especially OS development.

Contents. Chapter 1 Overview of the JavaScript C Engine...1. Chapter 2 JavaScript API Reference...23

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Object-Oriented Programming

Introduce C# as Object Oriented programming language. Explain, tokens,

Motivation was to facilitate development of systems software, especially OS development.

Object Oriented Design

CS201 Some Important Definitions

C# and Java. C# and Java are both modern object-oriented languages

Design Principles for a Beginning Programming Language

Instantiation of Template class

Objects Managing a Resource

QUIZ. What is wrong with this code that uses default arguments?

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Project 1: Remote Method Invocation CSE 291 Spring 2016

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

D Programming Language

CS 231 Data Structures and Algorithms, Fall 2016

Short Notes of CS201

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n

CS201 - Introduction to Programming Glossary By

Lecture 18 Tao Wang 1

STRUCTURING OF PROGRAM

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Interview Questions of C++

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

A brief introduction to C programming for Java programmers

Sample Copy. Not for Distribution.

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

StackVsHeap SPL/2010 SPL/20

Separate Compilation Model

CSE 307: Principles of Programming Languages

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

CS112 Lecture: Working with Numbers

Derived and abstract data types. TDT4205 Lecture 15

CS3157: Advanced Programming. Outline

Compilers Project 3: Semantic Analyzer

A brief introduction to C++

Preliminaries. Part I

Chapter 10 Introduction to Classes

Object Oriented Programming with c++ Question Bank

JVM ByteCode Interpreter

G Programming Languages - Fall 2012

Chapter 4 Defining Classes I

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

G52CPP C++ Programming Lecture 16

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

CPS 506 Comparative Programming Languages. Programming Language

APPENDIX A : Example Standard <--Prev page Next page -->

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Programming Languages Third Edition. Chapter 7 Basic Semantics

JAVA An overview for C++ programmers

The Design of Core C++ (Notes)

Structure of Programming Languages Lecture 10

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Concurrent Objects and Linearizability

Programming computer vision in C / C++

Pointers and Terminal Control

Lecture Topics. Administrivia

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

377 Student Guide to C++

C#: framework overview and in-the-small features

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

CS558 Programming Languages

Ch. 11: References & the Copy-Constructor. - continued -

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Lecture Notes on Memory Layout

2 ADT Programming User-defined abstract data types

Lecture Notes on Queues

05-01 Discussion Notes

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Array. Prepared By - Rifat Shahriyar

GEA 2017, Week 4. February 21, 2017

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Functions, Pointers, and the Basics of C++ Classes

C++ (Non for C Programmer) (BT307) 40 Hours

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Principles of Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

VALLIAMMAI ENGINEERING COLLEGE

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Compiler Construction D7011E

What goes inside when you declare a variable?

Assumptions. History

Object-Oriented Principles and Practice / C++

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

Programs in memory. The layout of memory is roughly:

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Transcription:

Kakadu and Java David Taubman, UNSW June 3, 2003 1 Brief Summary The Kakadu software framework is implemented in C++ using a fairly rigorous object oriented design strategy. All classes which are intended for public use are documented by comments in public header files, using a set of comment conventions which allows the kdu_hyperdoc utility to build high quality HTML documentation describing the classes and their interaction with each other. The same comments may embed special directives which allow kdu_hyperdoc to construct Java native interfaces to the classes in a meaningful and efficient manner. Virtually all high level Kakadu classes and virtually all of their member functions can be presented to Java applications using these tools. Moreover, by following the same conventions, you can get kdu_hyperdoc tobuildjava native interfaces to your own C++ classes, which might wrap up certain subsets of the Kakadu functionality efficiently. kdu_hyperdoc also tightly integrates the Java and native C++ HTML documentation which it builds. In addition to providing downward calls into the Kakadu system from Java, kdu_hyperdoc supports the construction of Java bindings in which the native code can implicitly call upward into a Java implementation. In particular, C++ virtual functions can be assigned a callback binding, which causes code to be constructed which calls the functions of an implementing Java class whenever the C++ function is invoked. This capability is currently used to bring all Kakadu system messages into Java in an elegant manner. Since Kakadu emphasizes robustness, considerable attention has been paid to exception handling in building the Java native interfaces. Exceptions can be thrown and caught across the Kakadu native interfaces, including and especially when the exceptions are thrown inside callback functions implemented in Java. The principle source of exceptions is generally the code which handles Kakadu system errors, which can and often should be implemented in Java for Javabased applications. 1

c Taubman, 2001 Kakadu and Java Page 2 2 Purpose One reason for building Java interfaces is to avoid platform dependencies in the implementation of certain applications. Although the native Kakadu implementation must be compiled for the platform of interest and shipped as binaries, the vast majority of the Kakadu system (the core system, plus most application specific add-ons) should compile on virtually any platform. However, graphical user interfaces and networking elements of an application must often be implemented differently for different platforms. You can now implement these elements in Java. Many of our clients have asked us about Java interfaces to the Kakadu system, because they like the features and efficiency of the Kakadu implementation, but prefer to build systems based around Java. The new Java interfaces are intended to fulfill the needs of these customers. 3 Getting Started First, you will need to compile the Java interfaces. For this, you should refer to the end of the compilation_instructions.txt file, which also provides a brief explanation of Java native interfaces in general. After reading through the explanatory notes in the next section, you should go ahead and look through the one Java example application which is currently supplied with Kakadu. It is called KduRender.java and may be found in the java/apps sub-directory, inside the Kakadu version directory which you were shipped with your license. This demo application is pretty simple, but deliberately so. Also, those of you who are Java nuts, will have to excuse my almost complete lack of fluency in Java. The KduRender.java application basically does the same kind of thing as the kdu_render native C++ application and many lines of code are deliberately shared in common between the two. Both are (at least in theory) fully platform independent, but KduRender.java writes its output incrementally to a popup image window, while kdu_render writes its output to a file the only option for platform independent native code. 4 Kakadu Calls in Java After a few initial tips, most of the Java native calls should become pretty self explanatory. Firstly, since there are no global functions, #defines, or enum types in Java, these are all lumped into a single Java class known as Kdu_globals, which contains only static functions and static, final variable declarations. There are not many global functions in Kakadu, but there are quite a few constants, which you will be glad to find available in Kdu_globals. Only those constants for which kdu_hyperdoc was able to find a type binding are included there, but all the constants you need should be of this form, due to the way the Kakadu header

c Taubman, 2001 Kakadu and Java Page 3 files have been written. For example, Kakadu header files define a constant like HH_BAND as rather than just #define HH_BAND ((int) 3) #define HH_BAND 3 This gives kdu_hyperdoc the information it needs to include the definition in the Kdu_globals Javaclass. The second convention you will want to note is that C++ class names are mapped to Java class names by capitalizing their first letter (I always use strictly lower-case names with underscores for my C++ names). Similarly, all function names are mapped to Java by capitalizing the first letter in the C++ function. Although this is not a recommended Java convention, it helps to avoid the accidental use of names with reserved meaning in Java. In particular, Java assigns a special meaning to class members with the name finalize, while Kakadu has a number of classes with their own finalize functions. These become Finalize injava. The Java interfaces are pure function-only interfaces. There are no public member variables, except for the native_ptr member in every exported class you should never modify this from Java, but you can safely copy it to create two Java classes which refer to exactly the same native object. Some functions do not bind well to Java. Among these are the C++ operator overloads, for which there is no equivalent in Java. To overcome these difficulties, some extra functions have been added to some of the Kakadu classes, to provide alternatives which do bind well. Extra functions have also been added to the small number of publically accessible Kakadu classes (or structs) which offer public member variables. These extra functions allow the underlying member variables to be manipulated safely across the Java bindings. The kdu_hyperdoc maps unsigned 32-bit integers to Java long, and maps unsigned 16-bit integers to Java int. Signed quantities are mapped in the most natural fashion, with 16, 32 and 64-bit integers mapped to Java s short, int and long, respectively. While these conventions are reasonable, they may prevent the disambiguation of overloaded C++ functions. In these cases, you must explicitly mark one or more of the offending C++ functions as having no Java binding. Do this by including the directive [BIND: no-java] in the comment description following the function s declaration. One of the complications which can arise when building Java native interfaces is that object destruction is not explicit in Java. This can damage memory efficient implementations, so we explicitly provide Destroy members for all Java objects which are bound to C++ objects that can be destroyed. The Destroy function will destroy the internal C++ representation, after which you should also throw away your reference to the Java object its native_ptr member will become null.

c Taubman, 2001 Kakadu and Java Page 4 Those of you who have worked with Kakadu for some time will note that we have significantly rationalized Kakadu s messaging and error/warning handling mechanisms. The purpose is twofold. Firstly, there is no longer any dependence upon C++ stream I/O and secondly, all of the messaging capabilities bind well to Java and allow errors and warnings to be caught and used in Java, whether they are generated from Java code or from the native code. To see how this works, you should consult the descriptions of kdu_message, kdu_message_formatter, kdu_customize_errors and kdu_customize_warnings in the HTML documentation and then review the examples of their usage in the KduRender.java application. One thing which might puzzle you at first is that kdu_error and kdu_warning objects do not have Java native bindings (you will note the absence of the [BIND: reference] tag from the descriptions of these classes in kdu_message.h ). This is deliberate, because these objects must never be created on the heap, only on the stack as local variables, and the Java bindings create all objects on the heap. The reason why these objects should never be created on the heap is that their destructors never complete they either exit the process or throw an exception. Nevertheless, the absence of these functions should cause no problem for you, because we have two new global function calls, kdu_print_error and kdu_print_warning, which create kdu_error and kdu_warning objects internally on the stack and destroy them immediately after delivering the relevant message string. These functions bind well to Java and work well with Java s excellent string construction syntax. You may also find these functions convenient alternatives for your C++ development. Be sure to refer to the HTML documentation rooted at the index.html file in the documentation sub-directory. If you build the interfaces and documentation in the manner recommended in the compilation_instructions.txt file, all Java functions will be documented along with their C++ counterparts. This allows you to rapidly identify which functions are available in Java and which are not. It also allows you to see how objects, arrays, reference arguments, etc., are passed across the Java interfaces. You will soon get the hang of our argument binding conventions. 5 Building Your Own Java Interfaces You can put kdu_hyperdoc toworkforyoutobuildjavanativeinterfacesto your own classes. Simply include the relevant header file in the list supplied to kdu_hyperdoc and include the relevant binding directives in comments attached to your C++ class declaration and optionally its member functions. There quite a number of subtleties you might come across, but most of them should be pointed out to you by helpful messages from kdu_hyperdoc if you do something which will cause problems. Class bindings recognized by kdu_hyperdoc fall into three categories, signalled by the [BIND: reference], [BIND: interface] and [BIND: copy] directives. The most useful and powerful of these is [BIND: reference], and

c Taubman, 2001 Kakadu and Java Page 5 you are recommended to use it everywhere, since the other bindings may not be used with derived classes. The [BIND: interface] optionisusedinspecial circumstances to avoid inefficient use of the heap when the C++ class is actually an interface to a separate internal C++ object. Classes specifying this binding must contain no virtual functions whatsoever, may not be derived, and may contain no member variables other than a single memory pointer. The [BIND: copy] option is used only for simple C++ objects, whose size is known (no derivation, or virtual functions), which can reasonably be passed by value across C++ functions. The only examples of such classes in Kakadu are kdu_coords and kdu_dims. As already mentioned, unless you really know what you are doing, you should use the [BIND: reference] convention. kdu_hyperdoc will automatically try to create Java bindings for all member functions of classes which specify one of the bindings mentioned above, except for functions which contain the [BIND: no-java] directive mentioned in the previous section. You should be aware of two additional binding directives which can be added to the comments appearing after a function declaration in your header file. These are [BIND: donate] and [BIND: callback]. The former specifies that the function call will donate ownership of the object from which it is invoked to some other object. This signals to Java that the C++ object should not be automatically destroyed if the Java object s finalize function is called during garbage collection. As a general rule, the C++ object belonging to any Java binding class which was directly instantiated from Java will be automatically destroyed during garbage collection, unless it has been donated. You can also provide the [BIND: donate] directive inside comments describing a specific function argument, in which case it has the reverse semantics. Specifically, in that case the directive indicates that the C++ object embedded in the function argument is being donated to the object whose function is being called. The [BIND: callback] directive has a very considerable impact on the construction of Java native bindings. It may only be applied to virtual C++ functions (not necessarily pure) of classes with the [BIND: reference] binding. Moreover, the option may currently only be applied to functions which take primitive arguments and return primitive types (or void). Callback functions must be meaningfully implemented in a derived class. They are implicitly called from one of the other member functions of the class. When this happens, the Java binding code will cause the Java function to be called. To do something meaningful, the Java class must be derived by you, the developer, providing a suitable implementation of the callback function. For an example of this, see the Kdu_sysout_message object, declared in KduRender.java. Note that you ARE allowed to throw Java exceptions from your implemention of the callback function.