2012-05-18 02:45:23 -05:00
|
|
|
//##################################################################################################
|
|
|
|
//
|
|
|
|
// Custom Visualization Core library
|
2013-09-20 08:22:29 -05:00
|
|
|
// Copyright (C) 2011-2013 Ceetron AS
|
|
|
|
//
|
|
|
|
// This library may be used under the terms of either the GNU General Public License or
|
|
|
|
// the GNU Lesser General Public License as follows:
|
|
|
|
//
|
|
|
|
// GNU General Public License Usage
|
|
|
|
// This library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
//
|
|
|
|
// See the GNU General Public License at <<http://www.gnu.org/licenses/gpl.html>>
|
|
|
|
// for more details.
|
|
|
|
//
|
|
|
|
// GNU Lesser General Public License Usage
|
|
|
|
// This library is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
//
|
|
|
|
// See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
|
|
|
|
// for more details.
|
2012-05-18 02:45:23 -05:00
|
|
|
//
|
|
|
|
//##################################################################################################
|
|
|
|
|
2013-09-20 08:22:29 -05:00
|
|
|
|
2012-05-18 02:45:23 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <map>
|
2016-11-24 01:03:45 -06:00
|
|
|
#include <vector>
|
2015-07-29 07:19:43 -05:00
|
|
|
#include <cstddef>
|
|
|
|
|
|
|
|
// Taken from gtest.h
|
|
|
|
//
|
|
|
|
// Due to C++ preprocessor weirdness, we need double indirection to
|
|
|
|
// concatenate two tokens when one of them is __LINE__. Writing
|
|
|
|
//
|
|
|
|
// foo ## __LINE__
|
|
|
|
//
|
|
|
|
// will result in the token foo__LINE__, instead of foo followed by
|
|
|
|
// the current line number. For more details, see
|
|
|
|
// http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.6
|
|
|
|
#define CAF_FACTORY_CONCATENATE_STRINGS(foo, bar) CAF_FACTORY_CONCATENATE_STRINGS_IMPL_(foo, bar)
|
|
|
|
#define CAF_FACTORY_CONCATENATE_STRINGS_IMPL_(foo, bar) foo ## bar
|
|
|
|
|
2016-06-08 05:13:49 -05:00
|
|
|
#define CAF_UNIQUE_COMPILE_UNIT_VAR_NAME(foo) CAF_FACTORY_CONCATENATE_STRINGS(foo, __LINE__)
|
2015-07-29 07:19:43 -05:00
|
|
|
|
|
|
|
#define CAF_FACTORY_REGISTER(BaseType, TypeToCreate, KeyType, key) \
|
2016-06-08 05:13:49 -05:00
|
|
|
static bool CAF_UNIQUE_COMPILE_UNIT_VAR_NAME(my##TypeToCreate) = caf::Factory<BaseType, KeyType>::instance()->registerCreator<TypeToCreate>(key)
|
2012-05-18 02:45:23 -05:00
|
|
|
|
|
|
|
namespace caf
|
|
|
|
{
|
|
|
|
|
|
|
|
//==================================================================================================
|
|
|
|
/// A generic Factory class template
|
|
|
|
/// Usage:
|
2013-05-16 04:14:57 -05:00
|
|
|
/// Simply add the classes that is supposed to be created by the factory by doing the folowing:
|
|
|
|
///
|
|
|
|
/// caf::Factory<BaseType, KeyType>::instance()->registerCreator<TypeToCreate>(key);
|
|
|
|
///
|
|
|
|
/// This must only be done once for each TypeToCreate. It will assert if you try to do it several times.
|
|
|
|
/// This method returns a bool to make it possible to make this initialization as a static variable initialization.
|
|
|
|
/// That is useful if you do not want a centralized registering (but rather making each class register itself):
|
|
|
|
///
|
|
|
|
/// static bool uniqueVarname = caf::Factory<BaseType, KeyType>::instance()->registerCreator<TypeToCreate>(key);
|
|
|
|
///
|
2015-07-29 07:19:43 -05:00
|
|
|
/// You can also use the macro CAF_FACTORY_REGISTER(BaseType, TypeToCreate, KeyType, key)
|
|
|
|
///
|
2013-05-16 04:14:57 -05:00
|
|
|
/// See also cafPdmUiFieldEditorHandle.h for an advanced example.
|
|
|
|
///
|
2012-06-26 09:10:41 -05:00
|
|
|
/// When you need an object:
|
2013-05-16 04:14:57 -05:00
|
|
|
///
|
2012-05-18 02:45:23 -05:00
|
|
|
/// BaseType* newObject = caf::Factory<BaseType, KeyType>::instance()->create(key);
|
|
|
|
//==================================================================================================
|
|
|
|
|
2013-05-16 04:14:57 -05:00
|
|
|
|
2012-05-18 02:45:23 -05:00
|
|
|
template<typename BaseType, typename KeyType>
|
|
|
|
class Factory
|
|
|
|
{
|
2012-06-26 09:10:41 -05:00
|
|
|
class ObjectCreatorBase;
|
2012-05-18 02:45:23 -05:00
|
|
|
public:
|
2012-06-26 09:10:41 -05:00
|
|
|
typedef typename std::map<KeyType, ObjectCreatorBase*>::iterator iterator_type;
|
|
|
|
|
2012-05-18 02:45:23 -05:00
|
|
|
static Factory<BaseType, KeyType> * instance()
|
|
|
|
{
|
|
|
|
static Factory<BaseType, KeyType>* fact = new Factory<BaseType, KeyType>;
|
|
|
|
return fact;
|
|
|
|
}
|
|
|
|
|
|
|
|
template< typename TypeToCreate >
|
2012-06-26 09:10:41 -05:00
|
|
|
bool registerCreator(const KeyType& key)
|
2012-05-18 02:45:23 -05:00
|
|
|
{
|
2012-06-26 09:10:41 -05:00
|
|
|
iterator_type entryIt;
|
2012-05-18 02:45:23 -05:00
|
|
|
|
|
|
|
entryIt = m_factoryMap.find(key);
|
|
|
|
if (entryIt == m_factoryMap.end())
|
|
|
|
{
|
|
|
|
m_factoryMap[key] = new ObjectCreator<TypeToCreate>();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(key != entryIt->first); // classNameKeyword has already been used
|
|
|
|
assert(false); // To be sure ..
|
2012-06-26 09:10:41 -05:00
|
|
|
return false;
|
2012-05-18 02:45:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-26 09:10:41 -05:00
|
|
|
BaseType* create(const KeyType& key)
|
2012-05-18 02:45:23 -05:00
|
|
|
{
|
2012-06-26 09:10:41 -05:00
|
|
|
iterator_type entryIt;
|
|
|
|
|
2012-05-18 02:45:23 -05:00
|
|
|
entryIt = m_factoryMap.find(key);
|
|
|
|
if (entryIt != m_factoryMap.end())
|
|
|
|
{
|
|
|
|
return entryIt->second->create();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 01:03:45 -06:00
|
|
|
std::vector<KeyType> allKeys()
|
|
|
|
{
|
|
|
|
std::vector<KeyType> keys;
|
|
|
|
|
|
|
|
iterator_type entryIt;
|
|
|
|
for (entryIt = m_factoryMap.begin(); entryIt != m_factoryMap.end(); ++entryIt)
|
|
|
|
{
|
|
|
|
keys.push_back(entryIt->first);
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys;
|
|
|
|
}
|
2012-06-26 09:10:41 -05:00
|
|
|
|
2012-05-18 02:45:23 -05:00
|
|
|
private:
|
|
|
|
Factory () {}
|
|
|
|
~Factory()
|
|
|
|
{
|
2012-06-26 09:10:41 -05:00
|
|
|
iterator_type entryIt;
|
|
|
|
|
2012-05-18 02:45:23 -05:00
|
|
|
for (entryIt = m_factoryMap.begin(); entryIt != m_factoryMap.end(); ++entryIt)
|
|
|
|
{
|
|
|
|
delete(entryIt->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Internal helper classes
|
|
|
|
|
|
|
|
class ObjectCreatorBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ObjectCreatorBase() {}
|
|
|
|
virtual ~ObjectCreatorBase() {}
|
|
|
|
virtual BaseType * create() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
template< typename TypeToCreate >
|
|
|
|
class ObjectCreator : public ObjectCreatorBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual BaseType * create() { return new TypeToCreate(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Map to store factory
|
|
|
|
std::map<KeyType, ObjectCreatorBase*> m_factoryMap;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-06-26 09:10:41 -05:00
|
|
|
}//End of namespace caf
|