[cig-commits] r13177 - in cs/cigma/trunk: src tests/libcigma
luis at geodynamics.org
luis at geodynamics.org
Wed Oct 29 15:11:56 PDT 2008
Author: luis
Date: 2008-10-29 15:11:55 -0700 (Wed, 29 Oct 2008)
New Revision: 13177
Modified:
cs/cigma/trunk/src/Cell.cpp
cs/cigma/trunk/src/Cell.h
cs/cigma/trunk/tests/libcigma/CellTest.cpp
cs/cigma/trunk/tests/libcigma/CellTest.h
Log:
Added factory method Cell::New(const char*)
Modified: cs/cigma/trunk/src/Cell.cpp
===================================================================
--- cs/cigma/trunk/src/Cell.cpp 2008-10-29 22:11:53 UTC (rev 13176)
+++ cs/cigma/trunk/src/Cell.cpp 2008-10-29 22:11:55 UTC (rev 13177)
@@ -1,12 +1,57 @@
#include <cstdlib>
#include <cassert>
+#include <map>
+#include "Exception.h"
+#include "Numeric.h"
#include "Cell.h"
-#include "Numeric.h"
-#include "Exception.h"
+#include "fe_hex8.h"
+#include "fe_tet4.h"
+#include "fe_quad4.h"
+#include "fe_tri3.h"
using namespace cigma;
+// ----------------------------------------------------------------------------
+typedef std::map<std::string, Cell::type> CellTypeMap;
+typedef CellTypeMap::value_type CellTypeMapEntry;
+
+CellTypeMapEntry cellTypeMapEntries[] = {
+ CellTypeMapEntry("hex8", Cell::HEX8),
+ CellTypeMapEntry("tet4", Cell::TET4),
+ CellTypeMapEntry("quad4", Cell::QUAD4),
+ CellTypeMapEntry("tri3", Cell::TRI3)
+};
+
+int numTypeEntries = sizeof(cellTypeMapEntries) / sizeof(CellTypeMapEntry);
+
+static CellTypeMap celltypes(cellTypeMapEntries, cellTypeMapEntries + numTypeEntries);
+
+Cell::type Cell::string2type(std::string name)
+{
+ return celltypes[name];
+}
+
+boost::shared_ptr<Cell> Cell::New(Cell::type cellType)
+{
+ boost::shared_ptr<Cell> tmp;
+ switch (cellType)
+ {
+ case HEX8: tmp.reset(new hex8); break;
+ case TET4: tmp.reset(new tet4); break;
+ case QUAD4: tmp.reset(new quad4); break;
+ case TRI3: tmp.reset(new tri3); break;
+ }
+ return tmp;
+}
+
+boost::shared_ptr<Cell> Cell::New(const char *name)
+{
+ return Cell::New(string2type(std::string(name)));
+}
+
+// ----------------------------------------------------------------------------
+
Cell::Cell()
{
//cout << "Calling Cell()" << endl;
Modified: cs/cigma/trunk/src/Cell.h
===================================================================
--- cs/cigma/trunk/src/Cell.h 2008-10-29 22:11:53 UTC (rev 13176)
+++ cs/cigma/trunk/src/Cell.h 2008-10-29 22:11:55 UTC (rev 13177)
@@ -1,8 +1,8 @@
#ifndef __CIGMA_CELL_H__
#define __CIGMA_CELL_H__
+#include <boost/shared_ptr.hpp>
#include "Quadrature.h"
-#include <boost/shared_ptr.hpp>
namespace cigma
{
@@ -43,6 +43,9 @@
} type;
virtual type cell_type() const = 0;
+ static type string2type(std::string name);
+ static boost::shared_ptr<Cell> New(Cell::type cellType);
+ static boost::shared_ptr<Cell> New(const char *name);
virtual boost::shared_ptr<Quadrature> default_quadrature() = 0; // XXX: move this out of Cell.h
Modified: cs/cigma/trunk/tests/libcigma/CellTest.cpp
===================================================================
--- cs/cigma/trunk/tests/libcigma/CellTest.cpp 2008-10-29 22:11:53 UTC (rev 13176)
+++ cs/cigma/trunk/tests/libcigma/CellTest.cpp 2008-10-29 22:11:55 UTC (rev 13177)
@@ -1,6 +1,9 @@
#include <iostream>
using namespace std;
+#include <boost/shared_ptr.hpp>
+using boost::shared_ptr;
+
#include "CellTest.h"
using namespace libcigma;
@@ -105,6 +108,21 @@
}
}
+void CellTest::test_factory()
+{
+ shared_ptr<Cell> cell_tet4 = Cell::New(Cell::TET4);
+ shared_ptr<Cell> cell_hex8 = Cell::New("hex8");
+ shared_ptr<Cell> cell_quad4 = Cell::New("quad4");
+ shared_ptr<Cell> cell_tri3 = Cell::New("tri3");
+
+ CPPUNIT_ASSERT_EQUAL(cell_tet4->cell_type(), Cell::TET4);
+ CPPUNIT_ASSERT_EQUAL(cell_hex8->cell_type(), Cell::HEX8);
+ CPPUNIT_ASSERT_EQUAL(cell_quad4->cell_type(), Cell::QUAD4);
+ CPPUNIT_ASSERT_EQUAL(cell_tri3->cell_type(), Cell::TRI3);
+
+ // XXX: pass bad string to Cell::New and handle exception
+}
+
void CellTest::test_hex8()
{
hex8 cell;
Modified: cs/cigma/trunk/tests/libcigma/CellTest.h
===================================================================
--- cs/cigma/trunk/tests/libcigma/CellTest.h 2008-10-29 22:11:53 UTC (rev 13176)
+++ cs/cigma/trunk/tests/libcigma/CellTest.h 2008-10-29 22:11:55 UTC (rev 13177)
@@ -11,6 +11,7 @@
{
CPPUNIT_TEST_SUITE(CellTest);
CPPUNIT_TEST(test_copy);
+ CPPUNIT_TEST(test_factory);
CPPUNIT_TEST(test_hex8);
CPPUNIT_TEST(test_tet4);
CPPUNIT_TEST(test_quad4);
@@ -21,6 +22,7 @@
CellTest() {}
~CellTest() {}
void test_copy();
+ void test_factory();
void test_hex8();
void test_tet4();
void test_quad4();
More information about the CIG-COMMITS
mailing list