javaextensions/satsa/framework/inc/jutils.h
changeset 21 2a9601315dfc
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef JUTILS_H
       
    20 #define JUTILS_H
       
    21 
       
    22 #include <e32base.h>
       
    23 #include <jni.h>
       
    24 
       
    25 #include <badesca.h>
       
    26 #include "javaosheaders.h"
       
    27 
       
    28 //----------------------------------------------------------------------------
       
    29 // RJString takes a Java JNI string and converts it to an
       
    30 // Epoc string. It retains the JNI environment and the string
       
    31 // in order to release the string resources during destruction
       
    32 class RJString : public TPtrC16
       
    33 {
       
    34 public:
       
    35     OS_IMPORT RJString(JNIEnv& aJni, jstring aString);
       
    36     OS_IMPORT ~RJString();
       
    37 
       
    38 private:
       
    39     // Prevent accidental copying because of the shared underlying Java
       
    40     // string
       
    41     RJString(const RJString&);
       
    42     RJString& operator=(const RJString&);
       
    43 
       
    44 private:
       
    45     JNIEnv& iJni;
       
    46     jstring iString;
       
    47 };
       
    48 
       
    49 //----------------------------------------------------------------------------
       
    50 
       
    51 OS_IMPORT jstring CreateJavaString(JNIEnv& aJni, const TDesC16& aString);
       
    52 
       
    53 OS_IMPORT void AddToJavaStringArrayL(JNIEnv& aJni, jobjectArray& aContainer, TInt aPosition, const TDesC& aString);
       
    54 
       
    55 OS_IMPORT jobjectArray CopyToNewJavaStringArrayL(JNIEnv& aJni, const CDesCArray& aNativeArray);
       
    56 
       
    57 
       
    58 
       
    59 class RJArray
       
    60 {
       
    61 public:
       
    62     OS_IMPORT RJArray(JNIEnv& aJni);
       
    63     OS_IMPORT ~RJArray();
       
    64     OS_IMPORT void* GetPrimitiveArrayCriticalLC(jarray aJavaArray, TBool aMutable = EFalse);
       
    65     static void CleanupArrayAccess(TAny* aRJArray);
       
    66 
       
    67 private:
       
    68     void ReleasePrimitiveArrayCritical();
       
    69 
       
    70 private:
       
    71     JNIEnv& iJni;
       
    72     jarray iJavaArray;
       
    73     TUint8* iArrayPtr;
       
    74     TBool iMutable;
       
    75 };
       
    76 
       
    77 
       
    78 
       
    79 class ArrayUtils
       
    80 {
       
    81 public:
       
    82     OS_IMPORT static TInt CopyToNative(JNIEnv& aJni, jbyteArray aJavaBuffer,
       
    83                                        TInt aOffset, TInt aLength, TDes8& aNativeBuffer);
       
    84     OS_IMPORT static TInt CopyToJava(JNIEnv& aJni, const TDesC8& aNativeBuffer,
       
    85                                      jbyteArray aJavaBuffer, TInt aOffset, TInt aLength);
       
    86     OS_IMPORT static jobjectArray CopyToNewJavaStringArray(JNIEnv& aJni,
       
    87             const RPointerArray<HBufC>& aNativeArray);
       
    88 
       
    89 };
       
    90 
       
    91 
       
    92 
       
    93 //----------------------------------------------------------------------------
       
    94 // Constants that define the date/time '00:00, 1 Jan 1970' when used to create a TTime object
       
    95 const TUint JavaUpperTimeFor1970 = 14474675;
       
    96 const TUint JavaLowerTimeFor1970 = 254771200;
       
    97 
       
    98 //----------------------------------------------------------------------------
       
    99 // Used for converting between a Java jlong value and an Epoc
       
   100 // TTime and vice-versa. The jlong represents the number of
       
   101 // milliseconds since 00:00 1st Jan 1970.
       
   102 class JavaEpocTime
       
   103 {
       
   104 public:
       
   105     OS_IMPORT static TTime CreateEpocTTime(jlong aJavaTime);
       
   106     OS_IMPORT static jlong CreateJavaTime(TTime aEpocTime);
       
   107 };
       
   108 
       
   109 //-----------------------------------------------------------------------------
       
   110 // Creating integer 'handles' from C++ objects for referencing them inside Java
       
   111 // The shift garauntees a positive integer, so object creation native methods
       
   112 // can overload the return value to be a handle or an error code
       
   113 //
       
   114 // Unhanding the integer requires the destination type to be known, so is
       
   115 // implemented as a template function, it should be invoked as
       
   116 //
       
   117 //      CXyz* xyz=JavaUnhand(aHandle);
       
   118 //
       
   119 const TInt KJavaHandleShift=2;
       
   120 
       
   121 inline TInt JavaMakeHandle(const TAny* aObject)
       
   122 {
       
   123     return reinterpret_cast<TUint>(aObject)>>KJavaHandleShift;
       
   124 }
       
   125 
       
   126 template <typename T>
       
   127 inline T* JavaUnhand(TInt aHandle)
       
   128 {
       
   129     return reinterpret_cast<T*>(aHandle<<KJavaHandleShift);
       
   130 }
       
   131 
       
   132 //-----------------------------------------------------------------------------
       
   133 // Provide the handle/unhand pattern for CBase derived classes, to gain the
       
   134 // Handle(), Unhand(), New() and TConstructor members, you should derive from
       
   135 // this class using the following pattern:
       
   136 //
       
   137 //  class CXyz : public CJavaPeer<CXyz>
       
   138 //      {...};
       
   139 //
       
   140 // The TConstructor member allows for more complex factory functions,
       
   141 // providing automatic use of the cleanup stack. Supposing the derived
       
   142 // class has a second phase constructor ConstructL(), the factory function
       
   143 // could be:
       
   144 //
       
   145 //  TInt CXyz::New(...)
       
   146 //      {
       
   147 //      TRAPD(h,TConstructor c;c->ConstructL(...);h=c.GetHandle();)
       
   148 //      return h;
       
   149 //      }
       
   150 //
       
   151 template <class T>
       
   152 class CJavaPeer : public CBase
       
   153 {
       
   154 protected:
       
   155     class TConstructor
       
   156     {
       
   157     public:
       
   158         inline TConstructor(T* aObject)
       
   159                 :iObject(aObject)
       
   160         {
       
   161             CleanupStack::PushL(aObject);
       
   162         }
       
   163         inline TConstructor()
       
   164                 :iObject(new(ELeave) T)
       
   165         {
       
   166             CleanupStack::PushL(iObject);
       
   167         }
       
   168         inline T* operator->() const
       
   169         {
       
   170             return static_cast<T*>(iObject);
       
   171         }
       
   172         inline TInt GetHandle()
       
   173         {
       
   174             CleanupStack::Pop();
       
   175             return JavaMakeHandle(iObject);
       
   176         }
       
   177     private:
       
   178         CBase* iObject;
       
   179     };
       
   180 public:
       
   181     inline TInt Handle() const
       
   182     {
       
   183         return JavaMakeHandle(this);
       
   184     }
       
   185     inline static T& Unhand(TInt aHandle)
       
   186     {
       
   187         return *JavaUnhand<T>(aHandle);
       
   188     }
       
   189     static TInt New()
       
   190     {
       
   191         T* self=new T;
       
   192         return self ? self->Handle() : KErrNoMemory;
       
   193     }
       
   194 };
       
   195 
       
   196 #endif // JUTILS_H