You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@etch.apache.org by ve...@apache.org on 2012/06/01 14:49:16 UTC
svn commit: r1345139 - in /incubator/etch/trunk/binding-cpp/runtime: ./
include/common/ src/main/ src/main/common/ src/main/serialization/
src/test/ src/test/common/
Author: veithm
Date: Fri Jun 1 12:49:15 2012
New Revision: 1345139
URL: http://svn.apache.org/viewvc?rev=1345139&view=rev
Log:
ETCH-181 EtchArrayValue Implementation and Native Array Improvements.
Change-Id: Idee8fa3307ba8ec08d6a702336385b9941cd803c
Added:
incubator/etch/trunk/binding-cpp/runtime/include/common/EtchArrayValue.h
incubator/etch/trunk/binding-cpp/runtime/src/main/common/EtchArrayValue.cpp
incubator/etch/trunk/binding-cpp/runtime/src/test/common/EtchArrayValueTest.cpp
Modified:
incubator/etch/trunk/binding-cpp/runtime/CMakeLists.txt
incubator/etch/trunk/binding-cpp/runtime/include/common/EtchNativeArray.h
incubator/etch/trunk/binding-cpp/runtime/include/common/EtchObjectType.h
incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt
incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchHashTableSerializer.cpp
incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchSetSerializer.cpp
incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt
incubator/etch/trunk/binding-cpp/runtime/src/test/common/EtchNativeArrayTest.cpp
Modified: incubator/etch/trunk/binding-cpp/runtime/CMakeLists.txt
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/CMakeLists.txt?rev=1345139&r1=1345138&r2=1345139&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/CMakeLists.txt (original)
+++ incubator/etch/trunk/binding-cpp/runtime/CMakeLists.txt Fri Jun 1 12:49:15 2012
@@ -19,6 +19,15 @@ project (etch-cpp)
message (STATUS "using external libraries ${ETCH_EXTERNAL_DEPENDS}")
+IF (EXISTS "${CMAKE_SOURCE_DIR}/CMakeLists_local.txt")
+INCLUDE ("${CMAKE_SOURCE_DIR}/CMakeLists_local.txt" OPTIONAL)
+ENDIF()
+
+# Set build settings
+IF ("${BUILD_CHECK_MEMORY}" STREQUAL "")
+ SET(BUILD_CHECK_MEMORY FALSE)
+ENDIF()
+
# Etch external
IF (NOT ETCH_EXTERNAL_DEPENDS)
MESSAGE (FATAL_ERROR "ETCH_EXTERNAL_DEPENDS not set")
@@ -33,11 +42,6 @@ SET(GTEST ${ETCH_EXTERNAL_DEPENDS}/gtest
# GMock
SET(GMOCK ${ETCH_EXTERNAL_DEPENDS}/gmock/1.6.0)
-
-# Set build settings
-SET(BUILD_CHECK_MEMORY FALSE)
-
-
#Build external CAPU project (OS Abstraction)
SET(CAPU_PROJECT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/lib/capu")
SET(CAPU_CMAKE_BUILD_DIR "${CAPU_PROJECT_DIR}/build")
Added: incubator/etch/trunk/binding-cpp/runtime/include/common/EtchArrayValue.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/common/EtchArrayValue.h?rev=1345139&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/common/EtchArrayValue.h (added)
+++ incubator/etch/trunk/binding-cpp/runtime/include/common/EtchArrayValue.h Fri Jun 1 12:49:15 2012
@@ -0,0 +1,127 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef __ETCHARRAYVALUE_H__
+#define __ETCHARRAYVALUE_H__
+
+#include "common/EtchObject.h"
+#include "common/EtchNativeArray.h"
+#include "serialization/EtchType.h"
+
+/**
+ * An array of values, where each value is of arbitrary type
+ * chosen from the basic types boolean, byte, short, int,
+ * long, float, double, String, an array of those, the extended
+ * types ArrayValue and StructValue, and specific types supported
+ * by ValueFactory.
+ *
+ * ArrayValue is not protected against concurrent access.
+ */
+class EtchArrayValue : public EtchObject {
+public:
+
+ /**
+ * TypeId for ArrayValue.
+ */
+ static const EtchObjectType TYPE;
+
+ /**
+ * Constructs the ArrayValue.
+ * @param array
+ * @param size
+ * @param dim
+ * @param customStructType
+ * @param typeCode
+ *
+ */
+ EtchArrayValue(EtchObject* array, capu::int32_t size, capu::int8_t typeCode, EtchType* customStructType, capu::int32_t dim);
+
+ /**
+ * Constructs an array value with no type info. This is used perhaps
+ * by extern struct serializers.
+ * @param array
+ * @param size
+ */
+ EtchArrayValue(EtchObject* array, capu::int32_t size);
+
+ virtual ~EtchArrayValue();
+
+ /**
+ * @return the TypeCode for this array value.
+ * For example, if the array is int[][], then
+ * the type would be TypeCode.INT4.
+ */
+ capu::int8_t getTypeCode();
+
+ /**
+ * @return a struct type if a custom type code.
+ */
+ EtchType* getCustomStructType();
+
+ /**
+ * @return the dimsionality of the array.
+ * For example, if the array is int[][], the
+ * dimensionality would be 2.
+ */
+ capu::int32_t getDim();
+
+ /**
+ * @return the capacity of the array.
+ */
+ capu::int32_t getSize();
+
+ /**
+ * @return the number of elements in the array.
+ */
+ capu::int32_t getIndex();
+
+ /**
+ * @return the number of elements in the array.
+ */
+ void setIndex(capu::int32_t val);
+
+
+ /**
+ * @param index
+ * @return the element at the specified index.
+ */
+ capu::int32_t get(capu::uint32_t index, capu::SmartPointer<EtchObject> &result);
+ /**
+ * Adds the value to the end of the array, making more space
+ * available if needed.
+ * @param value
+ */
+ status_t add(capu::SmartPointer<EtchObject> value);
+
+ /**
+ * @return the array value.
+ */
+ EtchNativeArray<capu::SmartPointer<EtchObject> >* getArray();
+
+private:
+ EtchNativeArray<capu::SmartPointer<EtchObject> >* mArray;
+ capu::int8_t mTypeCode;
+ EtchType* mCustomStructType;
+ capu::int32_t mDim;
+ capu::int32_t mAddIndex;
+ capu::int32_t mSize;
+};
+#endif /* ETCHARRAYVALUE_H */
+
Modified: incubator/etch/trunk/binding-cpp/runtime/include/common/EtchNativeArray.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/common/EtchNativeArray.h?rev=1345139&r1=1345138&r2=1345139&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/common/EtchNativeArray.h (original)
+++ incubator/etch/trunk/binding-cpp/runtime/include/common/EtchNativeArray.h Fri Jun 1 12:49:15 2012
@@ -19,7 +19,9 @@
#ifndef __ETCHNATIVEARRAY_H__
#define __ETCHNATIVEARRAY_H__
+#include "capu/util/SmartPointer.h"
#include "common/EtchObject.h"
+#include "common/EtchObjectType.h"
#include "common/EtchError.h"
class Pos {
@@ -86,35 +88,360 @@ public:
capu::int32_t size;
};
-template <class T>
-class EtchArray {
-public:
- capu::int32_t mDim;
- capu::int32_t mLen;
- void* mArray;
-
- EtchArray()
- : mDim(0), mLen(0), mArray(NULL) {
-
- }
+ //internal helper classes
+ template <class T>
+ class EtchArray;
+
+ template <class T>
+ class EtchArrayBase {
+ public:
+
+ /**
+ * Creates a new instance of EtchArrayBase
+ */
+ EtchArrayBase(capu::int32_t length, capu::int32_t dim = 1)
+ : mLength(length), mDim(dim) {
+ }
+
+ virtual ~EtchArrayBase() {
+ }
+
+ /**
+ * Returns the dimension
+ */
+ virtual capu::int32_t getDim() {
+ return mDim;
+ }
+
+ /**
+ * Returns the length
+ */
+ virtual capu::int32_t getLength() {
+ return mLength;
+ }
+
+ /**
+ * Sets an element at the given position
+ */
+ virtual status_t set(Pos pos, capu::int32_t index, const T &data) = 0;
+
+ /**
+ * Sets an array at the given position
+ */
+ virtual status_t set(Pos pos, capu::int32_t index, const T* data, capu::int32_t dataSize, capu::int32_t offset, capu::int32_t length, capu::int32_t *written) = 0;
+
+ /**
+ * Sets the sub array at the given position
+ */
+ virtual status_t set(Pos pos, capu::int32_t index, const capu::SmartPointer<EtchArray<T> > &data) = 0;
+
+ /**
+ * Returns the element at the given position
+ */
+ virtual status_t get(Pos pos, capu::int32_t index, T *result) = 0;
+
+ /**
+ * Returns the array at the given position
+ */
+ virtual status_t get(Pos pos, capu::int32_t index, T* data, capu::int32_t dataSize, capu::int32_t offset, capu::int32_t length, capu::int32_t *read) = 0;
+
+ /**
+ * Returns the subarray at the given position
+ */
+ virtual status_t get(Pos pos, capu::int32_t index, capu::SmartPointer<EtchArray<T> > *result) = 0;
+
+ /**
+ * creates a new nested array at the given position with the given length and dim
+ */
+ virtual status_t createArray(Pos pos, capu::int32_t index, capu::int32_t length, capu::int32_t dim) = 0;
+
+ /**
+ * copies the array into the given new array
+ */
+ virtual status_t copyToArray(capu::SmartPointer<EtchArrayBase<T> > newArray) = 0;
+
+
+ protected:
+ capu::int32_t mLength;
+ capu::int32_t mDim;
+ };
+
+ template <class T>
+ class EtchArrayData : public EtchArrayBase<T> {
+ public:
+
+ /**
+ * Create a new instance of EtchArrayData
+ */
+ EtchArrayData(capu::int32_t length)
+ : EtchArrayBase<T>(length) {
+ mData = new T[EtchArrayBase<T>::mLength];
+ }
+
+ /**
+ * Destructure
+ */
+ virtual ~EtchArrayData() {
+ delete[] mData;
+ }
+
+ /**
+ *@see EtchArrayBase
+ */
+ status_t set(Pos pos, capu::int32_t index, const T &data) {
+ if(pos.size < index || pos.pos[index] > EtchArrayBase<T>::mLength || pos.size-1 != index) {
+ return ETCH_ERANGE;
+ }
+ mData[pos.pos[index]] = data;
+ return ETCH_OK;
+ }
+
+ /**
+ *@see EtchArrayBase
+ */
+ status_t set(Pos pos, capu::int32_t index, const T* data, capu::int32_t dataSize, capu::int32_t offset, capu::int32_t length, capu::int32_t *written) {
+ if(pos.size < index || pos.pos[index] > EtchArrayBase<T>::mLength || pos.size-1 != index || dataSize < offset + length) {
+ return ETCH_ERANGE;
+ }
+ if(data != NULL) {
+ //TODO: replace with memcpy
+ capu::int32_t i;
+ for (i = 0; i < EtchArrayBase<T>::mLength && i < length; i++) {
+ mData[pos.pos[index]+i] = data[offset + i];
+ }
+ if (written != NULL) {
+ *written = i;
+ }
+ } else {
+ return ETCH_EINVAL;
+ }
+ return ETCH_OK;
+ }
+
+ /**
+ *@see EtchArrayBase
+ */
+ status_t set(Pos pos, capu::int32_t index, const capu::SmartPointer<EtchArray<T> > &data) {
+ return ETCH_ERANGE;
+ }
+
+ /**
+ *@see EtchArrayBase
+ */
+ status_t get(Pos pos, capu::int32_t index, T *result) {
+ if(pos.size < index || pos.pos[index] > EtchArrayBase<T>::mLength || pos.size-1 != index) {
+ return ETCH_ERANGE;
+ }
+ if(result != NULL) {
+ *result = mData[pos.pos[index]];
+ } else {
+ return ETCH_EINVAL;
+ }
+ return ETCH_OK;
+ }
+
+ /**
+ *@see EtchArrayBase
+ */
+ status_t get(Pos pos, capu::int32_t index, T* data, capu::int32_t dataSize, capu::int32_t offset, capu::int32_t length, capu::int32_t *read) {
+ if(pos.size < index || pos.pos[index] > EtchArrayBase<T>::mLength || pos.size-1 != index || dataSize < offset + length) {
+ return ETCH_ERANGE;
+ }
+ if(data != NULL) {
+ //TODO: replace with memcpy
+ capu::int32_t i;
+ for (i = 0; i < EtchArrayBase<T>::mLength && i < length; i++) {
+ data[offset + i] = mData[pos.pos[index]+i];
+ }
+ if (read != NULL) {
+ *read = i;
+ }
+ } else {
+ return ETCH_EINVAL;
+ }
+ return ETCH_OK;
+ }
+
+ /**
+ *@see EtchArrayBase
+ */
+ status_t get(Pos pos, capu::int32_t index, capu::SmartPointer<EtchArray<T> > *result) {
+ return ETCH_ERANGE;
+ }
+
+ /**
+ *@see EtchArrayBase
+ */
+ status_t createArray(Pos pos, capu::int32_t index, capu::int32_t length, capu::int32_t dim) {
+ return ETCH_ERANGE;
+ }
+
+ /**
+ *@see EtchArrayBase
+ */
+ status_t copyToArray(capu::SmartPointer<EtchArrayBase<T> > newArray) {
+ //TODO: replace with capu memory abstraction
+ capu::SmartPointer<EtchArrayData<T> > temp = capu::smartpointer_cast<EtchArrayData<T> >(newArray);
+ for(capu::int32_t i = 0; i < EtchArrayBase<T>::mLength; i++) {
+ temp->mData[i] = mData[i];
+ }
+ return ETCH_OK;
+ }
+ private:
+ T* mData;
+ };
+
+ template <class T>
+ class EtchArray : public EtchArrayBase<T> {
+ public:
+
+ /**
+ * Creats a new instance of the EtchArray class
+ */
+ EtchArray(capu::int32_t length, capu::int32_t dim)
+ : EtchArrayBase<T>(length, dim) {
+ mData = new capu::SmartPointer<EtchArrayBase<T> >[EtchArrayBase<T>::mLength];
+ }
+
+ /**
+ * Destructure
+ */
+ virtual ~EtchArray() {
+ delete[] mData;
+ }
+
+ /**
+ *@see EtchArrayBase
+ */
+ status_t set(Pos pos, capu::int32_t index, const T &data) {
+ if(mData[pos.pos[index]].get() != NULL) {
+ return mData[pos.pos[index]]->set(pos, index+1, data);
+ }
+ return ETCH_OK;
+ }
- EtchArray(capu::int32_t dim, capu::int32_t length)
- : mDim(dim), mLen(length), mArray(NULL) {
- if (dim == 1) {
- mArray = new T[length];
- } else {
- mArray = new EtchArray[length];
+ /**
+ *@see EtchArrayBase
+ */
+ status_t set(Pos pos, capu::int32_t index, const T* data, capu::int32_t dataSize, capu::int32_t offset, capu::int32_t length, capu::int32_t *written) {
+ if(pos.size < index || pos.pos[index] > EtchArrayBase<T>::mLength) {
+ return ETCH_ERANGE;
+ }
+
+ if(mData[pos.pos[index]].get() != NULL) {
+ return mData[pos.pos[index]]->set(pos, index+1, data, dataSize, offset, length, written);
+ } else {
+ return ETCH_EINVAL;
+ }
+ return ETCH_OK;
+ }
+
+ /**
+ *@see EtchArrayBase
+ */
+ status_t set(Pos pos, capu::int32_t index, const capu::SmartPointer<EtchArray<T> > &data) {
+ if(pos.size < index || pos.pos[index] > EtchArrayBase<T>::mLength || mData[pos.pos[index]].get() == NULL) {
+ return ETCH_ERANGE;
+ }
+
+ if (pos.size - index == 1) {
+ if (EtchArrayBase<T>::mDim -1 == data->getDim()) { //check if we set a subarray with correct dimension
+ mData[pos.pos[index]] = data;
+ } else {
+ return ETCH_ERANGE;
+ }
+ } else {
+ return mData[pos.pos[index]]->set(pos,index+1,data);
+ }
+
+ return ETCH_OK;
+ }
+
+ /**
+ *@see EtchArrayBase
+ */
+ status_t get(Pos pos, capu::int32_t index, T *result) {
+ if(pos.size < index || pos.pos[index] > EtchArrayBase<T>::mLength) {
+ return ETCH_ERANGE;
+ }
+
+ if(mData[pos.pos[index]].get() != NULL) {
+ return mData[pos.pos[index]]->get(pos, index+1, result);
+ }
+ else {
+ return ETCH_EINVAL;
+ }
+ return ETCH_OK;
}
- }
- EtchArray(capu::int32_t dim, capu::int32_t length, T* array)
- : mDim(dim), mLen(length), mArray(array) {
+ /**
+ *@see EtchArrayBase
+ */
+ status_t get(Pos pos, capu::int32_t index, T* data, capu::int32_t dataSize, capu::int32_t offset, capu::int32_t length, capu::int32_t *read) {
+ if(pos.size < index || pos.pos[index] > EtchArrayBase<T>::mLength) {
+ return ETCH_ERANGE;
+ }
+
+ if(mData[pos.pos[index]].get() != NULL) {
+ return mData[pos.pos[index]]->get(pos, index+1, data, dataSize, offset, length, read);
+ } else {
+ return ETCH_EINVAL;
+ }
+ return ETCH_OK;
+ }
+
+ /**
+ *@see EtchArrayBase
+ */
+ status_t get(Pos pos, capu::int32_t index, capu::SmartPointer<EtchArray<T> > *result) {
+ if(pos.size < index || pos.pos[index] > EtchArrayBase<T>::mLength || mData[pos.pos[index]].get() == NULL) {
+ return ETCH_ERANGE;
+ }
+ if (pos.size - index == 1) {
+ *result = capu::smartpointer_cast<EtchArray<T> >(mData[pos.pos[index]]);
+ } else {
+ return mData[pos.pos[index]]->get(pos,index+1,result);
+ }
+
+ return ETCH_OK;
+ }
+
+ /**
+ *@see EtchArrayBase
+ */
+ status_t createArray(Pos pos, capu::int32_t index, capu::int32_t length, capu::int32_t dim) {
+ if(mData[pos.pos[index]].get() == NULL) {
+ if(dim == 1) {
+ mData[pos.pos[index]] = new EtchArrayData<T>(length);
+ } else {
+ if (pos.size - index == 1) {
+ mData[pos.pos[index]] = new EtchArray<T>(length, dim);
+ }
+ }
+ } else {
+ if (dim == 1 || pos.size - index == 1) {
+ return ETCH_ERANGE;
+ }
+ return mData[pos.pos[index]]->createArray(pos, index+1, length, dim-1);
+ }
+ return ETCH_OK;
+ }
+
+ /**
+ *@see EtchArrayBase
+ */
+ status_t copyToArray(capu::SmartPointer<EtchArrayBase<T> > newArray) {
+ capu::SmartPointer<EtchArray<T> > temp = capu::smartpointer_cast<EtchArray<T> >(newArray);
+ for(capu::int32_t i = 0; i < EtchArrayBase<T>::mLength; i++) {
+ temp->mData[i] = mData[i];
+ }
+ return ETCH_OK;
+ }
+ private:
+ capu::SmartPointer<EtchArrayBase<T> > *mData;
+ };
- }
- virtual ~EtchArray() {
- }
-};
template<class T>
class EtchNativeArray :
@@ -129,12 +456,12 @@ public:
/**
* Constructs a EtchNativeArray object.
*/
- EtchNativeArray(capu::int32_t dim, capu::int32_t length);
+ EtchNativeArray(capu::int32_t length, capu::int32_t dim = 1);
/**
* Constructs a EtchNativeArray object.
*/
- EtchNativeArray(capu::int32_t dim, capu::int32_t length, T* array);
+ EtchNativeArray(capu::int32_t length, capu::int32_t dim, T* array);
/**
* Destructor for Etch Nativearray.
@@ -149,7 +476,14 @@ public:
*/
status_t get(Pos pos, T *result);
- status_t get(Pos pos, capu::int32_t length, T* data, capu::int32_t offset);
+ status_t get(Pos pos, T* data, capu::int32_t dataSize, capu::int32_t offset, capu::int32_t length, capu::int32_t *read = NULL);
+
+ /**
+ * gets the array stored at the given position
+ * returns ETCH_ERANGE, if out of bounds.
+ * returns ETCH_OK otherwise
+ */
+ status_t get(Pos pos, EtchNativeArray *&subarray);
/**
* sets the EtchObject at index i in result
@@ -157,9 +491,18 @@ public:
* returns ETCH_ERANGE, if out of bounds.
* returns ETCH_OK otherwise
*/
- status_t set(Pos pos, T result);
+ status_t set(Pos pos, const T &data);
+
+ status_t set(Pos pos, const T* data, capu::int32_t dataSize, capu::int32_t offset, capu::int32_t length, capu::int32_t *written = NULL);
+
+ /**
+ * sets the array at the given position
+ * returns ETCH_ERANGE, if out of bounds.
+ * returns ETCH_OK otherwise
+ */
+ status_t set(Pos pos, EtchNativeArray *subarray);
- status_t set(Pos pos, capu::int32_t length, T* data, capu::int32_t offset);
+ //status_t set(Pos pos, capu::int32_t length, T* data, capu::int32_t offset);
/**
* Returns the length of array
@@ -167,21 +510,36 @@ public:
capu::int32_t getLength();
/**
- * Returns the length of array
+ * Returns the dim of array
*/
capu::int32_t getDim();
/**
- * Returns the pointer to the beginning of array
+ * Creates a new nested array
+ * @pos of the neseted array
+ * @length length of the nested array
*/
- T* getArray();
+ status_t createArray(Pos pos, capu::int32_t length);
- status_t createArray(Pos pos, capu::int32_t dims, capu::int32_t length);
+ /**
+ * Copies the array into the given array
+ * @pos of the neseted array
+ * @length length of the nested array
+ */
+ status_t copyToArray(EtchNativeArray *newArray);
private:
- void deallocate(EtchArray<T> *array);
- EtchArray<T> mArray;
+
+private:
+ capu::SmartPointer<EtchArrayBase<T> > mData;
+
+ /**
+ * private constructor
+ * creates a new native array from an existing subarray
+ */
+ EtchNativeArray(capu::SmartPointer<EtchArray<T> > *array);
+
};
template<class T>
@@ -191,142 +549,89 @@ const EtchObjectType* EtchNativeArray<T>
}
template<class T>
-EtchNativeArray<T>::EtchNativeArray(capu::int32_t dim, capu::int32_t length)
-: EtchObject(EtchNativeArray<T>::TYPE()), mArray(dim, length) {
-
+EtchNativeArray<T>::EtchNativeArray(capu::int32_t length, capu::int32_t dim)
+ : EtchObject(EtchNativeArray::TYPE()) {
+ if (dim == 1) {
+ mData = new EtchArrayData<T>(length);
+ } else {
+ mData = new EtchArray<T>(length, dim);
+ }
}
template<class T>
-EtchNativeArray<T>::EtchNativeArray(capu::int32_t dim, capu::int32_t length, T* array)
-: EtchObject(EtchNativeArray<T>::TYPE()), mArray(dim, length, array) {
-
+EtchNativeArray<T>::EtchNativeArray(capu::SmartPointer<EtchArray<T> > *array)
+: EtchObject(EtchNativeArray::TYPE()){
+ mData = capu::smartpointer_cast<EtchArrayBase<T> > (*array);
}
template<class T>
EtchNativeArray<T>::~EtchNativeArray() {
- deallocate(&mArray);
}
template<class T>
-T * EtchNativeArray<T>::getArray() {
- return mArray;
+status_t EtchNativeArray<T>::get(Pos pos, T *result) {
+ return mData->get(pos, 0, result);
}
template<class T>
-status_t EtchNativeArray<T>::get(Pos pos, T * result) {
- if (mArray.mArray == NULL || result == NULL || (pos.size != mArray.mDim)) {
- return ETCH_EINVAL;
- }
- capu::int32_t i = 0;
- void * array = mArray.mArray;
- for (i = 1; i < mArray.mDim; i++) {
- array = ((EtchArray<T>*) array)[pos.pos[i]].mArray;
- }
- *result = ((T*) array)[pos.pos[i - 1]];
- return ETCH_OK;
+status_t EtchNativeArray<T>::get(Pos pos, T* data, capu::int32_t dataSize, capu::int32_t offset, capu::int32_t length, capu::int32_t *read) {
+ return mData->get(pos, 0, data, dataSize, offset, length, read);
}
-template<class T >
-status_t EtchNativeArray<T>::set(Pos pos, T result) {
- if (mArray.mArray == NULL || (pos.size != mArray.mDim)) {
- return ETCH_EINVAL;
- }
- capu::int32_t i = 0;
- void * array = mArray.mArray;
- for (i = 1; i < mArray.mDim; i++) {
- array = ((EtchArray<T>*) array)[pos.pos[i - 1]].mArray;
+template <class T>
+status_t EtchNativeArray<T>::get(Pos pos, EtchNativeArray<T> *&subarray) {
+ capu::SmartPointer<EtchArray<T> > temp = NULL;
+ status_t res = mData->get(pos, 0, &temp);
+ if (res != ETCH_OK) {
+ return res;
}
- ((T*) array)[pos.pos[i - 1]] = result;
+
+ subarray = new EtchNativeArray<T>(&temp);
return ETCH_OK;
}
template<class T>
-status_t EtchNativeArray<T>::set(Pos pos, capu::int32_t length, T* data, capu::int32_t offset) {
- if (mArray.mArray == NULL || (pos.size != mArray.mDim) || data == NULL) {
- return ETCH_EINVAL;
- }
- if (offset >= length) {
- return ETCH_ERANGE;
- }
- capu::int32_t i = 0;
- void * array = mArray.mArray;
- for (i = 1; i < mArray.mDim; i++) {
- array = ((EtchArray<T>*) array)[pos.pos[i - 1]].mArray;
- }
+status_t EtchNativeArray<T>::set(Pos pos, const T &data) {
+ return mData->set(pos, 0, data);
+}
- for (capu::int32_t z = offset; z < length; z++) {
- ((T*) array)[pos.pos[i - 1] + z] = data[z];
- }
- return ETCH_OK;
+template<class T>
+status_t EtchNativeArray<T>::set(Pos pos, const T* data, capu::int32_t dataSize, capu::int32_t offset, capu::int32_t length, capu::int32_t *written) {
+ return mData->set(pos, 0, data, dataSize, offset, length, written);
}
template<class T>
-status_t EtchNativeArray<T>::get(Pos pos, capu::int32_t length, T* data, capu::int32_t offset) {
- if (mArray.mArray == NULL || (pos.size != mArray.mDim) || data == NULL) {
- return ETCH_EINVAL;
- }
- if (offset >= length) {
+status_t EtchNativeArray<T>::set(Pos pos, EtchNativeArray<T> *subarray) {
+ if (subarray == NULL) {
return ETCH_ERANGE;
}
- capu::int32_t i = 0;
- void * array = mArray.mArray;
- for (i = 1; i < mArray.mDim; i++) {
- array = ((EtchArray<T>*) array)[pos.pos[i - 1]].mArray;
- }
- for (capu::int32_t z = offset; z < length; z++) {
- data[z] = ((T*) array)[pos.pos[i - 1] + z];
- }
- return ETCH_OK;
+ capu::SmartPointer<EtchArray<T> > temp = capu::smartpointer_cast<EtchArray<T> >(subarray->mData);
+ return mData->set(pos, 0, temp);
}
+
template<class T>
capu::int32_t EtchNativeArray<T>::getLength() {
- return mArray.mLen;
+ return mData->getLength();
}
template<class T>
-status_t EtchNativeArray<T>::createArray(Pos pos, capu::int32_t dim, capu::int32_t length) {
- if (mArray.mArray == NULL || (pos.size >= mArray.mDim)) {
- return ETCH_EINVAL;
- }
- capu::int32_t i = 0;
- void * array = mArray.mArray;
- for (i = 0; i < pos.size - 1; i++) {
- array = ((EtchArray<T>*) array)[pos.pos[i]].mArray;
- }
-
- ((EtchArray<T>*) array)[pos.pos[i]].mDim = dim;
- ((EtchArray<T>*) array)[pos.pos[i]].mLen = length;
-
- if (dim > 1) {
- ((EtchArray<T>*) array)[pos.pos[i]].mArray = new EtchArray<T>[length];
- } else if (dim == 1) {
- ((EtchArray<T>*) array)[pos.pos[i]].mArray = new T[length];
- }
- return ETCH_OK;
+capu::int32_t EtchNativeArray<T>::getDim() {
+ return mData->getDim();
}
template<class T>
-void EtchNativeArray<T>::deallocate(EtchArray<T> *array) {
-
- if (array == NULL)
- return;
- else if (array->mDim == 1)
- delete [] (T*) array->mArray;
- else if (array->mDim < 0)
- return;
- else {
- capu::int32_t i;
- for (i = 0; i < array->mLen; i++) {
- deallocate(&((EtchArray<T>*) array->mArray)[i]);
- }
- delete [] (EtchArray<T>*) array->mArray;
- }
+status_t EtchNativeArray<T>::createArray(Pos pos, capu::int32_t length) {
+ return mData->createArray(pos, 0, length, mData->getDim()-1);
}
template<class T>
-capu::int32_t EtchNativeArray<T>::getDim() {
- return mArray.mDim;
-}
+status_t EtchNativeArray<T>::copyToArray(EtchNativeArray *newArray) {
+ if (newArray == NULL) {
+ return ETCH_EINVAL;
+ }
+ return mData->copyToArray(newArray->mData);
+
+}
#endif //__ETCHNATIVEARRAY_H__
Modified: incubator/etch/trunk/binding-cpp/runtime/include/common/EtchObjectType.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/common/EtchObjectType.h?rev=1345139&r1=1345138&r2=1345139&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/common/EtchObjectType.h (original)
+++ incubator/etch/trunk/binding-cpp/runtime/include/common/EtchObjectType.h Fri Jun 1 12:49:15 2012
@@ -61,6 +61,7 @@ enum EtchObjectTypeIds {
EOTID_EXCEPTION,
EOTID_AUTH_EXCEPTION,
EOTID_RUNTIME_EXCEPTION,
+ EOTID_ARRAY_VALUE,
EOTID_NATIVE_INT8,
EOTID_NATIVE_INT16,
@@ -150,7 +151,7 @@ private:
return T::TYPE();
}
};
-
+
template <class T>
struct __Wrapper__<capu::SmartPointer<T> > {
Modified: incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt?rev=1345139&r1=1345138&r2=1345139&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt (original)
+++ incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt Fri Jun 1 12:49:15 2012
@@ -62,6 +62,7 @@ SET(MAIN_INCLUDES
${PROJECT_SOURCE_DIR}/include/common/EtchAuthenticationException.h
${PROJECT_SOURCE_DIR}/include/common/EtchRuntimeException.h
${PROJECT_SOURCE_DIR}/include/common/EtchDate.h
+ ${PROJECT_SOURCE_DIR}/include/common/EtchArrayValue.h
${PROJECT_SOURCE_DIR}/include/transport/EtchTcpConnection.h
${PROJECT_SOURCE_DIR}/include/transport/EtchTcpListener.h
${PROJECT_SOURCE_DIR}/include/transport/EtchTcpOption.h
@@ -117,6 +118,7 @@ SET(MAIN_SOURCES
common/EtchAuthenticationException.cpp
common/EtchRuntimeException.cpp
common/EtchDate.cpp
+ common/EtchArrayValue.cpp
util/EtchURL.cpp
util/EtchResources.cpp
transport/EtchFlexBuffer.cpp
Added: incubator/etch/trunk/binding-cpp/runtime/src/main/common/EtchArrayValue.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/main/common/EtchArrayValue.cpp?rev=1345139&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/main/common/EtchArrayValue.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/src/main/common/EtchArrayValue.cpp Fri Jun 1 12:49:15 2012
@@ -0,0 +1,102 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "common/EtchArrayValue.h"
+
+const EtchObjectType EtchArrayValue::TYPE(EOTID_ARRAY_VALUE, NULL);
+
+EtchArrayValue::EtchArrayValue(EtchObject* array, capu::int32_t size, capu::int8_t typeCode, EtchType* customStructType, capu::int32_t dim)
+: EtchObject(&EtchArrayValue::TYPE), mArray((EtchNativeArray<capu::SmartPointer<EtchObject> > *)array), mTypeCode(typeCode),
+mCustomStructType(customStructType), mDim(dim), mAddIndex(size), mSize(size) {
+
+}
+
+EtchArrayValue::EtchArrayValue(EtchObject* array, capu::int32_t size)
+: EtchObject(&EtchArrayValue::TYPE), mArray((EtchNativeArray<capu::SmartPointer<EtchObject> > *)array), mTypeCode(0),
+mCustomStructType(NULL), mDim(1), mAddIndex(size), mSize(size) {
+
+}
+
+EtchArrayValue::~EtchArrayValue() {
+
+}
+
+capu::int32_t EtchArrayValue::getDim() {
+ return mDim;
+}
+
+capu::int32_t EtchArrayValue::getSize() {
+ return mSize;
+}
+
+capu::int8_t EtchArrayValue::getTypeCode() {
+ return mTypeCode;
+}
+
+EtchNativeArray<capu::SmartPointer<EtchObject> >* EtchArrayValue::getArray() {
+ return mArray;
+}
+
+capu::int32_t EtchArrayValue::getIndex() {
+ return mAddIndex;
+}
+
+void EtchArrayValue::setIndex(capu::int32_t val) {
+ mAddIndex = val;
+}
+
+EtchType * EtchArrayValue::getCustomStructType() {
+ return mCustomStructType;
+}
+
+status_t EtchArrayValue::get(capu::uint32_t index, capu::SmartPointer<EtchObject>& result) {
+ if ((index >= getSize()) || (mArray == NULL))
+ return ETCH_EINVAL;
+ else {
+ mArray->get(index, &result);
+ return ETCH_OK;
+ }
+
+}
+
+status_t EtchArrayValue::add(capu::SmartPointer<EtchObject> value) {
+ if (value.get() == NULL) {
+ return ETCH_EINVAL;
+ }
+
+ //check if we have to resize the array
+ capu::int32_t n = mSize;
+ if (mAddIndex >= n) {
+ if (n == 0) {
+ n = 8;
+ } else {
+ n *= 2;
+ }
+ capu::SmartPointer<EtchObject> tmp;
+ EtchNativeArray<capu::SmartPointer<EtchObject> > *narray = new EtchNativeArray<capu::SmartPointer<EtchObject> >(n, mDim);
+ mArray->copyToArray(narray);
+ delete mArray;
+ mArray = narray;
+ mSize = n;
+ }
+ mArray->set(mAddIndex++, value);
+
+ return ETCH_OK;
+}
Modified: incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchHashTableSerializer.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchHashTableSerializer.cpp?rev=1345139&r1=1345138&r2=1345139&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchHashTableSerializer.cpp (original)
+++ incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchHashTableSerializer.cpp Fri Jun 1 12:49:15 2012
@@ -40,7 +40,7 @@ status_t EtchHashTableSerializer::export
capu::SmartPointer<EtchHashTable<capu::SmartPointer<EtchObject>, capu::SmartPointer<EtchObject> > > table = capu::smartpointer_cast<EtchHashTable<capu::SmartPointer<EtchObject>, capu::SmartPointer<EtchObject> > > (value);
EtchHashTable<capu::SmartPointer<EtchObject> , capu::SmartPointer<EtchObject> >::Iterator it = table->begin();
- capu::SmartPointer<EtchNativeArray<capu::SmartPointer<EtchObject> > > keysAndValuesArray = new EtchNativeArray<capu::SmartPointer<EtchObject> > (1, table->count()*2);
+ capu::SmartPointer<EtchNativeArray<capu::SmartPointer<EtchObject> > > keysAndValuesArray = new EtchNativeArray<capu::SmartPointer<EtchObject> > (table->count()*2);
capu::int32_t i = 0;
EtchHashTable<capu::SmartPointer<EtchObject> , capu::SmartPointer<EtchObject> >::Pair ptr;
Modified: incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchSetSerializer.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchSetSerializer.cpp?rev=1345139&r1=1345138&r2=1345139&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchSetSerializer.cpp (original)
+++ incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchSetSerializer.cpp Fri Jun 1 12:49:15 2012
@@ -38,7 +38,7 @@ status_t EtchSetSerializer::exportValue(
capu::SmartPointer<EtchHashSet<capu::SmartPointer<EtchObject> > > set = capu::smartpointer_cast<EtchHashSet<capu::SmartPointer<EtchObject> > > (value);
EtchHashSet<capu::SmartPointer<EtchObject> >::Iterator it = set->begin();
- capu::SmartPointer<EtchNativeArray<capu::SmartPointer<EtchObject> > > array = new EtchNativeArray<capu::SmartPointer<EtchObject> > (1, set->count());
+ capu::SmartPointer<EtchNativeArray<capu::SmartPointer<EtchObject> > > array = new EtchNativeArray<capu::SmartPointer<EtchObject> > (set->count());
capu::int32_t i = 0;
capu::SmartPointer<EtchObject> ptr;
Modified: incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt?rev=1345139&r1=1345138&r2=1345139&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt (original)
+++ incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt Fri Jun 1 12:49:15 2012
@@ -46,6 +46,7 @@ add_executable (etch-cpp-test
common/EtchAuthenticationExceptionTest.cpp
common/EtchRuntimeExceptionTest.cpp
common/EtchDateTest.cpp
+ common/EtchArrayValueTest.cpp
util/EtchURLTest.cpp
util/EtchResourcesTest.cpp
transport/EtchFlexBufferTest.cpp
Added: incubator/etch/trunk/binding-cpp/runtime/src/test/common/EtchArrayValueTest.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/test/common/EtchArrayValueTest.cpp?rev=1345139&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/test/common/EtchArrayValueTest.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/src/test/common/EtchArrayValueTest.cpp Fri Jun 1 12:49:15 2012
@@ -0,0 +1,156 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "gtest/gtest.h"
+#include "common/EtchArrayValue.h"
+#include "common/EtchInt32.h"
+#include "common/EtchDouble.h"
+#include "common/EtchFloat.h"
+#include "common/EtchBool.h"
+#include "capu/util/SmartPointer.h"
+
+TEST(EtchArrayValueTest, ConstructorTest1) {
+ EtchNativeArray<capu::SmartPointer<EtchInt32> > *array = new EtchNativeArray<capu::SmartPointer<EtchInt32> >(0);
+ EtchArrayValue *av = new EtchArrayValue(array, 0);
+ EXPECT_TRUE(av->getTypeCode() == 0);
+ EXPECT_TRUE(av->getCustomStructType() == NULL);
+ EXPECT_TRUE(av->getDim() == 1);
+ delete av;
+ delete array;
+}
+
+TEST(EtchArrayValueTest, ConstructorTest2) {
+ EtchNativeArray<capu::SmartPointer<EtchInt32> > *array = new EtchNativeArray<capu::SmartPointer<EtchInt32> >(0);
+ EtchString type("t1");
+ EtchType *t1 = new EtchType(type);
+ EtchArrayValue *av = new EtchArrayValue(array, 0, 1, t1, 2);
+ EXPECT_EQ(1, av->getTypeCode());
+ EXPECT_TRUE(t1->equals(av->getCustomStructType()));
+ EXPECT_EQ(2, av->getDim());
+ delete av;
+ delete t1;
+ delete array;
+}
+
+TEST(EtchArrayValueTest, ConstructorTest3) {
+ EtchNativeArray<capu::SmartPointer<EtchInt32> > *array = new EtchNativeArray<capu::SmartPointer<EtchInt32> >(0);
+ EtchString type("t1");
+ EtchType *t1 = new EtchType(type);
+ EtchArrayValue *av = new EtchArrayValue(array, 0, 3, t1, 4);
+ EXPECT_EQ(3, av->getTypeCode());
+ EXPECT_TRUE(t1->equals(av->getCustomStructType()));
+ EXPECT_EQ(4, av->getDim());
+ delete av;
+ delete t1;
+ delete array;
+}
+
+TEST(EtchArrayValueTest, getTest) {
+ EtchNativeArray<capu::SmartPointer<EtchObject> > *array = new EtchNativeArray<capu::SmartPointer<EtchObject> >(5);
+
+ capu::SmartPointer<EtchObject> array0 = new EtchInt32(5);
+ capu::SmartPointer<EtchObject> array1 = new EtchDouble(5);
+ capu::SmartPointer<EtchObject> array2 = new EtchFloat(5);
+ capu::SmartPointer<EtchObject> array3 = new EtchBool(true);
+ capu::SmartPointer<EtchObject> array4 = new EtchString("abc");
+ array->set(0, array0);
+ array->set(1, array1);
+ array->set(2, array2);
+ array->set(3, array3);
+ array->set(4, array4);
+
+ capu::SmartPointer<EtchObject> result;
+ EtchArrayValue *av = new EtchArrayValue(array, 5);
+
+ EXPECT_EQ(5, av->getSize());
+ capu::int32_t index = 0;
+
+ EXPECT_EQ(ETCH_OK, av->get(index++, result));
+ EXPECT_TRUE(result->equals(array0.get()));
+
+ EXPECT_EQ(ETCH_OK, av->get(index++, result));
+ EXPECT_TRUE(result->equals(array1.get()));
+
+ EXPECT_EQ(ETCH_OK, av->get(index++, result));
+ EXPECT_TRUE(result->equals(array2.get()));
+
+ EXPECT_EQ(ETCH_OK, av->get(index++, result));
+ EXPECT_TRUE(result->equals(array3.get()));
+
+ EXPECT_EQ(ETCH_OK, av->get(index++, result));
+ EXPECT_TRUE(result->equals(array4.get()));
+
+ array = (EtchNativeArray<capu::SmartPointer<EtchObject> > *) av->getArray();
+ delete array;
+ delete av;
+}
+
+TEST(EtchArrayValueTest, addTest) {
+ capu::SmartPointer<EtchObject> dt = new EtchBool(false);
+ EtchNativeArray<capu::SmartPointer<EtchObject> > *array = new EtchNativeArray<capu::SmartPointer<EtchObject> >(5);
+
+ capu::SmartPointer<EtchObject> array0 = new EtchInt32(5);
+ capu::SmartPointer<EtchObject> array1 = new EtchDouble(5);
+ capu::SmartPointer<EtchObject> array2 = new EtchFloat(5);
+ capu::SmartPointer<EtchObject> array3 = new EtchBool(true);
+ capu::SmartPointer<EtchObject> array4 = new EtchString("abc");
+ array->set(0, array0);
+ array->set(1, array1);
+ array->set(2, array2);
+ array->set(3, array3);
+ array->set(4, array4);
+ capu::SmartPointer<EtchObject> result;
+ EtchArrayValue *av = new EtchArrayValue(array, 5);
+
+ EXPECT_EQ(5, av->getSize());
+ capu::int32_t index = 0;
+
+ EXPECT_EQ(ETCH_OK, av->get(index++, result));
+ EXPECT_TRUE(result->equals(array0.get()));
+
+ EXPECT_EQ(ETCH_OK, av->get(index++, result));
+ EXPECT_TRUE(result->equals(array1.get()));
+
+ EXPECT_EQ(ETCH_OK, av->get(index++, result));
+ EXPECT_TRUE(result->equals(array2.get()));
+
+ EXPECT_EQ(ETCH_OK, av->get(index++, result));
+ EXPECT_TRUE(result->equals(array3.get()));
+
+ EXPECT_EQ(ETCH_OK, av->get(index++, result));
+ EXPECT_TRUE(result->equals(array4.get()));
+
+ //correct add
+ av->add(dt);
+ EXPECT_EQ(ETCH_OK, av->get(index++, result));
+ EXPECT_TRUE(result->equals(dt.get()));
+ //size should be doubled
+ EXPECT_EQ(10, av->getSize());
+
+ av->add(NULL);
+ EXPECT_EQ(10, av->getSize());
+ array = (EtchNativeArray<capu::SmartPointer<EtchObject> > *) av->getArray();
+
+ delete array;
+ delete av;
+}
+
+
+
Modified: incubator/etch/trunk/binding-cpp/runtime/src/test/common/EtchNativeArrayTest.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/test/common/EtchNativeArrayTest.cpp?rev=1345139&r1=1345138&r2=1345139&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/test/common/EtchNativeArrayTest.cpp (original)
+++ incubator/etch/trunk/binding-cpp/runtime/src/test/common/EtchNativeArrayTest.cpp Fri Jun 1 12:49:15 2012
@@ -21,8 +21,7 @@
#include "common/EtchNativeArray.h"
TEST(EtchNativeArrayTest, Constructor_Default) {
- EtchNativeArray<EtchInt32*>* na =
- new EtchNativeArray<EtchInt32*>(1, 2);
+ EtchNativeArray<EtchInt32*>* na = new EtchNativeArray<EtchInt32*>(2);
delete na;
}
@@ -31,13 +30,12 @@ TEST(EtchNativeArrayTest, Constructor_Ar
EtchInt32* int1 = new EtchInt32(42);
EtchInt32* int2 = new EtchInt32(43);
- EtchNativeArray<EtchInt32*>* na1 = new EtchNativeArray<EtchInt32*>(1, 2);
+ EtchNativeArray<EtchInt32*>* na1 = new EtchNativeArray<EtchInt32*>(2);
+ EXPECT_TRUE(na1->getObjectType()->equals(EtchNativeArray<EtchInt32*>::TYPE()));
na1->set(0, int1);
na1->set(1, int2);
- EXPECT_TRUE(na1->getObjectType()->equals(EtchNativeArray<EtchInt32*>::TYPE()));
-
EtchInt32* value = NULL;
capu::int32_t i = 0;
for (i = 0; i < 2; i++) {
@@ -51,13 +49,10 @@ TEST(EtchNativeArrayTest, Constructor_Ar
delete int2;
}
-TEST(EtchNativeArrayTest, setget) {
-
+TEST(EtchNativeArrayTest, setGetSingleValue) {
EtchInt32* int1 = new EtchInt32(42);
EtchInt32* int2 = new EtchInt32(43);
- EtchNativeArray<EtchInt32*>* na1 =
- new EtchNativeArray<EtchInt32*>(1, 2);
- EXPECT_TRUE(na1->getObjectType()->equals(EtchNativeArray<EtchInt32*>::TYPE()));
+ EtchNativeArray<EtchInt32*>* na1 = new EtchNativeArray<EtchInt32*>(2);
na1->set(0, int1);
na1->set(1, int2);
@@ -78,111 +73,277 @@ TEST(EtchNativeArrayTest, setget) {
ASSERT_EQ(ETCH_OK, ret);
ASSERT_EQ(44, newValue->get());
- //Test setget of block
- EtchNativeArray<EtchInt32*> *native_array = new EtchNativeArray<EtchInt32*> (1, 2);
- EtchInt32** int3 = new EtchInt32*[2];
- int3[0] = new EtchInt32();
- int3[1] = new EtchInt32();
+ delete na1;
+ delete int1;
+ delete int2;
+ delete newValue;
+}
- int3[0]->set(2);
- int3[1]->set(4);
+TEST(EtchNativeArrayTest, setGetArray) {
+ status_t ret;
+ EtchInt32** arr1 = new EtchInt32*[6];
+ EtchInt32** arr2 = new EtchInt32*[4];
+ arr1[0] = new EtchInt32(1);
+ arr1[1] = new EtchInt32(2);
+ arr1[2] = new EtchInt32(3);
+ arr1[3] = new EtchInt32(4);
+ arr1[4] = new EtchInt32(5);
+ arr1[5] = new EtchInt32(6);
+ EtchInt32* int2 = new EtchInt32(10);
+ EtchInt32* temp = NULL;
+ capu::int32_t elementsWritten = 0;
+ capu::int32_t elementsRead = 0;
+
+ EtchNativeArray<EtchInt32*> *na = new EtchNativeArray<EtchInt32*> (6);
+
+ //test invalids
+ //data == NULL
+ ret = na->set(0,NULL,0,0,0,&elementsWritten);
+ EXPECT_EQ(ETCH_EINVAL, ret);
+
+ //data == NULL
+ ret = na->get(0,NULL,0,0,0,&elementsRead);
+ EXPECT_EQ(ETCH_EINVAL, ret);
+
+ //buffer size smaller then length
+ ret = na->set(0,arr2, 2, 0, 4, &elementsWritten);
+ EXPECT_EQ(ETCH_ERANGE, ret);
+
+ //offset + length out of range
+ ret = na->set(0,arr2, 4, 3, 4, &elementsWritten);
+ EXPECT_EQ(ETCH_ERANGE, ret);
+
+ //buffer size smaller then length
+ ret = na->get(0, arr2, 2, 0, 4, &elementsRead);
+ EXPECT_EQ(ETCH_ERANGE, ret);
+
+ //offset + length out of range
+ ret = na->get(0, arr2, 4, 3, 4, &elementsRead);
+ EXPECT_EQ(ETCH_ERANGE, ret);
+
+ //set Array
+ ret = na->set(0, arr1, 6, 0, 6, &elementsWritten);
+ EXPECT_EQ(ret, ETCH_OK);
+ EXPECT_EQ(elementsWritten, 6);
+
+ na->get(0,&temp);
+ EXPECT_EQ(temp->get(), 1);
+ na->set(0, int2);
+ na->get(0,&temp);
+ EXPECT_EQ(temp->get(), int2->get());
+
+ //get Array
+ ret = na->get(0, arr2, 4, 0, 4, &elementsRead);
+ EXPECT_EQ(ret, ETCH_OK);
+ EXPECT_EQ(elementsRead, 4);
- ret = native_array->set(0, 0, NULL, 0);
- EXPECT_TRUE(ret == ETCH_EINVAL);
+ EXPECT_EQ(arr2[3]->get(), 4);
- ret = native_array->get(0, 0, NULL, 0);
- EXPECT_TRUE(ret == ETCH_EINVAL);
+ delete na;
+ delete arr1[0];
+ delete arr1[1];
+ delete arr1[2];
+ delete arr1[3];
+ delete arr1[4];
+ delete arr1[5];
+ delete[] arr1;
+ delete int2;
+ delete[] arr2;
- ret = native_array->set(0, 2, int3, 0);
- EXPECT_TRUE(ret == ETCH_OK);
+}
- ret = native_array->get(0, &value);
- EXPECT_TRUE(value->get() == 2);
+TEST(EtchNativeArrayTest, nativeTypes) {
- EtchInt32** int6 = new EtchInt32*[2];
- ret = native_array->get(0, 2, int6, 0);
- EXPECT_TRUE(ret == ETCH_OK);
+ status_t ret;
+ //Test setget of block of native type
+ EtchNativeArray<int> *na = new EtchNativeArray<int> (2);
+ capu::int32_t arr1[2];
+ capu::int32_t arr2[2];
+ capu::int32_t elementsWritten = 0;
+ capu::int32_t elementsRead = 0;
+
+ arr1[0] = 3;
+ arr1[1] = 5;
- EXPECT_TRUE(int6[0]->get() == 2);
- EXPECT_TRUE(int6[1]->get() == 4);
+ ret = na->set(0, arr1,2, 0, 2, &elementsWritten);
+ EXPECT_EQ(ETCH_OK, ret);
- //Test setget of block of native type
- EtchNativeArray<int> *native_array2 = new EtchNativeArray<int> (1, 2);
- capu::int32_t int4[2];
- capu::int32_t int5[2];
- int4[0] = 3;
- int4[1] = 5;
-
- ret = native_array2->set(0, 0, NULL, 0);
- EXPECT_TRUE(ret == ETCH_EINVAL);
-
- ret = native_array2->get(0, 0, NULL, 0);
- EXPECT_TRUE(ret == ETCH_EINVAL);
-
- ret = native_array2->set(0, 2, (int*) int4, 0);
- EXPECT_TRUE(ret == ETCH_OK);
-
- ret = native_array2->get(0, 2, (int*) int5, 0);
- EXPECT_TRUE(ret == ETCH_OK);
-
- EXPECT_TRUE(int5[0] == 3);
- EXPECT_TRUE(int5[1] == 5);
-
- //buffer size smaller then count
- ret = native_array2->set(0, 1, (int*) int4, 2);
- EXPECT_TRUE(ret == ETCH_ERANGE);
-
- //offset + count out of range
- ret = native_array2->set(0, 2, (int*) int4, 2);
- EXPECT_TRUE(ret == ETCH_ERANGE);
-
- //buffer size smaller then count
- ret = native_array2->get(0, 1, (int*) int5, 2);
- EXPECT_TRUE(ret == ETCH_ERANGE);
-
- //offset + count out of range
- ret = native_array2->set(0, 2, (int*) int5, 2);
- EXPECT_TRUE(ret == ETCH_ERANGE);
-
- delete int3[0];
- delete int3[1];
- delete[] int3;
- delete[] int6;
- delete na1;
- delete int1;
- delete int2;
- delete newValue;
- delete native_array;
- delete native_array2;
+ ret = na->get(0, arr2, 2, 0, 2, &elementsRead);
+ EXPECT_EQ(ETCH_OK, ret);
+
+ EXPECT_EQ(3, arr2[0]);
+ EXPECT_EQ(5, arr2[1]);
+
+ delete na;
}
TEST(EtchNativeArrayTest, multiDimensionalArrays) {
+ status_t ret;
+ EtchInt32* int00 = new EtchInt32(1);
+ EtchInt32* int01 = new EtchInt32(2);
+ EtchInt32* int10 = new EtchInt32(3);
+ EtchInt32* int11 = new EtchInt32(4);
+ EtchInt32* temp = NULL;
+
+ EtchNativeArray<EtchInt32*>* main1 = new EtchNativeArray<EtchInt32*>(2, 2);
+
+ main1->createArray(Pos(0), 2);
+ main1->createArray(Pos(1), 2);
+
+ main1->set(Pos(0, 0), int00);
+ main1->set(Pos(0, 1), int01);
+ main1->set(Pos(1, 0), int10);
+ main1->set(Pos(1, 1), int11);
+
+ ret = main1->get(Pos(1), &temp);
+ EXPECT_EQ(ETCH_ERANGE, ret);
+
+ ret = main1->get(Pos(1, 1), &temp);
+ EXPECT_EQ(ETCH_OK, ret);
+ EXPECT_EQ(4, temp->get());
+
+ delete main1;
+
+ EtchNativeArray<EtchInt32*>* main2 = new EtchNativeArray<EtchInt32*>(2, 3);
+
+ // create dim2
+ main2->createArray(Pos(0), 2);
+ main2->createArray(Pos(1), 2);
+
+ // create dim3
+ main2->createArray(Pos(0, 0), 2);
+ main2->createArray(Pos(0, 1), 2);
+ main2->createArray(Pos(1, 0), 4);
+ main2->createArray(Pos(1, 1), 4);
+
+ main2->set(Pos(0, 0, 0), int00);
+ main2->set(Pos(0, 0, 1), int01);
+ main2->set(Pos(0, 1, 0), int10);
+ main2->set(Pos(0, 1, 1), int11);
+ main2->set(Pos(1, 1, 3), int11);
+
+ ret = main2->get(Pos(1, 1), &temp);
+ EXPECT_EQ(ETCH_ERANGE, ret);
+
+ ret = main2->get(Pos(1, 1, 3), &temp);
+ EXPECT_EQ(ETCH_OK, ret);
+ EXPECT_EQ(4, temp->get());
+
+ delete main2;
+
+ delete int00;
+ delete int01;
+ delete int10;
+ delete int11;
+
+}
+
+TEST(EtchNativeArrayTest, getSubarray) {
+ status_t ret;
EtchInt32* int1 = new EtchInt32(1);
EtchInt32* int2 = new EtchInt32(2);
EtchInt32* int3 = new EtchInt32(3);
EtchInt32* int4 = new EtchInt32(4);
+ EtchInt32* temp = NULL;
+
+ EtchNativeArray<EtchInt32*>* mainArray = new EtchNativeArray<EtchInt32*>(2, 3);
+ EtchNativeArray<EtchInt32*>* subArray = NULL;
- EtchNativeArray<EtchInt32*>* mainArray = new EtchNativeArray<EtchInt32*>(2, 2);
+ // create dim2
+ mainArray->createArray(Pos(0), 2);
+ mainArray->createArray(Pos(1), 2);
+
+ // create dim3
+ mainArray->createArray(Pos(0, 0), 2);
+ mainArray->createArray(Pos(0, 1), 2);
+ mainArray->createArray(Pos(1, 0), 4);
+ mainArray->createArray(Pos(1, 1), 4);
+
+ mainArray->set(Pos(0, 0, 0), int1);
+ mainArray->set(Pos(0, 0, 1), int2);
+ mainArray->set(Pos(0, 1, 0), int3);
+ mainArray->set(Pos(0, 1, 1), int4);
+ mainArray->set(Pos(1, 1, 3), int4);
+
+ //get data element, must return error
+ ret = mainArray->get(Pos(1, 1, 3), subArray);
+ EXPECT_EQ(ETCH_ERANGE, ret);
+
+ //get 2-dim subarray
+ ret = mainArray->get(Pos(1), subArray);
+ EXPECT_EQ(ETCH_OK, ret);
+ //check data
+ subArray->get(Pos(1,3),&temp);
+ EXPECT_EQ(4, temp->get());
- const EtchObjectType *ot = mainArray->getObjectType();
- //get type of main array
- EXPECT_TRUE(ot->equals(EtchNativeArray<EtchInt32*>::TYPE()));
-
- EtchInt32* int5;
- mainArray->createArray(Pos(0), 1, 2);
- mainArray->createArray(Pos(1), 1, 2);
- mainArray->set(Pos(0, 0), int1);
- mainArray->set(Pos(0, 1), int2);
- mainArray->set(Pos(1, 0), int3);
- mainArray->set(Pos(1, 1), int4);
- mainArray->get(Pos(0, 0), &int5);
- EXPECT_EQ(1, int5->get());
+ delete mainArray;
+ //check data again to make sure is has not been deleted by mainArray
+ subArray->get(Pos(1,3),&temp);
+ EXPECT_EQ(4, temp->get());
+
+ delete subArray;
delete int1;
delete int2;
delete int3;
delete int4;
+}
+
+TEST(EtchNativeArrayTest, setSubarray) {
+ status_t ret;
+ EtchInt32* int1 = new EtchInt32(1);
+ EtchInt32* int2 = new EtchInt32(2);
+ EtchInt32* int3 = new EtchInt32(3);
+ EtchInt32* int4 = new EtchInt32(4);
+ EtchInt32* temp = NULL;
+
+ EtchNativeArray<EtchInt32*>* mainArray = new EtchNativeArray<EtchInt32*>(2, 3);
+ EtchNativeArray<EtchInt32*>* subArray = new EtchNativeArray<EtchInt32*>(2,1);
+ EtchNativeArray<EtchInt32*>* subArrayDim2 = new EtchNativeArray<EtchInt32*>(2,2);
+
+ // create dim2
+ mainArray->createArray(Pos(0), 2);
+ mainArray->createArray(Pos(1), 2);
+
+ // create dim3
+ mainArray->createArray(Pos(0, 0), 2);
+ mainArray->createArray(Pos(0, 1), 2);
+ mainArray->createArray(Pos(1, 0), 4);
+ mainArray->createArray(Pos(1, 1), 4);
+
+ //fill main array
+ mainArray->set(Pos(0, 0, 0), int1);
+ mainArray->set(Pos(0, 0, 1), int2);
+
+ //fill sub array
+ subArray->set(Pos(0),int3);
+ subArray->set(Pos(0),int4);
+
+ //set sub array in data array, must return error
+ ret = mainArray->set(Pos(1, 0, 0), subArray);
+ EXPECT_EQ(ETCH_ERANGE, ret);
+
+ //set sub array with invalid dim
+ ret = mainArray->set(Pos(1, 0), subArrayDim2);
+ EXPECT_EQ(ETCH_ERANGE, ret);
+
+ //set sub array with valid dim
+ ret = mainArray->set(Pos(1, 0), subArray);
+ EXPECT_EQ(ETCH_OK, ret);
+
+ //check data
+ ret = mainArray->get(Pos(1, 0, 0), &temp);
+ EXPECT_EQ(ETCH_OK, ret);
+ EXPECT_EQ(4, temp->get());
delete mainArray;
+ delete subArray;
+ delete subArrayDim2;
+
+ delete int1;
+ delete int2;
+ delete int3;
+ delete int4;
+
}
\ No newline at end of file