#ifndef UTILITY_H_INCLUDED #define UTILITY_H_INCLUDED #include #include #include #include #include // // GEOS Unit Test utilities // namespace utility { // // Type cast helper utilities // template inline bool isInstanceOf(InstanceType* instance) { return ( 0 != dynamic_cast(instance) ); } template inline Type* instanceOf(InstanceType* instance) { return dynamic_cast(instance); } // // Geometries structure comparators // template inline bool isSameStructure(T1 lhs, T2 rhs) { // Different types can't have the smame structure return false; } template inline bool isSameStructure(T* lhs, T* rhs) { using geos::geom::Polygon; using geos::geom::GeometryCollection; assert( 0 != lhs ); assert( 0 != rhs ); // Both are empty if (!(lhs->isEmpty() == rhs->isEmpty())) return false; // Both are non-empty if (!(!lhs->isEmpty()) == (!rhs->isEmpty())) return false; // Both are valid if (!(lhs->isValid() == rhs->isValid())) return false; // Dispatch to run more specific testes if (isInstanceOf(lhs) && isInstanceOf(rhs)) { return isSameStructure(instanceOf(lhs), instanceOf(rhs)); } else if (isInstanceOf(lhs) && isInstanceOf(rhs)) { return isSameStructure(instanceOf(lhs), instanceOf(rhs)); } return true; } template <> inline bool isSameStructure(geos::geom::Polygon* lhs, geos::geom::Polygon* rhs) { assert( 0 != lhs ); assert( 0 != rhs ); return (lhs->getNumInteriorRing() == rhs->getNumInteriorRing()); } template <> inline bool isSameStructure(geos::geom::GeometryCollection* lhs, geos::geom::GeometryCollection* rhs) { using geos::geom::Geometry; assert( 0 != lhs ); assert( 0 != rhs ); if (lhs->getNumGeometries() != rhs->getNumGeometries()) return false; for (unsigned int i = 0, n = lhs->getNumGeometries(); i < n; ++i) { // Dirty, but necessary! // isSameStructure promises to not to try to change geometries // @@ why doesn't you take const pointers ? Geometry* g1 = const_cast(lhs->getGeometryN(i)); Geometry* g2 = const_cast(rhs->getGeometryN(i)); if (!isSameStructure(g1, g2)) { return false; } } return true; } } // namespace unit #endif // #ifndef UTILITY_H_INCLUDED