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