001/**************************************************************************** 002 * Copyright/Copyleft: 003 * 004 * For this source the LGPL Lesser General Public License, 005 * published by the Free Software Foundation is valid. 006 * It means: 007 * 1) You can use this source without any restriction for any desired purpose. 008 * 2) You can redistribute copies of this source to everybody. 009 * 3) Every user of this source, also the user of redistribute copies 010 * with or without payment, must accept this license for further using. 011 * 4) But the LPGL is not appropriate for a whole software product, 012 * if this source is only a part of them. It means, the user 013 * must publish this part of source, 014 * but don't need to publish the whole source of the own product. 015 * 5) You can study and modify (improve) this source 016 * for own using or for redistribution, but you have to license the 017 * modified sources likewise under this LGPL Lesser General Public License. 018 * You mustn't delete this Copyright/Copyleft inscription in this source file. 019 * 020 * @author Hartmut Schorrig: hartmut.schorrig@vishia.de, www.vishia.org 021 * @version 0.93 2011-01-05 (year-month-day) 022 *******************************************************************************/ 023package org.vishia.java2C.test; 024 025/**This interface should demonstrate the translation of Java-Interfaces to C. Visit the translated 026 * <code>Ifc_Test.c</code> and <code>Ifc_Test.h</code>. 027 * <br> 028 * The C-File contains code for: 029 * <ul> 030 * <li>An implementation of all interface methods, which searches the appropriate method table 031 * of the interface inside the method table of the given instance, calling <code>getMtbl_ObjectJc(...)</code> 032 * and than calls the method dynamically. The user can call this method static linked, this method 033 * organized the dynmically call. ( Elsewhere the user can optimize and call via method table itself, 034 * Java2C translates such calls.) 035 * <li>Reflection for the interface, contains the reference to the method table. 036 * <li>The definition of the <code>sign_Mtbl_...</code> of this interface. 037 * <li>The definition of all final static variables which are not defined as <code>#define ...</code> 038 * </ul> 039 * The Headerfile contains declarations and definitions for: 040 * <ul> 041 * <li>The <code>typedef struct {...}</code> of the interface type. The struct contains only 042 * <code>union { ObjectJc object; } base; </code> 043 * <li>Typedef for the array type. 044 * <li>extern declaration for reflection. 045 * <li>extern declaration or <code>#define ...</code>for all static final values. 046 * <li>Definition of <code>MT_</code> method type of the interface methods. 047 * <li>Declaration of head of interface methods to implement the dynamic call. 048 * <li>extern Declaration of the <code>sign_Mtbl_...</code> of teh interface. 049 * <li>Definition of the struct of the method table. 050 * </ul> 051 * Compilation and linking the C-File: 052 * The kept C-File of any translated Java-interfaces should be compiled and provided as object-file 053 * maybe in a library outside of a usage of the interface in an implementation. 054 * It means, it should not be a part of a implementation library of the interface, 055 * but it should be a part of a library providing the interface stand-alone independent of its usage. 056 * It is the same like a Java-package with interfaces only. It has its compiled byte code. 057 * <br> 058 * 059 * @author Hartmut Schorrig 060 * 061 */ 062public interface Ifc 063{ 064 /**Example for a simple constant declared in java. Because it is fix and constant, 065 * it is translated in C as 066 * <pre class="CCode"> 067 * #define constValue_Ifc_Test 24 068 * </pre> 069 * In this form the constant is used as an immediate value. A usage of this value is independent 070 * of the content of the interface' C-file. The same behavior is known for java in such cases: 071 * A substitution of a class file which contains another value for the same constant doesn't update 072 * the value in already-compiled class files. 073 */ 074 static final int constValue = 24; 075 076 /**Example for a simple constant string literal. It is declared as 077 * <pre class="CCode"> 078 * extern const StringJc constString_Ifc_Test; 079 * </pre> 080 * and defined in the C-File. It means, its value is updated if the interface is compiled and supplied newly. 081 * The string literal itself is stored only one time in the code. It is better as translating 082 * a <code>#define constString_Ifc_Test "IfcTest"</code>. 083 */ 084 static final String constString = "IfcTest"; 085 086 /**Example for a calculated constant.At this time this constant is declared in Headerfile as 087 * <pre class="CCode"> 088 * extern const int32 constValue2_Ifc_Test; 089 * </pre> 090 * and defined in C-file as 091 * <pre class="CCode"> 092 * const int32 constValue2_Ifc_Test = (constValue_Ifc_Test + 1) / 2 + 1; 093 * </pre> 094 * where the calculation is made at compile time. It is possible because the input values are constants 095 * or constants defined per #define. 096 * <br> 097 * This strategy of compiling is limited. A constant defined in this form isn't able to use in C 098 * as constant input to built a adequate constant. The compiler may cause an error "initializer is not a constant" 099 * though the value is defined before. (testet with visual-Studio-6 C-compilation). 100 * Therefore it is better to build a 101 * <pre class="CCode"> 102 * #define constValue2_Ifc_Test (constValue_Ifc_Test + 1)/2 +1 103 * </pre> 104 * The behavior should be changed in the next releases of Java2C. 105 */ 106 static final int constValue2 = (constValue + 1)/2 +1; 107 108 109 /**Example for a calculated constant using another calculated constant as input. 110 * At this time the C-Compiler causes an error, see about. Therefore it is commented. 111 */ 112 //TODO: static final int constValue3 = (constValue2 + constValue+ 1)/2 +1; 113 114 115 /**Example for an interface method. This method is only abstract. 116 * In C it is a member of a so named <i>method table</i>. Therefore a method type definition 117 * is written in the Java2C-translated Headerfile: 118 * <pre class="CCode"> 119 * typedef int32 MT_processIfcMethod_Ifc_Test(ObjectJc* ithis, int32 input, ThCxt* _thCxt); 120 * </pre> 121 * The <i>method table</i> is defined one time for all interface methods, containing both methods 122 * of this example of Java-interface: 123 * <pre class="CCode"> 124 * typedef struct Mtbl_Ifc_Test_t 125 * { MtblHeadJc head; 126 * MT_processIfcMethod_Ifc_Test* processIfcMethod; 127 * MT_anotherIfcmethod_i_Ifc_Test* anotherIfcmethod_i; 128 * MT_anotherIfcmethod_f_Ifc_Test* anotherIfcmethod_f; 129 * Mtbl_ObjectJc ObjectJc; 130 * } Mtbl_Ifc_Test; 131 * </pre> 132 * An implementation uses this struct definition inside its more complex method table struct definition, 133 * defines a const method table instance and set the implementation of the method at the appropriate position, 134 * see {@link ImplIfc#processIfcMethod(int)}. 135 * @param input Example 136 * @return Example 137 */ 138 int processIfcMethod(int input); 139 140 141 142 143 /**Example for a second interface method, not a recentness, but see next {@link #anotherIfcmethod(float)}. 144 * @param input 145 * @return 146 */ 147 int anotherIfcmethod(int input); 148 149 /**Example for an interface method with same name as another in the same class, but other parameter set. 150 * The ambiguousness of the methods are detect and a unambiguous C-name is build. Therefore the two methods 151 * builds several method type declarations in headerfile: 152 * <pre class="CCode"> 153 * typedef int32 MT_anotherIfcmethod_i_Ifc_Test(ObjectJc* ithis, int32 input, ThCxt* _thCxt); 154 * typedef float MT_anotherIfcmethod_f_Ifc_Test(ObjectJc* ithis, float input, ThCxt* _thCxt); 155 * </pre> 156 * and the adequate positions in the method table. 157 * @param input 158 * @return 159 */ 160 float anotherIfcmethod(float input); 161 162}