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 class is an example for implementation of some interface methods.
026 * <br>
027 * The superclass {@link SimpleDataStruct} is a very simple class, only contains data.
028 * The interfaces are complex, it contains methods with same name but different parameters, 
029 * also dispersed to both interfaces. So the implementation of methods using method tables 
030 * can be demonstrated. See descriptions of the method.
031 * <br>
032 * Visit the translated ImplIfc_Test.c and ImplIfc_Test.h.
033 * <br> 
034 * The struct of the class is defined in the Headerfile:
035 * <pre class="CCode">
036 * typedef struct ImplIfc_Test_t
037 * { 
038 *   union { ObjectJc object; ExpandableDataStruct_Test_s super;
039 *           Ifc_Test_s Ifc_Test;Ifc2_Test_s Ifc2_Test;
040 *         } base; 
041 *   int32 val;  
042 * } ImplIfc_Test_s;
043 * 
044 * </pre>
045 * The other struct is the method table, it is shown as explanation on method {@link #anotherIfcmethod(float)}.
046 * The method table shows and represents the structure of dynamic call-able (virtual) methods 
047 * including the inheritance- and interface structure of this class. The implementation of interface
048 * of this class in Java is transformed in C in the first place there.
049 */
050public class ImplIfc extends SimpleClass implements Ifc, Ifc2
051{
052
053  /**Example for a simple class variable. Mark value to difference several instances. */
054  int val;
055  
056  /**An enhanced reference.
057   * 
058   */
059  Ifc ref;
060  
061  final AnyClass data = new AnyClass(this); 
062  
063  /**Example for simple Constructor. The Constructor is declared in C as
064   * <pre class="CCode">
065   * METHOD_C struct ImplIfc_Test_t* ctorO_ImplIfc_Test(ObjectJc* othis, int32 val, ThCxt* _thCxt);
066   * </pre>
067   * The implementation is the C-Routine
068   * <pre class="CCode">
069   * struct ImplIfc_Test_t* ctorO_ImplIfc_Test(ObjectJc* othis, int32 val, ThCxt* _thCxt)
070   * { ImplIfc_Test_s* ythis = (ImplIfc_Test_s*)othis;  //upcasting to the real class.
071   *   int sizeObj = getSizeInfo_ObjectJc(othis);
072   *   extern ClassJc const reflection_ImplIfc_Test_s;
073   *   STACKTRC_TENTRY("ctorO_ImplIfc_Test");
074   *   checkConsistence_ObjectJc(othis, sizeof(ImplIfc_Test_s), &reflection_ImplIfc_Test_s, _thCxt);  
075   *   setReflection_ObjectJc(othis, &reflection_ImplIfc_Test_s, sizeof(ImplIfc_Test_s));  
076   *   //j2c: Initialize all class variables:
077   *   {
078   *   }
079   *   { ythis->val = val;
080   *   }
081   *   STACKTRC_LEAVE;
082   *   return ythis;
083   * }
084   * The constructor routine is called with a given Memory space which is pre-initialized as ObjectJc,
085   * named <code>othis</code>. The <code>checkConsistence_ObjectJc(...)</code>-Routine checks whether
086   * the memory space is sufficient. It should be guaranteed. The <code>setReflection_ObjectJc(...)</code>
087   * completes the ObjectJc base with the appropriate reflections defined for this class. 
088   * In this example no class variable should be intitialized The core statements follows.
089   * See the {@link TestAllConcepts#TestAllConcepts()} Constructor for a more extensive example.
090   */
091  ImplIfc(int val)
092  { /**In the constructor a overridden method of a interface is used, call it dynamically. */
093    this.val = processIfcMethod(val); 
094  }
095  
096  /**Example for a method which is override-able and implements an interface method.
097   * This method implements {@link Ifc#processIfcMethod(int)}.
098   * The method is declared in C as
099   * <pre class="CCode">
100   * METHOD_C int32 processIfcMethod_i_ImplIfc_Test_F(ObjectJc* ithis, int32 input, ThCxt* _thCxt);
101   * </pre>
102   * The middle part of name is <code>_i_</code> because there is also a variant of the method 
103   * with another parameter types. The suffix <code>_F</code>  is because the implementation version
104   * of the method is the final version, but the user may call the also declared method:
105   * <pre class="CCode">
106   * METHOD_C int32 processIfcMethod_i_ImplIfc_Test(ObjectJc* ithis, int32 input, ThCxt* _thCxt);
107   * </pre>
108   * This variant implements the dynamic call (virtual method) internally.
109   * <br>
110   * The implementation of the final method is
111   * <pre class="CCode">
112   * int32 processIfcMethod_i_ImplIfc_Test_F(ObjectJc* ithis, int32 input, ThCxt* _thCxt)
113   * { ImplIfc_Test_s* ythis = (ImplIfc_Test_s*)ithis;
114   *   STACKTRC_TENTRY("processIfcMethod_i_ImplIfc_Test_F");
115   *   { { STACKTRC_LEAVE;
116   *       return ythis->base.super.xb + input + ythis->val;
117   *     }
118   *   }
119   *   STACKTRC_LEAVE;
120   * }
121   * </pre>
122   * The this-pointer is given in Form of ObjectJc, because it is an implementation of an interface method.
123   * The cast to the real type is done in the next line. It is a unchecked cast. 
124   * It may be correct, because this method is only called
125   * <ul>
126   * <li>via a Method table reference, where the choice of the method table part 
127   *   is depending on the correct type, In this case the type can only be correct because calling code.
128   * <li>via a direct call if the user denotes the reference with @ java2c=instanceType:"Type".
129   *   In this case the type should be correct because the user should named the type carefully.
130   * </ul> 
131   * Therefore a type test is not generated automatically. If a safety level is need, the programmer
132   * should write a first statement <code>instanceof Type</code> in Java. Another possibility were,
133   * to generate a type test depending on @ java2c=checkType - annotation. This is not implemented yet,
134   * but it was a possible feature for methods, which overrides or implements base methods.
135   * <br>
136   * The second generated method implementation for support dynamic call is:
137   * <pre class="CCode">
138   * / *J2C: dynamic call variant of the override-able method: * /
139   * int32 processIfcMethod_i_ImplIfc_Test(ObjectJc* ithis, int32 input, ThCxt* _thCxt)
140   * { Mtbl_Ifc_Test const* mtbl = (Mtbl_Ifc_Test const*)getMtbl_ObjectJc(ithis, sign_Mtbl_Ifc_Test);
141   *   return mtbl->processIfcMethod(ithis, input, _thCxt);
142   * }
143   * </pre>
144   * If the user doesn't determine that an instance is type of a given instance, a reference may refer
145   * an instance with derived type. The method may be overridden there. 
146   * Therefore the dynamic call should be done. But a normal C user doesn't may know this mechanism,
147   * it may be a call of the method from manual written C-code. Therefore the dynamic call is implemented here.
148   * Another way is to organize the dynamic call at user level, in the adequate kind as generated here.
149   */
150  public int processIfcMethod(int input)
151  {
152    return x1 + input + val;
153  }
154
155  /**Example for a method which is final but implements an interface method (the same is if it
156   * overrides methods from a super class).This method implements {@link Ifc#processIfcMethod(int)}.
157   * The method is declared in C as
158   * <pre class="CCode">
159   * METHOD_C float processIfcMethod_f_ImplIfc_Test(ObjectJc* ithis, float input, ThCxt* _thCxt);
160   * </pre>
161   * The middle part of name <code>_f_</code>, because another method which the same name exists. 
162   * For this method no variant with a suffix <code>_F</code> exists, because this method is final itself.
163   * No extra final method is necessary.
164   * <br>
165   * The implementation of the method is adequate to |{@link #processIfcMethod(int)}, see there.
166   */
167  final public float processIfcMethod(float input)
168  {
169    return 3.56F * input;
170  }
171
172  /**This method is declared in the interface {@link Ifc#processIfcMethod(int)} .
173   */
174  final public int anotherIfcmethod(int input)
175  {
176    return 34 + input;
177  }
178
179  @Override public String toString()
180  { return "ImplIfc";
181  }
182  
183  /**Example for a simple method, no implementation, no override-able.
184   * This method executes something to test.
185   * @return
186   */
187  final int testImplIfc()
188  { int a = processIfcMethod(5);
189    a += (int)processIfcMethod(6.28F);
190    a += (int)testIfc2(23.4F);
191    a += testOverrideAble(3.14F);
192    return a; 
193  }
194
195  /**This method implements {@link Ifc2#testIfc2(float)}. @java2c=override-able.
196   */
197  final public float testIfc2(float input)
198  {
199    return input / 3.14F;
200  }
201  
202  
203  /**This method should demonstrate override-able methods for Java2C outside of interface-concepts. 
204   * @java2c=override-able. */
205  int testOverrideAble(float value)
206  { return (int)value;
207  }
208
209  
210  /**This method should demonstrate overridden methods of base classes. The method is overridden
211   * in {@link TestAllConcepts}. 
212   */
213  int testOverridden(float value)
214  { return (int)value;
215  }
216
217  
218  /**This method demonstrates, how a concatenation of method calls works, if the method returns this itself.
219   * The return value is the same as the calling instance. This is recognized by Java2C,
220   * if the @ return contains the text <code>this</code> or if <code>@ java2c=return-this</code> is written.
221   * @java2c=return-this. 
222   * @param value any value
223   * @return this
224   */
225  final ImplIfc returnThis(int value)
226  {
227    val = value;
228    return this;
229  }
230  
231  
232  /**This method demonstrates, how a concatenation of method calls works, if the method returns this itself.
233   * The return value is the same as the calling instance. This is recognized by Java2C,
234   * if the @ return contains the text <code>this</code> or if <code>@ java2c=return-this</code> is written.
235   * The difference to {@link #returnThis(int)} is: This method is able to override, therefore a dynamic call is necessary.
236   * @java2c=return-this. 
237   * @param value any value
238   * @return this
239   */
240  ImplIfc returnThisOverrideable(int value)
241  {
242    val = value;
243    return this;
244  }
245  
246  
247  /**This method demonstrates, how a concatenation of method calls works, if the method returns any other reference.
248   * Concretely the return value is this, but the translator doesn't know that. 
249   * @param value any value
250   * @return any unknown instance, only concretely this.
251   */
252  AnyClass returnAnyInstanceOverrideable()
253  {
254    return data;
255  }
256  
257  
258  /**This method demonstrates, how a concatenation of method calls works, if the method returns any other reference.
259   * Concretely the return value is this, but the translator doesn't know that. 
260   * @param value any value
261   * @return any unknown instance, only concretely this.
262   */
263  final AnyClass returnAnyInstance()
264  {
265   return data;
266  }
267  
268  
269  /**This methods implements both methods declared in Interface {@link Ifc#anotherIfcmethod(float)}
270   * and {@link Ifc2#anotherIfcmethod(float)}. Only one implementation is necessary in Java 
271   * and in the translated C too. The Implementation is defined in the Headerfile:
272   * <pre class="CCode">
273   * METHOD_C int32 anotherIfcMethod_f_ImplIfc_Test_F(ImplIfc_Test_s* ythis, int32 input, ThCxt* _thCxt);
274   * METHOD_C int32 anotherIfcMethod_f_ImplIfc_Test(ImplIfc_Test_s* ythis, int32 input, ThCxt* _thCxt);
275   * </pre>
276   * The first form is the implementation method. The second form is the method, which realizes
277   * the dynamically call because the method is override-able.
278   * <br>
279   * The Method table of this class contains the struct of method tables for the interfaces. 
280   * Its definition in the headerfile is:
281   * <pre class="CCode">
282   * typedef struct Mtbl_ImplIfc_Test_t
283   * { MtblHeadJc head;
284   *   MT_testOverrideAble_ImplIfc_Test* testOverrideAble;
285   *   Mtbl_ExpandableDataStruct_Test ExpandableDataStruct_Test;
286   *   //Method table of interfaces:
287   *   Mtbl_Ifc_Test Ifc_Test;
288   *   Mtbl_Ifc2_Test Ifc2_Test;
289   * } Mtbl_ImplIfc_Test;
290   * </pre>
291   * Both interfaces are contained. The definition of the instance of methode table for this class
292   * in the C-File <code>ImplIfc_Test.c</code> contains the reference to the implementation method
293   * at twice positions:
294   * <pre class="CCode">
295   * const struct { Mtbl_ImplIfc_Test mtbl; MtblHeadJc end; } mtblImplIfc_Test = {
296    { { sign_Mtbl_ImplIfc_Test//J2C: Head of methodtable.
297      , (struct Size_Mtbl_t*)((2 +2) * sizeof(void*)) //size. NOTE: all elements are standard-pointer-types.
298      }
299    , <b>anotherIfcMethod_ImplIfc_Test_F</b> //anotherIfcMethod
300    , testOverrideAble_ImplIfc_Test_F //testOverrideAble
301    , { { sign_Mtbl_ExpandableDataStruct_Test//J2C: Head of methodtable.
302        , (struct Size_Mtbl_t*)((0 +2) * sizeof(void*)) //size. NOTE: all elements are standard-pointer-types.
303        }
304      , { { sign_Mtbl_ObjectJc//J2C: Head of methodtable.
305          , (struct Size_Mtbl_t*)((5 +2) * sizeof(void*)) //size. NOTE: all elements are standard-pointer-types.
306          }
307        , clone_ObjectJc_F //clone
308        , equals_ObjectJc_F //equals
309        , finalize_ObjectJc_F //finalize
310        , hashCode_ObjectJc_F //hashCode
311        , toString_ImplIfc_Test //toString
312        }
313      }
314    , { { sign_Mtbl_Ifc_Test//J2C: Head of methodtable.
315        , (struct Size_Mtbl_t*)((3 +2) * sizeof(void*)) //size. NOTE: all elements are standard-pointer-types.
316        }
317      , processIfcMethod_i_ImplIfc_Test_F //processIfcMethod
318      , anotherIfcmethod_i_ImplIfc_Test //anotherIfcmethod_i
319      , { { sign_Mtbl_ObjectJc//J2C: Head of methodtable.
320          , (struct Size_Mtbl_t*)((5 +2) * sizeof(void*)) //size. NOTE: all elements are standard-pointer-types.
321          }
322        , clone_ObjectJc_F //clone
323        , equals_ObjectJc_F //equals
324        , finalize_ObjectJc_F //finalize
325        , hashCode_ObjectJc_F //hashCode
326        , toString_ImplIfc_Test //toString
327        }
328      }
329    , { { sign_Mtbl_Ifc2_Test//J2C: Head of methodtable.
330        , (struct Size_Mtbl_t*)((3 +2) * sizeof(void*)) //size. NOTE: all elements are standard-pointer-types.
331        }
332      , processIfcMethod_f_ImplIfc_Test_F //processIfcMethod
333      , testIfc2_f_ImplIfc_Test_F //testIfc2
334      , <b>anotherIfcmethod_f_ImplIfc_Test_F</b> //anotherIfcmethod
335      , { { sign_Mtbl_ObjectJc//J2C: Head of methodtable.
336          , (struct Size_Mtbl_t*)((5 +2) * sizeof(void*)) //size. NOTE: all elements are standard-pointer-types.
337          }
338        , clone_ObjectJc_F //clone
339        , equals_ObjectJc_F //equals
340        , finalize_ObjectJc_F //finalize
341        , hashCode_ObjectJc_F //hashCode
342        , toString_ImplIfc_Test //toString
343        }
344      }
345    }, { signEnd_Mtbl_ObjectJc, null } }; //Mtbl
346   * </pre>
347   * The reference to <code>anotherIfcmethod_i_ImplIfc_Test</code> is accordingly to {@link #anotherIfcmethod(int)}.
348   * @java2c=override-able.
349   */
350  public float anotherIfcmethod(float input)
351  {
352    return 3.5F*input;
353  }
354  
355}