Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jslee02 committed Mar 30, 2017
1 parent 5fdd74d commit 972c269
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 40 deletions.
8 changes: 4 additions & 4 deletions dart/common/Factory.hpp
Expand Up @@ -59,10 +59,10 @@ template <typename KeyT,
class Factory final
{
public:
using ReturnType = SmartPointerT<BaseT>;
using Creator = std::function<ReturnType(Args...)>;
using CreatorMap = std::map<KeyT, Creator>;
using This = Factory<KeyT, BaseT, SmartPointerT>;
using CreatorReturnType = SmartPointerT<BaseT>;
using Creator = std::function<CreatorReturnType(Args...)>;
using CreatorMap = std::map<KeyT, Creator>;
using RegisterResult = std::pair<typename CreatorMap::iterator, bool>;

/// Registers a object creator function with a key.
Expand All @@ -85,7 +85,7 @@ class Factory final

/// Creates an object of the class that is registered with a key. Returns
/// nullptr if there is no object creator function associated with the key.
static ReturnType create(const KeyT& key, Args&&... args);
static CreatorReturnType create(const KeyT& key, Args&&... args);
// TODO(JS): Add create() for creating smart_pointers
// (see: https://github.com/dartsim/dart/pull/845)

Expand Down
4 changes: 2 additions & 2 deletions dart/common/detail/Factory-impl.hpp
Expand Up @@ -93,7 +93,7 @@ typename Factory<KeyT, BaseT, SmartPointerT, Args...>::RegisterResult
Factory<KeyT, BaseT, SmartPointerT, Args...>::registerCreator(const KeyT& key)
{
return registerCreator(
key, [](Args&&... args) -> ReturnType
key, [](Args&&... args) -> CreatorReturnType
{
return DefaultCreator<Derived, SmartPointerT, Args...>::run(
std::forward<Args>(args)...);
Expand Down Expand Up @@ -138,7 +138,7 @@ template <typename KeyT,
typename BaseT,
template<typename...> class SmartPointerT,
typename... Args>
typename Factory<KeyT, BaseT, SmartPointerT, Args...>::ReturnType
typename Factory<KeyT, BaseT, SmartPointerT, Args...>::CreatorReturnType
Factory<KeyT, BaseT, SmartPointerT, Args...>::create(
const KeyT& key, Args&&... args)
{
Expand Down
65 changes: 31 additions & 34 deletions unittests/unit/test_Factory.cpp
Expand Up @@ -53,6 +53,10 @@ enum class ObjectTypeEnumClass
class Fruit
{
public:
using FactoryEnum = common::Factory<ObjectTypeEnum, Fruit>;
using FactoryEnumClass = common::Factory<ObjectTypeEnumClass, Fruit>;
using FactoryString = common::Factory<std::string, Fruit>;

virtual std::string getName() const = 0;
};

Expand Down Expand Up @@ -148,67 +152,61 @@ TEST(Factory, Create)
//----------------------------------------------------------------------------
// enum class key type
//----------------------------------------------------------------------------
using FruitFactoryEnum = common::Factory<ObjectTypeEnum, Fruit>;

EXPECT_TRUE(!FruitFactoryEnum::create(OT_None));
EXPECT_TRUE(!Fruit::FactoryEnum::create(OT_None));

EXPECT_TRUE(FruitFactoryEnum::canCreate(OT_Apple));
EXPECT_EQ(FruitFactoryEnum::create(OT_Apple)->getName(), "apple");
EXPECT_TRUE(Fruit::FactoryEnum::canCreate(OT_Apple));
EXPECT_EQ(Fruit::FactoryEnum::create(OT_Apple)->getName(), "apple");

// Can't create an orange since it's not registered yet.
EXPECT_TRUE(!FruitFactoryEnum::canCreate(OT_Orange));
EXPECT_TRUE(!Fruit::FactoryEnum::canCreate(OT_Orange));

// Once it's registered we can create an orange.
FruitFactoryEnum::registerCreator<Orange>(OT_Orange);
EXPECT_TRUE(FruitFactoryEnum::canCreate(OT_Orange));
EXPECT_EQ(FruitFactoryEnum::create(OT_Orange)->getName(), "orange");
Fruit::FactoryEnum::registerCreator<Orange>(OT_Orange);
EXPECT_TRUE(Fruit::FactoryEnum::canCreate(OT_Orange));
EXPECT_EQ(Fruit::FactoryEnum::create(OT_Orange)->getName(), "orange");

FruitFactoryEnum::unregisterCreator(OT_Orange);
EXPECT_TRUE(!FruitFactoryEnum::canCreate(OT_Orange));
Fruit::FactoryEnum::unregisterCreator(OT_Orange);
EXPECT_TRUE(!Fruit::FactoryEnum::canCreate(OT_Orange));

//----------------------------------------------------------------------------
// enum class key type
//----------------------------------------------------------------------------
using FruitFactoryEnumClass = common::Factory<ObjectTypeEnumClass, Fruit>;

EXPECT_TRUE(!FruitFactoryEnumClass::create(ObjectTypeEnumClass::None));
EXPECT_TRUE(!Fruit::FactoryEnumClass::create(ObjectTypeEnumClass::None));

EXPECT_TRUE(FruitFactoryEnumClass::canCreate(ObjectTypeEnumClass::Apple));
EXPECT_EQ(FruitFactoryEnumClass::create(
EXPECT_TRUE(Fruit::FactoryEnumClass::canCreate(ObjectTypeEnumClass::Apple));
EXPECT_EQ(Fruit::FactoryEnumClass::create(
ObjectTypeEnumClass::Apple)->getName(), "apple");

// Can't create an orange since it's not registered yet.
EXPECT_TRUE(!FruitFactoryEnumClass::canCreate(ObjectTypeEnumClass::Orange));
EXPECT_TRUE(!Fruit::FactoryEnumClass::canCreate(ObjectTypeEnumClass::Orange));

// Once it's registered we can create an orange.
FruitFactoryEnumClass::registerCreator<Orange>(ObjectTypeEnumClass::Orange);
EXPECT_TRUE(FruitFactoryEnumClass::canCreate(ObjectTypeEnumClass::Orange));
EXPECT_EQ(FruitFactoryEnumClass::create(
Fruit::FactoryEnumClass::registerCreator<Orange>(ObjectTypeEnumClass::Orange);
EXPECT_TRUE(Fruit::FactoryEnumClass::canCreate(ObjectTypeEnumClass::Orange));
EXPECT_EQ(Fruit::FactoryEnumClass::create(
ObjectTypeEnumClass::Orange)->getName(), "orange");

FruitFactoryEnumClass::unregisterCreator(ObjectTypeEnumClass::Orange);
EXPECT_TRUE(!FruitFactoryEnumClass::canCreate(ObjectTypeEnumClass::Orange));
Fruit::FactoryEnumClass::unregisterCreator(ObjectTypeEnumClass::Orange);
EXPECT_TRUE(!Fruit::FactoryEnumClass::canCreate(ObjectTypeEnumClass::Orange));

//----------------------------------------------------------------------------
// std::string key type
//----------------------------------------------------------------------------
using FruitFactoryString = common::Factory<std::string, Fruit>;
EXPECT_TRUE(!Fruit::FactoryString::create("none"));

EXPECT_TRUE(!FruitFactoryString::create("none"));

EXPECT_TRUE(FruitFactoryString::canCreate("apple"));
EXPECT_EQ(FruitFactoryString::create("apple")->getName(), "apple");
EXPECT_TRUE(Fruit::FactoryString::canCreate("apple"));
EXPECT_EQ(Fruit::FactoryString::create("apple")->getName(), "apple");

// Can't create an orange since it's not registered yet.
EXPECT_TRUE(!FruitFactoryString::canCreate("orange"));
EXPECT_TRUE(!Fruit::FactoryString::canCreate("orange"));

// Once it's registered we can create an orange.
FruitFactoryString::registerCreator<Orange>("orange");
EXPECT_TRUE(FruitFactoryString::canCreate("orange"));
EXPECT_EQ(FruitFactoryString::create("orange")->getName(), "orange");
Fruit::FactoryString::registerCreator<Orange>("orange");
EXPECT_TRUE(Fruit::FactoryString::canCreate("orange"));
EXPECT_EQ(Fruit::FactoryString::create("orange")->getName(), "orange");

FruitFactoryString::unregisterCreator("orange");
EXPECT_TRUE(!FruitFactoryString::canCreate("orange"));
Fruit::FactoryString::unregisterCreator("orange");
EXPECT_TRUE(!Fruit::FactoryString::canCreate("orange"));
}

//==============================================================================
Expand Down Expand Up @@ -240,7 +238,6 @@ TEST(Factory, VariousCreatorFunctions)
FruitFactory::registerCreator(std::string("orange"), createOrange);
}


//==============================================================================
TEST(Factory, CreateWithArguments)
{
Expand Down

0 comments on commit 972c269

Please sign in to comment.