1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-02-07 13:08:09 +02:00

Made CTypeList threadsafe

This commit is contained in:
AlexVinS 2015-10-25 03:26:48 +03:00
parent 687a82886f
commit 0f9de1c560
2 changed files with 10 additions and 2 deletions

View File

@ -450,6 +450,7 @@ CTypeList::TypeInfoPtr CTypeList::registerType( const std::type_info *type )
ui16 CTypeList::getTypeID(const std::type_info * type) const
{
TSharedLock lock(mx);
auto i = typeInfos.find(type);
if(i != typeInfos.end())
return i->second->typeID;

View File

@ -147,7 +147,11 @@ public:
const char *name;
std::vector<TypeInfoPtr> children, parents;
};
typedef boost::shared_mutex TMutex;
typedef boost::unique_lock<TMutex> TUniqueLock;
typedef boost::shared_lock<TMutex> TSharedLock;
private:
mutable TMutex mx;
std::map<const std::type_info *, TypeInfoPtr, TypeComparer> typeInfos;
std::map<std::pair<TypeInfoPtr, TypeInfoPtr>, std::unique_ptr<const IPointerCaster>> casters; //for each pair <Base, Der> we provide a caster (each registered relations creates a single entry here)
@ -161,6 +165,7 @@ private:
template<boost::any(IPointerCaster::*CastingFunction)(const boost::any &) const>
boost::any castHelper(boost::any inputPtr, const std::type_info *fromArg, const std::type_info *toArg) const
{
TSharedLock lock(mx);
auto typesSequence = castSequence(fromArg, toArg);
boost::any ptr = inputPtr;
@ -179,13 +184,16 @@ private:
return ptr;
}
TypeInfoPtr registerType(const std::type_info *type);
TypeInfoPtr getTypeDescriptor(const std::type_info *type, bool throws = true) const; //if not throws, failure returns nullptr
TypeInfoPtr registerType(const std::type_info *type);
public:
CTypeList();
template <typename Base, typename Derived>
void registerType(const Base * b = nullptr, const Derived * d = nullptr)
{
TUniqueLock lock(mx);
static_assert(std::is_base_of<Base, Derived>::value, "First registerType template parameter needs to ba a base class of the second one.");
static_assert(std::has_virtual_destructor<Base>::value, "Base class needs to have a virtual destructor.");
static_assert(!std::is_same<Base, Derived>::value, "Parameters of registerTypes should be two diffrenet types.");
@ -200,7 +208,6 @@ public:
}
ui16 getTypeID(const std::type_info *type) const;
TypeInfoPtr getTypeDescriptor(const std::type_info *type, bool throws = true) const; //if not throws, failure returns nullptr
template <typename T>
ui16 getTypeID(const T * t = nullptr) const