// -*- c++ -*-

/*
 * Jakelib2 - General purpose C++ library
 * Copyright (C) 2001 Florian Wolff  (florian@donuz.de)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: Array.h,v 1.30 2003/08/28 17:05:50 florian Exp $
 */

#ifndef INCLUDE_JAKELIB2_LANG_ARRAY_H
# define INCLUDE_JAKELIB2_LANG_ARRAY_H

namespace jakelib {
  namespace lang {

    /**
     * This is the base class of all types of fixed-size arrays.
     * Array classes are created by using a set of macros. This is
     * done to avoid using c++ templates.
     *
     * There are array classes for 1-dimensional arrays for primitive
     * data types. These are called \c jints, \c jlongs, \c jchars,
     * \c jbooleans, \c jshorts, \c jdoubles, \c jfloats and
     * \c jbytes.
     *
     * 1-dimensional array classes are generated for most Object classes
     * as well, like \c Strings.
     *
     * <b>Example:</b>
     *  <code><pre>
     *   jints *arr = new jints(10);
     *   arr->set(5, 88273);
     *   jint x = arr->get(5);
     *   jint l = arr->length();
     *
     *   arr->destroy();</pre></code>
     *
     * @class Array
     * @version $Id: Array.h,v 1.30 2003/08/28 17:05:50 florian Exp $
     * @author Florian 'Overflo' Wolff (florian@donuz.de)
     * @author Alex Fabijanic (FabijanicA@nucorsteel.com)
     */
    class Array : public jakelib::lang::Object
    {
    public:

      /**
       * Returns the size (number of elements) of this array.
       */
      jint length();

      /**
       * Returns the size in bytes of one array element.
       */
      virtual jint sizeOfElement() = 0;

      /**
       * @return \c true if member at position idx is within array limits
       *         else \c false
       */
      jboolean inLimits(jint idx);
      
      /**
       * Frees the allocated memory and sets size to null.
       * Don't try to access array elements after calling \c destroy()!
       * \c destroy() is called by the destructor.
       */
      virtual void destroy() = 0;

      /**
       * Returns the raw data pointer.
       */
      virtual void* getArray() = 0;

    protected:

      /** The size (number of elements) of this array. */
      jint size;

      /**
       * @exception ArrayIndexOutOfBoundsException If \c idx is out of range
       *            (< 0 or >= \c length() ).
       */
      void throwIOBExc(jint idx);

      /**
       * The purpose of this function is to resize array to 0 elements and 
       * throw exception when GC_(RE)ALLOC fails to (re)allocate memory.
       * @exception MemoryException
       */
      void throwMemExc(jint sze);

      /**
       * The purpose of this function is to throw NullPointerException 
       * when one of the constructors is called and null pointer passed 
       * as argument.
       * @exception NullPointerException
       */
      void throwNPtrExc();

      virtual void memAlloc(jint newsize) = 0;
    };
  }
}


/**
 * @brief Macro <tt>JAKELIB2_DECLARE_ARRAY(T)</tt> will expand to a class
 * array declaration with 's' appended to original class name.
 * @param T class name
 * @version $Id: Array.h,v 1.30 2003/08/28 17:05:50 florian Exp $
 * @author Florian 'Overflo' Wolff (florian@donuz.de)
 * @author Alex Fabijanic (FabijanicA@nucorsteel.com)
 */
#define JAKELIB_DECLARE_ARRAY(T)              \
class T##s : public jakelib::lang::Array {    \
 public:                                      \
    T##s(jint newsize);                       \
    T##s(const T##s* array);                  \
    T##s(jint newsize, T** arr);              \
   ~T##s();                                   \
    jint sizeOfElement();                     \
    T* get(jint idx);                         \
    void* getArray() {return data;}           \
    jboolean set(jint idx, T* obj);           \
    void destroy();                           \
 private:                                     \
    T** data;                                 \
    void memAlloc(jint newsize);              \
    JAKELIB_DECLARE_CLASS(T##s);              \
};   

/**
 * @brief Expands to size constructor body - creates an empty array of size 'size'.
 */
#define JAKELIB_ARRAY_SIZE_CONSTRUCTOR {                                \
                                         memAlloc(newsize);             \
                                       }

/**
 * @brief Expands to pointer constructor body - creates a copy of array pointed to by obj.
 */
#define JAKELIB_ARRAY_PTR_CONSTRUCTOR {                                     \
                                        if (obj != null) {                  \
                                          memAlloc(obj->size);              \
                                          for(jint i=0;i<this->size;i++)    \
                                            this->data[i]=obj->data[i];     \
                                        }                                   \
                                        else {                              \
                                          throwNPtrExc();                   \
                                        }                                   \
                                      }

/**
 * @brief Expands to size/array constructor body.
 * Creates an array of size 'newsize' and fills it with elements of arr.
 */
#define JAKELIB_ARRAY_ARRAY_CONSTRUCTOR {                                      \
                                          if (arr != null) {                   \
                                           memAlloc(newsize);                  \
                                           for (jint i=0; i < this->size; i++) \
                                             this->data[i]=arr[i];             \
                                          }                                    \
                                          else {                               \
                                            throwNPtrExc();                    \
                                          }                                    \
                                        }
                                                                                                                                                                                          
/**
 * @brief Expands to destructor body.
 */
#define JAKELIB_ARRAY_DESTRUCTOR { destroy(); }

/**
 * @brief Expands to sizeOfElement() member function body
 * @return the size (in bytes) of the data contained in the array element.
 */
#define JAKELIB_ARRAY_SIZEOF_ELEMENT(T) {                                 \
                                          return sizeof(T*);              \
                                        }

/**
 * @brief Expands to get(jint idx) member function body - returns  
 * @return the array member at position idx 
 */
#define JAKELIB_ARRAY_GET {                                \
                            if (!inLimits(idx))            \
                               throwIOBExc(idx);           \
                            return this->data[idx];        \
                          }

/**
 * @brief Expands to set(jint idx, T* obj) member function body - assigns obj
 * to the array at position idx.  
 * @return <tt>true</tt> if idx in limits
 */
#define JAKELIB_ARRAY_SET {                                   \
                            if (!inLimits(idx))               \
                              throwIOBExc(idx);               \
                            this->data[idx] = obj;            \
                            return true;                      \
                          }

/**
 * @brief Expands to destroy() member function body - free memory, set size to 0
 */
#define JAKELIB_ARRAY_DESTROY {                                    \
                                if (this->data!=null) {            \
                                  GC_FREE(data);                   \
                                  this->size = 0;                  \
                                  this->data = null;               \
                                }                                  \
                              }

/**
 * @brief Expands to memAlloc(jint size) member function body.
 * The purpose of this function is to allocate memory for array data storage
 * and asign corresponding value to size member variable.
 * @return <tt>true</tt> idf successful.
 * @exception MemoryException.
 */
#define JAKELIB_ARRAY_MEM_ALLOC(T) {                                                            \
                                     jint nBytes = sizeof(T*) * newsize;                        \
                                     if ((this->data = (T**) GC_MALLOC(nBytes)) == null)        \
                                       throwMemExc(nBytes);                                     \
                                     this->size = newsize;                                      \
                                   }

/**
 * @brief Macro <tt>JAKELIB_IMPLEMENT_ARRAY(T)</tt> will expand to a class
 * array implementation with 's' appended to original class name.
 *
 * @param T class name
 */
#define JAKELIB_IMPLEMENT_ARRAY(T)                                               \
         T##s::T##s(jint newsize)              JAKELIB_ARRAY_SIZE_CONSTRUCTOR    \
         T##s::T##s(const T##s* obj)           JAKELIB_ARRAY_PTR_CONSTRUCTOR     \
         T##s::T##s(jint newsize, T** arr)     JAKELIB_ARRAY_ARRAY_CONSTRUCTOR   \
         T##s::~T##s()                         JAKELIB_ARRAY_DESTRUCTOR          \
jint     T##s::sizeOfElement()                 JAKELIB_ARRAY_SIZEOF_ELEMENT(T)   \
T*       T##s::get(jint idx)                   JAKELIB_ARRAY_GET                 \
jboolean T##s::set(jint idx, T* obj)           JAKELIB_ARRAY_SET                 \
void     T##s::destroy()                       JAKELIB_ARRAY_DESTROY             \
void     T##s::memAlloc(jint newsize)          JAKELIB_ARRAY_MEM_ALLOC(T)        \
JAKELIB_IMPLEMENT_ARRAYCLASS(T);

/**
 * @brief Macro <tt>JAKELIB_DECLARE_PRIMITIVE_ARRAY(T)</tt> will expand to a 
 * primitive data type array declaration with 's' appended to original type name.
 * This is a variation of JAKELIB_DECLARE_ARRAY(T) for primitive types.
 * The difference between arrays of primitive types and arrays of objects is
 * that primitive types are held within array as values while objects are held
 * as pointers. Hence the need for separate macros when  declaring/implementing 
 * primitive type arrays. 
 * @param T class name
 */

#define JAKELIB_DECLARE_PRIMITIVE_ARRAY(T)   \
class T##s : public jakelib::lang::Array {   \
 public:                                     \
    T##s(jint newsize);                      \
    T##s(const T##s* obj);                   \
    T##s(jint newsize, T* arr);              \
   ~T##s();                                  \
    jint sizeOfElement();                    \
    T get(jint idx);                         \
    void* getArray() {return data;}          \
    jboolean set(jint idx, T value);         \
    void destroy();                          \
 private:                                    \
    T* data;                                 \
    void memAlloc(jint newsize);             \
    JAKELIB_DECLARE_CLASS(T##s);             \
};

/**
 * @brief Expands to sizeOfElement() member function body
 * @return the size (in bytes) of the data  contained in the array element.
 */
#define JAKELIB_ARRAY_PRIMITIVE_SIZEOF_ELEMENT(T) {                          \
                                                    return sizeof(T);        \
                                                  }

/**
 * @brief Expands to memAlloc(jint size) member function body.
 * The purpose of this function is to allocate memory for array data storage
 * and asign corresponding value to size member variable.
 * @return <tt>true</tt> idf successful.
 * @exception MemoryException.
 */
#define JAKELIB_ARRAY_PRIMITIVE_MEM_ALLOC(T) {                                                          \
                                               jint nBytes = sizeof(T) * newsize;                       \
                                               if ((this->data = (T*) GC_MALLOC_ATOMIC(nBytes))==null)  \
                                                 throwMemExc(nBytes);                                   \
                                               this->size = newsize;                                    \
                                             } 


/**
 * @brief Macro <tt>JAKELIB_IMPLEMENT_PRIMITIVE_ARRAY(T)</tt> will expand to a 
 * primitive type array implementation with 's' appended to original type name.
 * @param T class name
 */
#define JAKELIB_IMPLEMENT_PRIMITIVE_ARRAY(T)                                            \
         T##s::T##s(jint newsize)             JAKELIB_ARRAY_SIZE_CONSTRUCTOR            \
         T##s::T##s(const T##s* obj)          JAKELIB_ARRAY_PTR_CONSTRUCTOR             \
         T##s::T##s(jint newsize, T* arr)     JAKELIB_ARRAY_ARRAY_CONSTRUCTOR           \
         T##s::~T##s()                        JAKELIB_ARRAY_DESTRUCTOR                  \
jint     T##s::sizeOfElement()                JAKELIB_ARRAY_PRIMITIVE_SIZEOF_ELEMENT(T) \
T        T##s::get(jint idx)                  JAKELIB_ARRAY_GET                         \
jboolean T##s::set(jint idx, T obj)           JAKELIB_ARRAY_SET                         \
void     T##s::destroy()                      JAKELIB_ARRAY_DESTROY                     \
void     T##s::memAlloc(jint newsize)         JAKELIB_ARRAY_PRIMITIVE_MEM_ALLOC(T)      \
JAKELIB_IMPLEMENT_PRIMITIVE_ARRAYCLASS(T);


//
// Declare arrays of primitive types:
//
JAKELIB_DECLARE_PRIMITIVE_ARRAY(jbyte);
JAKELIB_DECLARE_PRIMITIVE_ARRAY(jint);
JAKELIB_DECLARE_PRIMITIVE_ARRAY(jlong);
JAKELIB_DECLARE_PRIMITIVE_ARRAY(jdouble);
JAKELIB_DECLARE_PRIMITIVE_ARRAY(jfloat);
JAKELIB_DECLARE_PRIMITIVE_ARRAY(jchar);
JAKELIB_DECLARE_PRIMITIVE_ARRAY(jboolean);
JAKELIB_DECLARE_PRIMITIVE_ARRAY(jshort);

namespace jakelib {
  namespace lang {
    JAKELIB_DECLARE_ARRAY(Object);
  }
}

#endif
