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:36:12 UTC

svn commit: r1345073 - in /incubator/etch/trunk/binding-cpp/runtime: include/serialization/EtchFieldMap.h src/main/CMakeLists.txt src/main/serialization/EtchFieldMap.cpp src/test/CMakeLists.txt src/test/serialization/EtchFieldMapTest.cpp

Author: fitzner
Date: Fri Jun  1 11:36:12 2012
New Revision: 1345073

URL: http://svn.apache.org/viewvc?rev=1345073&view=rev
Log:
ETCH-184 Implementation ValueFactory

EtchFieldMap has been implemented

Change-Id: I3cdc834e18b242c966d11ebee6f88c8add08cdd4

Added:
    incubator/etch/trunk/binding-cpp/runtime/include/serialization/EtchFieldMap.h
    incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchFieldMap.cpp
    incubator/etch/trunk/binding-cpp/runtime/src/test/serialization/EtchFieldMapTest.cpp
Modified:
    incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt
    incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt

Added: incubator/etch/trunk/binding-cpp/runtime/include/serialization/EtchFieldMap.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/serialization/EtchFieldMap.h?rev=1345073&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/serialization/EtchFieldMap.h (added)
+++ incubator/etch/trunk/binding-cpp/runtime/include/serialization/EtchFieldMap.h Fri Jun  1 11:36:12 2012
@@ -0,0 +1,96 @@
+/* $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 __ETCHFIELDMAP_H__
+#define __ETCHFIELDMAP_H__
+
+#include "common/EtchString.h"
+#include "serialization/EtchField.h"
+#include "common/EtchHashTable.h"
+
+/**
+ * Mapping of id and name to EtchField
+ */
+class EtchFieldMap {
+public:
+
+  /**
+   * Constructor
+   */
+  EtchFieldMap();
+
+  /**
+   * Destructor
+   */
+  virtual ~EtchFieldMap();
+
+  /**
+   * Gets the EtchField subclass which corresponds to the specified id.
+   * @param id the id of an EtchField.
+   * @param field Field that will be get
+   * @return  ETCH_OK if get is successful performed
+   *          ETCH_EINVAL if field is null
+   *          ETCH_ENOT_EXIST if there is no pair with specified id
+   */
+  status_t get(capu::int32_t id, EtchField* field);
+
+  /**
+   * Gets the EtchField 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 EtchField.
+   * @param field Field that will be get
+   * @return ETCH_OK if get is successful performed
+   *         ETCH_EINVAL if field is null
+   *         ETCH_ENOT_EXIST if there is no pair with specified name
+   */
+  status_t get(EtchString name, EtchField* field);
+
+  /**
+   * Adds the EtchField subclass to the map.
+   *
+   * @param t the EtchField 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(EtchField t);
+
+  /**
+   * Locks the map preventing further changes.
+   */
+  void lock();
+
+  /**
+   * @return the number of values in the map.
+   */
+  capu::int32_t size();
+
+private:
+
+  EtchHashTable<capu::int32_t, EtchField, capu::Hash, capu::Comparator> mById;
+
+  EtchHashTable<EtchString, EtchField> mByName;
+
+  capu::bool_t mLocked;
+};
+
+#endif /* ETCHFIELDCOLLECTION_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=1345073&r1=1345072&r2=1345073&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:36:12 2012
@@ -76,6 +76,7 @@ SET(MAIN_INCLUDES
     ${PROJECT_SOURCE_DIR}/include/serialization/EtchComboValidator.h
     ${PROJECT_SOURCE_DIR}/include/serialization/EtchField.h
     ${PROJECT_SOURCE_DIR}/include/serialization/EtchValidatorObject.h
+    ${PROJECT_SOURCE_DIR}/include/serialization/EtchFieldMap.h
     ${PROJECT_SOURCE_DIR}/include/util/EtchUtil.h
     )
 
@@ -113,6 +114,7 @@ SET(MAIN_SOURCES
     serialization/EtchComboValidator.cpp
     serialization/EtchField.cpp
     serialization/EtchValidatorObject.cpp
+    serialization/EtchFieldMap.cpp
     util/EtchUtil.cpp
     )
 

Added: incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchFieldMap.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchFieldMap.cpp?rev=1345073&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchFieldMap.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchFieldMap.cpp Fri Jun  1 11:36:12 2012
@@ -0,0 +1,70 @@
+/* $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/EtchFieldMap.h"
+
+EtchFieldMap::EtchFieldMap()
+: mLocked(false) {
+
+}
+
+EtchFieldMap::~EtchFieldMap() {
+
+}
+
+status_t EtchFieldMap::add(EtchField t) {
+  if (mLocked)
+    return ETCH_EINVAL;
+  EtchField field;
+  if (mById.get(t.getId(), &field) != ETCH_ENOT_EXIST)
+    return ETCH_ERROR;
+  if (mByName.get(t.getName(), &field) != ETCH_ENOT_EXIST)
+    return ETCH_ERROR;
+
+  if (mById.put(t.getId(), t) != ETCH_OK)
+    return ETCH_ERROR;
+  if (mByName.put(t.getName(), t) != ETCH_OK)
+    return ETCH_ERROR;
+
+  return ETCH_OK;
+}
+
+void EtchFieldMap::lock() {
+  mLocked = true;
+};
+
+capu::int32_t EtchFieldMap::size() {
+  return mById.count();
+}
+
+status_t EtchFieldMap::get(EtchString name, EtchField* field) {
+  status_t result = mByName.get(name, field);
+  if (result == ETCH_ENOT_EXIST) {
+    if (mLocked)
+      return ETCH_EINVAL;
+    EtchField tmp(name);
+    if (add(tmp) == ETCH_OK) {
+      *field = tmp;
+      result = ETCH_OK;
+    }
+  }
+  return result;
+}
+
+status_t EtchFieldMap::get(capu::int32_t id, EtchField* field) {
+  return mById.get(id, field);
+}

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=1345073&r1=1345072&r2=1345073&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:36:12 2012
@@ -56,9 +56,10 @@ add_executable (etch-cpp-test
     serialization/EtchValidatorDoubleTest.cpp
     serialization/EtchValidatorLongTest.cpp
     serialization/EtchValidatorStringTest.cpp
+    serialization/EtchValidatorObjectTest.cpp
     serialization/EtchComboValidatorTest.cpp
     serialization/EtchFieldTest.cpp
-    serialization/EtchValidatorObjectTest.cpp
+    serialization/EtchFieldMapTest.cpp
     util/EtchUtilTest.cpp
     ${GTEST}/src/gtest-all.cc
     ${GMOCK}/src/gmock-all.cc

Added: incubator/etch/trunk/binding-cpp/runtime/src/test/serialization/EtchFieldMapTest.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/test/serialization/EtchFieldMapTest.cpp?rev=1345073&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/test/serialization/EtchFieldMapTest.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/src/test/serialization/EtchFieldMapTest.cpp Fri Jun  1 11:36:12 2012
@@ -0,0 +1,114 @@
+/* $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 "serialization/EtchField.h"
+#include "serialization/EtchFieldMap.h"
+
+TEST(EtchFieldMapTest, createTest) {
+  EtchFieldMap *test = NULL;
+  test = new EtchFieldMap();
+  EXPECT_TRUE(test != NULL);
+  delete test;
+}
+
+TEST(EtchFieldMapTest, addTest) {
+  EtchFieldMap *test = NULL;
+  test = new EtchFieldMap();
+  EtchString str = "testfield1";
+  EtchField field(10, str);
+  //add new field to collection
+  EXPECT_EQ(ETCH_OK, test->add(field));
+  //Try to add existing field
+  EXPECT_EQ(ETCH_ERROR, test->add(field));
+  //lock the collection
+  test->lock();
+  //try to add new field
+  EXPECT_EQ(ETCH_EINVAL, test->add(field));
+  delete test;
+}
+
+TEST(EtchFieldMapTest, getTest) {
+  EtchFieldMap *test = NULL;
+  test = new EtchFieldMap();
+  EtchString str = "testfield1";
+  EtchField field(10, str);
+  EtchField tmp1;
+  EtchField tmp2;
+  //add new field to collection
+  EXPECT_EQ(ETCH_OK, test->add(field));
+  //get the new field
+  EXPECT_EQ(ETCH_OK, test->get(10, &tmp1));
+  //check the content
+  EXPECT_TRUE(field.equals(&tmp1));
+  //try to get non existing element by name (which will be added)
+  EXPECT_EQ(ETCH_OK, test->get("testfield2", &tmp1));  
+  //lock the collection
+  test->lock();
+  //get field
+  EXPECT_EQ(ETCH_OK, test->get(10, &tmp1));
+  //check content
+  EXPECT_TRUE(field.equals(&tmp1));
+  //get field by name
+  EXPECT_EQ(ETCH_OK, test->get("testfield1", &tmp2));
+  //check content
+  EXPECT_TRUE(field.equals(&tmp2));
+  //Try to add a new field
+  EXPECT_EQ(ETCH_EINVAL, test->add(field));
+  //try to get non existing element
+  EXPECT_EQ(ETCH_ENOT_EXIST, test->get(11, &tmp1));
+  delete test;
+}
+
+TEST(EtchFieldMapTest, sizeTest) {
+  EtchFieldMap *test = NULL;
+  test = new EtchFieldMap();
+  EtchString str = "testfield1";
+  EtchField field(10, str);
+  //check size
+  EXPECT_EQ(0, test->size());
+  //add element
+  EXPECT_EQ(ETCH_OK, test->add(field));
+  //check size
+  EXPECT_EQ(1, test->size());
+  //try to add existing element
+  EXPECT_EQ(ETCH_ERROR, test->add(field));
+  //check size
+  EXPECT_EQ(1, test->size());
+  delete test;
+}
+
+TEST(EtchFieldMapTest, lockTest) {
+  EtchFieldMap *test = NULL;
+  test = new EtchFieldMap();
+  EtchString str = "testfield1";
+  EtchField field(10, str);
+  //check size of empty collection
+  EXPECT_EQ(0, test->size());
+  //add new field
+  EXPECT_EQ(ETCH_OK, test->add(field));
+  //try to add same field to collection
+  EXPECT_EQ(ETCH_ERROR, test->add(field));
+  //lock collection
+  test->lock();
+  //then try to add a field
+  EXPECT_EQ(ETCH_EINVAL, test->add(field));
+  //check size again
+  EXPECT_EQ(1, test->size());
+  delete test;
+}
\ No newline at end of file