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
026/**This is an example for a simple class without interface, but override-able methods.
027 * It based on ObjectJc, because the overriding of methods needs this base class.
028 * The <code>struct</code> in C is defined in the following form:
029 * <pre class="CCode">
030typedef struct SimpleClass_Test_t
031{ 
032  union { ObjectJc object; } base; 
033  int32 x1;
034} SimpleClass_Test_s;
035 * </pre>
036 * It contains the base data of <code>ObjectJc</code> wrapped with the <code>union</code>,
037 * because the access to the <code>ObjectJc</code> is written anyway in form <code>ref.base.object</code>.
038 * The <code>struct</code> contains the data. 
039 * <br><br>
040 * There are two additional defines for all classes in C:
041 * <pre class="CCode">
042typedef struct SimpleClass_Test_Y_t { ObjectArrayJc head; SimpleClass_Test_s data[50]; } SimpleClass_Test_Y;
043
044extern_C struct ClassJc_t const reflection_SimpleClass_Test_s;
045 * </pre>
046 * <ul>
047 * <li>The first line defines a array structure. It is ready to use always. The number of data is a placeholder.
048 * The structure type is used as a reference anytime. Than the really number of elements in the array
049 * depends from the memory allocation size and it is stored in the head data.  
050 * <li>The second line declares the existence of reflection for the class in C.
051 * </ul>
052 * Methods are defined as prototypes in headerfile:
053 * <pre class="CCode">
054METHOD_C struct SimpleClass_Test_t* ctorO_SimpleClass_Test(ObjectJc* othis, ThCxt* _thCxt);
055
056typedef int32 MT_addValue_SimpleClass_Test(SimpleClass_Test_s* ythis, int32 value, ThCxt* _thCxt);
057
058METHOD_C int32 addValue_SimpleClass_Test_F(SimpleClass_Test_s* ythis, int32 value, ThCxt* _thCxt);
059
060METHOD_C int32 addValue_SimpleClass_Test(SimpleClass_Test_s* ythis, int32 value, ThCxt* _thCxt);
061 * </pre>
062 * <ul>
063 * <li>The first line is the constructor definition, auto generated.
064 * <li>The second line is the method type definition as a so named <i>C-function pointer type</i>.
065 *   It is used in the method table.
066 * <li>The third line declares the final version of the method, 
067 *   whereas the forth line declares that method definition, which executes a dynamic linked call.
068 * </ul>
069 * The header file contains at last the method table type definition:
070 * <pre class="CCode">
071extern const char sign_Mtbl_SimpleClass_Test[]; //marker for methodTable check
072typedef struct Mtbl_SimpleClass_Test_t
073{ MtblHeadJc head;
074  MT_addValue_SimpleClass_Test* addValue;
075  Mtbl_ObjectJc ObjectJc;
076} Mtbl_SimpleClass_Test;
077 * </pre>
078 * See {@link org.vishia.java2C.Docu.D_SuperClassesAndInterfaces#D8_gen_MethodTable()}.  
079 */
080public class SimpleClass
081{
082
083        /**Any reference. It may be used in a derived class. 
084         * But because that, a finalize method is generated to clean the reference. */
085        Object anyRef;
086        
087  /**A value stored in the class' instance. */
088  int x1 = 5;
089  
090  /**Parametrized constructor to set an initial value. 
091   * @param value Intial value.
092   */
093  SimpleClass(int value)
094  {
095        x1 = value;
096  }
097  
098  /**Empty constructor: The variable x1 keeps its default value. */
099  SimpleClass(){}
100  
101  /**Add something. This method is able to override.
102   * @param value Value to add.
103   * @return The new value.
104   */
105  int addValue(int value)
106  { x1 += value;
107    return x1;
108  }
109  
110  /**Returns the current value. */
111  int getValue(){ return x1; }
112}