forked from mirror/openmw-tes3mp
- changed namespace
This commit is contained in:
parent
db1b93498d
commit
5248917a6c
2 changed files with 99 additions and 94 deletions
|
@ -19,13 +19,13 @@ namespace ESM
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template< class Base > class DerivedClassStorage;
|
|
||||||
|
|
||||||
namespace MWMechanics
|
namespace MWMechanics
|
||||||
{
|
{
|
||||||
class AiPackage;
|
class AiPackage;
|
||||||
|
|
||||||
|
template< class Base > class DerivedClassStorage;
|
||||||
class AiTemporaryBase;
|
class AiTemporaryBase;
|
||||||
typedef DerivedClassStorage<AiTemporaryBase> AiState;
|
typedef DerivedClassStorage<AiTemporaryBase> AiState;
|
||||||
|
|
||||||
|
|
|
@ -8,110 +8,115 @@
|
||||||
#include <boost/static_assert.hpp>
|
#include <boost/static_assert.hpp>
|
||||||
#include <boost/type_traits/is_base_of.hpp>
|
#include <boost/type_traits/is_base_of.hpp>
|
||||||
|
|
||||||
|
namespace MWMechanics
|
||||||
|
{
|
||||||
|
|
||||||
/// \brief stores an object of any class derived from Base
|
/** \brief stores one object of any class derived from Base.
|
||||||
template< class Base >
|
* Requesting a certain dereived class via get() either returns
|
||||||
class DerivedClassStorage
|
* the stored object if it has the correct type or otherwise replaces
|
||||||
{
|
* it with an object of the requested type.
|
||||||
private:
|
*/
|
||||||
Base* mStorage;
|
template< class Base >
|
||||||
|
class DerivedClassStorage
|
||||||
// assert that Derived is derived from Base.
|
{
|
||||||
template< class Derived >
|
private:
|
||||||
void assert_derived()
|
Base* mStorage;
|
||||||
{
|
|
||||||
// c++11:
|
|
||||||
// static_assert( std::is_base_of<Base,Derived> , "DerivedClassStorage may only store derived classes" );
|
|
||||||
|
|
||||||
// boost:
|
// assert that Derived is derived from Base.
|
||||||
BOOST_STATIC_ASSERT(boost::is_base_of<Base,Derived>::value);//,"DerivedClassStorage may only store derived classes");
|
template< class Derived >
|
||||||
}
|
void assert_derived()
|
||||||
|
{
|
||||||
//if needed you have to provide a clone member function
|
// c++11:
|
||||||
DerivedClassStorage( const DerivedClassStorage& other );
|
// static_assert( std::is_base_of<Base,Derived> , "DerivedClassStorage may only store derived classes" );
|
||||||
DerivedClassStorage& operator=( const DerivedClassStorage& );
|
|
||||||
|
// boost:
|
||||||
public:
|
BOOST_STATIC_ASSERT(boost::is_base_of<Base,Derived>::value);//,"DerivedClassStorage may only store derived classes");
|
||||||
/// \brief returns reference to stored object or deletes it and creates a fitting
|
}
|
||||||
template< class Derived >
|
|
||||||
Derived& get()
|
|
||||||
{
|
|
||||||
assert_derived<Derived>();
|
|
||||||
|
|
||||||
Derived* result = dynamic_cast<Derived*>(mStorage);
|
//if needed you have to provide a clone member function
|
||||||
|
DerivedClassStorage( const DerivedClassStorage& other );
|
||||||
|
DerivedClassStorage& operator=( const DerivedClassStorage& );
|
||||||
|
|
||||||
if(!result)
|
public:
|
||||||
|
/// \brief returns reference to stored object or deletes it and creates a fitting
|
||||||
|
template< class Derived >
|
||||||
|
Derived& get()
|
||||||
|
{
|
||||||
|
assert_derived<Derived>();
|
||||||
|
|
||||||
|
Derived* result = dynamic_cast<Derived*>(mStorage);
|
||||||
|
|
||||||
|
if(!result)
|
||||||
|
{
|
||||||
|
if(mStorage)
|
||||||
|
delete mStorage;
|
||||||
|
mStorage = result = new Derived();
|
||||||
|
}
|
||||||
|
|
||||||
|
//return a reference to the (new allocated) object
|
||||||
|
return *result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template< class Derived >
|
||||||
|
void store( const Derived& payload )
|
||||||
|
{
|
||||||
|
assert_derived<Derived>();
|
||||||
|
if(mStorage)
|
||||||
|
delete mStorage;
|
||||||
|
mStorage = new Derived(payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// \brief takes ownership of the passed object
|
||||||
|
template< class Derived >
|
||||||
|
void moveIn( Derived* p )
|
||||||
|
{
|
||||||
|
assert_derived<Derived>();
|
||||||
|
if(mStorage)
|
||||||
|
delete mStorage;
|
||||||
|
mStorage = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// \brief gives away ownership of object. Throws exception if storage does not contain Derived or is empty.
|
||||||
|
template< class Derived >
|
||||||
|
Derived* moveOut()
|
||||||
|
{
|
||||||
|
assert_derived<Derived>();
|
||||||
|
|
||||||
|
|
||||||
|
if(!mStorage)
|
||||||
|
throw std::runtime_error("Cant move out: empty storage.");
|
||||||
|
|
||||||
|
Derived* result = dynamic_cast<Derived*>(mStorage);
|
||||||
|
|
||||||
|
if(!mStorage)
|
||||||
|
throw std::runtime_error("Cant move out: wrong type requested.");
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool empty() const
|
||||||
|
{
|
||||||
|
return mStorage == NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::type_info& getType() const
|
||||||
|
{
|
||||||
|
return typeid(mStorage);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
DerivedClassStorage():mStorage(NULL){};
|
||||||
|
~DerivedClassStorage()
|
||||||
{
|
{
|
||||||
if(mStorage)
|
if(mStorage)
|
||||||
delete mStorage;
|
delete mStorage;
|
||||||
mStorage = result = new Derived();
|
};
|
||||||
}
|
|
||||||
|
|
||||||
//return a reference to the (new allocated) object
|
|
||||||
return *result;
|
|
||||||
}
|
|
||||||
|
|
||||||
template< class Derived >
|
|
||||||
void store( const Derived& payload )
|
|
||||||
{
|
|
||||||
assert_derived<Derived>();
|
|
||||||
if(mStorage)
|
|
||||||
delete mStorage;
|
|
||||||
mStorage = new Derived(payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// \brief takes ownership of the passed object
|
|
||||||
template< class Derived >
|
|
||||||
void moveIn( Derived* p )
|
|
||||||
{
|
|
||||||
assert_derived<Derived>();
|
|
||||||
if(mStorage)
|
|
||||||
delete mStorage;
|
|
||||||
mStorage = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// \brief gives away ownership of object. Throws exception if storage does not contain Derived or is empty.
|
|
||||||
template< class Derived >
|
|
||||||
Derived* moveOut()
|
|
||||||
{
|
|
||||||
assert_derived<Derived>();
|
|
||||||
|
|
||||||
|
|
||||||
if(!mStorage)
|
|
||||||
throw std::runtime_error("Cant move out: empty storage.");
|
|
||||||
|
|
||||||
Derived* result = dynamic_cast<Derived*>(mStorage);
|
|
||||||
|
|
||||||
if(!mStorage)
|
|
||||||
throw std::runtime_error("Cant move out: wrong type requested.");
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool empty() const
|
|
||||||
{
|
|
||||||
return mStorage == NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::type_info& getType() const
|
|
||||||
{
|
|
||||||
return typeid(mStorage);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
DerivedClassStorage():mStorage(NULL){};
|
|
||||||
~DerivedClassStorage()
|
|
||||||
{
|
|
||||||
if(mStorage)
|
|
||||||
delete mStorage;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
namespace MWMechanics
|
|
||||||
{
|
|
||||||
/// \brief base class for the temporary storage of AiPackages.
|
/// \brief base class for the temporary storage of AiPackages.
|
||||||
/**
|
/**
|
||||||
* Each AI package with temporary values needs a AiPackageStorage class
|
* Each AI package with temporary values needs a AiPackageStorage class
|
||||||
|
|
Loading…
Reference in a new issue