You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@etch.apache.org by fi...@apache.org on 2012/06/01 13:42:01 UTC
svn commit: r1345079 - in /incubator/etch/trunk/binding-cpp/runtime:
include/serialization/ src/main/ src/main/serialization/ src/test/
src/test/serialization/
Author: fitzner
Date: Fri Jun 1 11:42:00 2012
New Revision: 1345079
URL: http://svn.apache.org/viewvc?rev=1345079&view=rev
Log:
ETCH-184 EtchTypeCollection has been implemented
Change-Id: Ib6b57d6d2d2a930f18d4330234fcc242891a4744
Added:
incubator/etch/trunk/binding-cpp/runtime/include/serialization/EtchTypeMap.h
incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchTypeMap.cpp
incubator/etch/trunk/binding-cpp/runtime/src/test/serialization/EtchTypeMapTest.cpp
Modified:
incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt
incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchType.cpp
incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt
Added: incubator/etch/trunk/binding-cpp/runtime/include/serialization/EtchTypeMap.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/serialization/EtchTypeMap.h?rev=1345079&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/serialization/EtchTypeMap.h (added)
+++ incubator/etch/trunk/binding-cpp/runtime/include/serialization/EtchTypeMap.h Fri Jun 1 11:42:00 2012
@@ -0,0 +1,109 @@
+/* $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 __ETCHTYPEMAP_H__
+#define __ETCHTYPEMAP_H__
+
+#include "common/EtchString.h"
+#include "common/EtchHashTable.h"
+#include "common/EtchHashSet.h"
+#include "serialization/EtchType.h"
+
+class EtchTypeMap {
+public:
+
+ /**
+ * Constructor
+ */
+ EtchTypeMap();
+
+ /**
+ * Destructor
+ */
+ virtual ~EtchTypeMap();
+
+ /**
+ * Gets the EtchType subclass which corresponds to the specified id.
+ * @param id the id of an Type.
+ * @param type type which is found in collection with respect to id.
+ * @return ETCH_OK if get is successful performed
+ * ETCH_EINVAL if type is null
+ * ETCH_ENOT_EXIST if there is no pair with specified id
+ */
+ status_t get(capu::int32_t id, EtchType *&type);
+
+ /**
+ * Gets the EtchType subclass which corresponds to the specified
+ * name, or creates it if it isn't found and if this map is not
+ * locked.
+ * @param name the name of an Type.
+ * @param type type which is found in collection with respect to name or created.
+ * @return ETCH_OK if get is successful performed
+ * ETCH_EINVAL if type is null
+ * ETCH_ENOT_EXIST if there is no pair with specified id
+ */
+ status_t get(EtchString name, EtchType *&type);
+
+ /**
+ * Adds the EtchType subclass to the map.
+ *
+ * @param t the EtchType subclass to add.
+ *
+ * @return ETCH_EINVAL if it is locked
+ * ETCH_ERROR if there is an error
+ * ETCH_OK otherwise
+ *
+ * existing entry by id or name.
+ */
+ status_t add(EtchType *type);
+
+ /**
+ * Locks the map preventing further changes.
+ */
+ void lock();
+
+ /**
+ * @return the number of values in the map.
+ */
+ capu::int32_t size();
+
+ /**
+ * all of types element added to collection
+ * @param set hashset where the values will be put
+ * @return ETCH_OK if successfully completed
+ * ETCH_EINVAL if set is null
+ */
+ status_t getAll(EtchHashSet<EtchType*> *set);
+
+
+ //clear deallocates every element in type collection
+ void clear();
+
+private:
+
+ EtchHashTable<capu::int32_t, EtchType*, capu::Hash, capu::Comparator > mById;
+
+ EtchHashTable<EtchString, EtchType* > mByName;
+
+ capu::bool_t mLocked;
+
+};
+
+
+#endif /* ETCHTYPEMAP_H */
+
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=1345079&r1=1345078&r2=1345079&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 11:42:00 2012
@@ -81,6 +81,7 @@ SET(MAIN_INCLUDES
${PROJECT_SOURCE_DIR}/include/serialization/EtchImportExportHelper.h
${PROJECT_SOURCE_DIR}/include/serialization/EtchType.h
${PROJECT_SOURCE_DIR}/include/serialization/EtchStructValue.h
+ ${PROJECT_SOURCE_DIR}/include/serialization/EtchTypeMap.h
${PROJECT_SOURCE_DIR}/include/util/EtchUtil.h
)
@@ -122,6 +123,7 @@ SET(MAIN_SOURCES
serialization/EtchValueFactory.cpp
serialization/EtchType.cpp
serialization/EtchStructValue.cpp
+ serialization/EtchTypeMap.cpp
util/EtchUtil.cpp
)
Modified: incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchType.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchType.cpp?rev=1345079&r1=1345078&r2=1345079&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchType.cpp (original)
+++ incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchType.cpp Fri Jun 1 11:42:00 2012
@@ -24,13 +24,11 @@ const EtchObjectType EtchType::TYPE(EOTI
EtchType::EtchType()
: EtchObject(&EtchType::TYPE), mId(0), mTimeout(0), mName(""), mSuperType(NULL),
mResultType(NULL), mDirection(BOTH), mAsyncMode(NONE), mLocked(false), mComponentType(NULL), mHelper(NULL) {
-
}
EtchType::EtchType(capu::uint32_t id, EtchString &name)
: EtchObject(&EtchType::TYPE), mId(id), mTimeout(0), mName(name), mSuperType(NULL),
mResultType(NULL), mDirection(BOTH), mAsyncMode(NONE), mLocked(false), mComponentType(NULL), mHelper(NULL) {
-
}
EtchType::EtchType(EtchString &name)
@@ -40,8 +38,10 @@ mResultType(NULL), mDirection(BOTH), mAs
}
EtchType::~EtchType() {
- if (mHelper != NULL)
+ if (mHelper != NULL) {
+ //TODO: Check memory management
delete mHelper;
+ }
}
capu::uint64_t EtchType::getHashCode() {
Added: incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchTypeMap.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchTypeMap.cpp?rev=1345079&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchTypeMap.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchTypeMap.cpp Fri Jun 1 11:42:00 2012
@@ -0,0 +1,97 @@
+/* $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 "serialization/EtchTypeMap.h"
+
+// TODO: Check memory handling
+
+EtchTypeMap::EtchTypeMap()
+: mLocked(false) {
+}
+
+EtchTypeMap::~EtchTypeMap() {
+}
+
+status_t EtchTypeMap::add(EtchType *type) {
+ if (mLocked)
+ return ETCH_EINVAL;
+ EtchType *tmp = NULL;
+ // check id collision
+ if (mById.get(type->getId(), &tmp) != ETCH_ENOT_EXIST)
+ return ETCH_ERROR;
+ // check name collision
+ if (mByName.get(type->getName(), &tmp) != ETCH_ENOT_EXIST)
+ return ETCH_ERROR;
+
+ if (mById.put(type->getId(), type) != ETCH_OK)
+ return ETCH_ERROR;
+ if (mByName.put(type->getName(), type) != ETCH_OK)
+ return ETCH_ERROR;
+
+ return ETCH_OK;
+}
+
+void EtchTypeMap::lock() {
+ mLocked = true;
+};
+
+capu::int32_t EtchTypeMap::size() {
+ return mById.count();
+}
+
+status_t EtchTypeMap::get(EtchString name, EtchType *&type) {
+ status_t result = mByName.get(name, &type);
+ if (result == ETCH_ENOT_EXIST) {
+ if (mLocked)
+ return ETCH_EINVAL;
+ EtchType *tmp = new EtchType(name);
+ result = add(tmp);
+ if (result == ETCH_OK) {
+ type = tmp;
+ } else {
+ delete tmp;
+ }
+ }
+ return result;
+}
+
+status_t EtchTypeMap::get(capu::int32_t id, EtchType *&type) {
+ return mById.get(id, &type);
+}
+
+status_t EtchTypeMap::getAll(EtchHashSet<EtchType*>* set) {
+ if (set == NULL)
+ return ETCH_EINVAL;
+ EtchHashTable<EtchString, EtchType* >::Iterator it = mByName.begin();
+ EtchHashTable<EtchString, EtchType* >::Pair pair;
+ while (it.hasNext()) {
+ it.next(&pair);
+ set->put(pair.second);
+ }
+ return ETCH_OK;
+}
+
+void EtchTypeMap::clear() {
+ EtchHashTable<EtchString, EtchType* >::Iterator it = mByName.begin();
+ EtchHashTable<EtchString, EtchType* >::Pair pair;
+ while (it.hasNext()) {
+ it.next(&pair);
+ delete pair.second;
+ }
+ mByName.clear();
+ mById.clear();
+}
\ No newline at end of file
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=1345079&r1=1345078&r2=1345079&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 11:42:00 2012
@@ -62,6 +62,7 @@ add_executable (etch-cpp-test
serialization/EtchFieldMapTest.cpp
serialization/EtchTypeTest.cpp
serialization/EtchStructValueTest.cpp
+ serialization/EtchTypeMapTest.cpp
util/EtchUtilTest.cpp
${GTEST}/src/gtest-all.cc
${GMOCK}/src/gmock-all.cc
Added: incubator/etch/trunk/binding-cpp/runtime/src/test/serialization/EtchTypeMapTest.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/test/serialization/EtchTypeMapTest.cpp?rev=1345079&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/test/serialization/EtchTypeMapTest.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/src/test/serialization/EtchTypeMapTest.cpp Fri Jun 1 11:42:00 2012
@@ -0,0 +1,153 @@
+/* $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/EtchHashSet.h"
+#include "serialization/EtchType.h"
+#include "serialization/EtchTypeMap.h"
+
+TEST(EtchTypeMapTest, createTest) {
+ EtchTypeMap *test = NULL;
+ test = new EtchTypeMap();
+ EXPECT_TRUE(test != NULL);
+ delete test;
+}
+
+TEST(EtchTypeMapTest, addTest) {
+ EtchTypeMap *test = NULL;
+ test = new EtchTypeMap();
+ EtchString str = "testtype1";
+ EtchType* type = new EtchType(10, str);
+ //add new type to collection
+ EXPECT_EQ(ETCH_OK, test->add(type));
+ //Try to add existing type
+ EXPECT_EQ(ETCH_ERROR, test->add(type));
+ //lock the collection
+ test->lock();
+ //try to add new type
+ EXPECT_EQ(ETCH_EINVAL, test->add(type));
+ delete type;
+ delete test;
+}
+
+TEST(EtchTypeMapTest, getTest) {
+ EtchTypeMap *test = NULL;
+ test = new EtchTypeMap();
+ EtchString str = "testtype1";
+ EtchType* type = new EtchType(10, str);
+ EtchType* tmp1;
+ EtchType* tmp2;
+ //add new type to collection
+ EXPECT_EQ(ETCH_OK, test->add(type));
+ //get the new type
+ EXPECT_EQ(ETCH_OK, test->get(10, tmp1));
+ //check the content
+ EXPECT_TRUE(type->equals(tmp1));
+ //try to get non existing element by name and add it
+ EXPECT_EQ(ETCH_OK, test->get("testtype2", tmp1));
+
+ //lock the collection
+ test->lock();
+ //get type
+ EXPECT_EQ(ETCH_OK, test->get(10, tmp1));
+ //check content
+ EXPECT_TRUE(type->equals(tmp1));
+ //get type by name
+ EXPECT_EQ(ETCH_OK, test->get("testtype1", tmp2));
+ //check content
+ EXPECT_TRUE(type->equals(tmp2));
+ //Try to add a new type
+ EXPECT_EQ(ETCH_EINVAL, test->add(type));
+ //try to get non existing element
+ EXPECT_EQ(ETCH_ENOT_EXIST, test->get(11, tmp1));
+ //try to get non existing element by name and add it to the locked collection
+ EXPECT_EQ(ETCH_EINVAL, test->get("testtype3", tmp1));
+ //Get the already added element
+ EXPECT_EQ(ETCH_OK, test->get("testtype2", tmp1));
+
+ delete test;
+ delete type;
+ delete tmp1;
+}
+
+TEST(EtchTypeMapTest, sizeTest) {
+ EtchTypeMap *test = NULL;
+ test = new EtchTypeMap();
+ EtchString str = "testtype1";
+ EtchType* type = new EtchType(10, str);
+ //check size
+ EXPECT_EQ(0, test->size());
+ //add element
+ EXPECT_EQ(ETCH_OK, test->add(type));
+ //check size
+ EXPECT_EQ(1, test->size());
+ //try to add existing element
+ EXPECT_EQ(ETCH_ERROR, test->add(type));
+ //check size
+ EXPECT_EQ(1, test->size());
+ delete test;
+ delete type;
+}
+
+TEST(EtchTypeMapTest, lockTest) {
+ EtchTypeMap *test = NULL;
+ test = new EtchTypeMap();
+ EtchString str = "testtype1";
+ EtchType* type = new EtchType(10, str);
+ //check size of empty collection
+ EXPECT_EQ(0, test->size());
+ //add new type
+ EXPECT_EQ(ETCH_OK, test->add(type));
+ //try to add same type to collection
+ EXPECT_EQ(ETCH_ERROR, test->add(type));
+ //lock collection
+ test->lock();
+ //then try to add a type
+ EXPECT_EQ(ETCH_EINVAL, test->add(type));
+ //check size again
+ EXPECT_EQ(1, test->size());
+ delete test;
+ delete type;
+}
+
+TEST(EtchTypeMapTest, getAllTest) {
+ EtchTypeMap *test = NULL;
+ test = new EtchTypeMap();
+ EtchString str = "testtype1";
+ EtchType* type = new EtchType(10, str);
+ EtchHashSet<EtchType*> set;
+
+ //check size of empty collection
+ EXPECT_EQ(0, test->size());
+ //add new type
+ EXPECT_EQ(ETCH_OK, test->add(type));
+ //try to add same type to collection
+ EXPECT_EQ(ETCH_EINVAL, test->getAll(NULL));
+
+ EXPECT_EQ(ETCH_OK, test->getAll(&set));
+
+ EtchType *result;
+ EtchHashSet<EtchType*>::Iterator it = set.begin();
+
+ while (it.hasNext()) {
+ it.next(&result);
+ }
+
+ delete test;
+ delete type;
+}
\ No newline at end of file