diff --git a/.classpath b/.classpath deleted file mode 100644 index 3ed2f53..0000000 --- a/.classpath +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/.project b/.project deleted file mode 100644 index 48facef..0000000 --- a/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - github-intarsys-native-c - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/README.textile b/README.md similarity index 64% rename from README.textile rename to README.md index ec569d3..b16df0d 100644 --- a/README.textile +++ b/README.md @@ -1,33 +1,29 @@ -h1. intarsys native-c +# intarsys native-c -h2. Overview +## Overview This is "another" JNI wrapping library, but in some regards quite different. -p. The real memory / JNI access semantics is delegated to some concrete service implementation. Actually we don't provide one of our own, the default implementation we use is JNA. -p. The real strength of this library is its high level model of JNI access, allowing more easy and understandable API's. We feel that at this point most of the candidates we reviewed are quite poor or hard to understand. -p. The design goals were as follows: -# Provide access to all levels of a native interface. As we can not forsee what different libaries and systems will work together, access is provided from bottom up using the plain address as the least common denominator. -# Provide clean abstractions -# Differentiate between a "pointer" or "memory handle" and the data semantics. This allows to work as well using plain pointer arithmetics as object state access. +1. Provide access to all levels of a native interface. As we can not forsee what different libaries and systems will work together, access is provided from bottom up using the plain address as the least common denominator. +2. Provide clean abstractions +3. Differentiate between a "pointer" or "memory handle" and the data semantics. This allows to work as well using plain pointer arithmetics as object state access. -p. The high level model provides the following abstractions: -# Interface The concrete implementation for accessing JNI. This interface is quite small and allows for the adaption of many JNI libraries out there. It should not be to hard to create one of your own. -# Library The loadable library providing state and behavior. -# Function The behavior part of the library, accepting parameters and return results. -# Handle An abstraction of a pointer, without any data specific features. This could be viewed as a plain wrapper around an address, another representation for "void *". -# Object The data referenced by a handle (or pointer). The object deals with the bytes referenced by the handle. +1. Interface The concrete implementation for accessing JNI. This interface is quite small and allows for the adaption of many JNI libraries out there. It should not be to hard to create one of your own. +2. Library The loadable library providing state and behavior. +3. Function The behavior part of the library, accepting parameters and return results. +4. Handle An abstraction of a pointer, without any data specific features. This could be viewed as a plain wrapper around an address, another representation for "void *". +5. Object The data referenced by a handle (or pointer). The object deals with the bytes referenced by the handle. The object defines the semantics of how to interpret the memory referenced by the handle. The handle defines "where" to find data, the object defines "what" data you will find there - @@ -37,26 +33,23 @@ On the C side, the object is always represented by the handle / pointer. This me is always used and forwarded by reference. Do not confuse this with the handling on primitive types. In this code snippet -bc. -int a; -NativeInt b = new NativeInt(); -... -int result = function.invoke(Integer.class, a, b); -... - + + int a; + NativeInt b = new NativeInt(); + ... + int result = function.invoke(Integer.class, a, b); + ... the function is called using the value of "a" and a pointer to "b". - -p. This library currently does not provide any convenience for some aspects of native calls, for example -# primitive arrays -# float / double support -# String encodings -# callbacks -# maybe other features we didn't need until now and so are not aware of.. +- primitive arrays +- float / double support +- String encodings +- callbacks +- maybe other features we didn't need until now and so are not aware of.. The underlying infrastructure (JNA) may handle such constructs, but as long as the semantics is not defined on this library level, you should expect unportable @@ -65,215 +58,194 @@ behavior. We propose, even if not convenient, to express all call semantics using documented features only. Feel free to ask for the inclusion of more library features. -h2. Build +## Build + +The project is divided into an API project and a JNA implementation project. +Both are built using Maven using the ```clean install``` standard pattern: -The project is committed as a self contained Eclipse project and should compile without problems there. + mvn clean install -Anyone in Ant is kindly asked to provide a script... (as for maven) -h2. Dependencies +## Dependencies This library consists of a platform independent part and a provider implementation. A provider based on JNA is included and as such you need the jna.jar to compile and run. -h2. Usage +## Usage -h3. Common scenarios +### Common scenarios -h4. Call functions with primitive types +#### Call functions with primitive types The most simple calls use only primitive types. These are mapped by the "invoke" itself using builtin rules to target the C types. -p. The C part may look like this: -bc. -int intValue; -float floatValue; -... -int result = function(intValue, floatValue); -... + int intValue; + float floatValue; + ... + int result = function(intValue, floatValue); + ... -p. An equivalent call from Java would look like: -bc. -int intValue; -float floatValue; -... -int result = function.invoke(Integer.class, intValue, floatValue); -... -... + int intValue; + float floatValue; + ... + int result = function.invoke(Integer.class, intValue, floatValue); + ... + ... This seems straightforward.... -h4. Call functions with String parameters +#### Call functions with String parameters The first problems are expected when dealing with strings or char* on the C side. But no problem here: -p. The C part may look like this: -bc. -char* string = "test"; -... -int result = function(string); -... + char* string = "test"; + ... + int result = function(string); + ... + -p. An equivalent call from Java would look like: -bc. -String string = "test"; -... -int result = function.invoke(Integer.class, string); -... + String string = "test"; + ... + int result = function.invoke(Integer.class, string); + ... String semantics are handled internally. Even the platform wide character features can be accessed quite easy - simply wrap the String in a "WideString" to indicate the special treatment on invocation marshalling. -bc. -String string = "test"; -WideString wideString = new WideString(string); -... -int result = function.invoke(Integer.class, wideString); -... + + String string = "test"; + WideString wideString = new WideString(string); + ... + int result = function.invoke(Integer.class, wideString); + ... In much the same way you can request a wide string conversion on the result. -bc. -... -WideString result = function.invoke(WideString.class); -String string = result.toString(); -... + ... + WideString result = function.invoke(WideString.class); + String string = result.toString(); + ... -h4. Call with parameters "by reference" (out parameters) +#### Call with parameters "by reference" (out parameters) Other functions return values via "out" parameters, via references to C memory. -p. The C part may look like this: -bc. -int value; - -... -int result = function(&value); -if (value == 42) { + int value; + + ... + int result = function(&value); + if (value == 42) { + ... + } ... -} -... -p. An equivalent call from Java would look like: -bc. -NativeInt value = new NativeInt(); + NativeInt value = new NativeInt(); -int result = function.invoke(Integer.class, value); -if (value.intValue() == 42) { + int result = function.invoke(Integer.class, value); + if (value.intValue() == 42) { + ... + } ... -} -... The NativeObject is allocated in C memory and forwarded "by reference". This call pattern stays the same regardless of the complexity of the NativeObject - so this is the call for your Java side definition of the C struct: -bc. -// allocates memory for the struct in C -MyStruct value = new MyStruct(); + // allocates memory for the struct in C + MyStruct value = new MyStruct(); -// call function with a pointer to the new struct -int result = function.invoke(Integer.class, value); -... + // call function with a pointer to the new struct + int result = function.invoke(Integer.class, value); + ... -h4. Wrapping pointers returned from out parameters +#### Wrapping pointers returned from out parameters With C you find quite often a pointer to a newly create memory chunk returned via an out parameter. -bc. -my_struct *value; -... -function(&value); -int a = value->a; -... + my_struct *value; + ... + function(&value); + int a = value->a; + ... This is one of the rare occasions you will deal with the "NativeReference" directly. If we follow exactly the pattern we have used so far we get: -bc. -// allocate memory for holding a pointer to a struct -NativeReference ptrValue = new NativeReference(MyStruct.META); -// call the function with the pointer to the pointer -int result = function.invoke(Integer.class, ptrValue); -// dereference the result... -MyStruct value = (MyStruct)ptrValue.getValue(); - + // allocate memory for holding a pointer to a struct + NativeReference ptrValue = new NativeReference(MyStruct.META); + // call the function with the pointer to the pointer + int result = function.invoke(Integer.class, ptrValue); + // dereference the result... + MyStruct value = (MyStruct)ptrValue.getValue(); Here's a common pattern to manage "transparent" handles that do not designate data structures you should deal with - for sure you can declare a NativeVoid subclass of you own for better readability or to add methods. -bc. -// allocate memory for holding a void pointer -NativeReference ptrValue = new NativeReference(NativeVoid.META); -// call the function with the pointer to the pointer -int result = function.invoke(Integer.class, value); -// dereference the result... -NativeVoid value = (NativeVoid)ptrValue.getValue(); + // allocate memory for holding a void pointer + NativeReference ptrValue = new NativeReference(NativeVoid.META); + // call the function with the pointer to the pointer + int result = function.invoke(Integer.class, value); + // dereference the result... + NativeVoid value = (NativeVoid)ptrValue.getValue(); -h4. Wrapping a pointer returned as function result +#### Wrapping a pointer returned as function result Many functions return handles to newly created objects. Wrapping this result to the INativeObject framework is simply a matter of declaration: -bc. -NativeVoid result = function.invoke(NativeVoid.class, ...); + NativeVoid result = function.invoke(NativeVoid.class, ...); You have just wrapped a "void *" (void pointer)! Notice how you "hide" the C reference or pointer semantics. The less you think about it, the more it is intuitive :-). Using this code will return "null" in case of a "0" address. -p. NativeVoid is a "stateless" object, declaring to be of size "0". This is often the case with transparent handles to some proprietary / private information. Only the handle itself is used to manipulate state via the associated library functions. -p. In much the same way you can create a NativeStruct instances, holding public state information and allowing easy access to it. -p. Assume you have defined a NativeStruct subclass "MyStruct" of any memory layout. The above code sequence changes to -bc. -MyStruct structObj = function.invoke(MyStruct.class, ...); + MyStruct structObj = function.invoke(MyStruct.class, ...); Behind this code is a simple two step process. You can do it manually if the default is not exactly what you want, for example in some cases where an address of "-1" means failure. -# Create the INativeHandle wrapper. This object represents a memory address and implements some access primitives -# Use the INativeHandle to build the INativeObject wrapper that is situated at this memory location. This can be any of the INativeObject subclasses, such as a primitive, composite or even void representation. +- Create the INativeHandle wrapper. This object represents a memory address and implements some access primitives +- Use the INativeHandle to build the INativeObject wrapper that is situated at this memory location. This can be any of the INativeObject subclasses, such as a primitive, composite or even void representation. + -bc. -int address = function.invoke(Integer.class, ...); -if (address == 0) { - return null; -} -INativeHandle handle = NativeInterface.get().createHandle(address); -return NativeVoid.META.createNative(handle); + int address = function.invoke(Integer.class, ...); + if (address == 0) { + return null; + } + INativeHandle handle = NativeInterface.get().createHandle(address); + return NativeVoid.META.createNative(handle); -p. The pointer, represented by the INativeHandle is wrapped by a NativeObject which is what is referenced by the pointer. -h4. Primitive arrays and buffers +#### Primitive arrays and buffers You can call a native function using primitive arrays the same way as using plain primitives. The basic marshalling will map the values in both direction, so you see C side changes in the @@ -296,9 +268,9 @@ between Java and C. In any case you may use a NativeBuffer and access the memory allocated in heap space from both sides. NativeBuffer can be seen as an explicit equivalent to a DirectBuffer. -h2. License +## License -bc. +
 /*
  * Copyright (c) 2013, intarsys consulting GmbH
  *
@@ -330,13 +302,13 @@ bc.
  */
 
-h2. Service & Support +## Service & Support -Service&support should be funneled through the tools available with Github. +Service & support should be funneled through the tools available with Github. If you need further assistance, contact us. -bc. +
 
 intarsys consulting GmbH
 Kriegstrasse 100
 76135 Karlsruhe
@@ -344,3 +316,4 @@ Fon +49 721 38479-0
 Fax +49 721 38479-60
 info@intarsys.de
 www.intarsys.de
+
diff --git a/deploy/is-native-c.jar b/deploy/is-native-c.jar deleted file mode 100644 index 63fb007..0000000 Binary files a/deploy/is-native-c.jar and /dev/null differ diff --git a/doc/jna_license.txt b/doc/jna_license.txt deleted file mode 100644 index 7282b28..0000000 --- a/doc/jna_license.txt +++ /dev/null @@ -1,5 +0,0 @@ -This program uses jna (https://github.com/twall/jna). - -jna is governed by the LGPL license, available in this directory. - -jna source code is available at https://github.com/twall/jna. \ No newline at end of file diff --git a/doc/lgpl-2.1.txt b/doc/lgpl-2.1.txt deleted file mode 100644 index 602bfc9..0000000 --- a/doc/lgpl-2.1.txt +++ /dev/null @@ -1,504 +0,0 @@ - GNU LESSER GENERAL PUBLIC LICENSE - Version 2.1, February 1999 - - Copyright (C) 1991, 1999 Free Software Foundation, Inc. - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - -[This is the first released version of the Lesser GPL. It also counts - as the successor of the GNU Library Public License, version 2, hence - the version number 2.1.] - - Preamble - - The licenses for most software are designed to take away your -freedom to share and change it. By contrast, the GNU General Public -Licenses are intended to guarantee your freedom to share and change -free software--to make sure the software is free for all its users. - - This license, the Lesser General Public License, applies to some -specially designated software packages--typically libraries--of the -Free Software Foundation and other authors who decide to use it. You -can use it too, but we suggest you first think carefully about whether -this license or the ordinary General Public License is the better -strategy to use in any particular case, based on the explanations below. - - When we speak of free software, we are referring to freedom of use, -not price. Our General Public Licenses are designed to make sure that -you have the freedom to distribute copies of free software (and charge -for this service if you wish); that you receive source code or can get -it if you want it; that you can change the software and use pieces of -it in new free programs; and that you are informed that you can do -these things. - - To protect your rights, we need to make restrictions that forbid -distributors to deny you these rights or to ask you to surrender these -rights. These restrictions translate to certain responsibilities for -you if you distribute copies of the library or if you modify it. - - For example, if you distribute copies of the library, whether gratis -or for a fee, you must give the recipients all the rights that we gave -you. You must make sure that they, too, receive or can get the source -code. If you link other code with the library, you must provide -complete object files to the recipients, so that they can relink them -with the library after making changes to the library and recompiling -it. And you must show them these terms so they know their rights. - - We protect your rights with a two-step method: (1) we copyright the -library, and (2) we offer you this license, which gives you legal -permission to copy, distribute and/or modify the library. - - To protect each distributor, we want to make it very clear that -there is no warranty for the free library. Also, if the library is -modified by someone else and passed on, the recipients should know -that what they have is not the original version, so that the original -author's reputation will not be affected by problems that might be -introduced by others. - - Finally, software patents pose a constant threat to the existence of -any free program. We wish to make sure that a company cannot -effectively restrict the users of a free program by obtaining a -restrictive license from a patent holder. Therefore, we insist that -any patent license obtained for a version of the library must be -consistent with the full freedom of use specified in this license. - - Most GNU software, including some libraries, is covered by the -ordinary GNU General Public License. This license, the GNU Lesser -General Public License, applies to certain designated libraries, and -is quite different from the ordinary General Public License. We use -this license for certain libraries in order to permit linking those -libraries into non-free programs. - - When a program is linked with a library, whether statically or using -a shared library, the combination of the two is legally speaking a -combined work, a derivative of the original library. The ordinary -General Public License therefore permits such linking only if the -entire combination fits its criteria of freedom. The Lesser General -Public License permits more lax criteria for linking other code with -the library. - - We call this license the "Lesser" General Public License because it -does Less to protect the user's freedom than the ordinary General -Public License. It also provides other free software developers Less -of an advantage over competing non-free programs. These disadvantages -are the reason we use the ordinary General Public License for many -libraries. However, the Lesser license provides advantages in certain -special circumstances. - - For example, on rare occasions, there may be a special need to -encourage the widest possible use of a certain library, so that it becomes -a de-facto standard. To achieve this, non-free programs must be -allowed to use the library. A more frequent case is that a free -library does the same job as widely used non-free libraries. In this -case, there is little to gain by limiting the free library to free -software only, so we use the Lesser General Public License. - - In other cases, permission to use a particular library in non-free -programs enables a greater number of people to use a large body of -free software. For example, permission to use the GNU C Library in -non-free programs enables many more people to use the whole GNU -operating system, as well as its variant, the GNU/Linux operating -system. - - Although the Lesser General Public License is Less protective of the -users' freedom, it does ensure that the user of a program that is -linked with the Library has the freedom and the wherewithal to run -that program using a modified version of the Library. - - The precise terms and conditions for copying, distribution and -modification follow. Pay close attention to the difference between a -"work based on the library" and a "work that uses the library". The -former contains code derived from the library, whereas the latter must -be combined with the library in order to run. - - GNU LESSER GENERAL PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - 0. This License Agreement applies to any software library or other -program which contains a notice placed by the copyright holder or -other authorized party saying it may be distributed under the terms of -this Lesser General Public License (also called "this License"). -Each licensee is addressed as "you". - - A "library" means a collection of software functions and/or data -prepared so as to be conveniently linked with application programs -(which use some of those functions and data) to form executables. - - The "Library", below, refers to any such software library or work -which has been distributed under these terms. A "work based on the -Library" means either the Library or any derivative work under -copyright law: that is to say, a work containing the Library or a -portion of it, either verbatim or with modifications and/or translated -straightforwardly into another language. (Hereinafter, translation is -included without limitation in the term "modification".) - - "Source code" for a work means the preferred form of the work for -making modifications to it. For a library, complete source code means -all the source code for all modules it contains, plus any associated -interface definition files, plus the scripts used to control compilation -and installation of the library. - - Activities other than copying, distribution and modification are not -covered by this License; they are outside its scope. The act of -running a program using the Library is not restricted, and output from -such a program is covered only if its contents constitute a work based -on the Library (independent of the use of the Library in a tool for -writing it). Whether that is true depends on what the Library does -and what the program that uses the Library does. - - 1. You may copy and distribute verbatim copies of the Library's -complete source code as you receive it, in any medium, provided that -you conspicuously and appropriately publish on each copy an -appropriate copyright notice and disclaimer of warranty; keep intact -all the notices that refer to this License and to the absence of any -warranty; and distribute a copy of this License along with the -Library. - - You may charge a fee for the physical act of transferring a copy, -and you may at your option offer warranty protection in exchange for a -fee. - - 2. You may modify your copy or copies of the Library or any portion -of it, thus forming a work based on the Library, and copy and -distribute such modifications or work under the terms of Section 1 -above, provided that you also meet all of these conditions: - - a) The modified work must itself be a software library. - - b) You must cause the files modified to carry prominent notices - stating that you changed the files and the date of any change. - - c) You must cause the whole of the work to be licensed at no - charge to all third parties under the terms of this License. - - d) If a facility in the modified Library refers to a function or a - table of data to be supplied by an application program that uses - the facility, other than as an argument passed when the facility - is invoked, then you must make a good faith effort to ensure that, - in the event an application does not supply such function or - table, the facility still operates, and performs whatever part of - its purpose remains meaningful. - - (For example, a function in a library to compute square roots has - a purpose that is entirely well-defined independent of the - application. Therefore, Subsection 2d requires that any - application-supplied function or table used by this function must - be optional: if the application does not supply it, the square - root function must still compute square roots.) - -These requirements apply to the modified work as a whole. If -identifiable sections of that work are not derived from the Library, -and can be reasonably considered independent and separate works in -themselves, then this License, and its terms, do not apply to those -sections when you distribute them as separate works. But when you -distribute the same sections as part of a whole which is a work based -on the Library, the distribution of the whole must be on the terms of -this License, whose permissions for other licensees extend to the -entire whole, and thus to each and every part regardless of who wrote -it. - -Thus, it is not the intent of this section to claim rights or contest -your rights to work written entirely by you; rather, the intent is to -exercise the right to control the distribution of derivative or -collective works based on the Library. - -In addition, mere aggregation of another work not based on the Library -with the Library (or with a work based on the Library) on a volume of -a storage or distribution medium does not bring the other work under -the scope of this License. - - 3. You may opt to apply the terms of the ordinary GNU General Public -License instead of this License to a given copy of the Library. To do -this, you must alter all the notices that refer to this License, so -that they refer to the ordinary GNU General Public License, version 2, -instead of to this License. (If a newer version than version 2 of the -ordinary GNU General Public License has appeared, then you can specify -that version instead if you wish.) Do not make any other change in -these notices. - - Once this change is made in a given copy, it is irreversible for -that copy, so the ordinary GNU General Public License applies to all -subsequent copies and derivative works made from that copy. - - This option is useful when you wish to copy part of the code of -the Library into a program that is not a library. - - 4. You may copy and distribute the Library (or a portion or -derivative of it, under Section 2) in object code or executable form -under the terms of Sections 1 and 2 above provided that you accompany -it with the complete corresponding machine-readable source code, which -must be distributed under the terms of Sections 1 and 2 above on a -medium customarily used for software interchange. - - If distribution of object code is made by offering access to copy -from a designated place, then offering equivalent access to copy the -source code from the same place satisfies the requirement to -distribute the source code, even though third parties are not -compelled to copy the source along with the object code. - - 5. A program that contains no derivative of any portion of the -Library, but is designed to work with the Library by being compiled or -linked with it, is called a "work that uses the Library". Such a -work, in isolation, is not a derivative work of the Library, and -therefore falls outside the scope of this License. - - However, linking a "work that uses the Library" with the Library -creates an executable that is a derivative of the Library (because it -contains portions of the Library), rather than a "work that uses the -library". The executable is therefore covered by this License. -Section 6 states terms for distribution of such executables. - - When a "work that uses the Library" uses material from a header file -that is part of the Library, the object code for the work may be a -derivative work of the Library even though the source code is not. -Whether this is true is especially significant if the work can be -linked without the Library, or if the work is itself a library. The -threshold for this to be true is not precisely defined by law. - - If such an object file uses only numerical parameters, data -structure layouts and accessors, and small macros and small inline -functions (ten lines or less in length), then the use of the object -file is unrestricted, regardless of whether it is legally a derivative -work. (Executables containing this object code plus portions of the -Library will still fall under Section 6.) - - Otherwise, if the work is a derivative of the Library, you may -distribute the object code for the work under the terms of Section 6. -Any executables containing that work also fall under Section 6, -whether or not they are linked directly with the Library itself. - - 6. As an exception to the Sections above, you may also combine or -link a "work that uses the Library" with the Library to produce a -work containing portions of the Library, and distribute that work -under terms of your choice, provided that the terms permit -modification of the work for the customer's own use and reverse -engineering for debugging such modifications. - - You must give prominent notice with each copy of the work that the -Library is used in it and that the Library and its use are covered by -this License. You must supply a copy of this License. If the work -during execution displays copyright notices, you must include the -copyright notice for the Library among them, as well as a reference -directing the user to the copy of this License. Also, you must do one -of these things: - - a) Accompany the work with the complete corresponding - machine-readable source code for the Library including whatever - changes were used in the work (which must be distributed under - Sections 1 and 2 above); and, if the work is an executable linked - with the Library, with the complete machine-readable "work that - uses the Library", as object code and/or source code, so that the - user can modify the Library and then relink to produce a modified - executable containing the modified Library. (It is understood - that the user who changes the contents of definitions files in the - Library will not necessarily be able to recompile the application - to use the modified definitions.) - - b) Use a suitable shared library mechanism for linking with the - Library. A suitable mechanism is one that (1) uses at run time a - copy of the library already present on the user's computer system, - rather than copying library functions into the executable, and (2) - will operate properly with a modified version of the library, if - the user installs one, as long as the modified version is - interface-compatible with the version that the work was made with. - - c) Accompany the work with a written offer, valid for at - least three years, to give the same user the materials - specified in Subsection 6a, above, for a charge no more - than the cost of performing this distribution. - - d) If distribution of the work is made by offering access to copy - from a designated place, offer equivalent access to copy the above - specified materials from the same place. - - e) Verify that the user has already received a copy of these - materials or that you have already sent this user a copy. - - For an executable, the required form of the "work that uses the -Library" must include any data and utility programs needed for -reproducing the executable from it. However, as a special exception, -the materials to be distributed need not include anything that is -normally distributed (in either source or binary form) with the major -components (compiler, kernel, and so on) of the operating system on -which the executable runs, unless that component itself accompanies -the executable. - - It may happen that this requirement contradicts the license -restrictions of other proprietary libraries that do not normally -accompany the operating system. Such a contradiction means you cannot -use both them and the Library together in an executable that you -distribute. - - 7. You may place library facilities that are a work based on the -Library side-by-side in a single library together with other library -facilities not covered by this License, and distribute such a combined -library, provided that the separate distribution of the work based on -the Library and of the other library facilities is otherwise -permitted, and provided that you do these two things: - - a) Accompany the combined library with a copy of the same work - based on the Library, uncombined with any other library - facilities. This must be distributed under the terms of the - Sections above. - - b) Give prominent notice with the combined library of the fact - that part of it is a work based on the Library, and explaining - where to find the accompanying uncombined form of the same work. - - 8. You may not copy, modify, sublicense, link with, or distribute -the Library except as expressly provided under this License. Any -attempt otherwise to copy, modify, sublicense, link with, or -distribute the Library is void, and will automatically terminate your -rights under this License. However, parties who have received copies, -or rights, from you under this License will not have their licenses -terminated so long as such parties remain in full compliance. - - 9. You are not required to accept this License, since you have not -signed it. However, nothing else grants you permission to modify or -distribute the Library or its derivative works. These actions are -prohibited by law if you do not accept this License. Therefore, by -modifying or distributing the Library (or any work based on the -Library), you indicate your acceptance of this License to do so, and -all its terms and conditions for copying, distributing or modifying -the Library or works based on it. - - 10. Each time you redistribute the Library (or any work based on the -Library), the recipient automatically receives a license from the -original licensor to copy, distribute, link with or modify the Library -subject to these terms and conditions. You may not impose any further -restrictions on the recipients' exercise of the rights granted herein. -You are not responsible for enforcing compliance by third parties with -this License. - - 11. If, as a consequence of a court judgment or allegation of patent -infringement or for any other reason (not limited to patent issues), -conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot -distribute so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you -may not distribute the Library at all. For example, if a patent -license would not permit royalty-free redistribution of the Library by -all those who receive copies directly or indirectly through you, then -the only way you could satisfy both it and this License would be to -refrain entirely from distribution of the Library. - -If any portion of this section is held invalid or unenforceable under any -particular circumstance, the balance of the section is intended to apply, -and the section as a whole is intended to apply in other circumstances. - -It is not the purpose of this section to induce you to infringe any -patents or other property right claims or to contest validity of any -such claims; this section has the sole purpose of protecting the -integrity of the free software distribution system which is -implemented by public license practices. Many people have made -generous contributions to the wide range of software distributed -through that system in reliance on consistent application of that -system; it is up to the author/donor to decide if he or she is willing -to distribute software through any other system and a licensee cannot -impose that choice. - -This section is intended to make thoroughly clear what is believed to -be a consequence of the rest of this License. - - 12. If the distribution and/or use of the Library is restricted in -certain countries either by patents or by copyrighted interfaces, the -original copyright holder who places the Library under this License may add -an explicit geographical distribution limitation excluding those countries, -so that distribution is permitted only in or among countries not thus -excluded. In such case, this License incorporates the limitation as if -written in the body of this License. - - 13. The Free Software Foundation may publish revised and/or new -versions of the Lesser General Public License from time to time. -Such new versions will be similar in spirit to the present version, -but may differ in detail to address new problems or concerns. - -Each version is given a distinguishing version number. If the Library -specifies a version number of this License which applies to it and -"any later version", you have the option of following the terms and -conditions either of that version or of any later version published by -the Free Software Foundation. If the Library does not specify a -license version number, you may choose any version ever published by -the Free Software Foundation. - - 14. If you wish to incorporate parts of the Library into other free -programs whose distribution conditions are incompatible with these, -write to the author to ask for permission. For software which is -copyrighted by the Free Software Foundation, write to the Free -Software Foundation; we sometimes make exceptions for this. Our -decision will be guided by the two goals of preserving the free status -of all derivatives of our free software and of promoting the sharing -and reuse of software generally. - - NO WARRANTY - - 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO -WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. -EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR -OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY -KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE -LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME -THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN -WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY -AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU -FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR -CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE -LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING -RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A -FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF -SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH -DAMAGES. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Libraries - - If you develop a new library, and you want it to be of the greatest -possible use to the public, we recommend making it free software that -everyone can redistribute and change. You can do so by permitting -redistribution under these terms (or, alternatively, under the terms of the -ordinary General Public License). - - To apply these terms, attach the following notices to the library. It is -safest to attach them to the start of each source file to most effectively -convey the exclusion of warranty; and each file should have at least the -"copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - -Also add information on how to contact you by electronic and paper mail. - -You should also get your employer (if you work as a programmer) or your -school, if any, to sign a "copyright disclaimer" for the library, if -necessary. Here is a sample; alter the names: - - Yoyodyne, Inc., hereby disclaims all copyright interest in the - library `Frob' (a library for tweaking knobs) written by James Random Hacker. - - , 1 April 1990 - Ty Coon, President of Vice - -That's all there is to it! - - diff --git a/lib/jna.jar b/lib/jna.jar deleted file mode 100644 index ed8a456..0000000 Binary files a/lib/jna.jar and /dev/null differ diff --git a/nativec-api/pom.xml b/nativec-api/pom.xml new file mode 100644 index 0000000..079cdfd --- /dev/null +++ b/nativec-api/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + + se.jguru.codestyle.poms.java + jguru-codestyle-java-api-parent + 1.2.0 + + + + de.intarsys.opensource.nativec.api + nativec-api + 1.0.0-SNAPSHOT + jar + + + intarsys + classpath://codestyle/license + + + + Intarsys Consulting GmbH + https://www.intarsys.de + + + 2013 + + + + Intarsys License + https://www.intarsys.de/licenses/intarsysSourceLicense.txt + repo + A business-friendly OSS license + + + + + + + + + org.codehaus.mojo + license-maven-plugin + 2.0.0 + + + + de.intarsys.opensource.nativec.codestyle + nativec-codestyle + 1.0.0-SNAPSHOT + + + + + + + diff --git a/src/de/intarsys/nativec/api/CLong.java b/nativec-api/src/main/java/de/intarsys/nativec/api/CLong.java similarity index 63% rename from src/de/intarsys/nativec/api/CLong.java rename to nativec-api/src/main/java/de/intarsys/nativec/api/CLong.java index 1bfd674..e6b9a1f 100644 --- a/src/de/intarsys/nativec/api/CLong.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/api/CLong.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,57 +29,50 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.api; - -/** - * A plain Java object representing a platform long. In contrast to the fixed 8 - * byte Java primitive type the platform long may vary in size, depending on - * operating system and processor architecture. - * - */ public class CLong extends Number { - private Number value; - - public CLong(long pValue) { - value = pValue; - } + private final Number value; - @Override - public double doubleValue() { - return value.doubleValue(); - } + public CLong(long pValue) { + value = pValue; + } - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj instanceof CLong) { - return value.equals(((CLong) obj).value); - } - return false; - } + @Override + public double doubleValue() { + return value.doubleValue(); + } - @Override - public float floatValue() { - return value.floatValue(); - } + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj instanceof CLong) { + return value.equals(((CLong) obj).value); + } + return false; + } - @Override - public int hashCode() { - return value.hashCode(); - } + @Override + public float floatValue() { + return value.floatValue(); + } - @Override - public int intValue() { - return value.intValue(); - } + @Override + public int hashCode() { + return value.hashCode(); + } - @Override - public long longValue() { - return value.longValue(); - } + @Override + public int intValue() { + return value.intValue(); + } + @Override + public long longValue() { + return value.longValue(); + } } diff --git a/src/de/intarsys/nativec/api/CWideString.java b/nativec-api/src/main/java/de/intarsys/nativec/api/CWideString.java similarity index 78% rename from src/de/intarsys/nativec/api/CWideString.java rename to nativec-api/src/main/java/de/intarsys/nativec/api/CWideString.java index 6806f24..523b336 100644 --- a/src/de/intarsys/nativec/api/CWideString.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/api/CWideString.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,27 +29,19 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.api; - -/** - * Wrapper class to indicate use of a "wide" string on native side. - *

- * Use this to indicate on a native call parameter or return type that you will - * deal with the platforms wide string capabilities. - * - */ final public class CWideString { - private final String string; - - public CWideString(String string) { - super(); - this.string = string; - } + private final String string; - public String getString() { - return string; - } + public CWideString(String string) { + super(); + this.string = string; + } + public String getString() { + return string; + } } diff --git a/src/de/intarsys/nativec/api/ICallback.java b/nativec-api/src/main/java/de/intarsys/nativec/api/ICallback.java similarity index 77% rename from src/de/intarsys/nativec/api/ICallback.java rename to nativec-api/src/main/java/de/intarsys/nativec/api/ICallback.java index 715b11e..8731899 100644 --- a/src/de/intarsys/nativec/api/ICallback.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/api/ICallback.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,23 +29,23 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.api; public interface ICallback { - @Deprecated - public static final Object CallingConventionCdecl = INativeFunction.CallingConventionCdecl; + @Deprecated + Object CallingConventionCdecl = INativeFunction.CallingConventionCdecl; - @Deprecated - public static final Object CallingConventionStdcall = INativeFunction.CallingConventionCdecl; + @Deprecated + Object CallingConventionStdcall = INativeFunction.CallingConventionCdecl; - public Object getCallingConvention(); + Object getCallingConvention(); - public Class[] getParameterTypes(); + Class[] getParameterTypes(); - public Class getReturnType(); - - public Object invoke(Object[] objects); + Class getReturnType(); + Object invoke(Object[] objects); } diff --git a/src/de/intarsys/nativec/api/INativeCallback.java b/nativec-api/src/main/java/de/intarsys/nativec/api/INativeCallback.java similarity index 91% rename from src/de/intarsys/nativec/api/INativeCallback.java rename to nativec-api/src/main/java/de/intarsys/nativec/api/INativeCallback.java index 2711d75..b1f2ab9 100644 --- a/src/de/intarsys/nativec/api/INativeCallback.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/api/INativeCallback.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,9 +29,10 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.api; public interface INativeCallback { - // no methods + // no methods } diff --git a/src/de/intarsys/nativec/api/INativeFunction.java b/nativec-api/src/main/java/de/intarsys/nativec/api/INativeFunction.java similarity index 74% rename from src/de/intarsys/nativec/api/INativeFunction.java rename to nativec-api/src/main/java/de/intarsys/nativec/api/INativeFunction.java index 6e81ec4..233ef10 100644 --- a/src/de/intarsys/nativec/api/INativeFunction.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/api/INativeFunction.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,25 +29,21 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.api; - -/** - * The representation of a native function. - */ public interface INativeFunction { - public static final Object CallingConventionCdecl = new Object(); - public static final Object CallingConventionStdcall = new Object(); + Object CallingConventionCdecl = new Object(); + Object CallingConventionStdcall = new Object(); - /** - * Invoke the native function. - * - * @param returnType - * The expected return type. - * @param objects - * The arguments to the function - * @return The result of executing the function - */ - public T invoke(Class returnType, Object... objects); + /** + * Invoke the native function. + * + * @param returnType The expected return type. + * @param objects The arguments to the function + * + * @return The result of executing the function + */ + T invoke(Class returnType, Object... objects); } diff --git a/nativec-api/src/main/java/de/intarsys/nativec/api/INativeHandle.java b/nativec-api/src/main/java/de/intarsys/nativec/api/INativeHandle.java new file mode 100644 index 0000000..489fdfb --- /dev/null +++ b/nativec-api/src/main/java/de/intarsys/nativec/api/INativeHandle.java @@ -0,0 +1,247 @@ +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.api; +public interface INativeHandle { + + /** + * The start address of the memory chunk + * + * @return The start address of the memory chunk + */ + long getAddress(); + + /** + * Marshal the data at byte offset index from the start of the + * memory chunk to a byte. + * + * @param index The byte offset from the start of the memory chunk + * + * @return A byte marshaled from the memory chunk + */ + byte getByte(int index); + + /** + * Marshal the data at byte offset index from the start of the + * memory chunk to a byte array of length count. + * + * @param index The byte offset from the start of the memory chunk + * @param count The size of the byte array + * + * @return A byte array marshaled from the memory chunk + */ + byte[] getByteArray(int index, int count); + + /** + * Marshal the data at byte offset index from the start of the + * memory chunk to a long. Get only the "platform" number of bytes. + * + * @param index The byte offset from the start of the memory chunk + * + * @return A long marshaled from the memory chunk + */ + long getCLong(int index); + + /** + * Marshal the data at byte offset index from the start of the + * memory chunk to an int. + * + * @param index The byte offset from the start of the memory chunk + * + * @return An int marshaled from the memory chunk + */ + int getInt(int index); + + /** + * Marshal the data at byte offset index from the start of the + * memory chunk to a long value (which is always 8 byte). + * + * @param index The byte offset from the start of the memory chunk + * + * @return A long marshaled from the memory chunk + */ + long getLong(int index); + + /** + * Marshal the data at byte offset index from the start of the + * memory chunk to an {@link INativeHandle}. + * + * @param index The byte offset from the start of the memory chunk + * + * @return An {@link INativeHandle} marshaled from the memory chunk + */ + INativeHandle getNativeHandle(int index); + + /** + * Marshal the data at byte offset index from the start of the + * memory chunk to a short. + * + * @param index The byte offset from the start of the memory chunk + * + * @return A short marshaled from the memory chunk + */ + short getShort(int index); + + /** + * The size for the handle in bytes. + *

+ * You can not access bytes from outside the range defined by getAdddress + + * size. + */ + int getSize(); + + /** + * Set the valid size for the handle to count bytes. + *

+ * You can not access bytes from outside the range defined by getAdddress + + * size. + * + * @param count The size of the memory managed by the {@link INativeHandle} + */ + void setSize(int count); + + /** + * Marshal the data at byte offset index from the start of the + * memory chunk to a String. + * + * @param index The byte offset from the start of the memory chunk + * + * @return A String marshaled from the memory chunk + */ + String getString(int index); + + /** + * Marshal the data at byte offset index from the start of the + * memory chunk to a String using the platform wide character conversion. + * + * @param index The byte offset from the start of the memory chunk + * + * @return A String marshaled from the memory chunk + */ + String getWideString(int index); + + /** + * Create a new {@link INativeHandle}, offset from this by + * offset bytes. + * + * @param offset The byte offset from the start of the memory chunk + * + * @return A new {@link INativeHandle} pointing to "getAddress() + offset". + */ + INativeHandle offset(int offset); + + /** + * Write a byte to the memory at byte offset index from the + * start of the memory chunk. + * + * @param index The byte offset from the start of the memory chunk + * @param value The value to write. + */ + void setByte(int index, byte value); + + /** + * Write a byte array to the memory at byte offset index from + * the start of the memory chunk. The method will write + * valueCount bytes from value starting at + * valueOffset. + * + * @param index The byte offset from the start of the memory chunk + * @param value The value to write. + */ + void setByteArray(int index, byte[] value, int valueOffset, + int valueCount); + + /** + * Write a long to the memory at byte offset index from the + * start of the memory chunk. Write only the "platform" number of bytes. The + * caller is responsible for observing the value range. + * + * @param index The byte offset from the start of the memory chunk + * @param value The value to write. + */ + void setCLong(int index, long value); + + /** + * Write an int to the memory at byte offset index from the + * start of the memory chunk. + * + * @param index The byte offset from the start of the memory chunk + * @param value The value to write. + */ + void setInt(int index, int value); + + /** + * Write a long to the memory at byte offset index from the + * start of the memory chunk. + * + * @param index The byte offset from the start of the memory chunk + * @param value The value to write. + */ + void setLong(int index, long value); + + /** + * Write an {@link INativeHandle} to the memory at byte offset + * index from the start of the memory chunk. + * + * @param index The byte offset from the start of the memory chunk + * @param valueHandle The value to write. + */ + void setNativeHandle(int index, INativeHandle valueHandle); + + /** + * Write a short to the memory at byte offset index from the + * start of the memory chunk. + * + * @param index The byte offset from the start of the memory chunk + * @param value The value to write. + */ + void setShort(int index, short value); + + /** + * Write a String to the memory at byte offset indexfrom the + * start of the memory chunk. + * + * @param index The byte offset from the start of the memory chunk + * @param value The value to write. + */ + void setString(int index, String value); + + /** + * Write a String to the memory at byte offset indexfrom the + * start of the memory chunk using the platform wide character conversion. + * + * @param index The byte offset from the start of the memory chunk + * @param value The value to write. + */ + void setWideString(int index, String value); +} diff --git a/nativec-api/src/main/java/de/intarsys/nativec/api/INativeInterface.java b/nativec-api/src/main/java/de/intarsys/nativec/api/INativeInterface.java new file mode 100644 index 0000000..3ebf6b2 --- /dev/null +++ b/nativec-api/src/main/java/de/intarsys/nativec/api/INativeInterface.java @@ -0,0 +1,119 @@ +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.api; +public interface INativeInterface { + + /** + * Add a directory to the search path. + * + * @param path The path to be added; + */ + void addSearchPath(String path); + + /** + * Allocate c memory and return the respective {@link INativeHandle}. + * + * @param size The size in bytes. + * + * @return The new allocated {@link INativeHandle} + */ + INativeHandle allocate(int size); + + INativeCallback createCallback(ICallback callback); + + /** + * Create an {@link INativeFunction} from a function pointer. + *

+ * There is no special handling for the 0 address! + * + * @param address The function pointer. + * + * @return The function object. + */ + INativeFunction createFunction(long address); + + INativeFunction createFunction(long address, Object callingConvention); + + /** + * Create a void {@link INativeHandle} to a memory address. + *

+ * There is no special handling for the 0 address! + * + * @param address The memory address. + * + * @return The handle to the memory address. + */ + INativeHandle createHandle(long address); + + /** + * Load a new {@link INativeLibrary}. + * + * @param name The name of the library to load. + * + * @return The new {@link INativeLibrary} + */ + INativeLibrary createLibrary(String name); + + /** + * Load a new {@link INativeLibrary}. + * + * @param name The name of the library to load. + * @param callingConvention The calling convention to use as default for functions in this + * library. + * + * @return The new {@link INativeLibrary} + */ + INativeLibrary createLibrary(String name, Object callingConvention); + + /** + * The platform long size. + * + * @return The platform long size. + */ + int longSize(); + + /** + * The platform pointer size. + * + * @return The platform pointer size. + */ + int pointerSize(); + + /** + * The platform wide char size. + * + * @return The platform wide char size. + */ + int wideCharSize(); +} diff --git a/src/de/intarsys/nativec/api/INativeLibrary.java b/nativec-api/src/main/java/de/intarsys/nativec/api/INativeLibrary.java similarity index 74% rename from src/de/intarsys/nativec/api/INativeLibrary.java rename to nativec-api/src/main/java/de/intarsys/nativec/api/INativeLibrary.java index 7d8b474..3e01517 100644 --- a/src/de/intarsys/nativec/api/INativeLibrary.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/api/INativeLibrary.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,29 +29,26 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.api; - -/** - * A native library (DLL or shared library). - * - */ public interface INativeLibrary { - /** - * Lookup a {@link INativeFunction} from the library. - * - * @param name - * The function name - * @return The {@link INativeFunction} - */ - public INativeFunction getFunction(String name); - /** - * Lookup a global in the library. - * - * @param symbolName - * The global name - * @return The {@link INativeHandle} to the global. - */ - public INativeHandle getGlobal(String symbolName); + /** + * Lookup a {@link INativeFunction} from the library. + * + * @param name The function name + * + * @return The {@link INativeFunction} + */ + INativeFunction getFunction(String name); + + /** + * Lookup a global in the library. + * + * @param symbolName The global name + * + * @return The {@link INativeHandle} to the global. + */ + INativeHandle getGlobal(String symbolName); } diff --git a/src/de/intarsys/nativec/api/IValueHolder.java b/nativec-api/src/main/java/de/intarsys/nativec/api/IValueHolder.java similarity index 80% rename from src/de/intarsys/nativec/api/IValueHolder.java rename to nativec-api/src/main/java/de/intarsys/nativec/api/IValueHolder.java index 5f503ef..e2c2695 100644 --- a/src/de/intarsys/nativec/api/IValueHolder.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/api/IValueHolder.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2007, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,26 +29,24 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.api; - -/** - * Generic interface for objects holding references to others. - */ public interface IValueHolder { - /** - * Dereference the {@link IValueHolder}. - * - * @return The referenced object. - */ - public T get(); + /** + * Dereference the {@link IValueHolder}. + * + * @return The referenced object. + */ + T get(); - /** - * Assign a new value. - * - * @param newValue - * @return The previous value (optional) - */ - public T set(T newValue); + /** + * Assign a new value. + * + * @param newValue + * + * @return The previous value (optional) + */ + T set(T newValue); } diff --git a/nativec-api/src/main/java/de/intarsys/nativec/api/NativeInterface.java b/nativec-api/src/main/java/de/intarsys/nativec/api/NativeInterface.java new file mode 100644 index 0000000..40b7a6e --- /dev/null +++ b/nativec-api/src/main/java/de/intarsys/nativec/api/NativeInterface.java @@ -0,0 +1,110 @@ +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.api; + +import java.util.Iterator; +import java.util.ServiceLoader; +public class NativeInterface { + + public static final String PROP_NATIVEINTERFACE = "de.intarsys.nativec.api.INativeInterface"; + + private static final Class[] NO_PARAMETERS = new Class[0]; + + private static final Object[] NO_ARGUMENTS = new Object[0]; + + public static INativeHandle NULL; + private static INativeInterface ACTIVE; + private static String NAME; + + static protected INativeInterface createNativeInterface() { + + String className = getName(); + if (className == null) { + className = System.getProperty(PROP_NATIVEINTERFACE); + } + + if (className != null) { + try { + + final Class clazz = Class.forName(className); + return (INativeInterface) clazz.getDeclaredConstructor(NO_PARAMETERS).newInstance(NO_ARGUMENTS); + + } catch (Exception e) { + throw new NoClassDefFoundError(className); + } + } + return findNativeInterface(); + } + + static protected INativeInterface findNativeInterface() { + + final ServiceLoader loader = ServiceLoader.load(INativeInterface.class); + final Iterator ps = loader.iterator(); + + if (ps.hasNext()) { + try { + return ps.next(); + } catch (Throwable e) { + // ignore and try on + } + } + + return null; + } + + synchronized static public INativeInterface get() { + if (ACTIVE == null) { + set(createNativeInterface()); + } + return ACTIVE; + } + + synchronized static public String getName() { + return NAME; + } + + synchronized static public void setName(String name) { + NAME = name; + } + + synchronized static public void set(INativeInterface nativeInterface) { + + if (nativeInterface == null) { + throw new NullPointerException("INativeInterface not available."); + } + + ACTIVE = nativeInterface; + NULL = nativeInterface.createHandle(0); + } +} diff --git a/nativec-api/src/main/java/de/intarsys/nativec/api/NativeTools.java b/nativec-api/src/main/java/de/intarsys/nativec/api/NativeTools.java new file mode 100644 index 0000000..11f80fa --- /dev/null +++ b/nativec-api/src/main/java/de/intarsys/nativec/api/NativeTools.java @@ -0,0 +1,195 @@ +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.api; + +import de.intarsys.nativec.type.INativeType; +import de.intarsys.nativec.type.NativeArray; +import de.intarsys.nativec.type.NativeArrayType; +import de.intarsys.nativec.type.NativeBuffer; +import de.intarsys.nativec.type.NativeBufferType; +import de.intarsys.nativec.type.NativeInt; +import de.intarsys.nativec.type.NativeLong; +public class NativeTools { + + private static INativeHandle toNativeHandle(long ptr) { + return NativeInterface.get().createHandle(ptr); + } + + public static byte[] fromNativeByteArray(long ptr, int count) { + + if (ptr == 0) { + return null; + } + + INativeHandle handle = toNativeHandle(ptr); + INativeType type = NativeBufferType.create(count); + NativeBuffer nativeValue = (NativeBuffer) type.createNative(handle); + + return nativeValue.getBytes(); + } + + public static long fromNativeCLong(long ptr) { + + NativeLong nativeValue; + + INativeHandle handle = toNativeHandle(ptr); + nativeValue = (NativeLong) NativeLong.META.createNative(handle); + return nativeValue.intValue(); + } + + public static IValueHolder fromNativeCLongHolder(long ptr) { + return new ObjectValueHolder(fromNativeCLong(ptr)); + } + + public static int fromNativeInt(long ptr) { + + INativeHandle handle = toNativeHandle(ptr); + NativeInt nativeValue = (NativeInt) NativeInt.META.createNative(handle); + + return nativeValue.intValue(); + } + + public static int[] fromNativeIntArray(INativeHandle handle, int count) { + int[] ints; + int offset; + + ints = new int[count]; + offset = NativeInt.META.getByteCount(); + for (int index = 0; index < count; index++) { + ints[index] = handle.getInt(index * offset); + } + return ints; + } + + public static int[] fromNativeIntArray(long ptr, int count) { + INativeHandle handle = toNativeHandle(ptr); + return fromNativeIntArray(handle, count); + } + + public static IValueHolder fromNativeIntHolder(long ptr) { + return new ObjectValueHolder(fromNativeInt(ptr)); + } + + public static String fromNativeString(long ptr, int count) { + return null; + } + + public static INativeHandle toHandle(long address) { + return NativeInterface.get().createHandle(address); + } + + public static void toNativeByteArray(long ptr, byte[] value) { + if (value != null) { + + INativeType type; + NativeBuffer buffer; + + INativeHandle handle = toNativeHandle(ptr); + type = NativeBufferType.create(value.length); + buffer = (NativeBuffer) type.createNative(handle); + buffer.setValue(value); + } + } + + public static void toNativeCLong(long ptr, int[] value) { + + if (value != null) { + + final INativeHandle handle = toNativeHandle(ptr); + final NativeArrayType type = NativeArrayType.create(NativeLong.META, value.length); + + NativeArray array = (NativeArray) type.createNative(handle); + for (int index = 0; index < value.length; index++) { + array.setValue(index, value[index]); + } + } + } + + public static void toNativeCLong(long ptr, IValueHolder value) { + toNativeCLong(ptr, value.get().longValue()); + } + + public static void toNativeCLong(long ptr, long value) { + + final INativeHandle handle = toNativeHandle(ptr); + final NativeLong nativeValue = (NativeLong) NativeLong.META.createNative(handle); + + nativeValue.setValue(value); + } + + public static void toNativeCLong(long ptr, long[] value) { + if (value != null) { + NativeArrayType type; + NativeArray array; + + final INativeHandle handle = toNativeHandle(ptr); + type = NativeArrayType.create(NativeLong.META, value.length); + array = (NativeArray) type.createNative(handle); + for (int index = 0; index < value.length; index++) { + array.setValue(index, value[index]); + } + } + } + + public static void toNativeInt(long ptr, int value) { + + final INativeHandle handle = toNativeHandle(ptr); + handle.setSize(NativeInt.META.getByteCount()); + handle.setInt(0, value); + } + + public static void toNativeInt(long ptr, int[] value) { + if (value != null) { + + NativeArrayType type; + NativeArray array; + + final INativeHandle handle = toNativeHandle(ptr); + + type = NativeArrayType.create(NativeInt.META, value.length); + array = (NativeArray) type.createNative(handle); + for (int index = 0; index < value.length; index++) { + array.setValue(index, value[index]); + } + } + } + + public static void toNativeInt(long ptr, IValueHolder value) { + toNativeInt(ptr, value.get()); + } + + public static void toNativePointer(long ptr, INativeHandle value) { + toNativeHandle(ptr).setNativeHandle(0, value); + } +} diff --git a/src/de/intarsys/nativec/api/ObjectValueHolder.java b/nativec-api/src/main/java/de/intarsys/nativec/api/ObjectValueHolder.java similarity index 79% rename from src/de/intarsys/nativec/api/ObjectValueHolder.java rename to nativec-api/src/main/java/de/intarsys/nativec/api/ObjectValueHolder.java index 085b9a7..172ec85 100644 --- a/src/de/intarsys/nativec/api/ObjectValueHolder.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/api/ObjectValueHolder.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2007, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,33 +29,27 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.api; - -/** - * A object holding nothing but a value - * - * This can easily be plugged in for a {@link IValueHolder}. - */ public class ObjectValueHolder implements IValueHolder { - private T value; - - public ObjectValueHolder(T value) { - super(); - this.value = value; - } + private T value; - @Override - public T get() { - return value; - } + public ObjectValueHolder(T value) { + super(); + this.value = value; + } - @Override - public T set(T newValue) { - T oldValue = value; - value = newValue; - return oldValue; - } + @Override + public T get() { + return value; + } + @Override + public T set(T newValue) { + T oldValue = value; + value = newValue; + return oldValue; + } } diff --git a/nativec-api/src/main/java/de/intarsys/nativec/api/package.html b/nativec-api/src/main/java/de/intarsys/nativec/api/package.html new file mode 100644 index 0000000..afbb2f4 --- /dev/null +++ b/nativec-api/src/main/java/de/intarsys/nativec/api/package.html @@ -0,0 +1,69 @@ + + + +This is the main package of the native c component. +

+ Here we define the API's and the VM singleton access to native c. +

+ The component itself was built, as we didn't (once again) find anything + around that satisfied our needs. We wanted: +

    +
  • Clean, layered design
  • +
  • Unrestricted access to all modeled objects and concepts.
  • +
  • Easy adaption and integration with other views. This means primarily unrestricted + access to pointers. There is no use in a native library if i can't bootstrap + with a simple address (in our point of view). +
  • +
  • Clean design for c data structures (primitive, composites and references)
  • +
  • Exchangeable native implementation
  • +
  • Platform independence
  • +
+ +So we created an API and a data structure component and mapped it to JNA, which has +great ideas (but in our opinion is to sealed off and has not well done on the +data structure side) and can serve as a good foundation. + +There are some topics we didn't care about yet - this means they may be working or may +not, its simply not tested. JNA brings a lot of basic features... + +
    +
  • Pointer size was not really an issue
  • +
  • We didn't care about byte order
  • +
+ +64 bit implementation is under way. + + + diff --git a/nativec-api/src/main/java/de/intarsys/nativec/type/INativeObject.java b/nativec-api/src/main/java/de/intarsys/nativec/type/INativeObject.java new file mode 100644 index 0000000..0ff9f66 --- /dev/null +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/INativeObject.java @@ -0,0 +1,73 @@ +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.type; + +import de.intarsys.nativec.api.INativeHandle; +public interface INativeObject { + + /** + * The bytes that make up the {@link INativeObject}. + * + * @return The bytes that make up the {@link INativeObject}. + */ + byte[] getBytes(); + + /** + * The {@link INativeHandle} to the c memory for the object. + * + * @return The {@link INativeHandle} to the c memory for the object. + */ + INativeHandle getNativeHandle(); + + /** + * The {@link INativeType} for the object. + * + * @return The {@link INativeType} for the object. + */ + INativeType getNativeType(); + + /** + * A Java side representation from the memory. + * + * @return A Java side representation for the {@link INativeObject}. + */ + Object getValue(); + + /** + * Assign (and marshall to memory) the Java side representation. + * + * @param value The new Java value. + */ + void setValue(Object value); +} diff --git a/src/de/intarsys/nativec/type/INativeObject.java b/nativec-api/src/main/java/de/intarsys/nativec/type/INativeType.java similarity index 56% rename from src/de/intarsys/nativec/type/INativeObject.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/INativeType.java index 5378686..19db0a7 100644 --- a/src/de/intarsys/nativec/type/INativeObject.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/INativeType.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,54 +29,59 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; +public interface INativeType { -/** - * The Java object representation of a c memory construct. - *

- * The {@link INativeObject} has a reference to c memory (the - * {@link INativeHandle} and "marshalling" methods "getValue" and "setValue" to - * get and set the Java representation. - * - */ -public interface INativeObject { + /** + * Create an array type from this. + * + * @param size The predefined size for the array. + * + * @return The derived type. + */ + INativeType Array(int size); - /** - * The bytes that make up the {@link INativeObject}. - * - * @return The bytes that make up the {@link INativeObject}. - */ - public byte[] getBytes(); + /** + * Create an {@link INativeObject} for this type from the Java object. + * + * @param value + * + * @return The new {@link INativeObject} + */ + INativeObject createNative(Object value); - /** - * The {@link INativeHandle} to the c memory for the object. - * - * @return The {@link INativeHandle} to the c memory for the object. - */ - public INativeHandle getNativeHandle(); + /** + * Create a new {@link INativeObject} from a {@link INativeHandle}. + * + * @param handle The handle to memory. + * + * @return The new {@link INativeObject} + */ + INativeObject createNative(INativeHandle handle); - /** - * The {@link INativeType} for the object. - * - * @return The {@link INativeType} for the object. - */ - public INativeType getNativeType(); + /** + * The boundary where this type as a struct member would want to be aligned. + * A structure can override this value with packing. + * + * @return The preferred alignment boundary. + */ + int getPreferredBoundary(); - /** - * A Java side representation from the memory. - * - * @return A Java side representation for the {@link INativeObject}. - */ - public Object getValue(); + /** + * The size of the type in c memory. + * + * @return The size of the type in c memory. + */ + int getByteCount(); - /** - * Assign (and marshall to memory) the Java side representation. - * - * @param value - * The new Java value. - */ - public void setValue(Object value); + /** + * Create a reference type to this. + * + * @return The derived type. + */ + INativeType Ref(); } diff --git a/src/de/intarsys/nativec/type/NativeAbstractStringType.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeAbstractStringType.java similarity index 65% rename from src/de/intarsys/nativec/type/NativeAbstractStringType.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeAbstractStringType.java index 887ef1d..2fcf203 100644 --- a/src/de/intarsys/nativec/type/NativeAbstractStringType.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeAbstractStringType.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,49 +29,45 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.NativeInterface; - -/** - * A meta class implementation - */ public class NativeAbstractStringType extends NativeType { - private final int characterSize; - private final int stringSize; - - protected NativeAbstractStringType(int pCharacterSize) { - super(); - characterSize = pCharacterSize; - stringSize = 0; - } + private final int characterSize; + private final int stringSize; - protected NativeAbstractStringType(int pStringSize, int pCharacterSize) { - super(); - characterSize = pCharacterSize; - stringSize = pStringSize; - } + protected NativeAbstractStringType(int pCharacterSize) { + super(); + characterSize = pCharacterSize; + stringSize = 0; + } - @Override - public int getByteCount() { - if (stringSize == 0) { - throw new IllegalStateException(); - } - return stringSize * characterSize; - } + protected NativeAbstractStringType(int pStringSize, int pCharacterSize) { + super(); + characterSize = pCharacterSize; + stringSize = pStringSize; + } - public int getPreferredBoundary() { - return NativeInterface.get().pointerSize(); - } + @Override + public int getByteCount() { + if (stringSize == 0) { + throw new IllegalStateException(); + } + return stringSize * characterSize; + } - public int getStringSize() { - return stringSize; - } + public int getPreferredBoundary() { + return NativeInterface.get().pointerSize(); + } - public boolean hasByteCount() { - return stringSize != 0; - } + public int getStringSize() { + return stringSize; + } -} \ No newline at end of file + public boolean hasByteCount() { + return stringSize != 0; + } +} diff --git a/nativec-api/src/main/java/de/intarsys/nativec/type/NativeArray.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeArray.java new file mode 100644 index 0000000..c54d57e --- /dev/null +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeArray.java @@ -0,0 +1,176 @@ +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.type; + +import de.intarsys.nativec.api.INativeHandle; +public class NativeArray extends NativeObject { + + /** + * The meta class instance + */ + public static final NativeArrayType META = new NativeArrayType( + NativeVoid.META, 0); + + static { + NativeType.register(NativeArray.class, META); + } + + private INativeObject[] values; + private NativeArrayType type; + + protected NativeArray(NativeArrayType type) { + this.type = type; + allocate(); + } + + protected NativeArray(NativeArrayType type, INativeHandle handle) { + super(handle); + this.type = type; + } + + public static NativeArray create(INativeType baseType, int size) { + NativeArrayType type = new NativeArrayType(baseType, size); + return new NativeArray(type); + } + + public INativeType getBaseType() { + return type.getBaseType(); + } + + public void setBaseType(INativeType baseType) { + type = NativeArrayType.create(baseType, type.getArraySize()); + this.values = null; + handle.setSize(getByteCount()); + } + + @Override + public int getByteCount() { + return type.getByteCount(); + } + + /** + * The {@link INativeObject} at index in the sequence (the index'th element + * of the array). + * + * @param index The index of the element to be reported. + * + * @return The NativeObject at index + */ + synchronized public INativeObject getNativeObject(int index) { + if (values == null) { + values = new INativeObject[getSize()]; + } + INativeObject result = values[index]; + if (result == null) { + int elementOffset = index * type.getBaseSize(); + result = type.getBaseType().createNative( + handle.offset(elementOffset)); + values[index] = result; + } + return result; + } + + /* + * (non-Javadoc) + * + * @see de.intarsys.graphic.freetype.NativeObject#getMetaClass() + */ + @Override + public INativeType getNativeType() { + return type; + } + + /** + * The number of NativeObject instances in the sequence represented by this + * (in other terms the array size). + * + * @return The number of NativeObject instances in the sequence represented + * by this + */ + public int getSize() { + return type.getArraySize(); + } + + public void setSize(int size) { + type = NativeArrayType.create(type.getBaseType(), size); + this.values = null; + handle.setSize(getByteCount()); + } + + public Object getValue() { + throw new UnsupportedOperationException( + "getValue not implemented for NativeArray"); + } + + public void setValue(Object value) { + throw new UnsupportedOperationException( + "setValue not implemented for NativeArray"); + } + + public Object getValue(int index) { + return getNativeObject(index).getValue(); + } + + public void setValue(int index, Object value) { + getNativeObject(index).setValue(value); + } + + /* + * (non-Javadoc) + * + * @see de.intarsys.graphic.freetype.NativeObject#toNestedString() + */ + @Override + public String toNestedString() { + return "[...]"; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("["); + for (int i = 0; i < getSize(); i++) { + // NativeObject element = get(i); + // sb.append(element.toString()); + // sb.append(", "); + } + sb.append("]"); + return sb.toString(); + } +} diff --git a/src/de/intarsys/nativec/type/NativeArrayType.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeArrayType.java similarity index 53% rename from src/de/intarsys/nativec/type/NativeArrayType.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeArrayType.java index 361f132..ee19103 100644 --- a/src/de/intarsys/nativec/type/NativeArrayType.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeArrayType.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,74 +29,68 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; - -/** - * The type for a {@link NativeArray} - */ public class NativeArrayType extends NativeType { - public static NativeArrayType create(INativeType baseType, int size) { - return new NativeArrayType(baseType, size); - } - - private final int arraySize; - - /** - * The size of the base type of the array. - */ - private final int baseSize; - - /** - * The base type of the array. - */ - private final INativeType baseType; + private final int arraySize; + /** + * The size of the base type of the array. + */ + private final int baseSize; + /** + * The base type of the array. + */ + private final INativeType baseType; - protected NativeArrayType(INativeType baseDeclaration, int arraySize) { - super(); - this.baseType = baseDeclaration; - this.baseSize = baseDeclaration.getByteCount(); - this.arraySize = arraySize; - } + protected NativeArrayType(INativeType baseDeclaration, int arraySize) { + super(); + this.baseType = baseDeclaration; + this.baseSize = baseDeclaration.getByteCount(); + this.arraySize = arraySize; + } - @Override - public INativeObject createNative(INativeHandle handle) { - NativeArray array = new NativeArray(this, handle); - if (arraySize > 0) { - array.setSize(arraySize); - } - return array; - } + public static NativeArrayType create(INativeType baseType, int size) { + return new NativeArrayType(baseType, size); + } - @Override - public INativeObject createNative(Object value) { - NativeArray array = new NativeArray(this); - return array; - } + @Override + public INativeObject createNative(INativeHandle handle) { + NativeArray array = new NativeArray(this, handle); + if (arraySize > 0) { + array.setSize(arraySize); + } + return array; + } - public int getArraySize() { - return arraySize; - } + @Override + public INativeObject createNative(Object value) { + NativeArray array = new NativeArray(this); + return array; + } - public int getBaseSize() { - return baseSize; - } + public int getArraySize() { + return arraySize; + } - public INativeType getBaseType() { - return baseType; - } + public int getBaseSize() { + return baseSize; + } - @Override - public int getByteCount() { - // todo take into account alignment / packing? - return arraySize * baseSize; - } + public INativeType getBaseType() { + return baseType; + } - public int getPreferredBoundary() { - return baseType.getPreferredBoundary(); - } + @Override + public int getByteCount() { + // todo take into account alignment / packing? + return arraySize * baseSize; + } + public int getPreferredBoundary() { + return baseType.getPreferredBoundary(); + } } diff --git a/nativec-api/src/main/java/de/intarsys/nativec/type/NativeBuffer.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeBuffer.java new file mode 100644 index 0000000..576b5b2 --- /dev/null +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeBuffer.java @@ -0,0 +1,117 @@ +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.type; + +import java.nio.ByteBuffer; + +import de.intarsys.nativec.api.INativeHandle; +import de.intarsys.nativec.api.NativeInterface; +public class NativeBuffer extends NativeObject { + + /** + * The meta class instance + */ + public static final NativeBufferType META = new NativeBufferType(); + + static { + NativeType.register(NativeBuffer.class, META); + } + + private NativeBufferType type; + + public NativeBuffer(byte[] bytes) { + type = new NativeBufferType(bytes.length); + handle = NativeInterface.get().allocate(bytes.length); + handle.setByteArray(0, bytes, 0, bytes.length); + } + + protected NativeBuffer(INativeHandle handle) { + super(handle); + } + + public NativeBuffer(int pSize) { + type = new NativeBufferType(pSize); + handle = NativeInterface.get().allocate(pSize); + } + + /* + * (non-Javadoc) + * + * @see de.intarsys.tools.nativec.NativeObject#getByteCount() + */ + @Override + public int getByteCount() { + // todo alignment? + return type.getByteCount(); + } + + /* + * (non-Javadoc) + * + * @see de.intarsys.graphic.freetype.NativeObject#getMetaClass() + */ + @Override + public INativeType getNativeType() { + return META; + } + + /** + * The number of elements in the NativeBuffer + * + * @return The number of elements in the NativeBuffer + */ + public int getSize() { + return type.getByteCount(); + } + + public void setSize(int size) { + type = new NativeBufferType(size); + handle.setSize(getByteCount()); + } + + public Object getValue() { + return getBytes(); + } + + public void setValue(Object value) { + if (value instanceof byte[]) { + byte[] data = (byte[]) value; + setByteArray(0, data, 0, data.length); + } else if (value instanceof ByteBuffer) { + byte[] data = new byte[getSize()]; + ((ByteBuffer) value).get(data); + setByteArray(0, data, 0, data.length); + } + } +} diff --git a/src/de/intarsys/nativec/type/NativeBufferType.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeBufferType.java similarity index 64% rename from src/de/intarsys/nativec/type/NativeBufferType.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeBufferType.java index 0c8e1ae..1b1673e 100644 --- a/src/de/intarsys/nativec/type/NativeBufferType.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeBufferType.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,54 +29,50 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; - -/** - * The meta class implementation - */ public class NativeBufferType extends NativeType { - public static NativeBufferType create(int size) { - return new NativeBufferType(size); - } - - final private int bufferSize; + final private int bufferSize; - protected NativeBufferType() { - super(); - this.bufferSize = 0; - } + protected NativeBufferType() { + super(); + this.bufferSize = 0; + } - protected NativeBufferType(int bufferSize) { - super(); - this.bufferSize = bufferSize; - } + protected NativeBufferType(int bufferSize) { + super(); + this.bufferSize = bufferSize; + } - @Override - public INativeObject createNative(INativeHandle handle) { - NativeBuffer buffer; + public static NativeBufferType create(int size) { + return new NativeBufferType(size); + } - buffer = new NativeBuffer(handle); - // setSize() creates a new type object - // TODO create constructor to use the existing one? - buffer.setSize(bufferSize); - return buffer; - } + @Override + public INativeObject createNative(INativeHandle handle) { + NativeBuffer buffer; - public int getBufferSize() { - return bufferSize; - } + buffer = new NativeBuffer(handle); + // setSize() creates a new type object + // TODO create constructor to use the existing one? + buffer.setSize(bufferSize); + return buffer; + } - @Override - public int getByteCount() { - return bufferSize; - } + public int getBufferSize() { + return bufferSize; + } - public int getPreferredBoundary() { - return 1; - } + @Override + public int getByteCount() { + return bufferSize; + } -} \ No newline at end of file + public int getPreferredBoundary() { + return 1; + } +} diff --git a/src/de/intarsys/nativec/type/NativeByte.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeByte.java similarity index 50% rename from src/de/intarsys/nativec/type/NativeByte.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeByte.java index e92bb98..caa6d28 100644 --- a/src/de/intarsys/nativec/type/NativeByte.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeByte.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,96 +29,94 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.NativeTools; - -/** - * A wrapper for a primitive byte. - * - */ public class NativeByte extends NativeNumber { - /** The meta class instance */ - public static final NativeByteType META = new NativeByteType(); - - static { - NativeType.register(NativeByte.class, META); - } - - static public NativeByte createFromAddress(long address) { - return (NativeByte) NativeByte.META.createNative(NativeTools - .toHandle(address)); - } - - /** - * Create a new wrapper - */ - public NativeByte() { - allocate(); - } - - /** - * Create a new wrapper - */ - public NativeByte(byte value) { - allocate(); - setValue(value); - } - - protected NativeByte(INativeHandle handle) { - super(handle); - } - - @Override - public byte byteValue() { - return handle.getByte(0); - } - - @Override - public INativeType getNativeType() { - return META; - } - - @Override - public Object getValue() { - return new Byte(byteValue()); - } - - @Override - public int intValue() { - return byteValue(); - } - - @Override - public long longValue() { - return byteValue(); - } - - public void setValue(byte value) { - handle.setByte(0, value); - } - - @Override - public void setValue(Object value) { - setValue(((Number) value).byteValue()); - } - - @Override - public short shortValue() { - return byteValue(); - } - - @Override - public String toString() { - if (getNativeHandle() == null) { - return "nope - no handle"; //$NON-NLS-1$ - } - if (getNativeHandle().getAddress() == 0) { - return "nope - null pointer"; //$NON-NLS-1$ - } - return String.valueOf(byteValue()); - } + /** + * The meta class instance + */ + public static final NativeByteType META = new NativeByteType(); + + static { + NativeType.register(NativeByte.class, META); + } + + /** + * Create a new wrapper + */ + public NativeByte() { + allocate(); + } + + /** + * Create a new wrapper + */ + public NativeByte(byte value) { + allocate(); + setValue(value); + } + + protected NativeByte(INativeHandle handle) { + super(handle); + } + + static public NativeByte createFromAddress(long address) { + return (NativeByte) NativeByte.META.createNative(NativeTools + .toHandle(address)); + } + + @Override + public byte byteValue() { + return handle.getByte(0); + } + + @Override + public INativeType getNativeType() { + return META; + } + + @Override + public Object getValue() { + return new Byte(byteValue()); + } + + public void setValue(byte value) { + handle.setByte(0, value); + } + + @Override + public void setValue(Object value) { + setValue(((Number) value).byteValue()); + } + + @Override + public int intValue() { + return byteValue(); + } + + @Override + public long longValue() { + return byteValue(); + } + + @Override + public short shortValue() { + return byteValue(); + } + + @Override + public String toString() { + if (getNativeHandle() == null) { + return "nope - no handle"; //$NON-NLS-1$ + } + if (getNativeHandle().getAddress() == 0) { + return "nope - null pointer"; //$NON-NLS-1$ + } + return String.valueOf(byteValue()); + } } diff --git a/src/de/intarsys/nativec/type/NativeByteType.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeByteType.java similarity index 76% rename from src/de/intarsys/nativec/type/NativeByteType.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeByteType.java index e446a6b..076ec2e 100644 --- a/src/de/intarsys/nativec/type/NativeByteType.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeByteType.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,32 +29,29 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; - -/** - * The meta class implementation - */ public class NativeByteType extends NativeNumberType { - protected NativeByteType() { - super(); - } + protected NativeByteType() { + super(); + } - @Override - public INativeObject createNative(INativeHandle handle) { - return new NativeByte(handle); - } + @Override + public INativeObject createNative(INativeHandle handle) { + return new NativeByte(handle); + } - @Override - public INativeObject createNative(Object value) { - return new NativeByte(((Number) value).byteValue()); - } + @Override + public INativeObject createNative(Object value) { + return new NativeByte(((Number) value).byteValue()); + } - @Override - public int getByteCount() { - return NativeByte.SIZE_BYTE; - } -} \ No newline at end of file + @Override + public int getByteCount() { + return NativeByte.SIZE_BYTE; + } +} diff --git a/nativec-api/src/main/java/de/intarsys/nativec/type/NativeFunction.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeFunction.java new file mode 100644 index 0000000..48cbc91 --- /dev/null +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeFunction.java @@ -0,0 +1,77 @@ +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.type; + +import de.intarsys.nativec.api.INativeFunction; +import de.intarsys.nativec.api.INativeHandle; +import de.intarsys.nativec.api.NativeInterface; + +public class NativeFunction extends NativeVoid { + + public static final NativeFunctionType META = new NativeFunctionType(); + private Object callingConvention; + private INativeFunction function; + protected NativeFunction(INativeHandle handle) { + super(handle); + callingConvention = INativeFunction.CallingConventionCdecl; + } + + public Object getCallingConvention() { + return callingConvention; + } + + public void setCallingConvention(Object callingConvention) { + if (function != null) { + throw new IllegalStateException(); + } + this.callingConvention = callingConvention; + } + + public INativeFunction getFunction() { + // TODO do we want to check if handle has changed? + if (function == null) { + function = NativeInterface.get().createFunction( + getNativeHandle().getAddress(), callingConvention); + } + return function; + } + + public static class NativeFunctionType extends NativeVoidType { + + @Override + public INativeObject createNative(INativeHandle handle) { + return new NativeFunction(handle); + } + } +} diff --git a/src/de/intarsys/nativec/type/NativeGenericStruct.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeGenericStruct.java similarity index 71% rename from src/de/intarsys/nativec/type/NativeGenericStruct.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeGenericStruct.java index e894a52..88bdd2c 100644 --- a/src/de/intarsys/nativec/type/NativeGenericStruct.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeGenericStruct.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,43 +29,38 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; - -/** - * The generic struct may change its type at runtime. - * - */ public class NativeGenericStruct extends NativeStruct { - private NativeStructType type; - - protected NativeGenericStruct(NativeStructType type) { - super(); - this.type = type; - allocate(); - } + private NativeStructType type; - protected NativeGenericStruct(NativeStructType type, INativeHandle handle) { - super(handle); - this.type = type; - } + protected NativeGenericStruct(NativeStructType type) { + super(); + this.type = type; + allocate(); + } - @Override - public INativeType getNativeType() { - return type; - } + protected NativeGenericStruct(NativeStructType type, INativeHandle handle) { + super(handle); + this.type = type; + } - @Override - public NativeStructType getStructType() { - return type; - } + @Override + public INativeType getNativeType() { + return type; + } - public void setNativeType(NativeStructType type) { - this.type = type; - this.values = null; - } + public void setNativeType(NativeStructType type) { + this.type = type; + this.values = null; + } + @Override + public NativeStructType getStructType() { + return type; + } } diff --git a/src/de/intarsys/nativec/type/NativeInt.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeInt.java similarity index 50% rename from src/de/intarsys/nativec/type/NativeInt.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeInt.java index e87ec2a..a469874 100644 --- a/src/de/intarsys/nativec/type/NativeInt.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeInt.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,96 +29,94 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.NativeTools; - -/** - * A wrapper for a primitive int (which is always 4 bytes except on ILP64 - * systems which can be treated as non-existent for our purposes). - */ public class NativeInt extends NativeNumber { - /** The meta class instance */ - public static final NativeIntType META = new NativeIntType(); - - static { - NativeType.register(NativeInt.class, META); - } - - static public NativeInt createFromAddress(long address) { - return (NativeInt) NativeInt.META.createNative(NativeTools - .toHandle(address)); - } - - /** - * Create a new wrapper - */ - public NativeInt() { - allocate(); - } - - protected NativeInt(INativeHandle handle) { - super(handle); - } - - /** - * Create a new wrapper - */ - public NativeInt(long value) { - allocate(); - setValue(value); - } - - @Override - public byte byteValue() { - return (byte) intValue(); - } - - @Override - public INativeType getNativeType() { - return META; - } - - @Override - public Object getValue() { - return new Integer(intValue()); - } - - @Override - public int intValue() { - return handle.getInt(0); - } - - @Override - public long longValue() { - return intValue(); - } - - public void setValue(int value) { - handle.setInt(0, value); - } - - @Override - public void setValue(Object value) { - setValue(((Number) value).intValue()); - } - - @Override - public short shortValue() { - return (short) intValue(); - } - - @Override - public String toString() { - if (getNativeHandle() == null) { - return "nope - no handle"; //$NON-NLS-1$ - } - if (getNativeHandle().getAddress() == 0) { - return "nope - null pointer"; //$NON-NLS-1$ - } - return String.valueOf(intValue()); - } + /** + * The meta class instance + */ + public static final NativeIntType META = new NativeIntType(); + + static { + NativeType.register(NativeInt.class, META); + } + + /** + * Create a new wrapper + */ + public NativeInt() { + allocate(); + } + + protected NativeInt(INativeHandle handle) { + super(handle); + } + + /** + * Create a new wrapper + */ + public NativeInt(long value) { + allocate(); + setValue(value); + } + + static public NativeInt createFromAddress(long address) { + return (NativeInt) NativeInt.META.createNative(NativeTools + .toHandle(address)); + } + + @Override + public byte byteValue() { + return (byte) intValue(); + } + + @Override + public INativeType getNativeType() { + return META; + } + + @Override + public Object getValue() { + return new Integer(intValue()); + } + + public void setValue(int value) { + handle.setInt(0, value); + } + + @Override + public void setValue(Object value) { + setValue(((Number) value).intValue()); + } + + @Override + public int intValue() { + return handle.getInt(0); + } + + @Override + public long longValue() { + return intValue(); + } + + @Override + public short shortValue() { + return (short) intValue(); + } + + @Override + public String toString() { + if (getNativeHandle() == null) { + return "nope - no handle"; //$NON-NLS-1$ + } + if (getNativeHandle().getAddress() == 0) { + return "nope - null pointer"; //$NON-NLS-1$ + } + return String.valueOf(intValue()); + } } diff --git a/src/de/intarsys/nativec/type/NativeIntType.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeIntType.java similarity index 76% rename from src/de/intarsys/nativec/type/NativeIntType.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeIntType.java index 45b55cf..211f6ce 100644 --- a/src/de/intarsys/nativec/type/NativeIntType.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeIntType.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,32 +29,29 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; - -/** - * The meta class implementation - */ public class NativeIntType extends NativeNumberType { - protected NativeIntType() { - super(); - } + protected NativeIntType() { + super(); + } - @Override - public INativeObject createNative(INativeHandle handle) { - return new NativeInt(handle); - } + @Override + public INativeObject createNative(INativeHandle handle) { + return new NativeInt(handle); + } - @Override - public INativeObject createNative(Object value) { - return new NativeInt(((Number) value).longValue()); - } + @Override + public INativeObject createNative(Object value) { + return new NativeInt(((Number) value).longValue()); + } - @Override - public int getByteCount() { - return NativeInt.SIZE_INT; - } -} \ No newline at end of file + @Override + public int getByteCount() { + return NativeInt.SIZE_INT; + } +} diff --git a/src/de/intarsys/nativec/type/NativeLong.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeLong.java similarity index 50% rename from src/de/intarsys/nativec/type/NativeLong.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeLong.java index 319cdc3..e8bbe3f 100644 --- a/src/de/intarsys/nativec/type/NativeLong.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeLong.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,96 +29,94 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.NativeTools; - -/** - * A wrapper for a primitive long. The size of a long depends on the platform; - * on a 64-bit Linux or Mac OS X we have 8 byte longs... - */ public class NativeLong extends NativeNumber { - /** The meta class instance */ - public static final NativeLongType META = new NativeLongType(); - - static { - NativeType.register(NativeLong.class, META); - } - - static public NativeLong createFromAddress(long address) { - return (NativeLong) NativeLong.META.createNative(NativeTools - .toHandle(address)); - } - - /** - * Create a new wrapper - */ - public NativeLong() { - allocate(); - } - - protected NativeLong(INativeHandle handle) { - super(handle); - } - - /** - * Create a new wrapper - */ - public NativeLong(long value) { - allocate(); - setValue(value); - } - - @Override - public byte byteValue() { - return (byte) intValue(); - } - - @Override - public INativeType getNativeType() { - return META; - } - - @Override - public Object getValue() { - return new Long(longValue()); - } - - @Override - public int intValue() { - return (int) longValue(); - } - - @Override - public long longValue() { - return handle.getCLong(0); - } - - public void setValue(long value) { - handle.setCLong(0, value); - } - - @Override - public void setValue(Object value) { - setValue(((Number) value).longValue()); - } - - @Override - public short shortValue() { - return (short) longValue(); - } - - @Override - public String toString() { - if (getNativeHandle() == null) { - return "nope - no handle"; //$NON-NLS-1$ - } - if (getNativeHandle().getAddress() == 0) { - return "nope - null pointer"; //$NON-NLS-1$ - } - return String.valueOf(longValue()); - } + /** + * The meta class instance + */ + public static final NativeLongType META = new NativeLongType(); + + static { + NativeType.register(NativeLong.class, META); + } + + /** + * Create a new wrapper + */ + public NativeLong() { + allocate(); + } + + protected NativeLong(INativeHandle handle) { + super(handle); + } + + /** + * Create a new wrapper + */ + public NativeLong(long value) { + allocate(); + setValue(value); + } + + static public NativeLong createFromAddress(long address) { + return (NativeLong) NativeLong.META.createNative(NativeTools + .toHandle(address)); + } + + @Override + public byte byteValue() { + return (byte) intValue(); + } + + @Override + public INativeType getNativeType() { + return META; + } + + @Override + public Object getValue() { + return new Long(longValue()); + } + + public void setValue(long value) { + handle.setCLong(0, value); + } + + @Override + public void setValue(Object value) { + setValue(((Number) value).longValue()); + } + + @Override + public int intValue() { + return (int) longValue(); + } + + @Override + public long longValue() { + return handle.getCLong(0); + } + + @Override + public short shortValue() { + return (short) longValue(); + } + + @Override + public String toString() { + if (getNativeHandle() == null) { + return "nope - no handle"; //$NON-NLS-1$ + } + if (getNativeHandle().getAddress() == 0) { + return "nope - null pointer"; //$NON-NLS-1$ + } + return String.valueOf(longValue()); + } } diff --git a/nativec-api/src/main/java/de/intarsys/nativec/type/NativeLongLP64.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeLongLP64.java new file mode 100644 index 0000000..3d92d75 --- /dev/null +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeLongLP64.java @@ -0,0 +1,129 @@ +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.type; + +import de.intarsys.nativec.api.INativeHandle; +import de.intarsys.nativec.api.NativeTools; +public class NativeLongLP64 extends NativeNumber { + + /** + * The meta class instance + */ + public static final NativeLongLP64Type META = new NativeLongLP64Type(); + + static { + NativeType.register(NativeLongLP64.class, META); + } + + /** + * Create a new wrapper + */ + public NativeLongLP64() { + allocate(); + } + + protected NativeLongLP64(INativeHandle handle) { + super(handle); + } + + /** + * Create a new wrapper + */ + public NativeLongLP64(long value) { + allocate(); + setValue(value); + } + + static public NativeLongLP64 createFromAddress(long address) { + return (NativeLongLP64) NativeLongLP64.META.createNative(NativeTools + .toHandle(address)); + } + + @Override + public byte byteValue() { + return (byte) intValue(); + } + + @Override + public INativeType getNativeType() { + return META; + } + + @Override + public Object getValue() { + return new Long(longValue()); + } + + public void setValue(long value) { + if (NativeObject.SIZE_PTR == 4) { + handle.setInt(0, (int) value); + return; + } + handle.setLong(0, value); + } + + @Override + public void setValue(Object value) { + setValue(((Number) value).longValue()); + } + + @Override + public int intValue() { + return (int) longValue(); + } + + @Override + public long longValue() { + if (NativeObject.SIZE_PTR == 4) { + return handle.getInt(0); + } + return handle.getLong(0); + } + + @Override + public short shortValue() { + return (short) longValue(); + } + + @Override + public String toString() { + if (getNativeHandle() == null) { + return "nope - no handle"; //$NON-NLS-1$ + } + if (getNativeHandle().getAddress() == 0) { + return "nope - null pointer"; //$NON-NLS-1$ + } + return String.valueOf(longValue()); + } +} diff --git a/src/de/intarsys/nativec/type/NativeLongLP64Type.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeLongLP64Type.java similarity index 61% rename from src/de/intarsys/nativec/type/NativeLongLP64Type.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeLongLP64Type.java index 46eb639..e1235e9 100644 --- a/src/de/intarsys/nativec/type/NativeLongLP64Type.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeLongLP64Type.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,54 +29,51 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; - -/** - * The meta class implementation - */ public class NativeLongLP64Type extends NativeNumberType { - /** - * Utility method: return the given number as another number object with - * compatible byte size - */ - public static Object coerce(Number value) { - if (NativeObject.SIZE_PTR == 4) { - return value.intValue(); - } - return value.longValue(); - } + protected NativeLongLP64Type() { + super(); + } - /** - * Utility method: return the java class whose instances have compatible - * byte size - */ - public static Class primitiveClass() { - if (NativeObject.SIZE_PTR == 4) { - return Integer.class; - } - return Long.class; - } + /** + * Utility method: return the given number as another number object with + * compatible byte size + */ + public static Object coerce(Number value) { + if (NativeObject.SIZE_PTR == 4) { + return value.intValue(); + } + return value.longValue(); + } - protected NativeLongLP64Type() { - super(); - } + /** + * Utility method: return the java class whose instances have compatible + * byte size + */ + public static Class primitiveClass() { + if (NativeObject.SIZE_PTR == 4) { + return Integer.class; + } + return Long.class; + } - @Override - public INativeObject createNative(INativeHandle handle) { - return new NativeLongLP64(handle); - } + @Override + public INativeObject createNative(INativeHandle handle) { + return new NativeLongLP64(handle); + } - @Override - public INativeObject createNative(Object value) { - return new NativeLongLP64(((Number) value).longValue()); - } + @Override + public INativeObject createNative(Object value) { + return new NativeLongLP64(((Number) value).longValue()); + } - @Override - public int getByteCount() { - return NativeObject.SIZE_PTR; - } -} \ No newline at end of file + @Override + public int getByteCount() { + return NativeObject.SIZE_PTR; + } +} diff --git a/src/de/intarsys/nativec/type/NativeLongType.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeLongType.java similarity index 76% rename from src/de/intarsys/nativec/type/NativeLongType.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeLongType.java index 0f107d8..271cd5a 100644 --- a/src/de/intarsys/nativec/type/NativeLongType.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeLongType.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,32 +29,29 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; - -/** - * The meta class implementation - */ public class NativeLongType extends NativeNumberType { - protected NativeLongType() { - super(); - } + protected NativeLongType() { + super(); + } - @Override - public INativeObject createNative(INativeHandle handle) { - return new NativeLong(handle); - } + @Override + public INativeObject createNative(INativeHandle handle) { + return new NativeLong(handle); + } - @Override - public INativeObject createNative(Object value) { - return new NativeLong(((Number) value).longValue()); - } + @Override + public INativeObject createNative(Object value) { + return new NativeLong(((Number) value).longValue()); + } - @Override - public int getByteCount() { - return NativeLong.SIZE_LONG; - } -} \ No newline at end of file + @Override + public int getByteCount() { + return NativeLong.SIZE_LONG; + } +} diff --git a/src/de/intarsys/nativec/type/NativeNumber.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeNumber.java similarity index 65% rename from src/de/intarsys/nativec/type/NativeNumber.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeNumber.java index 6259626..4ef3a59 100644 --- a/src/de/intarsys/nativec/type/NativeNumber.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeNumber.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,51 +29,48 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; - -/** - * An abstract superclass for the implementation of number - * {@link NativeSimple}. - */ public abstract class NativeNumber extends NativeSimple { - /** - * Create a wrapper - */ - protected NativeNumber() { - } - protected NativeNumber(INativeHandle handle) { - super(handle); - } + /** + * Create a wrapper + */ + protected NativeNumber() { + } + + protected NativeNumber(INativeHandle handle) { + super(handle); + } - /** - * This as a java primitive byte value. - * - * @return This as a java primitive byte value. - */ - public abstract byte byteValue(); + /** + * This as a java primitive byte value. + * + * @return This as a java primitive byte value. + */ + public abstract byte byteValue(); - /** - * This as a java primitive int value. - * - * @return This as a java primitive int value. - */ - public abstract int intValue(); + /** + * This as a java primitive int value. + * + * @return This as a java primitive int value. + */ + public abstract int intValue(); - /** - * This as a java primitive long value. - * - * @return This as a java primitive long value. - */ - public abstract long longValue(); + /** + * This as a java primitive long value. + * + * @return This as a java primitive long value. + */ + public abstract long longValue(); - /** - * This as a java primitive short value. - * - * @return This as a java primitive short value. - */ - public abstract short shortValue(); + /** + * This as a java primitive short value. + * + * @return This as a java primitive short value. + */ + public abstract short shortValue(); } diff --git a/src/de/intarsys/nativec/type/NativeNumberType.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeNumberType.java similarity index 89% rename from src/de/intarsys/nativec/type/NativeNumberType.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeNumberType.java index ef64399..c2c6c7c 100644 --- a/src/de/intarsys/nativec/type/NativeNumberType.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeNumberType.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,15 +29,12 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; - -/** - * The meta class implementation - */ public class NativeNumberType extends NativeSimpleType { - protected NativeNumberType() { - super(); - } -} \ No newline at end of file + protected NativeNumberType() { + super(); + } +} diff --git a/nativec-api/src/main/java/de/intarsys/nativec/type/NativeObject.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeObject.java new file mode 100644 index 0000000..0e67bed --- /dev/null +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeObject.java @@ -0,0 +1,277 @@ +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.type; + +import de.intarsys.nativec.api.INativeHandle; +import de.intarsys.nativec.api.NativeInterface; +public abstract class NativeObject implements INativeObject { + + public static final int SIZE_BYTE = 1; + + public static final int SIZE_INT = 4; + + public static final int SHIFT_INT = 2; + + public static final int SIZE_LONGLONG = 8; + + public static final int SHIFT_LONGLONG = 3; + + public static final int SIZE_LONG = NativeInterface.get().longSize(); + + public static final int SHIFT_LONG = SIZE_LONG == 4 ? 2 : 3; + + public static final int SIZE_PTR = NativeInterface.get().pointerSize(); + + public static final int SIZE_SHORT = 2; + + /** + * DEBUG flag + */ + public static boolean DEBUG = true; + + /** + * The handle to the memory chunk used by this object. While in fact this is + * final, Java language semantics does not allow to declare so! + *

+ * The handle should only be assigned in the constructor, via parameter or + * "allocate". + */ + protected INativeHandle handle; + + /** + * + */ + protected NativeObject() { + // + } + + /** + * Create a new NativeObject in C-Memory at pointer "handle". The bytes + * belonging to this object may already have been copied from C-Memory and + * made available in bytes at location offset. + * + * @param handle The pointer in C-memory + */ + protected NativeObject(INativeHandle handle) { + this.handle = handle; + } + + /** + * Manage the objects memory in Java. C memory will be valid at least as + * long as we hold a reference to the buffer. C memory is undefined (but not + * a memory leak) and may be reclaimed at any time after dropping our + * pointer to the buffer. + */ + protected void allocate() { + int size = getByteCount(); + handle = NativeInterface.get().allocate(size); + } + + /** + * This is a special form of the "createNative" signature, implementing a + * "type cast" on the same memory location. + * + * @param declaration The new base declaration type. + * + * @return The {@link INativeObject} at the same memory location as this, + * but of a different type. + */ + public INativeObject cast(INativeType declaration) { + return declaration.createNative(handle); + } + + public INativeObject createReference() { + NativeReference ref = NativeReference.create(getNativeType()); + ref.setValue(this); + return ref; + } + + /** + * The byte at index as a byte. + * + * @param index The index of the element to be reported. + * + * @return The element at index as a native byte. + */ + public byte getByte(int index) { + return handle.getByte(index); + } + + /** + * The element at index as an array of bytes with dimension count. This is a + * lightweight optimization. + * + * @param index The index of the element to be reported. + * + * @return The element at index as an array of native byte with dimension + * count. + */ + public byte[] getByteArray(int index, int count) { + return handle.getByteArray(index, count); + } + + /** + * The number of bytes occupied by this. + * + * @return The number of bytes occupied by this. + */ + public abstract int getByteCount(); + + /** + * The bytes copied from C-memory that represent this. + * + * @return The bytes copied from C-memory that represent this. + */ + public byte[] getBytes() { + return handle.getByteArray(0, getByteCount()); + } + + /** + * The element at index as a native long. Only the "platform" number of + * bytes are read. + * + * @param index The index of the element to be reported. + * + * @return The element at index as a native long. + */ + public long getCLong(int index) { + return handle.getCLong(index); + } + + /** + * The element at index as a native int. + * + * @param index The index of the element to be reported. + * + * @return The element at index as a native int. + */ + public int getInt(int index) { + return handle.getInt(index); + } + + /** + * The C-Pointer where the associated memory is found. + * + * @return The C-Pointer where the associated memory is found. + */ + public INativeHandle getNativeHandle() { + return handle; + } + + public INativeHandle getNativeHandle(int index) { + return handle.getNativeHandle(index); + } + + /** + * The meta information and behavior for the NativeObject. + *

+ * There is exactly one meta instance for all NativeObject instances of a + * certain type. + * + * @return The meta information and behavior for the NativeObject. + */ + abstract public INativeType getNativeType(); + + /** + * The element at index as a native short. This is a lightweight + * optimization. + * + * @param index The index of the element to be reported. + * + * @return The element at index as a native short. + */ + public short getShort(int index) { + return handle.getShort(index); + } + + public String getString(int index) { + return handle.getString(index); + } + + public String getWideString(int index) { + return handle.getWideString(index); + } + + /** + * Answer true if this is "null". This means the associated + * C-pointer is pointing to 0. + * + * @return Answer true if this is "null". + */ + public boolean isNull() { + return handle == null || handle.getAddress() == 0; + } + + public void setByte(int index, byte value) { + handle.setByte(index, value); + } + + public void setByteArray(int index, byte[] value, int valueOffset, + int valueCount) { + handle.setByteArray(index, value, valueOffset, valueCount); + } + + public void setCLong(int index, long value) { + handle.setCLong(index, value); + } + + public void setInt(int index, int value) { + handle.setInt(index, value); + } + + public void setNativeHandle(int index, INativeHandle value) { + handle.setNativeHandle(index, value); + } + + public void setShort(int index, short value) { + handle.setShort(index, value); + } + + public void setString(int index, String value) { + handle.setString(index, value); + } + + public void setWideString(int index, String value) { + handle.setWideString(index, value); + } + + /** + * A string for debugging purposes. + * + * @return A string for debugging purposes. + */ + public String toNestedString() { + return toString(); + } +} diff --git a/nativec-api/src/main/java/de/intarsys/nativec/type/NativeReference.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeReference.java new file mode 100644 index 0000000..4b5b369 --- /dev/null +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeReference.java @@ -0,0 +1,130 @@ +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.type; + +import de.intarsys.nativec.api.INativeHandle; +import de.intarsys.nativec.api.NativeInterface; +public class NativeReference extends NativeObject { + + /** + * The meta class instance + */ + public static final NativeReferenceType META = new NativeReferenceType( + NativeVoid.META); + + static { + NativeType.register(NativeReference.class, META); + } + + private INativeHandle dereferenceHandle; + private T dereferenced; + private NativeReferenceType type; + + protected NativeReference(NativeReferenceType type) { + this.type = type; + allocate(); + } + + protected NativeReference(NativeReferenceType type, INativeHandle handle) { + super(handle); + this.type = type; + handle.setSize(getByteCount()); + } + + public static NativeReference create( + INativeType baseType) { + NativeReferenceType type = new NativeReferenceType(baseType); + return new NativeReference(type); + } + + public INativeType getBaseType() { + return type.getBaseType(); + } + + public void setBaseType(INativeType baseType) { + type = NativeReferenceType.create(baseType); + dereferenceHandle = null; + dereferenced = null; + } + + @Override + public int getByteCount() { + return NativeInterface.get().pointerSize(); + } + + @Override + public INativeType getNativeType() { + return type; + } + + public long getReferencedAddress() { + return getNativeHandle(0).getAddress(); + } + + synchronized public T getValue() { + dereferenceHandle = handle.getNativeHandle(0); + // check if not yet computed or handle changed + if (dereferenced == null + || !dereferenced.getNativeHandle().equals(dereferenceHandle)) { + dereferenced = (T) type.getBaseType().createNative( + dereferenceHandle); + } + return dereferenced; + } + + synchronized public void setValue(Object value) { + if (value instanceof INativeObject) { + T nativeObject = (T) value; + INativeHandle valueHandle = nativeObject.getNativeHandle(); + handle.setNativeHandle(0, valueHandle); + dereferenceHandle = valueHandle; + dereferenced = nativeObject; + } else if (value instanceof INativeHandle) { + INativeHandle valueHandle = (INativeHandle) value; + handle.setNativeHandle(0, valueHandle); + dereferenceHandle = valueHandle; + dereferenced = null; + } else { + throw new IllegalArgumentException(); + } + } + + @Override + public String toString() { + if (handle == null) { + return "Ref to null"; + } + return "Ref to " + getReferencedAddress(); + } +} diff --git a/src/de/intarsys/nativec/type/NativeReferenceType.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeReferenceType.java similarity index 62% rename from src/de/intarsys/nativec/type/NativeReferenceType.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeReferenceType.java index 85efb1d..5d34648 100644 --- a/src/de/intarsys/nativec/type/NativeReferenceType.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeReferenceType.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,53 +29,51 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.NativeInterface; - -/** - * A declaration for a slot containing a reference to a native object. - */ public class NativeReferenceType extends NativeType { - public static NativeReferenceType create(INativeType baseType) { - return new NativeReferenceType(baseType); - } + /** + * The declaration for the dereference slot + */ + private final INativeType baseType; - /** The declaration for the dereference slot */ - private final INativeType baseType; + /** + * Declare a reference to a base type + */ + protected NativeReferenceType(INativeType baseDeclaration) { + super(); + this.baseType = baseDeclaration; + } - /** - * Declare a reference to a base type - */ - protected NativeReferenceType(INativeType baseDeclaration) { - super(); - this.baseType = baseDeclaration; - } + public static NativeReferenceType create(INativeType baseType) { + return new NativeReferenceType(baseType); + } - @Override - public INativeObject createNative(INativeHandle handle) { - return new NativeReference(this, handle); - } + @Override + public INativeObject createNative(INativeHandle handle) { + return new NativeReference(this, handle); + } - @Override - public INativeObject createNative(Object value) { - return new NativeReference(this); - } + @Override + public INativeObject createNative(Object value) { + return new NativeReference(this); + } - public INativeType getBaseType() { - return baseType; - } + public INativeType getBaseType() { + return baseType; + } - @Override - public int getByteCount() { - return NativeInterface.get().pointerSize(); - } - - public int getPreferredBoundary() { - return NativeInterface.get().pointerSize(); - } + @Override + public int getByteCount() { + return NativeInterface.get().pointerSize(); + } + public int getPreferredBoundary() { + return NativeInterface.get().pointerSize(); + } } diff --git a/src/de/intarsys/nativec/type/NativeShort.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeShort.java similarity index 50% rename from src/de/intarsys/nativec/type/NativeShort.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeShort.java index c96b2fc..f40f132 100644 --- a/src/de/intarsys/nativec/type/NativeShort.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeShort.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,96 +29,94 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.NativeTools; - -/** - * A wrapper for a primitive short. - * - */ public class NativeShort extends NativeNumber { - /** The meta class instance */ - public static final NativeShortType META = new NativeShortType(); - - static { - NativeType.register(NativeShort.class, META); - } - - static public NativeShort createFromAddress(long address) { - return (NativeShort) NativeShort.META.createNative(NativeTools - .toHandle(address)); - } - - /** - * Create a new wrapper - */ - public NativeShort() { - allocate(); - } - - protected NativeShort(INativeHandle handle) { - super(handle); - } - - /** - * Create a new wrapper - */ - public NativeShort(short value) { - allocate(); - setValue(value); - } - - @Override - public byte byteValue() { - return (byte) shortValue(); - } - - @Override - public INativeType getNativeType() { - return META; - } - - @Override - public Object getValue() { - return new Short(shortValue()); - } - - @Override - public int intValue() { - return shortValue(); - } - - @Override - public long longValue() { - return shortValue(); - } - - @Override - public void setValue(Object value) { - setValue(((Number) value).shortValue()); - } - - public void setValue(short value) { - handle.setShort(0, value); - } - - @Override - public short shortValue() { - return handle.getShort(0); - } - - @Override - public String toString() { - if (getNativeHandle() == null) { - return "nope - no handle"; //$NON-NLS-1$ - } - if (getNativeHandle().getAddress() == 0) { - return "nope - null pointer"; //$NON-NLS-1$ - } - return String.valueOf(shortValue()); - } + /** + * The meta class instance + */ + public static final NativeShortType META = new NativeShortType(); + + static { + NativeType.register(NativeShort.class, META); + } + + /** + * Create a new wrapper + */ + public NativeShort() { + allocate(); + } + + protected NativeShort(INativeHandle handle) { + super(handle); + } + + /** + * Create a new wrapper + */ + public NativeShort(short value) { + allocate(); + setValue(value); + } + + static public NativeShort createFromAddress(long address) { + return (NativeShort) NativeShort.META.createNative(NativeTools + .toHandle(address)); + } + + @Override + public byte byteValue() { + return (byte) shortValue(); + } + + @Override + public INativeType getNativeType() { + return META; + } + + @Override + public Object getValue() { + return new Short(shortValue()); + } + + @Override + public void setValue(Object value) { + setValue(((Number) value).shortValue()); + } + + public void setValue(short value) { + handle.setShort(0, value); + } + + @Override + public int intValue() { + return shortValue(); + } + + @Override + public long longValue() { + return shortValue(); + } + + @Override + public short shortValue() { + return handle.getShort(0); + } + + @Override + public String toString() { + if (getNativeHandle() == null) { + return "nope - no handle"; //$NON-NLS-1$ + } + if (getNativeHandle().getAddress() == 0) { + return "nope - null pointer"; //$NON-NLS-1$ + } + return String.valueOf(shortValue()); + } } diff --git a/src/de/intarsys/nativec/type/NativeShortType.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeShortType.java similarity index 76% rename from src/de/intarsys/nativec/type/NativeShortType.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeShortType.java index 47c5b6f..b835162 100644 --- a/src/de/intarsys/nativec/type/NativeShortType.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeShortType.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,32 +29,29 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; - -/** - * The meta class implementation - */ public class NativeShortType extends NativeNumberType { - protected NativeShortType() { - super(); - } + protected NativeShortType() { + super(); + } - @Override - public NativeObject createNative(INativeHandle handle) { - return new NativeShort(handle); - } + @Override + public NativeObject createNative(INativeHandle handle) { + return new NativeShort(handle); + } - @Override - public NativeObject createNative(Object value) { - return new NativeShort(((Number) value).shortValue()); - } + @Override + public NativeObject createNative(Object value) { + return new NativeShort(((Number) value).shortValue()); + } - @Override - public int getByteCount() { - return NativeShort.SIZE_SHORT; - } -} \ No newline at end of file + @Override + public int getByteCount() { + return NativeShort.SIZE_SHORT; + } +} diff --git a/src/de/intarsys/nativec/type/NativeSimple.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeSimple.java similarity index 70% rename from src/de/intarsys/nativec/type/NativeSimple.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeSimple.java index 09a8161..9a895db 100644 --- a/src/de/intarsys/nativec/type/NativeSimple.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeSimple.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,39 +29,31 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; - -/** - * An abstract superclass for the implementation of primitive - * {@link NativeObject} instances. These {@link NativeObject} instances are not - * constructed from other {@link NativeObject} instances but implemented - * directly in Java, having a fixed size. - * - */ public abstract class NativeSimple extends NativeObject { - /** - * Create a wrapper for a NativePrimitive - */ - protected NativeSimple() { - } - - protected NativeSimple(INativeHandle handle) { - super(handle); - handle.setSize(getByteCount()); - } + /** + * Create a wrapper for a NativePrimitive + */ + protected NativeSimple() { + } - /* - * (non-Javadoc) - * - * @see de.intarsys.tools.nativec.NativeObject#getByteCount() - */ - @Override - public int getByteCount() { - return getNativeType().getByteCount(); - } + protected NativeSimple(INativeHandle handle) { + super(handle); + handle.setSize(getByteCount()); + } + /* + * (non-Javadoc) + * + * @see de.intarsys.tools.nativec.NativeObject#getByteCount() + */ + @Override + public int getByteCount() { + return getNativeType().getByteCount(); + } } diff --git a/src/de/intarsys/nativec/type/NativeSimpleType.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeSimpleType.java similarity index 86% rename from src/de/intarsys/nativec/type/NativeSimpleType.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeSimpleType.java index 929ae07..dbd137b 100644 --- a/src/de/intarsys/nativec/type/NativeSimpleType.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeSimpleType.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,20 +29,16 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; - -/** - * The meta class implementation - */ public class NativeSimpleType extends NativeType { - protected NativeSimpleType() { - super(); - } - - public int getPreferredBoundary() { - return getByteCount(); - } + protected NativeSimpleType() { + super(); + } -} \ No newline at end of file + public int getPreferredBoundary() { + return getByteCount(); + } +} diff --git a/src/de/intarsys/nativec/type/NativeStaticStruct.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeStaticStruct.java similarity index 84% rename from src/de/intarsys/nativec/type/NativeStaticStruct.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeStaticStruct.java index 8454eb5..ae571e8 100644 --- a/src/de/intarsys/nativec/type/NativeStaticStruct.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeStaticStruct.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,24 +29,19 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; - -/** - * A struct with a statically defined type. - * - */ public abstract class NativeStaticStruct extends NativeStruct { - protected NativeStaticStruct() { - allocate(); - } - - protected NativeStaticStruct(INativeHandle handle) { - super(handle); - handle.setSize(getByteCount()); - } + protected NativeStaticStruct() { + allocate(); + } + protected NativeStaticStruct(INativeHandle handle) { + super(handle); + handle.setSize(getByteCount()); + } } diff --git a/nativec-api/src/main/java/de/intarsys/nativec/type/NativeString.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeString.java new file mode 100644 index 0000000..90a5230 --- /dev/null +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeString.java @@ -0,0 +1,116 @@ +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.type; + +import de.intarsys.nativec.api.INativeHandle; +import de.intarsys.nativec.api.NativeTools; +public class NativeString extends NativeObject { + + /** + * The meta class instance + */ + public static final NativeStringType META = new NativeStringType(); + + static { + NativeType.register(NativeString.class, META); + } + + private int size = 0; + private NativeStringType type; + protected NativeString(NativeStringType pType) { + type = pType; + allocate(); + } + + protected NativeString(NativeStringType pType, INativeHandle handle) { + super(handle); + type = pType; + if (type.hasByteCount()) { + handle.setSize(type.getByteCount()); + } + } + + protected NativeString(NativeStringType pType, String value) { + type = pType; + if (type.getStringSize() == 0) { + this.size = value.length() + 1; + } + allocate(); + setValue(value); + } + + public NativeString(String value) { + this(META, value); + } + + static public NativeString createFromAddress(long address) { + return (NativeString) NativeString.META.createNative(NativeTools + .toHandle(address)); + } + + @Override + public int getByteCount() { + if (type.hasByteCount()) { + return type.getByteCount(); + } + return size; + } + + @Override + public INativeType getNativeType() { + return type; + } + + public Object getValue() { + return stringValue(); + } + + public void setValue(Object value) { + setValue((String) value); + } + + public void setValue(String value) { + this.size = value.length() + 1; + handle.setString(0, value); + } + + /** + * The java object corresponding to this. + * + * @return The java object corresponding to this. + */ + public String stringValue() { + return handle.getString(0); + } +} diff --git a/src/de/intarsys/nativec/type/NativeStringType.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeStringType.java similarity index 71% rename from src/de/intarsys/nativec/type/NativeStringType.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeStringType.java index 8af13ce..743e25a 100644 --- a/src/de/intarsys/nativec/type/NativeStringType.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeStringType.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,40 +29,36 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; - -/** - * The meta class implementation - */ public class NativeStringType extends NativeAbstractStringType { - public static NativeStringType create(int size) { - return new NativeStringType(size); - } - - protected NativeStringType() { - super(1); - } + protected NativeStringType() { + super(1); + } - protected NativeStringType(int size) { - super(size, 1); - } + protected NativeStringType(int size) { + super(size, 1); + } - public NativeObject createNative() { - return new NativeString(this); - } + public static NativeStringType create(int size) { + return new NativeStringType(size); + } - @Override - public NativeObject createNative(INativeHandle handle) { - return new NativeString(this, handle); - } + public NativeObject createNative() { + return new NativeString(this); + } - @Override - public NativeObject createNative(Object value) { - return new NativeString(this, (String) value); - } + @Override + public NativeObject createNative(INativeHandle handle) { + return new NativeString(this, handle); + } -} \ No newline at end of file + @Override + public NativeObject createNative(Object value) { + return new NativeString(this, (String) value); + } +} diff --git a/nativec-api/src/main/java/de/intarsys/nativec/type/NativeStruct.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeStruct.java new file mode 100644 index 0000000..c340d65 --- /dev/null +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeStruct.java @@ -0,0 +1,130 @@ +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.type; + +import java.util.Iterator; + +import de.intarsys.nativec.api.INativeHandle; +public abstract class NativeStruct extends NativeObject { + + /** + * The meta class instance + */ + public static final NativeStructType META = new NativeStructType(); + + static { + NativeType.register(NativeGenericStruct.class, META); + } + + protected INativeObject[] values; + + public NativeStruct() { + super(); + } + + public NativeStruct(INativeHandle handle) { + super(handle); + } + + @Override + public int getByteCount() { + return getNativeType().getByteCount(); + } + + /** + * The NativeObject at the named slot name. + *

+ * The marshalling is delegated to the StructMember in the + * StructDeclaration. + * + * @param name The name of the slot in the structure. + * + * @return The NativeObject at the named slot name. + */ + public INativeObject getNativeObject(String name) { + return getStructType().getNativeObject(this, name); + } + + protected StructMember getStructField(String name) { + return getStructType().getField(name); + } + + public NativeStructType getStructType() { + return (NativeStructType) getNativeType(); + } + + public Object getValue() { + throw new UnsupportedOperationException( + "getValue not implemented for NativeStruct"); + } + + public void setValue(Object value) { + throw new UnsupportedOperationException( + "getValue not implemented for NativeStruct"); + } + + /* + * (non-Javadoc) + * + * @see de.intarsys.tools.nativec.NativeObject#toNestedString() + */ + @Override + public String toNestedString() { + return "Struct " + getClass().getName(); + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(getClass().getName()); + sb.append("\n"); + for (Iterator it = getStructType().getFields().iterator(); it.hasNext(); ) { + StructMember field = (StructMember) it.next(); + sb.append(field.getName()); + sb.append("="); + try { + sb.append(getNativeObject(field.getName()).toString()); + } catch (RuntimeException e) { + sb.append("**error**"); + } + sb.append("\n"); + } + return sb.toString(); + } +} diff --git a/nativec-api/src/main/java/de/intarsys/nativec/type/NativeStructType.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeStructType.java new file mode 100644 index 0000000..23846d4 --- /dev/null +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeStructType.java @@ -0,0 +1,171 @@ +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.type; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import de.intarsys.nativec.api.NativeInterface; +public class NativeStructType extends NativeType { + + /** + * The named members of the structure definition + */ + private final Map fields = new HashMap(); + /** + * The boundary where the struct as a member of another struct would want to + * be aligned. + */ + private int byteBoundary = 1; + /** + * The offset where theoretically a new member could be placed. Where it + * will actually be placed depends on the member itself. + */ + private int byteOffset = 0; + /** + * The size in bytes of the member collection + */ + private int byteSize = 0; + /** + * The number of members in the structure + */ + private int fieldsSize = 0; + + /** + * Where members should be aligned in native memory + */ + // expecting pointer size to be a reasonable default - have to look that up + private int packing = NativeInterface.get().pointerSize(); + + protected NativeStructType() { + super(); + } + + protected NativeStructType(Class instanceClass) { + super(instanceClass); + } + + /** + * Declare a new member for the struct. + * + * @param name The name of the new member slot. + * @param declaration The type declaration for the slot + */ + public StructMember declare(String name, INativeType declaration) { + int size; + int boundary; + + size = declaration.getByteCount(); + boundary = Math.min(declaration.getPreferredBoundary(), packing); + byteBoundary = Math.max(byteBoundary, boundary); + + byteOffset = byteOffset - ((byteOffset + boundary - 1) % boundary + 1) + + boundary; + StructMember field = new StructMember(this, name, fieldsSize++, + declaration, byteOffset); + fields.put(name, field); + byteOffset = byteOffset + size; + byteSize = byteOffset; + return field; + } + + public int getByteBoundary() { + return byteBoundary; + } + + @Override + public int getByteCount() { + return getByteSize(); + } + + /** + * The total size of the StructDeclaration. + * + * @return The total size of the StructDeclaration. + */ + public int getByteSize() { + return byteSize; + } + + public StructMember getField(String name) { + return fields.get(name); + } + + /** + * The collection of StructMember instances in declaration order. + * + * @return The collection of StructMember instances in declaration order. + */ + public List getFields() { + return new ArrayList(fields.values()); + } + + public int getFieldsSize() { + return fieldsSize; + } + + public INativeObject getNativeObject(NativeStruct struct, String name) { + StructMember member = fields.get(name); + return member.getNativeObject(struct); + } + + public int getPacking() { + return packing; + } + + public void setPacking(int pPacking) { + if (!fields.isEmpty()) { + throw new IllegalStateException( + "packing must be set before members are declared"); + } + packing = pPacking; + } + + @Override + public int getPreferredBoundary() { + return getByteBoundary(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + for (StructMember field : getFields()) { + sb.append(field.toString()); + sb.append("\n"); + } + return sb.toString(); + } +} diff --git a/nativec-api/src/main/java/de/intarsys/nativec/type/NativeType.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeType.java new file mode 100644 index 0000000..8d6ce44 --- /dev/null +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeType.java @@ -0,0 +1,108 @@ +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.type; + +import java.util.HashMap; +import java.util.Map; + +import de.intarsys.nativec.api.INativeHandle; +public abstract class NativeType implements INativeType { + + /** + * All known meta classes. + */ + private static Map, INativeType> META_CLASSES = new HashMap, INativeType>(); + protected NativeType() { + // + } + + protected NativeType(Class instanceClass) { + register(instanceClass, this); + } + + public static synchronized INativeType lookup(Class clazz) { + + INativeType result = META_CLASSES.get(clazz); + if (result == null) { + ClassLoader classLoader; + + clazz.getClasses(); + classLoader = Thread.currentThread().getContextClassLoader(); + if (classLoader == null) { + classLoader = NativeType.class.getClassLoader(); + } + + try { + Class.forName(clazz.getName(), true, classLoader); + } catch (ClassNotFoundException ex) { + // ignore + } + result = META_CLASSES.get(clazz); + } + return result; + } + public static synchronized void register(Class clazz, INativeType type) { + META_CLASSES.put(clazz, type); + } + + /** + * Create a Declaration that represents an array of this. + * + * @return Create a Declaration that represents an array of this. + */ + public INativeType Array(int size) { + return new NativeArrayType(this, size); + } + + public INativeObject createNative(INativeHandle handle) { + throw new IllegalStateException("meta constructor missing"); + } + + public INativeObject createNative(Object value) { + throw new IllegalStateException("meta constructor missing"); + } + + public int getByteCount() { + return 0; + } + + /** + * Create a Declaration that represents a reference to this. + * + * @return Create a Declaration that represents a reference to this. + */ + public INativeType Ref() { + return new NativeReferenceType(this); + } +} diff --git a/src/de/intarsys/nativec/type/NativeVoid.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeVoid.java similarity index 64% rename from src/de/intarsys/nativec/type/NativeVoid.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeVoid.java index de6e044..ed17098 100644 --- a/src/de/intarsys/nativec/type/NativeVoid.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeVoid.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,51 +29,48 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.NativeInterface; import de.intarsys.nativec.api.NativeTools; - -/** - * An object representing "void" ("nothing" or rather nothing we can specify - * more explicitly). Mostly (only?) useful with references. - */ public class NativeVoid extends NativeSimple { - /** The meta class instance */ - public static final NativeVoidType META = new NativeVoidType(); - - public static final NativeVoid NULL = (NativeVoid) META - .createNative(NativeInterface.NULL); + /** + * The meta class instance + */ + public static final NativeVoidType META = new NativeVoidType(); - static { - NativeType.register(NativeVoid.class, META); - } + public static final NativeVoid NULL = (NativeVoid) META + .createNative(NativeInterface.NULL); - static public NativeVoid createFromAddress(long address) { - return (NativeVoid) NativeVoid.META.createNative(NativeTools - .toHandle(address)); - } + static { + NativeType.register(NativeVoid.class, META); + } - protected NativeVoid(INativeHandle handle) { - super(handle); - } + protected NativeVoid(INativeHandle handle) { + super(handle); + } - @Override - public INativeType getNativeType() { - return META; - } + static public NativeVoid createFromAddress(long address) { + return (NativeVoid) NativeVoid.META.createNative(NativeTools + .toHandle(address)); + } - @Override - public Object getValue() { - throw new UnsupportedOperationException(); - } + @Override + public INativeType getNativeType() { + return META; + } - @Override - public void setValue(Object value) { - throw new UnsupportedOperationException(); - } + @Override + public Object getValue() { + throw new UnsupportedOperationException(); + } + @Override + public void setValue(Object value) { + throw new UnsupportedOperationException(); + } } diff --git a/src/de/intarsys/nativec/type/NativeVoidType.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeVoidType.java similarity index 77% rename from src/de/intarsys/nativec/type/NativeVoidType.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeVoidType.java index 3980c68..fc9e2fb 100644 --- a/src/de/intarsys/nativec/type/NativeVoidType.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeVoidType.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,33 +29,29 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; - -/** - * The meta class implementation - */ public class NativeVoidType extends NativeSimpleType { - protected NativeVoidType() { - super(); - } - - @Override - public INativeObject createNative(INativeHandle handle) { - return new NativeVoid(handle); - } + protected NativeVoidType() { + super(); + } - @Override - public INativeObject createNative(Object value) { - throw new UnsupportedOperationException(); - } + @Override + public INativeObject createNative(INativeHandle handle) { + return new NativeVoid(handle); + } - @Override - public int getByteCount() { - return 0; - } + @Override + public INativeObject createNative(Object value) { + throw new UnsupportedOperationException(); + } -} \ No newline at end of file + @Override + public int getByteCount() { + return 0; + } +} diff --git a/nativec-api/src/main/java/de/intarsys/nativec/type/NativeWideString.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeWideString.java new file mode 100644 index 0000000..c581e20 --- /dev/null +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeWideString.java @@ -0,0 +1,118 @@ +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.type; + +import de.intarsys.nativec.api.INativeHandle; +import de.intarsys.nativec.api.NativeTools; +public class NativeWideString extends NativeObject { + + /** + * The meta class instance + */ + public static final NativeWideStringType META = new NativeWideStringType(); + + static { + NativeType.register(NativeWideString.class, META); + } + + private int size = 0; + private NativeWideStringType type; + protected NativeWideString(NativeWideStringType pType) { + type = pType; + allocate(); + } + + protected NativeWideString(NativeWideStringType pType, INativeHandle handle) { + super(handle); + type = pType; + if (type.hasByteCount()) { + handle.setSize(type.getByteCount()); + } + } + + protected NativeWideString(NativeWideStringType pType, String value) { + type = pType; + if (type.getStringSize() == 0) { + size = value.length() + 1; + size = size << 1; + } + allocate(); + setValue(value); + } + + public NativeWideString(String value) { + this(META, value); + } + + static public NativeWideString createFromAddress(long address) { + return (NativeWideString) NativeWideString.META + .createNative(NativeTools.toHandle(address)); + } + + @Override + public int getByteCount() { + if (type.hasByteCount()) { + return type.getByteCount(); + } + return size; + } + + @Override + public INativeType getNativeType() { + return type; + } + + public Object getValue() { + return stringValue(); + } + + public void setValue(Object value) { + setValue((String) value); + } + + public void setValue(String value) { + size = value.length() + 1; + size = size << 1; + handle.setWideString(0, value); + } + + /** + * The java object corresponding to this. + * + * @return The java object corresponding to this. + */ + public String stringValue() { + return handle.getWideString(0); + } +} diff --git a/src/de/intarsys/nativec/type/NativeWideStringType.java b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeWideStringType.java similarity index 67% rename from src/de/intarsys/nativec/type/NativeWideStringType.java rename to nativec-api/src/main/java/de/intarsys/nativec/type/NativeWideStringType.java index 49b5df6..a8f6a2e 100644 --- a/src/de/intarsys/nativec/type/NativeWideStringType.java +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/NativeWideStringType.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,48 +29,44 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.CWideString; import de.intarsys.nativec.api.INativeHandle; - -/** - * The meta class implementation - */ public class NativeWideStringType extends NativeAbstractStringType { - public static CWideString convert(String string) { - if (string == null) { - return null; - } - return new CWideString(string); - } - - public static NativeWideStringType create(int size) { - return new NativeWideStringType(size); - } + protected NativeWideStringType() { + super(2); + } - protected NativeWideStringType() { - super(2); - } + protected NativeWideStringType(int size) { + super(size, 2); + } - protected NativeWideStringType(int size) { - super(size, 2); - } + public static CWideString convert(String string) { + if (string == null) { + return null; + } + return new CWideString(string); + } - public NativeObject createNative() { - return new NativeWideString(this); - } + public static NativeWideStringType create(int size) { + return new NativeWideStringType(size); + } - @Override - public NativeObject createNative(INativeHandle handle) { - return new NativeWideString(this, handle); - } + public NativeObject createNative() { + return new NativeWideString(this); + } - @Override - public NativeObject createNative(Object value) { - return new NativeWideString(this, (String) value); - } + @Override + public NativeObject createNative(INativeHandle handle) { + return new NativeWideString(this, handle); + } -} \ No newline at end of file + @Override + public NativeObject createNative(Object value) { + return new NativeWideString(this, (String) value); + } +} diff --git a/nativec-api/src/main/java/de/intarsys/nativec/type/StructMember.java b/nativec-api/src/main/java/de/intarsys/nativec/type/StructMember.java new file mode 100644 index 0000000..6bd01cb --- /dev/null +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/StructMember.java @@ -0,0 +1,339 @@ +/*- + * #%L + * Nazgul Project: nativec-api + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.type; + +import de.intarsys.nativec.api.INativeHandle; +public class StructMember { + + /** + * The members name + */ + protected final String name; + /** + * the index of the member within the struct + */ + protected final int index; + /** + * the members declaration + */ + private final INativeType memberType; + /** + * The offset of the declaration within its structure + */ + private final int offset; + + /** + * the definition context of the member + */ + private final NativeStructType structType; + + /** + * Create a slot with name "name" and the declaration "memberDeclaration" + */ + protected StructMember(NativeStructType structType, String name, int index, + INativeType memberType, int offset) { + super(); + this.structType = structType; + this.name = name; + this.index = index; + this.memberType = memberType; + this.offset = offset; + } + + /** + * Performance shortcut to access "byte" in the struct member. + * + * @param struct The container struct instance + * @param index The memory offset from the struct member base + * + * @return The byte at index within the memory range of struct + */ + public byte getByte(NativeStruct struct, int index) { + return struct.handle.getByte(offset + index); + } + + /** + * Performance shortcut to access "byte[]" in the struct member. + * + * @param struct The container struct instance + * @param index The memory offset from the struct member base + * + * @return The byte array starting at index of length count within the + * memory range of struct + */ + public byte[] getByteArray(NativeStruct struct, int index, int count) { + return struct.handle.getByteArray(offset + index, count); + } + + /** + * Performance shortcut to access "platform sized long" in the struct + * member. + * + * @param struct The container struct instance + * @param index The memory offset from the struct member base + * + * @return The platform sized long at index within the memory range of + * struct + */ + public long getCLong(NativeStruct struct, int index) { + return struct.handle.getCLong(offset + index); + } + + /** + * Performance shortcut to access "int" in the struct member. + * + * @param struct The container struct instance + * @param index The memory offset from the struct member base + * + * @return The int at index within the memory range of struct + */ + public int getInt(NativeStruct struct, int index) { + return struct.handle.getInt(offset + index); + } + + /** + * Performance shortcut to access "long" in the struct member. + * + * @param struct The container struct instance + * @param index The memory offset from the struct member base + * + * @return The long at index within the memory range of struct + */ + public long getLong(NativeStruct struct, int index) { + return struct.handle.getLong(offset + index); + } + + /** + * The type declaration of the slot. + * + * @return The type declaration of the slot. + */ + protected INativeType getMemberType() { + return memberType; + } + + /** + * The slots name. + * + * @return The slots name. + */ + public String getName() { + return name; + } + + /** + * Performance shortcut to access "INativeHandle" in the struct member. + * + * @param struct The container struct instance + * @param index The memory offset from the struct member base + * + * @return The INativeHandle at index within the memory range of struct + */ + public INativeHandle getNativeHandle(NativeStruct struct, int index) { + return struct.handle.getNativeHandle(offset + index); + } + + synchronized public INativeObject getNativeObject(NativeStruct struct) { + if (struct.values == null) { + struct.values = new INativeObject[structType.getFieldsSize()]; + } + INativeObject result = struct.values[index]; + if (result == null) { + result = memberType.createNative(struct.getNativeHandle().offset( + offset)); + struct.values[this.index] = result; + } + return result; + } + + /** + * The offset of the slot relative to the StructDeclaration. + * + * @return The offset of the slot relative to the StructDeclaration. + */ + protected int getOffset() { + return offset; + } + + /** + * Performance shortcut to access "short" in the struct member. + * + * @param struct The container struct instance + * @param index The memory offset from the struct member base + * + * @return The short at index within the memory range of struct + */ + public short getShort(NativeStruct struct, int index) { + return struct.handle.getShort(offset + index); + } + + /** + * Performance shortcut to access "String" in the struct member. + * + * @param struct The container struct instance + * @param index The memory offset from the struct member base + * + * @return The String at index within the memory range of struct + */ + public String getString(NativeStruct struct, int index) { + return struct.handle.getString(offset + index); + } + + public Object getValue(NativeStruct struct) { + return getNativeObject(struct).getValue(); + } + + /** + * Performance shortcut to access "String" (from wide characters) in the + * struct member. + * + * @param struct The container struct instance + * @param index The memory offset from the struct member base + * + * @return The wide character String at index within the memory range of + * struct + */ + public String getWideString(NativeStruct struct, int index) { + return struct.handle.getWideString(offset + index); + } + + /** + * Performance shortcut to access "byte" in the struct member. + * + * @param struct The container struct instance + * @param index The memory offset from the struct member base + */ + public void setByte(NativeStruct struct, int index, byte value) { + struct.handle.setByte(offset + index, value); + } + + /** + * Performance shortcut to access "byte[]" in the struct member. + * + * @param struct The container struct instance + * @param index The memory offset from the struct member base + */ + public void setByteArray(NativeStruct struct, int index, byte[] value, + int valueOffset, int valueCount) { + struct.handle.setByteArray(offset + index, value, valueOffset, + valueCount); + } + + /** + * Performance shortcut to access "platform sized long" in the struct + * member. + * + * @param struct The container struct instance + * @param index The memory offset from the struct member base + */ + public void setCLong(NativeStruct struct, int index, long value) { + struct.handle.setCLong(offset + index, value); + } + + /** + * Performance shortcut to access "int" in the struct member. + * + * @param struct The container struct instance + * @param index The memory offset from the struct member base + */ + public void setInt(NativeStruct struct, int index, int value) { + struct.handle.setInt(offset + index, value); + } + + /** + * Performance shortcut to access "long" in the struct member. + * + * @param struct The container struct instance + * @param index The memory offset from the struct member base + */ + public void setLong(NativeStruct struct, int index, long value) { + struct.handle.setLong(offset + index, value); + } + + /** + * Performance shortcut to access "INativeHandle" in the struct member. + * + * @param struct The container struct instance + * @param index The memory offset from the struct member base + */ + public void setNativeHandle(NativeStruct struct, int index, + INativeHandle value) { + struct.handle.setNativeHandle(offset + index, value); + } + + /** + * Performance shortcut to access "short" in the struct member. + * + * @param struct The container struct instance + * @param index The memory offset from the struct member base + */ + public void setShort(NativeStruct struct, int index, short value) { + struct.handle.setShort(offset + index, value); + } + + /** + * Performance shortcut to access "String" in the struct member. + * + * @param struct The container struct instance + * @param index The memory offset from the struct member base + */ + public void setString(NativeStruct struct, int index, String value) { + struct.handle.setString(offset + index, value); + } + + public void setValue(NativeStruct struct, Object value) { + getNativeObject(struct).setValue(value); + } + + /** + * Performance shortcut to access "String" (from wide characters) in the + * struct member. + * + * @param struct The container struct instance + * @param index The memory offset from the struct member base + */ + public void setWideString(NativeStruct struct, int index, String value) { + struct.handle.setWideString(offset + index, value); + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "[" + getName() + "]"; + } +} diff --git a/nativec-api/src/main/java/de/intarsys/nativec/type/package.html b/nativec-api/src/main/java/de/intarsys/nativec/type/package.html new file mode 100644 index 0000000..c69999c --- /dev/null +++ b/nativec-api/src/main/java/de/intarsys/nativec/type/package.html @@ -0,0 +1,38 @@ + + + +Here you find the data types and data structures for native c. + + diff --git a/nativec-codestyle/pom.xml b/nativec-codestyle/pom.xml new file mode 100644 index 0000000..42c97cf --- /dev/null +++ b/nativec-codestyle/pom.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + + de.intarsys.opensource.nativec.codestyle + nativec-codestyle + 1.0.0-SNAPSHOT + jar + + + Intarsys Consulting GmbH + https://www.intarsys.de + + + 2013 + + + + Intarsys License + https://www.intarsys.de/licenses/intarsysSourceLicense.txt + repo + A business-friendly OSS license + + + + diff --git a/nativec-codestyle/src/main/resources/codestyle/license/intarsys/header.txt b/nativec-codestyle/src/main/resources/codestyle/license/intarsys/header.txt new file mode 100644 index 0000000..98f3f9c --- /dev/null +++ b/nativec-codestyle/src/main/resources/codestyle/license/intarsys/header.txt @@ -0,0 +1,25 @@ +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +- Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + +- Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +- Neither the name of intarsys nor the names of its contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/nativec-codestyle/src/main/resources/codestyle/license/intarsys/license.txt b/nativec-codestyle/src/main/resources/codestyle/license/intarsys/license.txt new file mode 100644 index 0000000..b985ee6 --- /dev/null +++ b/nativec-codestyle/src/main/resources/codestyle/license/intarsys/license.txt @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2013, intarsys consulting GmbH + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ diff --git a/nativec-codestyle/src/main/resources/codestyle/license/intarsys/licenseDescriptionTemplate.ftl b/nativec-codestyle/src/main/resources/codestyle/license/intarsys/licenseDescriptionTemplate.ftl new file mode 100644 index 0000000..09c32b7 --- /dev/null +++ b/nativec-codestyle/src/main/resources/codestyle/license/intarsys/licenseDescriptionTemplate.ftl @@ -0,0 +1 @@ +Nazgul Project: ${project.name} \ No newline at end of file diff --git a/nativec-codestyle/src/main/resources/codestyle/license/licenses.properties b/nativec-codestyle/src/main/resources/codestyle/license/licenses.properties new file mode 100644 index 0000000..2942b1d --- /dev/null +++ b/nativec-codestyle/src/main/resources/codestyle/license/licenses.properties @@ -0,0 +1 @@ +intarsys=Intarsys OpenSource License, based on Apache License version 2.0 \ No newline at end of file diff --git a/nativec-jna-impl/pom.xml b/nativec-jna-impl/pom.xml new file mode 100644 index 0000000..9110c74 --- /dev/null +++ b/nativec-jna-impl/pom.xml @@ -0,0 +1,83 @@ + + + 4.0.0 + + + se.jguru.codestyle.poms.java + jguru-codestyle-java-parent + 1.2.0 + + + + de.intarsys.opensource.nativec.impl.jna + nativec-jna-impl + 1.0.0-SNAPSHOT + jar + + + intarsys + + + 2013 + + + Intarsys Consulting GmbH + https://www.intarsys.de + + + + + Intarsys License + https://www.intarsys.de/licenses/intarsysSourceLicense.txt + repo + A business-friendly OSS license + + + + + + de.intarsys.opensource + native-c-api + 1.0.0-SNAPSHOT + + + + net.java.dev.jna + jna + 5.11.0 + + + + de.intarsys.opensource.nativec.codestyle + nativec-codestyle + 1.0.0-SNAPSHOT + compile + + + + + + + + + + org.codehaus.mojo + license-maven-plugin + 2.0.0 + + + + de.intarsys.opensource.nativec.codestyle + nativec-codestyle + 1.0.0-SNAPSHOT + + + + + + + diff --git a/nativec-jna-impl/src/main/java/de/intarsys/nativec/Opaque.java b/nativec-jna-impl/src/main/java/de/intarsys/nativec/Opaque.java new file mode 100644 index 0000000..3c0f099 --- /dev/null +++ b/nativec-jna-impl/src/main/java/de/intarsys/nativec/Opaque.java @@ -0,0 +1,43 @@ +/*- + * #%L + * Nazgul Project: nativec-jna-impl + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec; + +import de.intarsys.nativec.type.NativeVoid; + +public class Opaque extends PseudoObject { + + public Opaque(NativeVoid nativeObject) { + super(nativeObject); + } +} diff --git a/nativec-jna-impl/src/main/java/de/intarsys/nativec/PseudoObject.java b/nativec-jna-impl/src/main/java/de/intarsys/nativec/PseudoObject.java new file mode 100644 index 0000000..f96c106 --- /dev/null +++ b/nativec-jna-impl/src/main/java/de/intarsys/nativec/PseudoObject.java @@ -0,0 +1,49 @@ +/*- + * #%L + * Nazgul Project: nativec-jna-impl + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec; + +import de.intarsys.nativec.type.INativeObject; + +public class PseudoObject { + + private T nativeObject; + + public PseudoObject(T pNativeObject) { + nativeObject = pNativeObject; + } + + public T getNativeObject() { + return nativeObject; + } +} diff --git a/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JNATools.java b/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JNATools.java new file mode 100644 index 0000000..54fb738 --- /dev/null +++ b/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JNATools.java @@ -0,0 +1,46 @@ +/*- + * #%L + * Nazgul Project: nativec-jna-impl + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.jna; + +import com.sun.jna.Pointer; + +public class JNATools { + + public static long getPeer(Pointer p) { + if (p == null) { + return 0; + } + return Pointer.nativeValue(p); + } +} diff --git a/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JnaNativeCallback.java b/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JnaNativeCallback.java new file mode 100644 index 0000000..261ec63 --- /dev/null +++ b/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JnaNativeCallback.java @@ -0,0 +1,127 @@ +/*- + * #%L + * Nazgul Project: nativec-jna-impl + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.jna; + +import com.sun.jna.CallbackProxy; +import com.sun.jna.NativeLong; +import com.sun.jna.Pointer; +import com.sun.jna.WString; +import de.intarsys.nativec.api.CLong; +import de.intarsys.nativec.api.CWideString; +import de.intarsys.nativec.api.ICallback; +import de.intarsys.nativec.api.INativeCallback; +import de.intarsys.nativec.api.INativeHandle; +import de.intarsys.nativec.type.INativeType; +import de.intarsys.nativec.type.NativeObject; +import de.intarsys.nativec.type.NativeType; + +abstract public class JnaNativeCallback implements INativeCallback, + CallbackProxy { + + private ICallback callback; + + public JnaNativeCallback(ICallback pCallback) { + callback = pCallback; + } + + public static Class translateType(Class type) { + // TODO implement more elegant type translation + if (CWideString.class.isAssignableFrom(type)) { + return WString.class; + } else if (CLong.class.isAssignableFrom(type)) { + return NativeLong.class; + } else if (NativeObject.class.isAssignableFrom(type)) { + return Pointer.class; + } else if (INativeHandle.class.isAssignableFrom(type)) { + return Pointer.class; + } + return type; + } + + public Object callback(Object[] args) { + for (int index = 0; index < args.length; index++) { + Class parameterType; + Object object; + + parameterType = callback.getParameterTypes()[index]; + if (CWideString.class.isAssignableFrom(parameterType)) { + WString wstring = (WString) args[index]; + object = new CWideString(wstring.toString()); + } else if (CLong.class.isAssignableFrom(parameterType)) { + Number nativeNumber = (Number) args[index]; + object = new CLong(nativeNumber.longValue()); + } else if (NativeObject.class.isAssignableFrom(parameterType)) { + Pointer pointer = (Pointer) args[index]; + if (pointer == null) { + object = null; + } else { + INativeHandle handle = new JnaNativeHandle(pointer); + INativeType type = NativeType.lookup(parameterType); + if (type == null) { + throw new IllegalArgumentException("no type for '" + + parameterType + "'"); + } + object = type.createNative(handle); + } + } else if (INativeHandle.class.isAssignableFrom(parameterType)) { + Pointer pointer = (Pointer) args[index]; + if (pointer == null) { + object = null; + } else { + object = new JnaNativeHandle(pointer); + } + } else { + object = args[index]; + } + args[index] = object; + } + // TODO map return value too + return callback.invoke(args); + } + + public Class[] getParameterTypes() { + + Class[] genericParameterTypes = callback.getParameterTypes(); + Class[] nativeParameterTypes = new Class[genericParameterTypes.length]; + + for (int index = 0; index < genericParameterTypes.length; index++) { + nativeParameterTypes[index] = translateType(genericParameterTypes[index]); + } + return nativeParameterTypes; + } + + public Class getReturnType() { + return translateType(callback.getReturnType()); + } +} diff --git a/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JnaNativeCallbackAlt.java b/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JnaNativeCallbackAlt.java new file mode 100644 index 0000000..d638e0d --- /dev/null +++ b/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JnaNativeCallbackAlt.java @@ -0,0 +1,45 @@ +/*- + * #%L + * Nazgul Project: nativec-jna-impl + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.jna; + +import com.sun.jna.AltCallingConvention; +import de.intarsys.nativec.api.ICallback; + +public class JnaNativeCallbackAlt extends JnaNativeCallback implements + AltCallingConvention { + + public JnaNativeCallbackAlt(ICallback pCallback) { + super(pCallback); + } +} diff --git a/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JnaNativeCallbackStd.java b/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JnaNativeCallbackStd.java new file mode 100644 index 0000000..9fef206 --- /dev/null +++ b/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JnaNativeCallbackStd.java @@ -0,0 +1,43 @@ +/*- + * #%L + * Nazgul Project: nativec-jna-impl + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.jna; + +import de.intarsys.nativec.api.ICallback; + +public class JnaNativeCallbackStd extends JnaNativeCallback { + + public JnaNativeCallbackStd(ICallback pCallback) { + super(pCallback); + } +} diff --git a/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JnaNativeFunction.java b/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JnaNativeFunction.java new file mode 100644 index 0000000..68d0cfe --- /dev/null +++ b/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JnaNativeFunction.java @@ -0,0 +1,117 @@ +/*- + * #%L + * Nazgul Project: nativec-jna-impl + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.jna; + +import com.sun.jna.Function; +import com.sun.jna.NativeLong; +import com.sun.jna.Pointer; +import com.sun.jna.WString; +import de.intarsys.nativec.api.CLong; +import de.intarsys.nativec.api.CWideString; +import de.intarsys.nativec.api.INativeFunction; +import de.intarsys.nativec.api.INativeHandle; +import de.intarsys.nativec.type.INativeObject; +import de.intarsys.nativec.type.INativeType; +import de.intarsys.nativec.type.NativeObject; +import de.intarsys.nativec.type.NativeType; + +public class JnaNativeFunction implements INativeFunction { + + private Function function; + + public JnaNativeFunction(Function function) { + this.function = function; + } + + protected Function getFunction() { + return function; + } + + public T invoke(Class returnType, Object... objects) { + for (int i = 0; i < objects.length; i++) { + Object object = objects[i]; + if (object instanceof INativeObject) { + JnaNativeHandle handle = (JnaNativeHandle) ((INativeObject) object) + .getNativeHandle(); + if (handle == null) { + objects[i] = null; + } else { + objects[i] = handle.getPointer(); + } + } else if (object instanceof INativeHandle) { + JnaNativeHandle handle = (JnaNativeHandle) object; + objects[i] = handle.getPointer(); + } else if (object instanceof CWideString) { + objects[i] = new WString(((CWideString) object).getString()); + } else if (object instanceof CLong) { + objects[i] = new NativeLong(((CLong) object).longValue()); + } + } + // if (Log.isLoggable(Level.FINE)) { + // Log.log(Level.FINE, "jna invoke " + function.getName()); + // } + T result; + if (CWideString.class.isAssignableFrom(returnType)) { + WString wstring = (WString) function.invoke(WString.class, objects); + result = (T) new CWideString(wstring.toString()); + } else if (CLong.class.isAssignableFrom(returnType)) { + NativeLong nativeLong = (NativeLong) function.invoke( + NativeLong.class, objects); + result = (T) new CLong(nativeLong.longValue()); + } else if (NativeObject.class.isAssignableFrom(returnType)) { + Pointer pointer = (Pointer) function.invoke(Pointer.class, objects); + if (pointer == null) { + result = null; + } else { + INativeHandle handle = new JnaNativeHandle(pointer); + INativeType type = NativeType.lookup(returnType); + if (type == null) { + throw new IllegalArgumentException("no type for '" + + returnType + "'"); + } + result = (T) type.createNative(handle); + } + } else if (INativeHandle.class.isAssignableFrom(returnType)) { + Pointer pointer = (Pointer) function.invoke(Pointer.class, objects); + if (pointer == null) { + result = null; + } else { + result = (T) new JnaNativeHandle(pointer); + } + } else { + result = (T) function.invoke(returnType, objects); + } + return result; + } +} diff --git a/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JnaNativeHandle.java b/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JnaNativeHandle.java new file mode 100644 index 0000000..c339565 --- /dev/null +++ b/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JnaNativeHandle.java @@ -0,0 +1,214 @@ +/*- + * #%L + * Nazgul Project: nativec-jna-impl + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.jna; + +import com.sun.jna.Native; +import com.sun.jna.Pointer; +import de.intarsys.nativec.api.INativeHandle; + +public class JnaNativeHandle implements INativeHandle { + + final private Pointer pointer; + + private int size; + + public JnaNativeHandle(JnaNativeHandle handle, int offset) { + this.pointer = new Pointer(handle.getAddress() + offset); + this.size = handle.size - offset; + } + + public JnaNativeHandle(long address) { + this.pointer = new Pointer(address); + } + + public JnaNativeHandle(Pointer pointer) { + this.pointer = pointer; + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof INativeHandle)) { + return false; + } + INativeHandle otherHandle = (INativeHandle) obj; + return JNATools.getPeer(pointer) == otherHandle.getAddress() + && size == otherHandle.getSize(); + } + + @Override + public long getAddress() { + return JNATools.getPeer(pointer); + } + + @Override + public byte getByte(int index) { + return pointer.getByte(index); + } + + @Override + public byte[] getByteArray(int index, int count) { + return pointer.getByteArray(index, count); + } + + @Override + public long getCLong(int index) { + if (Native.LONG_SIZE == 4) { + return pointer.getInt(index); + } + return pointer.getLong(index); + } + + @Override + public int getInt(int index) { + return pointer.getInt(index); + } + + @Override + public long getLong(int index) { + return pointer.getLong(index); + } + + @Override + public INativeHandle getNativeHandle(int index) { + Pointer tempPointer = pointer.getPointer(index); + if (tempPointer == null) { + // TODO can we reuse the existing "NULL" object? + return new JnaNativeHandle(0); + } + return new JnaNativeHandle(tempPointer); + } + + public Pointer getPointer() { + return pointer; + } + + @Override + public short getShort(int index) { + return pointer.getShort(index); + } + + @Override + public int getSize() { + return size; + } + + @Override + public void setSize(int pSize) { + this.size = pSize; + } + + @Override + public String getString(int index) { + // if (buffer == null) { + // return null; + // } + // // todo encoding + // buffer.position(offset + index); + // int i = 0; + // while (buffer.get() != 0) { + // i++; + // } + // buffer.position(offset + index); + // byte[] valueBytes = new byte[i]; + // buffer.get(valueBytes); + // return new String(valueBytes); + return getPointer().getString(index); + } + + @Override + public String getWideString(int index) { + return getPointer().getWideString(index); + } + + @Override + public int hashCode() { + return pointer.hashCode(); + } + + @Override + public INativeHandle offset(int offset) { + return new JnaNativeHandle(this, offset); + } + + @Override + public void setByte(int index, byte value) { + pointer.setByte(index, value); + } + + @Override + public void setByteArray(int index, byte[] value, int valueOffset, + int valueCount) { + pointer.write(index, value, valueOffset, valueCount); + } + + @Override + public void setCLong(int index, long value) { + if (Native.LONG_SIZE == 4) { + pointer.setInt(index, (int) value); + return; + } + pointer.setLong(index, value); + } + + @Override + public void setInt(int index, int value) { + pointer.setInt(index, value); + } + + @Override + public void setLong(int index, long value) { + pointer.setLong(index, value); + } + + @Override + public void setNativeHandle(int index, INativeHandle handle) { + pointer.setPointer(index, new Pointer(handle.getAddress())); + } + + @Override + public void setShort(int index, short value) { + pointer.setShort(index, value); + } + + @Override + public void setString(int index, String value) { + // // todo encoding + getPointer().setString(index, value); + } + + @Override + public void setWideString(int index, String value) { + getPointer().setWideString(index, value); + } +} diff --git a/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JnaNativeInterface.java b/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JnaNativeInterface.java new file mode 100644 index 0000000..3920af5 --- /dev/null +++ b/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JnaNativeInterface.java @@ -0,0 +1,203 @@ +/*- + * #%L + * Nazgul Project: nativec-jna-impl + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of intarsys nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ +package de.intarsys.nativec.jna; + +import java.lang.reflect.Constructor; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.sun.jna.Function; +import com.sun.jna.Library; +import com.sun.jna.Memory; +import com.sun.jna.Native; +import com.sun.jna.Pointer; +import de.intarsys.nativec.api.ICallback; +import de.intarsys.nativec.api.INativeCallback; +import de.intarsys.nativec.api.INativeFunction; +import de.intarsys.nativec.api.INativeHandle; +import de.intarsys.nativec.api.INativeInterface; +import de.intarsys.nativec.api.INativeLibrary; + +/** + * An {@link INativeInterface} implemented using JNA, a LGPL licensed Java + * native interface abstraction. + *

+ * In our point of view, JNA has the power of deploying all what we wanted to + * have, but is ill designed in some key hot spots - so we worked around and + * built on top of our own interfaces. + */ +public class JnaNativeInterface implements INativeInterface { + + private static Constructor pointerFactory; + + static { + /* + * Ugly hack - multithreaded loading caused a deadlock here as + * com.sun.jna.Native requests a native library which later requires a + * lock on the class loader, while another piece of code + * (Security.addProvider) first locks the class loader and later on + * requests the runtime to load a native library.

We try here to + * provide the same ordering (which is based on a lot of assumptions...) + */ + ClassLoader loader = JnaNativeInterface.class.getClassLoader(); + synchronized (loader) { + try { + Class.forName("com.sun.jna.Native", true, loader); + } catch (ClassNotFoundException e) { + throw new InternalError("can not load JNA"); + } + } + + /* + * Another hack. We *can* use a stripped down version of the Memory + * class if available. If not simply fallback... + */ + try { + Class clazz = Class.forName("com.sun.jna.FastMemory", true, loader); + pointerFactory = clazz.getConstructor(Integer.TYPE); + } catch (Exception e) { + try { + pointerFactory = Memory.class.getConstructor(Long.TYPE); + } catch (Exception e1) { + throw new InternalError("can not load JNA"); + } + } + } + + private List searchPaths = new ArrayList(); + + @Override + public void addSearchPath(String path) { + if (searchPaths.contains(path)) { + return; + } + searchPaths.add(path); + } + + @Override + public INativeHandle allocate(int size) { + return new JnaNativeHandle(createMemory(size)); + } + + @Override + public INativeCallback createCallback(ICallback callback) { + if (callback == null) { + return null; + } + if (callback.getCallingConvention() == INativeFunction.CallingConventionStdcall) { + return new JnaNativeCallbackAlt(callback); + } + if (callback.getCallingConvention() == INativeFunction.CallingConventionCdecl) { + return new JnaNativeCallbackStd(callback); + } + throw new IllegalArgumentException("illegal calling convention"); + } + + @Override + public INativeFunction createFunction(long address) { + return createFunction(address, INativeFunction.CallingConventionCdecl); + } + + @Override + public INativeFunction createFunction(long address, Object callingConvention) { + int callFlags; + if (callingConvention == INativeFunction.CallingConventionCdecl) { + callFlags = Function.C_CONVENTION; + } else if (callingConvention == INativeFunction.CallingConventionStdcall) { + callFlags = Function.ALT_CONVENTION; + } else { + throw new IllegalArgumentException("illegal calling convention"); + } + Pointer pointer = new Pointer(address); + Function function = Function.getFunction(pointer, callFlags); + return new JnaNativeFunction(function); + } + + @Override + public INativeHandle createHandle(long address) { + return new JnaNativeHandle(address); + } + + @Override + public INativeLibrary createLibrary(String name) { + return createLibrary(name, INativeFunction.CallingConventionCdecl); + } + + @Override + public INativeLibrary createLibrary(String name, Object callingConvention) { + Map options = new HashMap<>(2); + if (callingConvention == INativeFunction.CallingConventionCdecl) { + options.put(Library.OPTION_CALLING_CONVENTION, + Function.C_CONVENTION); + } else if (callingConvention == INativeFunction.CallingConventionStdcall) { + options.put(Library.OPTION_CALLING_CONVENTION, + Function.ALT_CONVENTION); + } else { + throw new IllegalArgumentException("illegal calling convention"); + } + options.put(Library.OPTION_OPEN_FLAGS, -1); + return new JnaNativeLibrary(this, name, options); + } + + protected Pointer createMemory(int size) { + try { + Pointer p = pointerFactory.newInstance(size); + p.clear(size); + return p; + } catch (Exception e) { + throw new InternalError("can not create Pointer"); + } + } + + protected List getSearchPaths() { + return searchPaths; + } + + @Override + public int longSize() { + return Native.LONG_SIZE; + } + + @Override + public int pointerSize() { + return Native.POINTER_SIZE; + } + + @Override + public int wideCharSize() { + return 2; + } +} diff --git a/src/de/intarsys/nativec/jna/JnaNativeLibrary.java b/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JnaNativeLibrary.java similarity index 60% rename from src/de/intarsys/nativec/jna/JnaNativeLibrary.java rename to nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JnaNativeLibrary.java index eb9def7..db37d83 100644 --- a/src/de/intarsys/nativec/jna/JnaNativeLibrary.java +++ b/nativec-jna-impl/src/main/java/de/intarsys/nativec/jna/JnaNativeLibrary.java @@ -1,20 +1,23 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * +/*- + * #%L + * Nazgul Project: nativec-jna-impl + * %% + * Copyright (C) 2013 - 2022 Intarsys Consulting GmbH + * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -26,6 +29,7 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. + * #L% */ package de.intarsys.nativec.jna; @@ -35,48 +39,46 @@ import com.sun.jna.Function; import com.sun.jna.NativeLibrary; import com.sun.jna.Pointer; - import de.intarsys.nativec.api.INativeFunction; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.INativeLibrary; public class JnaNativeLibrary implements INativeLibrary { - final private NativeLibrary library; - - final private JnaNativeInterface nativeInterface; + final private NativeLibrary library; - public JnaNativeLibrary(JnaNativeInterface nativeInterface, String name, - Map options) { - this.nativeInterface = nativeInterface; - // this is a kludge - but i can't ask for the searchpaths already - // registered - for (Iterator it = nativeInterface.getSearchPaths().iterator(); it - .hasNext();) { - String path = it.next(); - NativeLibrary.addSearchPath(name, path); - } - library = NativeLibrary.getInstance(name, options); - } + final private JnaNativeInterface nativeInterface; - @Override - public INativeFunction getFunction(String name) { - Function function = getLibrary().getFunction(name); - return new JnaNativeFunction(function); - } + public JnaNativeLibrary(JnaNativeInterface nativeInterface, String name, + Map options) { + this.nativeInterface = nativeInterface; + // this is a kludge - but i can't ask for the searchpaths already + // registered + for (Iterator it = nativeInterface.getSearchPaths().iterator(); it + .hasNext(); ) { + String path = it.next(); + NativeLibrary.addSearchPath(name, path); + } + library = NativeLibrary.getInstance(name, options); + } - @Override - public INativeHandle getGlobal(String symbolName) { - Pointer pointer = getLibrary().getGlobalVariableAddress(symbolName); - return new JnaNativeHandle(pointer); - } + @Override + public INativeFunction getFunction(String name) { + Function function = getLibrary().getFunction(name); + return new JnaNativeFunction(function); + } - protected NativeLibrary getLibrary() { - return library; - } + @Override + public INativeHandle getGlobal(String symbolName) { + Pointer pointer = getLibrary().getGlobalVariableAddress(symbolName); + return new JnaNativeHandle(pointer); + } - protected JnaNativeInterface getNativeInterface() { - return nativeInterface; - } + protected NativeLibrary getLibrary() { + return library; + } + protected JnaNativeInterface getNativeInterface() { + return nativeInterface; + } } diff --git a/resource/META-INF/services/de.intarsys.nativec.api.INativeInterface b/nativec-jna-impl/src/main/resources/META-INF/services/de.intarsys.nativec.api.INativeInterface similarity index 100% rename from resource/META-INF/services/de.intarsys.nativec.api.INativeInterface rename to nativec-jna-impl/src/main/resources/META-INF/services/de.intarsys.nativec.api.INativeInterface diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..bb67989 --- /dev/null +++ b/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + + + se.jguru.codestyle.poms.java + jguru-codestyle-java-reactor-parent + 1.2.0 + + + + de.intarsys.opensource.nativec.parent + nativec-parent + 1.0.0-SNAPSHOT + pom + + + intarsys + + + + Intarsys Consulting GmbH + https://www.intarsys.de + + + + + Intarsys License + https://www.intarsys.de/licenses/intarsysSourceLicense.txt + repo + A business-friendly OSS license + + + + + nativec-codestyle + nativec-api + nativec-jna-impl + + diff --git a/src/de/intarsys/nativec/Opaque.java b/src/de/intarsys/nativec/Opaque.java deleted file mode 100644 index 22b0265..0000000 --- a/src/de/intarsys/nativec/Opaque.java +++ /dev/null @@ -1,11 +0,0 @@ -package de.intarsys.nativec; - -import de.intarsys.nativec.type.NativeVoid; - -public class Opaque extends PseudoObject { - - public Opaque(NativeVoid nativeObject) { - super(nativeObject); - } - -} diff --git a/src/de/intarsys/nativec/PseudoObject.java b/src/de/intarsys/nativec/PseudoObject.java deleted file mode 100644 index 665391e..0000000 --- a/src/de/intarsys/nativec/PseudoObject.java +++ /dev/null @@ -1,17 +0,0 @@ -package de.intarsys.nativec; - -import de.intarsys.nativec.type.INativeObject; - -public class PseudoObject { - - private T nativeObject; - - public PseudoObject(T pNativeObject) { - nativeObject = pNativeObject; - } - - public T getNativeObject() { - return nativeObject; - } - -} diff --git a/src/de/intarsys/nativec/api/INativeHandle.java b/src/de/intarsys/nativec/api/INativeHandle.java deleted file mode 100644 index 7d7e391..0000000 --- a/src/de/intarsys/nativec/api/INativeHandle.java +++ /dev/null @@ -1,271 +0,0 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * - Neither the name of intarsys nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package de.intarsys.nativec.api; - -/** - * A "handle" to a piece of memory (in c space). - *

- * The handle combines an address and a memory chunk of a specified size. - */ -public interface INativeHandle { - - /** - * The start address of the memory chunk - * - * @return The start address of the memory chunk - */ - public long getAddress(); - - /** - * Marshal the data at byte offset index from the start of the - * memory chunk to a byte. - * - * @param index - * The byte offset from the start of the memory chunk - * @return A byte marshaled from the memory chunk - */ - public byte getByte(int index); - - /** - * Marshal the data at byte offset index from the start of the - * memory chunk to a byte array of length count. - * - * @param index - * The byte offset from the start of the memory chunk - * @param count - * The size of the byte array - * @return A byte array marshaled from the memory chunk - */ - public byte[] getByteArray(int index, int count); - - /** - * Marshal the data at byte offset index from the start of the - * memory chunk to a long. Get only the "platform" number of bytes. - * - * @param index - * The byte offset from the start of the memory chunk - * @return A long marshaled from the memory chunk - */ - public long getCLong(int index); - - /** - * Marshal the data at byte offset index from the start of the - * memory chunk to an int. - * - * @param index - * The byte offset from the start of the memory chunk - * @return An int marshaled from the memory chunk - */ - public int getInt(int index); - - /** - * Marshal the data at byte offset index from the start of the - * memory chunk to a long value (which is always 8 byte). - * - * @param index - * The byte offset from the start of the memory chunk - * @return A long marshaled from the memory chunk - */ - public long getLong(int index); - - /** - * Marshal the data at byte offset index from the start of the - * memory chunk to an {@link INativeHandle}. - * - * @param index - * The byte offset from the start of the memory chunk - * @return An {@link INativeHandle} marshaled from the memory chunk - */ - public INativeHandle getNativeHandle(int index); - - /** - * Marshal the data at byte offset index from the start of the - * memory chunk to a short. - * - * @param index - * The byte offset from the start of the memory chunk - * @return A short marshaled from the memory chunk - */ - public short getShort(int index); - - /** - * The size for the handle in bytes. - *

- * You can not access bytes from outside the range defined by getAdddress + - * size. - * - */ - public int getSize(); - - /** - * Marshal the data at byte offset index from the start of the - * memory chunk to a String. - * - * @param index - * The byte offset from the start of the memory chunk - * @return A String marshaled from the memory chunk - */ - public String getString(int index); - - /** - * Marshal the data at byte offset index from the start of the - * memory chunk to a String using the platform wide character conversion. - * - * @param index - * The byte offset from the start of the memory chunk - * @return A String marshaled from the memory chunk - */ - public String getWideString(int index); - - /** - * Create a new {@link INativeHandle}, offset from this by - * offset bytes. - * - * @param offset - * The byte offset from the start of the memory chunk - * @return A new {@link INativeHandle} pointing to "getAddress() + offset". - */ - public INativeHandle offset(int offset); - - /** - * Write a byte to the memory at byte offset index from the - * start of the memory chunk. - * - * @param index - * The byte offset from the start of the memory chunk - * @param value - * The value to write. - */ - public void setByte(int index, byte value); - - /** - * Write a byte array to the memory at byte offset index from - * the start of the memory chunk. The method will write - * valueCount bytes from value starting at - * valueOffset. - * - * @param index - * The byte offset from the start of the memory chunk - * @param value - * The value to write. - */ - public void setByteArray(int index, byte[] value, int valueOffset, - int valueCount); - - /** - * Write a long to the memory at byte offset index from the - * start of the memory chunk. Write only the "platform" number of bytes. The - * caller is responsible for observing the value range. - * - * @param index - * The byte offset from the start of the memory chunk - * @param value - * The value to write. - */ - public void setCLong(int index, long value); - - /** - * Write an int to the memory at byte offset index from the - * start of the memory chunk. - * - * @param index - * The byte offset from the start of the memory chunk - * @param value - * The value to write. - */ - public void setInt(int index, int value); - - /** - * Write a long to the memory at byte offset index from the - * start of the memory chunk. - * - * @param index - * The byte offset from the start of the memory chunk - * @param value - * The value to write. - */ - public void setLong(int index, long value); - - /** - * Write an {@link INativeHandle} to the memory at byte offset - * index from the start of the memory chunk. - * - * @param index - * The byte offset from the start of the memory chunk - * @param valueHandle - * The value to write. - */ - public void setNativeHandle(int index, INativeHandle valueHandle); - - /** - * Write a short to the memory at byte offset index from the - * start of the memory chunk. - * - * @param index - * The byte offset from the start of the memory chunk - * @param value - * The value to write. - */ - public void setShort(int index, short value); - - /** - * Set the valid size for the handle to count bytes. - *

- * You can not access bytes from outside the range defined by getAdddress + - * size. - * - * @param count - * The size of the memory managed by the {@link INativeHandle} - */ - public void setSize(int count); - - /** - * Write a String to the memory at byte offset indexfrom the - * start of the memory chunk. - * - * @param index - * The byte offset from the start of the memory chunk - * @param value - * The value to write. - */ - public void setString(int index, String value); - - /** - * Write a String to the memory at byte offset indexfrom the - * start of the memory chunk using the platform wide character conversion. - * - * @param index - * The byte offset from the start of the memory chunk - * @param value - * The value to write. - */ - public void setWideString(int index, String value); - -} diff --git a/src/de/intarsys/nativec/api/INativeInterface.java b/src/de/intarsys/nativec/api/INativeInterface.java deleted file mode 100644 index b803b4c..0000000 --- a/src/de/intarsys/nativec/api/INativeInterface.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * - Neither the name of intarsys nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package de.intarsys.nativec.api; - -/** - * The abstraction of a generic interface to c native code. - */ -public interface INativeInterface { - - /** - * Add a directory to the search path. - * - * @param path - * The path to be added; - */ - public void addSearchPath(String path); - - /** - * Allocate c memory and return the respective {@link INativeHandle}. - * - * @param size - * The size in bytes. - * @return The new allocated {@link INativeHandle} - */ - public INativeHandle allocate(int size); - - public INativeCallback createCallback(ICallback callback); - - /** - * Create an {@link INativeFunction} from a function pointer. - *

- * There is no special handling for the 0 address! - * - * @param address - * The function pointer. - * @return The function object. - */ - public INativeFunction createFunction(long address); - - public INativeFunction createFunction(long address, Object callingConvention); - - /** - * Create a void {@link INativeHandle} to a memory address. - *

- * There is no special handling for the 0 address! - * - * @param address - * The memory address. - * @return The handle to the memory address. - */ - public INativeHandle createHandle(long address); - - /** - * Load a new {@link INativeLibrary}. - * - * @param name - * The name of the library to load. - * @return The new {@link INativeLibrary} - */ - public INativeLibrary createLibrary(String name); - - /** - * Load a new {@link INativeLibrary}. - * - * @param name - * The name of the library to load. - * @param callingConvention - * The calling convention to use as default for functions in this - * library. - * @return The new {@link INativeLibrary} - */ - public INativeLibrary createLibrary(String name, Object callingConvention); - - /** - * The platform long size. - * - * @return The platform long size. - */ - public int longSize(); - - /** - * The platform pointer size. - * - * @return The platform pointer size. - */ - public int pointerSize(); - - /** - * The platform wide char size. - * - * @return The platform wide char size. - */ - public int wideCharSize(); - -} diff --git a/src/de/intarsys/nativec/api/NativeInterface.java b/src/de/intarsys/nativec/api/NativeInterface.java deleted file mode 100644 index 51aab5f..0000000 --- a/src/de/intarsys/nativec/api/NativeInterface.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * - Neither the name of intarsys nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package de.intarsys.nativec.api; - -import java.util.Iterator; -import java.util.ServiceLoader; - -/** - * Access the VM singleton for {@link INativeInterface}. - *

- * To make this work, just do one of the following: - *

    - *
  • set the class name of your implementation with {@link NativeInterface}
  • - *
  • set a {@link INativeInterface} of your choice in {@link NativeInterface}. - *
  • - *
  • set system property "de.intarsys.nativec.api.INativeInterface" to the - * class name of your implementation.
  • - *
  • include a service provider file - * "de.intarsys.nativec.api.INativeInterface" in your deployment with the class - * name of your implementation.
  • - *
- * - */ -public class NativeInterface { - - private static INativeInterface ACTIVE; - - private static String NAME; - - public static INativeHandle NULL; - - public static final String PROP_NATIVEINTERFACE = "de.intarsys.nativec.api.INativeInterface"; //$NON-NLS-1$ - - static protected INativeInterface createNativeInterface() { - String className = getName(); - if (className == null) { - className = System.getProperty(PROP_NATIVEINTERFACE); - } - if (className != null) { - try { - Class clazz = Class.forName(className); - return (INativeInterface) clazz.newInstance(); - } catch (Exception e) { - throw new NoClassDefFoundError(className); - } - } - return findNativeInterface(); - } - - static protected INativeInterface findNativeInterface() { - ServiceLoader loader = ServiceLoader - .load(INativeInterface.class); - Iterator ps = loader.iterator(); - if (ps.hasNext()) { - try { - return ps.next(); - } catch (Throwable e) { - // ignore and try on - } - } - return null; - } - - synchronized static public INativeInterface get() { - if (ACTIVE == null) { - set(createNativeInterface()); - } - return ACTIVE; - } - - synchronized static public String getName() { - return NAME; - } - - synchronized static public void set(INativeInterface nativeInterface) { - if (nativeInterface == null) { - throw new NullPointerException("no native interface available"); - } - ACTIVE = nativeInterface; - NULL = nativeInterface.createHandle(0); - } - - synchronized static public void setName(String name) { - NAME = name; - } - -} diff --git a/src/de/intarsys/nativec/api/NativeTools.java b/src/de/intarsys/nativec/api/NativeTools.java deleted file mode 100644 index 5ceffd5..0000000 --- a/src/de/intarsys/nativec/api/NativeTools.java +++ /dev/null @@ -1,202 +0,0 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * - Neither the name of intarsys nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package de.intarsys.nativec.api; - -import de.intarsys.nativec.type.INativeType; -import de.intarsys.nativec.type.NativeArray; -import de.intarsys.nativec.type.NativeArrayType; -import de.intarsys.nativec.type.NativeBuffer; -import de.intarsys.nativec.type.NativeBufferType; -import de.intarsys.nativec.type.NativeInt; -import de.intarsys.nativec.type.NativeLong; - -/** - * Tool class for for dealing with the native framework. - */ -public class NativeTools { - - public static byte[] fromNativeByteArray(long ptr, int count) { - INativeHandle handle; - INativeType type; - NativeBuffer nativeValue; - - if (ptr == 0) { - return null; - } - handle = NativeInterface.get().createHandle(ptr); - type = NativeBufferType.create(count); - nativeValue = (NativeBuffer) type.createNative(handle); - return nativeValue.getBytes(); - } - - public static long fromNativeCLong(long ptr) { - INativeHandle handle; - NativeLong nativeValue; - - handle = NativeInterface.get().createHandle(ptr); - nativeValue = (NativeLong) NativeLong.META.createNative(handle); - return nativeValue.intValue(); - } - - public static IValueHolder fromNativeCLongHolder(long ptr) { - return new ObjectValueHolder(fromNativeCLong(ptr)); - } - - public static int fromNativeInt(long ptr) { - INativeHandle handle; - NativeInt nativeValue; - - handle = NativeInterface.get().createHandle(ptr); - nativeValue = (NativeInt) NativeInt.META.createNative(handle); - return nativeValue.intValue(); - } - - public static int[] fromNativeIntArray(INativeHandle handle, int count) { - int[] ints; - int offset; - - ints = new int[count]; - offset = NativeInt.META.getByteCount(); - for (int index = 0; index < count; index++) { - ints[index] = handle.getInt(index * offset); - } - return ints; - } - - public static int[] fromNativeIntArray(long ptr, int count) { - INativeHandle handle; - - handle = NativeInterface.get().createHandle(ptr); - return fromNativeIntArray(handle, count); - } - - public static IValueHolder fromNativeIntHolder(long ptr) { - return new ObjectValueHolder(fromNativeInt(ptr)); - } - - public static String fromNativeString(long ptr, int count) { - return null; - } - - public static INativeHandle toHandle(long address) { - return NativeInterface.get().createHandle(address); - } - - public static void toNativeByteArray(long ptr, byte[] value) { - if (value != null) { - INativeHandle handle; - INativeType type; - NativeBuffer buffer; - - handle = NativeInterface.get().createHandle(ptr); - type = NativeBufferType.create(value.length); - buffer = (NativeBuffer) type.createNative(handle); - buffer.setValue(value); - } - } - - public static void toNativeCLong(long ptr, int[] value) { - if (value != null) { - INativeHandle handle; - NativeArrayType type; - NativeArray array; - - handle = NativeInterface.get().createHandle(ptr); - type = NativeArrayType.create(NativeLong.META, value.length); - array = (NativeArray) type.createNative(handle); - for (int index = 0; index < value.length; index++) { - array.setValue(index, value[index]); - } - } - } - - public static void toNativeCLong(long ptr, IValueHolder value) { - toNativeCLong(ptr, value.get().longValue()); - } - - public static void toNativeCLong(long ptr, long value) { - INativeHandle handle; - NativeLong nativeValue; - - handle = NativeInterface.get().createHandle(ptr); - nativeValue = (NativeLong) NativeLong.META.createNative(handle); - nativeValue.setValue(value); - } - - public static void toNativeCLong(long ptr, long[] value) { - if (value != null) { - INativeHandle handle; - NativeArrayType type; - NativeArray array; - - handle = NativeInterface.get().createHandle(ptr); - type = NativeArrayType.create(NativeLong.META, value.length); - array = (NativeArray) type.createNative(handle); - for (int index = 0; index < value.length; index++) { - array.setValue(index, value[index]); - } - } - } - - public static void toNativeInt(long ptr, int value) { - INativeHandle handle; - - handle = NativeInterface.get().createHandle(ptr); - handle.setSize(NativeInt.META.getByteCount()); - handle.setInt(0, value); - } - - public static void toNativeInt(long ptr, int[] value) { - if (value != null) { - INativeHandle handle; - NativeArrayType type; - NativeArray array; - - handle = NativeInterface.get().createHandle(ptr); - type = NativeArrayType.create(NativeInt.META, value.length); - array = (NativeArray) type.createNative(handle); - for (int index = 0; index < value.length; index++) { - array.setValue(index, value[index]); - } - } - } - - public static void toNativeInt(long ptr, IValueHolder value) { - toNativeInt(ptr, value.get()); - } - - public static void toNativePointer(long ptr, INativeHandle value) { - INativeHandle handle; - - handle = NativeInterface.get().createHandle(ptr); - handle.setNativeHandle(0, value); - } - -} diff --git a/src/de/intarsys/nativec/api/package.html b/src/de/intarsys/nativec/api/package.html deleted file mode 100644 index ef7284f..0000000 --- a/src/de/intarsys/nativec/api/package.html +++ /dev/null @@ -1,35 +0,0 @@ - - -This is the main package of the native c component. -

-Here we define the API's and the VM singleton access to native c. -

-The component itself was built, as we didn't (once again) find anything -around that satisfied our needs. We wanted: -

    -
  • Clean, layered design
  • -
  • Unrestricted access to all modeled objects and concepts.
  • -
  • Easy adaption and integration with other views. This means primarily unrestricted -access to pointers. There is no use in a native library if i can't bootstrap -with a simple address (in our point of view).
  • -
  • Clean design for c data structures (primitive, composites and references)
  • -
  • Exchangeable native implementation
  • -
  • Platform independence
  • -
- -So we created an API and a data structure component and mapped it to JNA, which has -great ideas (but in our opinion is to sealed off and has not well done on the -data structure side) and can serve as a good foundation. - -There are some topics we didn't care about yet - this means they may be working or may -not, its simply not tested. JNA brings a lot of basic features... - -
    -
  • Pointer size was not really an issue
  • -
  • We didn't care about byte order
  • -
- -64 bit implementation is under way. - - - \ No newline at end of file diff --git a/src/de/intarsys/nativec/jna/JNATools.java b/src/de/intarsys/nativec/jna/JNATools.java deleted file mode 100644 index 57fa5ff..0000000 --- a/src/de/intarsys/nativec/jna/JNATools.java +++ /dev/null @@ -1,14 +0,0 @@ -package de.intarsys.nativec.jna; - -import com.sun.jna.Pointer; - -public class JNATools { - - public static long getPeer(Pointer p) { - if (p == null) { - return 0; - } - return Pointer.nativeValue(p); - } - -} diff --git a/src/de/intarsys/nativec/jna/JnaNativeCallback.java b/src/de/intarsys/nativec/jna/JnaNativeCallback.java deleted file mode 100644 index 38770ca..0000000 --- a/src/de/intarsys/nativec/jna/JnaNativeCallback.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * - Neither the name of intarsys nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package de.intarsys.nativec.jna; - -import com.sun.jna.CallbackProxy; -import com.sun.jna.NativeLong; -import com.sun.jna.Pointer; -import com.sun.jna.WString; - -import de.intarsys.nativec.api.CLong; -import de.intarsys.nativec.api.CWideString; -import de.intarsys.nativec.api.ICallback; -import de.intarsys.nativec.api.INativeCallback; -import de.intarsys.nativec.api.INativeHandle; -import de.intarsys.nativec.type.INativeType; -import de.intarsys.nativec.type.NativeObject; -import de.intarsys.nativec.type.NativeType; - -abstract public class JnaNativeCallback implements INativeCallback, - CallbackProxy { - - public static Class translateType(Class type) { - // TODO implement more elegant type translation - if (CWideString.class.isAssignableFrom(type)) { - return WString.class; - } else if (CLong.class.isAssignableFrom(type)) { - return NativeLong.class; - } else if (NativeObject.class.isAssignableFrom(type)) { - return Pointer.class; - } else if (INativeHandle.class.isAssignableFrom(type)) { - return Pointer.class; - } - return type; - } - - private ICallback callback; - - public JnaNativeCallback(ICallback pCallback) { - callback = pCallback; - } - - public Object callback(Object[] args) { - for (int index = 0; index < args.length; index++) { - Class parameterType; - Object object; - - parameterType = callback.getParameterTypes()[index]; - if (CWideString.class.isAssignableFrom(parameterType)) { - WString wstring = (WString) args[index]; - object = new CWideString(wstring.toString()); - } else if (CLong.class.isAssignableFrom(parameterType)) { - Number nativeNumber = (Number) args[index]; - object = new CLong(nativeNumber.longValue()); - } else if (NativeObject.class.isAssignableFrom(parameterType)) { - Pointer pointer = (Pointer) args[index]; - if (pointer == null) { - object = null; - } else { - INativeHandle handle = new JnaNativeHandle(pointer); - INativeType type = NativeType.lookup(parameterType); - if (type == null) { - throw new IllegalArgumentException("no type for '" - + parameterType + "'"); - } - object = type.createNative(handle); - } - } else if (INativeHandle.class.isAssignableFrom(parameterType)) { - Pointer pointer = (Pointer) args[index]; - if (pointer == null) { - object = null; - } else { - object = new JnaNativeHandle(pointer); - } - } else { - object = args[index]; - } - args[index] = object; - } - // TODO map return value too - return callback.invoke(args); - } - - public Class[] getParameterTypes() { - Class[] genericParameterTypes; - Class[] nativeParameterTypes; - - genericParameterTypes = callback.getParameterTypes(); - nativeParameterTypes = new Class[genericParameterTypes.length]; - - for (int index = 0; index < genericParameterTypes.length; index++) { - nativeParameterTypes[index] = translateType(genericParameterTypes[index]); - } - return nativeParameterTypes; - } - - public Class getReturnType() { - return translateType(callback.getReturnType()); - } - -} diff --git a/src/de/intarsys/nativec/jna/JnaNativeCallbackAlt.java b/src/de/intarsys/nativec/jna/JnaNativeCallbackAlt.java deleted file mode 100644 index a1a4cda..0000000 --- a/src/de/intarsys/nativec/jna/JnaNativeCallbackAlt.java +++ /dev/null @@ -1,14 +0,0 @@ -package de.intarsys.nativec.jna; - -import com.sun.jna.AltCallingConvention; - -import de.intarsys.nativec.api.ICallback; - -public class JnaNativeCallbackAlt extends JnaNativeCallback implements - AltCallingConvention { - - public JnaNativeCallbackAlt(ICallback pCallback) { - super(pCallback); - } - -} diff --git a/src/de/intarsys/nativec/jna/JnaNativeCallbackStd.java b/src/de/intarsys/nativec/jna/JnaNativeCallbackStd.java deleted file mode 100644 index 34002da..0000000 --- a/src/de/intarsys/nativec/jna/JnaNativeCallbackStd.java +++ /dev/null @@ -1,11 +0,0 @@ -package de.intarsys.nativec.jna; - -import de.intarsys.nativec.api.ICallback; - -public class JnaNativeCallbackStd extends JnaNativeCallback { - - public JnaNativeCallbackStd(ICallback pCallback) { - super(pCallback); - } - -} diff --git a/src/de/intarsys/nativec/jna/JnaNativeFunction.java b/src/de/intarsys/nativec/jna/JnaNativeFunction.java deleted file mode 100644 index db3c867..0000000 --- a/src/de/intarsys/nativec/jna/JnaNativeFunction.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * - Neither the name of intarsys nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package de.intarsys.nativec.jna; - -import com.sun.jna.Function; -import com.sun.jna.NativeLong; -import com.sun.jna.Pointer; -import com.sun.jna.WString; - -import de.intarsys.nativec.api.CLong; -import de.intarsys.nativec.api.INativeFunction; -import de.intarsys.nativec.api.INativeHandle; -import de.intarsys.nativec.api.CWideString; -import de.intarsys.nativec.type.INativeObject; -import de.intarsys.nativec.type.INativeType; -import de.intarsys.nativec.type.NativeObject; -import de.intarsys.nativec.type.NativeType; - -public class JnaNativeFunction implements INativeFunction { - - private Function function; - - public JnaNativeFunction(Function function) { - this.function = function; - } - - protected Function getFunction() { - return function; - } - - public T invoke(Class returnType, Object... objects) { - for (int i = 0; i < objects.length; i++) { - Object object = objects[i]; - if (object instanceof INativeObject) { - JnaNativeHandle handle = (JnaNativeHandle) ((INativeObject) object) - .getNativeHandle(); - if (handle == null) { - objects[i] = null; - } else { - objects[i] = handle.getPointer(); - } - } else if (object instanceof INativeHandle) { - JnaNativeHandle handle = (JnaNativeHandle) object; - objects[i] = handle.getPointer(); - } else if (object instanceof CWideString) { - objects[i] = new WString(((CWideString) object).getString()); - } else if (object instanceof CLong) { - objects[i] = new NativeLong(((CLong) object).longValue()); - } - } - // if (Log.isLoggable(Level.FINE)) { - // Log.log(Level.FINE, "jna invoke " + function.getName()); - // } - T result; - if (CWideString.class.isAssignableFrom(returnType)) { - WString wstring = (WString) function.invoke(WString.class, objects); - result = (T) new CWideString(wstring.toString()); - } else if (CLong.class.isAssignableFrom(returnType)) { - NativeLong nativeLong = (NativeLong) function.invoke( - NativeLong.class, objects); - result = (T) new CLong(nativeLong.longValue()); - } else if (NativeObject.class.isAssignableFrom(returnType)) { - Pointer pointer = (Pointer) function.invoke(Pointer.class, objects); - if (pointer == null) { - result = null; - } else { - INativeHandle handle = new JnaNativeHandle(pointer); - INativeType type = NativeType.lookup(returnType); - if (type == null) { - throw new IllegalArgumentException("no type for '" - + returnType + "'"); - } - result = (T) type.createNative(handle); - } - } else if (INativeHandle.class.isAssignableFrom(returnType)) { - Pointer pointer = (Pointer) function.invoke(Pointer.class, objects); - if (pointer == null) { - result = null; - } else { - result = (T) new JnaNativeHandle(pointer); - } - } else { - result = (T) function.invoke(returnType, objects); - } - return result; - } -} diff --git a/src/de/intarsys/nativec/jna/JnaNativeHandle.java b/src/de/intarsys/nativec/jna/JnaNativeHandle.java deleted file mode 100644 index 5978c3b..0000000 --- a/src/de/intarsys/nativec/jna/JnaNativeHandle.java +++ /dev/null @@ -1,212 +0,0 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * - Neither the name of intarsys nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package de.intarsys.nativec.jna; - -import com.sun.jna.Native; -import com.sun.jna.Pointer; - -import de.intarsys.nativec.api.INativeHandle; - -public class JnaNativeHandle implements INativeHandle { - - final private Pointer pointer; - - private int size; - - public JnaNativeHandle(JnaNativeHandle handle, int offset) { - this.pointer = new Pointer(handle.getAddress() + offset); - this.size = handle.size - offset; - } - - public JnaNativeHandle(long address) { - this.pointer = new Pointer(address); - } - - public JnaNativeHandle(Pointer pointer) { - this.pointer = pointer; - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof INativeHandle)) { - return false; - } - INativeHandle otherHandle = (INativeHandle) obj; - return JNATools.getPeer(pointer) == otherHandle.getAddress() - && size == otherHandle.getSize(); - } - - @Override - public long getAddress() { - return JNATools.getPeer(pointer); - } - - @Override - public byte getByte(int index) { - return pointer.getByte(index); - } - - @Override - public byte[] getByteArray(int index, int count) { - return pointer.getByteArray(index, count); - } - - @Override - public long getCLong(int index) { - if (Native.LONG_SIZE == 4) { - return pointer.getInt(index); - } - return pointer.getLong(index); - } - - @Override - public int getInt(int index) { - return pointer.getInt(index); - } - - @Override - public long getLong(int index) { - return pointer.getLong(index); - } - - @Override - public INativeHandle getNativeHandle(int index) { - Pointer tempPointer = pointer.getPointer(index); - if (tempPointer == null) { - // TODO can we reuse the existing "NULL" object? - return new JnaNativeHandle(0); - } - return new JnaNativeHandle(tempPointer); - } - - public Pointer getPointer() { - return pointer; - } - - @Override - public short getShort(int index) { - return pointer.getShort(index); - } - - @Override - public int getSize() { - return size; - } - - @Override - public String getString(int index) { - // if (buffer == null) { - // return null; - // } - // // todo encoding - // buffer.position(offset + index); - // int i = 0; - // while (buffer.get() != 0) { - // i++; - // } - // buffer.position(offset + index); - // byte[] valueBytes = new byte[i]; - // buffer.get(valueBytes); - // return new String(valueBytes); - return getPointer().getString(index); - } - - @Override - public String getWideString(int index) { - return getPointer().getString(index, true); - } - - @Override - public int hashCode() { - return pointer.hashCode(); - } - - @Override - public INativeHandle offset(int offset) { - return new JnaNativeHandle(this, offset); - } - - @Override - public void setByte(int index, byte value) { - pointer.setByte(index, value); - } - - @Override - public void setByteArray(int index, byte[] value, int valueOffset, - int valueCount) { - pointer.write(index, value, valueOffset, valueCount); - } - - @Override - public void setCLong(int index, long value) { - if (Native.LONG_SIZE == 4) { - pointer.setInt(index, (int) value); - return; - } - pointer.setLong(index, value); - } - - @Override - public void setInt(int index, int value) { - pointer.setInt(index, value); - } - - @Override - public void setLong(int index, long value) { - pointer.setLong(index, value); - } - - @Override - public void setNativeHandle(int index, INativeHandle handle) { - pointer.setPointer(index, new Pointer(handle.getAddress())); - } - - @Override - public void setShort(int index, short value) { - pointer.setShort(index, value); - } - - @Override - public void setSize(int pSize) { - this.size = pSize; - } - - @Override - public void setString(int index, String value) { - // // todo encoding - getPointer().setString(index, value); - } - - @Override - public void setWideString(int index, String value) { - getPointer().setString(index, value, true); - } - -} diff --git a/src/de/intarsys/nativec/jna/JnaNativeInterface.java b/src/de/intarsys/nativec/jna/JnaNativeInterface.java deleted file mode 100644 index 3332b77..0000000 --- a/src/de/intarsys/nativec/jna/JnaNativeInterface.java +++ /dev/null @@ -1,203 +0,0 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * - Neither the name of intarsys nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package de.intarsys.nativec.jna; - -import java.lang.reflect.Constructor; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import com.sun.jna.Function; -import com.sun.jna.Library; -import com.sun.jna.Memory; -import com.sun.jna.Native; -import com.sun.jna.Pointer; -import com.sun.org.apache.xalan.internal.xsltc.compiler.util.InternalError; - -import de.intarsys.nativec.api.ICallback; -import de.intarsys.nativec.api.INativeCallback; -import de.intarsys.nativec.api.INativeFunction; -import de.intarsys.nativec.api.INativeHandle; -import de.intarsys.nativec.api.INativeInterface; -import de.intarsys.nativec.api.INativeLibrary; - -/** - * An {@link INativeInterface} implemented using JNA, a LGPL licensed Java - * native interface abstraction. - *

- * In our point of view, JNA has the power of deploying all what we wanted to - * have, but is ill designed in some key hot spots - so we worked around and - * built on top of our own interfaces. - * - */ -public class JnaNativeInterface implements INativeInterface { - - private static Constructor pointerFactory; - - static { - /* - * Ugly hack - multithreaded loading caused a deadlock here as - * com.sun.jna.Native requests a native library which later requires a - * lock on the class loader, while another piece of code - * (Security.addProvider) first locks the class loader and later on - * requests the runtime to load a native library.

We try here to - * provide the same ordering (which is based on a lot of assumptions...) - */ - ClassLoader loader = JnaNativeInterface.class.getClassLoader(); - synchronized (loader) { - try { - Class.forName("com.sun.jna.Native", true, loader); - } catch (ClassNotFoundException e) { - throw new InternalError("can not load JNA"); - } - } - - /* - * Another hack. We *can* use a stripped down version of the Memory - * class if available. If not simply fallback... - */ - try { - Class clazz = Class.forName("com.sun.jna.FastMemory", true, loader); - pointerFactory = clazz.getConstructor(Integer.TYPE); - } catch (Exception e) { - try { - pointerFactory = Memory.class.getConstructor(Long.TYPE); - } catch (Exception e1) { - throw new InternalError("can not load JNA"); - } - } - } - - private List searchPaths = new ArrayList(); - - @Override - public void addSearchPath(String path) { - if (searchPaths.contains(path)) { - return; - } - searchPaths.add(path); - } - - @Override - public INativeHandle allocate(int size) { - return new JnaNativeHandle(createMemory(size)); - } - - @Override - public INativeCallback createCallback(ICallback callback) { - if (callback == null) { - return null; - } - if (callback.getCallingConvention() == INativeFunction.CallingConventionStdcall) { - return new JnaNativeCallbackAlt(callback); - } - if (callback.getCallingConvention() == INativeFunction.CallingConventionCdecl) { - return new JnaNativeCallbackStd(callback); - } - throw new IllegalArgumentException("illegal calling convention"); - } - - @Override - public INativeFunction createFunction(long address) { - return createFunction(address, INativeFunction.CallingConventionCdecl); - } - - @Override - public INativeFunction createFunction(long address, Object callingConvention) { - int callFlags; - if (callingConvention == INativeFunction.CallingConventionCdecl) { - callFlags = Function.C_CONVENTION; - } else if (callingConvention == INativeFunction.CallingConventionStdcall) { - callFlags = Function.ALT_CONVENTION; - } else { - throw new IllegalArgumentException("illegal calling convention"); - } - Pointer pointer = new Pointer(address); - Function function = Function.getFunction(pointer, callFlags); - return new JnaNativeFunction(function); - } - - @Override - public INativeHandle createHandle(long address) { - return new JnaNativeHandle(address); - } - - @Override - public INativeLibrary createLibrary(String name) { - return createLibrary(name, INativeFunction.CallingConventionCdecl); - } - - @Override - public INativeLibrary createLibrary(String name, Object callingConvention) { - Map options = new HashMap<>(2); - if (callingConvention == INativeFunction.CallingConventionCdecl) { - options.put(Library.OPTION_CALLING_CONVENTION, - Function.C_CONVENTION); - } else if (callingConvention == INativeFunction.CallingConventionStdcall) { - options.put(Library.OPTION_CALLING_CONVENTION, - Function.ALT_CONVENTION); - } else { - throw new IllegalArgumentException("illegal calling convention"); - } - options.put(Library.OPTION_OPEN_FLAGS, -1); - return new JnaNativeLibrary(this, name, options); - } - - protected Pointer createMemory(int size) { - try { - Pointer p = pointerFactory.newInstance(size); - p.clear(size); - return p; - } catch (Exception e) { - throw new InternalError("can not create Pointer"); - } - } - - protected List getSearchPaths() { - return searchPaths; - } - - @Override - public int longSize() { - return Native.LONG_SIZE; - } - - @Override - public int pointerSize() { - return Native.POINTER_SIZE; - } - - @Override - public int wideCharSize() { - return 2; - } - -} diff --git a/src/de/intarsys/nativec/type/INativeType.java b/src/de/intarsys/nativec/type/INativeType.java deleted file mode 100644 index 224be22..0000000 --- a/src/de/intarsys/nativec/type/INativeType.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * - Neither the name of intarsys nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package de.intarsys.nativec.type; - -import de.intarsys.nativec.api.INativeHandle; - -/** - * The type (factory) for {@link INativeObject} instances. - *

- * A type has declaration style method to create more complex types. - * - */ -public interface INativeType { - - /** - * Create an array type from this. - * - * @param size - * The predefined size for the array. - * - * @return The derived type. - */ - public INativeType Array(int size); - - /** - * Create an {@link INativeObject} for this type from the Java object. - * - * @param value - * - * @return The new {@link INativeObject} - */ - public INativeObject createNative(Object value); - - /** - * Create a new {@link INativeObject} from a {@link INativeHandle}. - * - * @param handle - * The handle to memory. - * @return The new {@link INativeObject} - */ - public INativeObject createNative(INativeHandle handle); - - /** - * The boundary where this type as a struct member would want to be aligned. - * A structure can override this value with packing. - * - * @return The preferred alignment boundary. - */ - public int getPreferredBoundary(); - - /** - * The size of the type in c memory. - * - * @return The size of the type in c memory. - */ - public int getByteCount(); - - /** - * Create a reference type to this. - * - * @return The derived type. - */ - public INativeType Ref(); - -} diff --git a/src/de/intarsys/nativec/type/NativeArray.java b/src/de/intarsys/nativec/type/NativeArray.java deleted file mode 100644 index 56779a8..0000000 --- a/src/de/intarsys/nativec/type/NativeArray.java +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * - Neither the name of intarsys nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package de.intarsys.nativec.type; - -import de.intarsys.nativec.api.INativeHandle; - -/** - * An {@link INativeObject} that represents a homogeneous (this means of equal - * length and type) sequence of other {@link INativeObject} instances. - */ -public class NativeArray extends NativeObject { - - /** The meta class instance */ - public static final NativeArrayType META = new NativeArrayType( - NativeVoid.META, 0); - - static { - NativeType.register(NativeArray.class, META); - } - - public static NativeArray create(INativeType baseType, int size) { - NativeArrayType type = new NativeArrayType(baseType, size); - return new NativeArray(type); - } - - private INativeObject[] values; - - private NativeArrayType type; - - protected NativeArray(NativeArrayType type) { - this.type = type; - allocate(); - } - - protected NativeArray(NativeArrayType type, INativeHandle handle) { - super(handle); - this.type = type; - } - - public INativeType getBaseType() { - return type.getBaseType(); - } - - @Override - public int getByteCount() { - return type.getByteCount(); - } - - /** - * The {@link INativeObject} at index in the sequence (the index'th element - * of the array). - * - * @param index - * The index of the element to be reported. - * - * @return The NativeObject at index - */ - synchronized public INativeObject getNativeObject(int index) { - if (values == null) { - values = new INativeObject[getSize()]; - } - INativeObject result = values[index]; - if (result == null) { - int elementOffset = index * type.getBaseSize(); - result = type.getBaseType().createNative( - handle.offset(elementOffset)); - values[index] = result; - } - return result; - } - - /* - * (non-Javadoc) - * - * @see de.intarsys.graphic.freetype.NativeObject#getMetaClass() - */ - @Override - public INativeType getNativeType() { - return type; - } - - /** - * The number of NativeObject instances in the sequence represented by this - * (in other terms the array size). - * - * @return The number of NativeObject instances in the sequence represented - * by this - */ - public int getSize() { - return type.getArraySize(); - } - - public Object getValue() { - throw new UnsupportedOperationException( - "getValue not implemented for NativeArray"); - } - - public Object getValue(int index) { - return getNativeObject(index).getValue(); - } - - public void setBaseType(INativeType baseType) { - type = NativeArrayType.create(baseType, type.getArraySize()); - this.values = null; - handle.setSize(getByteCount()); - } - - public void setSize(int size) { - type = NativeArrayType.create(type.getBaseType(), size); - this.values = null; - handle.setSize(getByteCount()); - } - - public void setValue(int index, Object value) { - getNativeObject(index).setValue(value); - } - - public void setValue(Object value) { - throw new UnsupportedOperationException( - "setValue not implemented for NativeArray"); - } - - /* - * (non-Javadoc) - * - * @see de.intarsys.graphic.freetype.NativeObject#toNestedString() - */ - @Override - public String toNestedString() { - return "[...]"; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("["); - for (int i = 0; i < getSize(); i++) { - // NativeObject element = get(i); - // sb.append(element.toString()); - // sb.append(", "); - } - sb.append("]"); - return sb.toString(); - } - -} diff --git a/src/de/intarsys/nativec/type/NativeBuffer.java b/src/de/intarsys/nativec/type/NativeBuffer.java deleted file mode 100644 index 349d5f0..0000000 --- a/src/de/intarsys/nativec/type/NativeBuffer.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * - Neither the name of intarsys nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package de.intarsys.nativec.type; - -import java.nio.ByteBuffer; - -import de.intarsys.nativec.api.INativeHandle; -import de.intarsys.nativec.api.NativeInterface; - -/** - * A wrapper for a sequence of bytes. - * - */ -public class NativeBuffer extends NativeObject { - - /** The meta class instance */ - public static final NativeBufferType META = new NativeBufferType(); - - static { - NativeType.register(NativeBuffer.class, META); - } - - private NativeBufferType type; - - public NativeBuffer(byte[] bytes) { - type = new NativeBufferType(bytes.length); - handle = NativeInterface.get().allocate(bytes.length); - handle.setByteArray(0, bytes, 0, bytes.length); - } - - protected NativeBuffer(INativeHandle handle) { - super(handle); - } - - public NativeBuffer(int pSize) { - type = new NativeBufferType(pSize); - handle = NativeInterface.get().allocate(pSize); - } - - /* - * (non-Javadoc) - * - * @see de.intarsys.tools.nativec.NativeObject#getByteCount() - */ - @Override - public int getByteCount() { - // todo alignment? - return type.getByteCount(); - } - - /* - * (non-Javadoc) - * - * @see de.intarsys.graphic.freetype.NativeObject#getMetaClass() - */ - @Override - public INativeType getNativeType() { - return META; - } - - /** - * The number of elements in the NativeBuffer - * - * @return The number of elements in the NativeBuffer - */ - public int getSize() { - return type.getByteCount(); - } - - public Object getValue() { - return getBytes(); - } - - public void setSize(int size) { - type = new NativeBufferType(size); - handle.setSize(getByteCount()); - } - - public void setValue(Object value) { - if (value instanceof byte[]) { - byte[] data = (byte[]) value; - setByteArray(0, data, 0, data.length); - } else if (value instanceof ByteBuffer) { - byte[] data = new byte[getSize()]; - ((ByteBuffer) value).get(data); - setByteArray(0, data, 0, data.length); - } - } -} diff --git a/src/de/intarsys/nativec/type/NativeFunction.java b/src/de/intarsys/nativec/type/NativeFunction.java deleted file mode 100644 index 862bbfe..0000000 --- a/src/de/intarsys/nativec/type/NativeFunction.java +++ /dev/null @@ -1,47 +0,0 @@ -package de.intarsys.nativec.type; - -import de.intarsys.nativec.api.INativeFunction; -import de.intarsys.nativec.api.INativeHandle; -import de.intarsys.nativec.api.NativeInterface; - -public class NativeFunction extends NativeVoid { - - public static class NativeFunctionType extends NativeVoidType { - - @Override - public INativeObject createNative(INativeHandle handle) { - return new NativeFunction(handle); - } - - } - - public static final NativeFunctionType META = new NativeFunctionType(); - - private Object callingConvention; - private INativeFunction function; - - protected NativeFunction(INativeHandle handle) { - super(handle); - callingConvention = INativeFunction.CallingConventionCdecl; - } - - public Object getCallingConvention() { - return callingConvention; - } - - public INativeFunction getFunction() { - // TODO do we want to check if handle has changed? - if (function == null) { - function = NativeInterface.get().createFunction( - getNativeHandle().getAddress(), callingConvention); - } - return function; - } - - public void setCallingConvention(Object callingConvention) { - if(function != null) { - throw new IllegalStateException(); - } - this.callingConvention = callingConvention; - } -} diff --git a/src/de/intarsys/nativec/type/NativeLongLP64.java b/src/de/intarsys/nativec/type/NativeLongLP64.java deleted file mode 100644 index 7dd44a2..0000000 --- a/src/de/intarsys/nativec/type/NativeLongLP64.java +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * - Neither the name of intarsys nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package de.intarsys.nativec.type; - -import de.intarsys.nativec.api.INativeHandle; -import de.intarsys.nativec.api.NativeTools; - -/** - * A wrapper for the Windows type LONG_PTR. This type has the same size as a - * pointer but is semantically a simple integer. (same as NativeLong on LP64 - * platforms size-wise) - */ -public class NativeLongLP64 extends NativeNumber { - - /** The meta class instance */ - public static final NativeLongLP64Type META = new NativeLongLP64Type(); - - static { - NativeType.register(NativeLongLP64.class, META); - } - - static public NativeLongLP64 createFromAddress(long address) { - return (NativeLongLP64) NativeLongLP64.META.createNative(NativeTools - .toHandle(address)); - } - - /** - * Create a new wrapper - */ - public NativeLongLP64() { - allocate(); - } - - protected NativeLongLP64(INativeHandle handle) { - super(handle); - } - - /** - * Create a new wrapper - */ - public NativeLongLP64(long value) { - allocate(); - setValue(value); - } - - @Override - public byte byteValue() { - return (byte) intValue(); - } - - @Override - public INativeType getNativeType() { - return META; - } - - @Override - public Object getValue() { - return new Long(longValue()); - } - - @Override - public int intValue() { - return (int) longValue(); - } - - @Override - public long longValue() { - if (NativeObject.SIZE_PTR == 4) { - return handle.getInt(0); - } - return handle.getLong(0); - } - - public void setValue(long value) { - if (NativeObject.SIZE_PTR == 4) { - handle.setInt(0, (int) value); - return; - } - handle.setLong(0, value); - } - - @Override - public void setValue(Object value) { - setValue(((Number) value).longValue()); - } - - @Override - public short shortValue() { - return (short) longValue(); - } - - @Override - public String toString() { - if (getNativeHandle() == null) { - return "nope - no handle"; //$NON-NLS-1$ - } - if (getNativeHandle().getAddress() == 0) { - return "nope - null pointer"; //$NON-NLS-1$ - } - return String.valueOf(longValue()); - } - -} diff --git a/src/de/intarsys/nativec/type/NativeObject.java b/src/de/intarsys/nativec/type/NativeObject.java deleted file mode 100644 index 2e46c0e..0000000 --- a/src/de/intarsys/nativec/type/NativeObject.java +++ /dev/null @@ -1,290 +0,0 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * - Neither the name of intarsys nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package de.intarsys.nativec.type; - -import de.intarsys.nativec.api.INativeHandle; -import de.intarsys.nativec.api.NativeInterface; - -/** - * An instance of an external (C memory) represented object. The C-object is - * represented using the handle (the pointer to the object in C memory) and its - * type {@link INativeType}. - *

- * Supported data types are - *

    - *
  • primitive types (char, byte, int, short, long, String...)
  • - *
  • byte buffer
  • - *
  • array types
  • - *
  • structures
  • - *
  • references
  • - *
- *

- */ -public abstract class NativeObject implements INativeObject { - - public static final int SIZE_BYTE = 1; - - public static final int SIZE_INT = 4; - - public static final int SHIFT_INT = 2; - - public static final int SIZE_LONGLONG = 8; - - public static final int SHIFT_LONGLONG = 3; - - public static final int SIZE_LONG = NativeInterface.get().longSize(); - - public static final int SHIFT_LONG = SIZE_LONG == 4 ? 2 : 3; - - public static final int SIZE_PTR = NativeInterface.get().pointerSize(); - - public static final int SIZE_SHORT = 2; - - /** DEBUG flag */ - public static boolean DEBUG = true; - - /** - * The handle to the memory chunk used by this object. While in fact this is - * final, Java language semantics does not allow to declare so! - *

- * The handle should only be assigned in the constructor, via parameter or - * "allocate". - */ - protected INativeHandle handle; - - /** - * - */ - protected NativeObject() { - // - } - - /** - * Create a new NativeObject in C-Memory at pointer "handle". The bytes - * belonging to this object may already have been copied from C-Memory and - * made available in bytes at location offset. - * - * @param handle - * The pointer in C-memory - */ - protected NativeObject(INativeHandle handle) { - this.handle = handle; - } - - /** - * Manage the objects memory in Java. C memory will be valid at least as - * long as we hold a reference to the buffer. C memory is undefined (but not - * a memory leak) and may be reclaimed at any time after dropping our - * pointer to the buffer. - */ - protected void allocate() { - int size = getByteCount(); - handle = NativeInterface.get().allocate(size); - } - - /** - * This is a special form of the "createNative" signature, implementing a - * "type cast" on the same memory location. - * - * @param declaration - * The new base declaration type. - * @return The {@link INativeObject} at the same memory location as this, - * but of a different type. - */ - public INativeObject cast(INativeType declaration) { - return declaration.createNative(handle); - } - - public INativeObject createReference() { - NativeReference ref = NativeReference.create(getNativeType()); - ref.setValue(this); - return ref; - } - - /** - * The byte at index as a byte. - * - * @param index - * The index of the element to be reported. - * @return The element at index as a native byte. - */ - public byte getByte(int index) { - return handle.getByte(index); - } - - /** - * The element at index as an array of bytes with dimension count. This is a - * lightweight optimization. - * - * @param index - * The index of the element to be reported. - * @return The element at index as an array of native byte with dimension - * count. - */ - public byte[] getByteArray(int index, int count) { - return handle.getByteArray(index, count); - } - - /** - * The number of bytes occupied by this. - * - * @return The number of bytes occupied by this. - */ - public abstract int getByteCount(); - - /** - * The bytes copied from C-memory that represent this. - * - * @return The bytes copied from C-memory that represent this. - */ - public byte[] getBytes() { - return handle.getByteArray(0, getByteCount()); - } - - /** - * The element at index as a native long. Only the "platform" number of - * bytes are read. - * - * @param index - * The index of the element to be reported. - * @return The element at index as a native long. - */ - public long getCLong(int index) { - return handle.getCLong(index); - } - - /** - * The element at index as a native int. - * - * @param index - * The index of the element to be reported. - * @return The element at index as a native int. - */ - public int getInt(int index) { - return handle.getInt(index); - } - - /** - * The C-Pointer where the associated memory is found. - * - * @return The C-Pointer where the associated memory is found. - */ - public INativeHandle getNativeHandle() { - return handle; - } - - public INativeHandle getNativeHandle(int index) { - return handle.getNativeHandle(index); - } - - /** - * The meta information and behavior for the NativeObject. - *

- * There is exactly one meta instance for all NativeObject instances of a - * certain type. - * - * @return The meta information and behavior for the NativeObject. - * - */ - abstract public INativeType getNativeType(); - - /** - * The element at index as a native short. This is a lightweight - * optimization. - * - * @param index - * The index of the element to be reported. - * @return The element at index as a native short. - */ - public short getShort(int index) { - return handle.getShort(index); - } - - public String getString(int index) { - return handle.getString(index); - } - - public String getWideString(int index) { - return handle.getWideString(index); - } - - /** - * Answer true if this is "null". This means the associated - * C-pointer is pointing to 0. - * - * @return Answer true if this is "null". - */ - public boolean isNull() { - return handle == null || handle.getAddress() == 0; - } - - public void setByte(int index, byte value) { - handle.setByte(index, value); - } - - public void setByteArray(int index, byte[] value, int valueOffset, - int valueCount) { - handle.setByteArray(index, value, valueOffset, valueCount); - } - - public void setCLong(int index, long value) { - handle.setCLong(index, value); - } - - public void setInt(int index, int value) { - handle.setInt(index, value); - } - - public void setNativeHandle(int index, INativeHandle value) { - handle.setNativeHandle(index, value); - } - - public void setShort(int index, short value) { - handle.setShort(index, value); - } - - public void setString(int index, String value) { - handle.setString(index, value); - } - - public void setWideString(int index, String value) { - handle.setWideString(index, value); - } - - /** - * A string for debugging purposes. - * - * @return A string for debugging purposes. - */ - public String toNestedString() { - return toString(); - } - -} \ No newline at end of file diff --git a/src/de/intarsys/nativec/type/NativeReference.java b/src/de/intarsys/nativec/type/NativeReference.java deleted file mode 100644 index 7460bc2..0000000 --- a/src/de/intarsys/nativec/type/NativeReference.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * - Neither the name of intarsys nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package de.intarsys.nativec.type; - -import de.intarsys.nativec.api.INativeHandle; -import de.intarsys.nativec.api.NativeInterface; - -/** - * An object representing a reference to another object ("pointer"). - */ -public class NativeReference extends NativeObject { - - /** The meta class instance */ - public static final NativeReferenceType META = new NativeReferenceType( - NativeVoid.META); - - static { - NativeType.register(NativeReference.class, META); - } - - public static NativeReference create( - INativeType baseType) { - NativeReferenceType type = new NativeReferenceType(baseType); - return new NativeReference(type); - } - - private INativeHandle dereferenceHandle; - - private T dereferenced; - - private NativeReferenceType type; - - protected NativeReference(NativeReferenceType type) { - this.type = type; - allocate(); - } - - protected NativeReference(NativeReferenceType type, INativeHandle handle) { - super(handle); - this.type = type; - handle.setSize(getByteCount()); - } - - public INativeType getBaseType() { - return type.getBaseType(); - } - - @Override - public int getByteCount() { - return NativeInterface.get().pointerSize(); - } - - @Override - public INativeType getNativeType() { - return type; - } - - public long getReferencedAddress() { - return getNativeHandle(0).getAddress(); - } - - synchronized public T getValue() { - dereferenceHandle = handle.getNativeHandle(0); - // check if not yet computed or handle changed - if (dereferenced == null - || !dereferenced.getNativeHandle().equals(dereferenceHandle)) { - dereferenced = (T) type.getBaseType().createNative( - dereferenceHandle); - } - return dereferenced; - } - - public void setBaseType(INativeType baseType) { - type = NativeReferenceType.create(baseType); - dereferenceHandle = null; - dereferenced = null; - } - - synchronized public void setValue(Object value) { - if (value instanceof INativeObject) { - T nativeObject = (T) value; - INativeHandle valueHandle = nativeObject.getNativeHandle(); - handle.setNativeHandle(0, valueHandle); - dereferenceHandle = valueHandle; - dereferenced = nativeObject; - } else if (value instanceof INativeHandle) { - INativeHandle valueHandle = (INativeHandle) value; - handle.setNativeHandle(0, valueHandle); - dereferenceHandle = valueHandle; - dereferenced = null; - } else { - throw new IllegalArgumentException(); - } - } - - @Override - public String toString() { - if (handle == null) { - return "Ref to null"; - } - return "Ref to " + getReferencedAddress(); - } - -} diff --git a/src/de/intarsys/nativec/type/NativeString.java b/src/de/intarsys/nativec/type/NativeString.java deleted file mode 100644 index 6ba6a15..0000000 --- a/src/de/intarsys/nativec/type/NativeString.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * - Neither the name of intarsys nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package de.intarsys.nativec.type; - -import de.intarsys.nativec.api.INativeHandle; -import de.intarsys.nativec.api.NativeTools; - -/** - * A wrapper for a C single byte null terminated string. - */ -public class NativeString extends NativeObject { - - /** The meta class instance */ - public static final NativeStringType META = new NativeStringType(); - - static { - NativeType.register(NativeString.class, META); - } - - static public NativeString createFromAddress(long address) { - return (NativeString) NativeString.META.createNative(NativeTools - .toHandle(address)); - } - - private int size = 0; - private NativeStringType type; - - protected NativeString(NativeStringType pType) { - type = pType; - allocate(); - } - - protected NativeString(NativeStringType pType, INativeHandle handle) { - super(handle); - type = pType; - if (type.hasByteCount()) { - handle.setSize(type.getByteCount()); - } - } - - protected NativeString(NativeStringType pType, String value) { - type = pType; - if (type.getStringSize() == 0) { - this.size = value.length() + 1; - } - allocate(); - setValue(value); - } - - public NativeString(String value) { - this(META, value); - } - - @Override - public int getByteCount() { - if (type.hasByteCount()) { - return type.getByteCount(); - } - return size; - } - - @Override - public INativeType getNativeType() { - return type; - } - - public Object getValue() { - return stringValue(); - } - - public void setValue(Object value) { - setValue((String) value); - } - - public void setValue(String value) { - this.size = value.length() + 1; - handle.setString(0, value); - } - - /** - * The java object corresponding to this. - * - * @return The java object corresponding to this. - */ - public String stringValue() { - return handle.getString(0); - } -} diff --git a/src/de/intarsys/nativec/type/NativeStruct.java b/src/de/intarsys/nativec/type/NativeStruct.java deleted file mode 100644 index 7e668c8..0000000 --- a/src/de/intarsys/nativec/type/NativeStruct.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * - Neither the name of intarsys nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package de.intarsys.nativec.type; - -import java.util.Iterator; - -import de.intarsys.nativec.api.INativeHandle; - -/** - * An abstract superclass for the implementation of structured - * {@link NativeObject} instances. These objects are built using named slots - * with other {@link INativeObject} instances (as opposed to {@link NativeArray}, - * using indexed slots). - * - */ -public abstract class NativeStruct extends NativeObject { - - /** The meta class instance */ - public static final NativeStructType META = new NativeStructType(); - - static { - NativeType.register(NativeGenericStruct.class, META); - } - - protected INativeObject[] values; - - public NativeStruct() { - super(); - } - - public NativeStruct(INativeHandle handle) { - super(handle); - } - - @Override - public int getByteCount() { - return getNativeType().getByteCount(); - } - - /** - * The NativeObject at the named slot name. - *

- * The marshalling is delegated to the StructMember in the - * StructDeclaration. - * - * @param name - * The name of the slot in the structure. - * - * @return The NativeObject at the named slot name. - */ - public INativeObject getNativeObject(String name) { - return getStructType().getNativeObject(this, name); - } - - protected StructMember getStructField(String name) { - return getStructType().getField(name); - } - - public NativeStructType getStructType() { - return (NativeStructType) getNativeType(); - } - - public Object getValue() { - throw new UnsupportedOperationException( - "getValue not implemented for NativeStruct"); - } - - public void setValue(Object value) { - throw new UnsupportedOperationException( - "getValue not implemented for NativeStruct"); - } - - /* - * (non-Javadoc) - * - * @see de.intarsys.tools.nativec.NativeObject#toNestedString() - */ - @Override - public String toNestedString() { - return "Struct " + getClass().getName(); - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append(getClass().getName()); - sb.append("\n"); - for (Iterator it = getStructType().getFields().iterator(); it.hasNext();) { - StructMember field = (StructMember) it.next(); - sb.append(field.getName()); - sb.append("="); - try { - sb.append(getNativeObject(field.getName()).toString()); - } catch (RuntimeException e) { - sb.append("**error**"); - } - sb.append("\n"); - } - return sb.toString(); - } -} diff --git a/src/de/intarsys/nativec/type/NativeStructType.java b/src/de/intarsys/nativec/type/NativeStructType.java deleted file mode 100644 index c48befe..0000000 --- a/src/de/intarsys/nativec/type/NativeStructType.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * - Neither the name of intarsys nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package de.intarsys.nativec.type; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import de.intarsys.nativec.api.NativeInterface; - -/** - * The meta class implementation - */ -public class NativeStructType extends NativeType { - - /** - * The boundary where the struct as a member of another struct would want to - * be aligned. - */ - private int byteBoundary = 1; - - /** - * The offset where theoretically a new member could be placed. Where it - * will actually be placed depends on the member itself. - */ - private int byteOffset = 0; - - /** The size in bytes of the member collection */ - private int byteSize = 0; - - /** The named members of the structure definition */ - private final Map fields = new HashMap(); - - /** - * The number of members in the structure - */ - private int fieldsSize = 0; - - /** Where members should be aligned in native memory */ - // expecting pointer size to be a reasonable default - have to look that up - private int packing = NativeInterface.get().pointerSize(); - - protected NativeStructType() { - super(); - } - - protected NativeStructType(Class instanceClass) { - super(instanceClass); - } - - /** - * Declare a new member for the struct. - * - * @param name - * The name of the new member slot. - * @param declaration - * The type declaration for the slot - */ - public StructMember declare(String name, INativeType declaration) { - int size; - int boundary; - - size = declaration.getByteCount(); - boundary = Math.min(declaration.getPreferredBoundary(), packing); - byteBoundary = Math.max(byteBoundary, boundary); - - byteOffset = byteOffset - ((byteOffset + boundary - 1) % boundary + 1) - + boundary; - StructMember field = new StructMember(this, name, fieldsSize++, - declaration, byteOffset); - fields.put(name, field); - byteOffset = byteOffset + size; - byteSize = byteOffset; - return field; - } - - public int getByteBoundary() { - return byteBoundary; - } - - @Override - public int getByteCount() { - return getByteSize(); - } - - /** - * The total size of the StructDeclaration. - * - * @return The total size of the StructDeclaration. - */ - public int getByteSize() { - return byteSize; - } - - public StructMember getField(String name) { - return fields.get(name); - } - - /** - * The collection of StructMember instances in declaration order. - * - * @return The collection of StructMember instances in declaration order. - */ - public List getFields() { - return new ArrayList(fields.values()); - } - - public int getFieldsSize() { - return fieldsSize; - } - - public INativeObject getNativeObject(NativeStruct struct, String name) { - StructMember member = fields.get(name); - return member.getNativeObject(struct); - } - - public int getPacking() { - return packing; - } - - @Override - public int getPreferredBoundary() { - return getByteBoundary(); - } - - public void setPacking(int pPacking) { - if (!fields.isEmpty()) { - throw new IllegalStateException( - "packing must be set before members are declared"); - } - packing = pPacking; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - for (StructMember field : getFields()) { - sb.append(field.toString()); - sb.append("\n"); - } - return sb.toString(); - } - -} \ No newline at end of file diff --git a/src/de/intarsys/nativec/type/NativeType.java b/src/de/intarsys/nativec/type/NativeType.java deleted file mode 100644 index 6f6219c..0000000 --- a/src/de/intarsys/nativec/type/NativeType.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * - Neither the name of intarsys nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package de.intarsys.nativec.type; - -import java.util.HashMap; -import java.util.Map; - -import de.intarsys.nativec.api.INativeHandle; - -/** - * A common superclass for {@link INativeType} implementations - */ -public abstract class NativeType implements INativeType { - - /** All known meta classes. */ - private static Map, INativeType> metaClasses = new HashMap, INativeType>(); - - public static synchronized INativeType lookup(Class clazz) { - INativeType result = metaClasses.get(clazz); - if (result == null) { - ClassLoader classLoader; - - clazz.getClasses(); - classLoader = Thread.currentThread().getContextClassLoader(); - if (classLoader == null) { - classLoader = NativeType.class.getClassLoader(); - } - - try { - Class.forName(clazz.getName(), true, classLoader); - } catch (ClassNotFoundException ex) { - // ignore - } - result = metaClasses.get(clazz); - } - return result; - } - - public static synchronized void register(Class clazz, INativeType type) { - metaClasses.put(clazz, type); - } - - protected NativeType() { - // - } - - protected NativeType(Class instanceClass) { - register(instanceClass, this); - } - - /** - * Create a Declaration that represents an array of this. - * - * @return Create a Declaration that represents an array of this. - */ - public INativeType Array(int size) { - return new NativeArrayType(this, size); - } - - public INativeObject createNative(INativeHandle handle) { - throw new IllegalStateException("meta constructor missing"); - } - - public INativeObject createNative(Object value) { - throw new IllegalStateException("meta constructor missing"); - } - - public int getByteCount() { - return 0; - } - - /** - * Create a Declaration that represents a reference to this. - * - * @return Create a Declaration that represents a reference to this. - */ - public INativeType Ref() { - return new NativeReferenceType(this); - } -} diff --git a/src/de/intarsys/nativec/type/NativeWideString.java b/src/de/intarsys/nativec/type/NativeWideString.java deleted file mode 100644 index d8903c7..0000000 --- a/src/de/intarsys/nativec/type/NativeWideString.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * - Neither the name of intarsys nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package de.intarsys.nativec.type; - -import de.intarsys.nativec.api.INativeHandle; -import de.intarsys.nativec.api.NativeTools; - -/** - * A wrapper for a C wide (double byte) string. - */ -public class NativeWideString extends NativeObject { - - /** The meta class instance */ - public static final NativeWideStringType META = new NativeWideStringType(); - - static { - NativeType.register(NativeWideString.class, META); - } - - static public NativeWideString createFromAddress(long address) { - return (NativeWideString) NativeWideString.META - .createNative(NativeTools.toHandle(address)); - } - - private int size = 0; - private NativeWideStringType type; - - protected NativeWideString(NativeWideStringType pType) { - type = pType; - allocate(); - } - - protected NativeWideString(NativeWideStringType pType, INativeHandle handle) { - super(handle); - type = pType; - if (type.hasByteCount()) { - handle.setSize(type.getByteCount()); - } - } - - protected NativeWideString(NativeWideStringType pType, String value) { - type = pType; - if (type.getStringSize() == 0) { - size = value.length() + 1; - size = size << 1; - } - allocate(); - setValue(value); - } - - public NativeWideString(String value) { - this(META, value); - } - - @Override - public int getByteCount() { - if (type.hasByteCount()) { - return type.getByteCount(); - } - return size; - } - - @Override - public INativeType getNativeType() { - return type; - } - - public Object getValue() { - return stringValue(); - } - - public void setValue(Object value) { - setValue((String) value); - } - - public void setValue(String value) { - size = value.length() + 1; - size = size << 1; - handle.setWideString(0, value); - } - - /** - * The java object corresponding to this. - * - * @return The java object corresponding to this. - */ - public String stringValue() { - return handle.getWideString(0); - } -} diff --git a/src/de/intarsys/nativec/type/StructMember.java b/src/de/intarsys/nativec/type/StructMember.java deleted file mode 100644 index f10965a..0000000 --- a/src/de/intarsys/nativec/type/StructMember.java +++ /dev/null @@ -1,359 +0,0 @@ -/* - * Copyright (c) 2008, intarsys consulting GmbH - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * - Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * - Neither the name of intarsys nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package de.intarsys.nativec.type; - -import de.intarsys.nativec.api.INativeHandle; - -/** - * A field definition within a {@link NativeStructType}. - */ -public class StructMember { - - /** the members declaration */ - private final INativeType memberType; - - /** The members name */ - protected final String name; - - /** the index of the member within the struct */ - protected final int index; - - /** The offset of the declaration within its structure */ - private final int offset; - - /** the definition context of the member */ - private final NativeStructType structType; - - /** - * Create a slot with name "name" and the declaration "memberDeclaration" - */ - protected StructMember(NativeStructType structType, String name, int index, - INativeType memberType, int offset) { - super(); - this.structType = structType; - this.name = name; - this.index = index; - this.memberType = memberType; - this.offset = offset; - } - - /** - * Performance shortcut to access "byte" in the struct member. - * - * @param struct - * The container struct instance - * @param index - * The memory offset from the struct member base - * @return The byte at index within the memory range of struct - */ - public byte getByte(NativeStruct struct, int index) { - return struct.handle.getByte(offset + index); - } - - /** - * Performance shortcut to access "byte[]" in the struct member. - * - * @param struct - * The container struct instance - * @param index - * The memory offset from the struct member base - * @return The byte array starting at index of length count within the - * memory range of struct - */ - public byte[] getByteArray(NativeStruct struct, int index, int count) { - return struct.handle.getByteArray(offset + index, count); - } - - /** - * Performance shortcut to access "platform sized long" in the struct - * member. - * - * @param struct - * The container struct instance - * @param index - * The memory offset from the struct member base - * @return The platform sized long at index within the memory range of - * struct - */ - public long getCLong(NativeStruct struct, int index) { - return struct.handle.getCLong(offset + index); - } - - /** - * Performance shortcut to access "int" in the struct member. - * - * @param struct - * The container struct instance - * @param index - * The memory offset from the struct member base - * @return The int at index within the memory range of struct - */ - public int getInt(NativeStruct struct, int index) { - return struct.handle.getInt(offset + index); - } - - /** - * Performance shortcut to access "long" in the struct member. - * - * @param struct - * The container struct instance - * @param index - * The memory offset from the struct member base - * @return The long at index within the memory range of struct - */ - public long getLong(NativeStruct struct, int index) { - return struct.handle.getLong(offset + index); - } - - /** - * The type declaration of the slot. - * - * @return The type declaration of the slot. - */ - protected INativeType getMemberType() { - return memberType; - } - - /** - * The slots name. - * - * @return The slots name. - */ - public String getName() { - return name; - } - - /** - * Performance shortcut to access "INativeHandle" in the struct member. - * - * @param struct - * The container struct instance - * @param index - * The memory offset from the struct member base - * @return The INativeHandle at index within the memory range of struct - */ - public INativeHandle getNativeHandle(NativeStruct struct, int index) { - return struct.handle.getNativeHandle(offset + index); - } - - synchronized public INativeObject getNativeObject(NativeStruct struct) { - if (struct.values == null) { - struct.values = new INativeObject[structType.getFieldsSize()]; - } - INativeObject result = struct.values[index]; - if (result == null) { - result = memberType.createNative(struct.getNativeHandle().offset( - offset)); - struct.values[this.index] = result; - } - return result; - } - - /** - * The offset of the slot relative to the StructDeclaration. - * - * @return The offset of the slot relative to the StructDeclaration. - */ - protected int getOffset() { - return offset; - } - - /** - * Performance shortcut to access "short" in the struct member. - * - * @param struct - * The container struct instance - * @param index - * The memory offset from the struct member base - * @return The short at index within the memory range of struct - */ - public short getShort(NativeStruct struct, int index) { - return struct.handle.getShort(offset + index); - } - - /** - * Performance shortcut to access "String" in the struct member. - * - * @param struct - * The container struct instance - * @param index - * The memory offset from the struct member base - * @return The String at index within the memory range of struct - */ - public String getString(NativeStruct struct, int index) { - return struct.handle.getString(offset + index); - } - - public Object getValue(NativeStruct struct) { - return getNativeObject(struct).getValue(); - } - - /** - * Performance shortcut to access "String" (from wide characters) in the - * struct member. - * - * @param struct - * The container struct instance - * @param index - * The memory offset from the struct member base - * @return The wide character String at index within the memory range of - * struct - */ - public String getWideString(NativeStruct struct, int index) { - return struct.handle.getWideString(offset + index); - } - - /** - * Performance shortcut to access "byte" in the struct member. - * - * @param struct - * The container struct instance - * @param index - * The memory offset from the struct member base - */ - public void setByte(NativeStruct struct, int index, byte value) { - struct.handle.setByte(offset + index, value); - } - - /** - * Performance shortcut to access "byte[]" in the struct member. - * - * @param struct - * The container struct instance - * @param index - * The memory offset from the struct member base - */ - public void setByteArray(NativeStruct struct, int index, byte[] value, - int valueOffset, int valueCount) { - struct.handle.setByteArray(offset + index, value, valueOffset, - valueCount); - } - - /** - * Performance shortcut to access "platform sized long" in the struct - * member. - * - * @param struct - * The container struct instance - * @param index - * The memory offset from the struct member base - */ - public void setCLong(NativeStruct struct, int index, long value) { - struct.handle.setCLong(offset + index, value); - } - - /** - * Performance shortcut to access "int" in the struct member. - * - * @param struct - * The container struct instance - * @param index - * The memory offset from the struct member base - */ - public void setInt(NativeStruct struct, int index, int value) { - struct.handle.setInt(offset + index, value); - } - - /** - * Performance shortcut to access "long" in the struct member. - * - * @param struct - * The container struct instance - * @param index - * The memory offset from the struct member base - */ - public void setLong(NativeStruct struct, int index, long value) { - struct.handle.setLong(offset + index, value); - } - - /** - * Performance shortcut to access "INativeHandle" in the struct member. - * - * @param struct - * The container struct instance - * @param index - * The memory offset from the struct member base - */ - public void setNativeHandle(NativeStruct struct, int index, - INativeHandle value) { - struct.handle.setNativeHandle(offset + index, value); - } - - /** - * Performance shortcut to access "short" in the struct member. - * - * @param struct - * The container struct instance - * @param index - * The memory offset from the struct member base - */ - public void setShort(NativeStruct struct, int index, short value) { - struct.handle.setShort(offset + index, value); - } - - /** - * Performance shortcut to access "String" in the struct member. - * - * @param struct - * The container struct instance - * @param index - * The memory offset from the struct member base - */ - public void setString(NativeStruct struct, int index, String value) { - struct.handle.setString(offset + index, value); - } - - public void setValue(NativeStruct struct, Object value) { - getNativeObject(struct).setValue(value); - } - - /** - * Performance shortcut to access "String" (from wide characters) in the - * struct member. - * - * @param struct - * The container struct instance - * @param index - * The memory offset from the struct member base - */ - public void setWideString(NativeStruct struct, int index, String value) { - struct.handle.setWideString(offset + index, value); - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return "[" + getName() + "]"; - } -} diff --git a/src/de/intarsys/nativec/type/package.html b/src/de/intarsys/nativec/type/package.html deleted file mode 100644 index 557247c..0000000 --- a/src/de/intarsys/nativec/type/package.html +++ /dev/null @@ -1,5 +0,0 @@ - - -Here you find the data types and data structures for native c. - - \ No newline at end of file