Skip to content

Commit

Permalink
Add integral any poly types
Browse files Browse the repository at this point in the history
  • Loading branch information
Levi-Armstrong committed Jul 21, 2024
1 parent 03098fc commit d096ca0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tesseract_common/include/tesseract_common/any_poly.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ TESSERACT_COMMON_IGNORE_WARNINGS_POP
#include <tesseract_common/serialization.h>
#include <tesseract_common/type_erasure.h>

#include <string>

/** @brief If shared library, this must go in the header after the class definition */
#define TESSERACT_ANY_EXPORT_KEY(C, K) \
namespace tesseract_serialization::any_poly \
Expand Down Expand Up @@ -78,6 +80,7 @@ struct AnyConcept // NOLINT
A assign = c;
bool eq = (c == cp);
bool neq = (c != cp);
UNUSED(assign);
UNUSED(eq);
UNUSED(neq);
}
Expand Down Expand Up @@ -131,4 +134,10 @@ BOOST_CLASS_TRACKING(tesseract_common::AnyPolyBase, boost::serialization::track_
BOOST_CLASS_EXPORT_KEY(tesseract_common::AnyPoly)
BOOST_CLASS_TRACKING(tesseract_common::AnyPoly, boost::serialization::track_never)

TESSERACT_ANY_EXPORT_KEY(bool, IntegralBool)
TESSERACT_ANY_EXPORT_KEY(int, IntegralInt)
TESSERACT_ANY_EXPORT_KEY(unsigned, IntegralUnsigned)
TESSERACT_ANY_EXPORT_KEY(double, IntegralDouble)
TESSERACT_ANY_EXPORT_KEY(float, IntegralFloat)
TESSERACT_ANY_EXPORT_KEY(std::string, StdString)
#endif // TESSERACT_COMMON_ANY_POLY_H
7 changes: 7 additions & 0 deletions tesseract_common/src/any_poly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ TESSERACT_SERIALIZE_ARCHIVES_INSTANTIATE(tesseract_common::AnyPoly)

BOOST_CLASS_EXPORT_IMPLEMENT(tesseract_common::AnyPolyBase)
BOOST_CLASS_EXPORT_IMPLEMENT(tesseract_common::AnyPoly)

TESSERACT_ANY_EXPORT_IMPLEMENT(IntegralBool)
TESSERACT_ANY_EXPORT_IMPLEMENT(IntegralInt)
TESSERACT_ANY_EXPORT_IMPLEMENT(IntegralUnsigned)
TESSERACT_ANY_EXPORT_IMPLEMENT(IntegralDouble)
TESSERACT_ANY_EXPORT_IMPLEMENT(IntegralFloat)
TESSERACT_ANY_EXPORT_IMPLEMENT(StdString)
46 changes: 46 additions & 0 deletions tesseract_common/test/tesseract_common_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,52 @@ TEST(TesseractCommonUnit, anyUnit) // NOLINT
EXPECT_ANY_THROW(nany_type.as<tesseract_common::Toolpath>()); // NOLINT
}

template <typename T>
void runAnyPolyTest(T value, const std::string& type_str)
{
tesseract_common::AnyPoly any_type{ value };
EXPECT_TRUE(any_type.getType() == std::type_index(typeid(T)));
EXPECT_TRUE(any_type.as<T>() == value);

// Check clone
tesseract_common::AnyPoly any_copy = any_type;
EXPECT_TRUE(any_copy == any_type);
EXPECT_TRUE(any_copy.as<T>() == value);

const std::string filepath{ tesseract_common::getTempPath() + "any_" + type_str + "_type_boost.xml" };
{
std::ofstream os(filepath);
boost::archive::xml_oarchive oa(os);
oa << BOOST_SERIALIZATION_NVP(any_type);
}

tesseract_common::AnyPoly nany_type;
{
std::ifstream ifs(filepath);
assert(ifs.good());
boost::archive::xml_iarchive ia(ifs);

// restore the schedule from the archive
ia >> BOOST_SERIALIZATION_NVP(nany_type);
}

EXPECT_TRUE(nany_type.getType() == std::type_index(typeid(T)));
EXPECT_TRUE(nany_type.as<T>() == value);

// Test bad cast
EXPECT_ANY_THROW(nany_type.as<tesseract_common::Toolpath>()); // NOLINT
}

TEST(TesseractCommonUnit, anyIntegralTypesUnit) // NOLINT
{
runAnyPolyTest<bool>(true, "bool");
runAnyPolyTest<int>(-10, "int");
runAnyPolyTest<unsigned>(5, "unsigned");
runAnyPolyTest<double>(1.2, "double");
runAnyPolyTest<float>(-0.2F, "float");
runAnyPolyTest<std::string>("this", "string");
}

TEST(TesseractCommonUnit, anySharedPtrUnit) // NOLINT
{
tesseract_common::AnyPoly any_type;
Expand Down

0 comments on commit d096ca0

Please sign in to comment.