1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-29 21:56:54 +02:00

More refactoring

This commit is contained in:
AlexVinS 2014-12-21 19:25:12 +03:00
parent b86938de5e
commit 74161198c4
2 changed files with 25 additions and 37 deletions

View File

@ -238,26 +238,22 @@ void CConnection::sendPackToServer(const CPack &pack, PlayerColor player, ui32 r
void CConnection::disableStackSendingByID() void CConnection::disableStackSendingByID()
{ {
iser.sendStackInstanceByIds = false; CSerializer::sendStackInstanceByIds = false;
oser.sendStackInstanceByIds = false;
} }
void CConnection::enableStackSendingByID() void CConnection::enableStackSendingByID()
{ {
iser.sendStackInstanceByIds = true; CSerializer::sendStackInstanceByIds = true;
oser.sendStackInstanceByIds = true;
} }
void CConnection::disableSmartPointerSerialization() void CConnection::disableSmartPointerSerialization()
{ {
iser.smartPointerSerialization = false; iser.smartPointerSerialization = oser.smartPointerSerialization = false;
oser.smartPointerSerialization = false;
} }
void CConnection::enableSmartPointerSerializatoin() void CConnection::enableSmartPointerSerializatoin()
{ {
iser.smartPointerSerialization = true; iser.smartPointerSerialization = oser.smartPointerSerialization = true;
oser.smartPointerSerialization = true;
} }
void CConnection::prepareForSendingHeroes() void CConnection::prepareForSendingHeroes()
@ -279,12 +275,12 @@ void CConnection::enterPregameConnectionMode()
void CConnection::disableSmartVectorMemberSerialization() void CConnection::disableSmartVectorMemberSerialization()
{ {
iser.smartVectorMembersSerialization = oser.smartVectorMembersSerialization = false; CSerializer::smartVectorMembersSerialization = false;
} }
void CConnection::enableSmartVectorMemberSerializatoin() void CConnection::enableSmartVectorMemberSerializatoin()
{ {
iser.smartVectorMembersSerialization = oser.smartVectorMembersSerialization = true; CSerializer::smartVectorMembersSerialization = true;
} }
CSaveFile::CSaveFile( const std::string &fname ): serializer(this) CSaveFile::CSaveFile( const std::string &fname ): serializer(this)

View File

@ -438,15 +438,15 @@ public:
void addStdVecItems(CGameState *gs, LibClasses *lib = VLC); void addStdVecItems(CGameState *gs, LibClasses *lib = VLC);
}; };
class IBinaryWriter class IBinaryWriter : public virtual CSerializer
{ {
public: public:
virtual int write(const void * data, unsigned size) = 0; virtual int write(const void * data, unsigned size) = 0;
}; };
class DLL_LINKAGE CSaverBase : public virtual CSerializer class DLL_LINKAGE CSaverBase
{ {
private: protected:
IBinaryWriter * writer; IBinaryWriter * writer;
public: public:
CSaverBase(IBinaryWriter * w): writer(w){}; CSaverBase(IBinaryWriter * w): writer(w){};
@ -528,6 +528,7 @@ struct SaveIfStackInstance
return false; return false;
} }
}; };
template<typename Ser> template<typename Ser>
struct SaveIfStackInstance<Ser, CStackInstance *> struct SaveIfStackInstance<Ser, CStackInstance *>
{ {
@ -546,6 +547,7 @@ struct SaveIfStackInstance<Ser, CStackInstance *>
return true; return true;
} }
}; };
template<typename Ser,typename T> template<typename Ser,typename T>
struct LoadIfStackInstance struct LoadIfStackInstance
{ {
@ -655,7 +657,8 @@ public:
} }
}; };
template <typename T> class CPointerSaver : public CBasicPointerSaver template <typename T>
class CPointerSaver : public CBasicPointerSaver
{ {
public: public:
void savePtr(CSaverBase &ar, const void *data) const override void savePtr(CSaverBase &ar, const void *data) const override
@ -702,11 +705,6 @@ public:
addSaver(d); addSaver(d);
} }
// Serializer * This()
// {
// return static_cast<Serializer*>(this);
// }
template<class T> template<class T>
COSer & operator<<(const T &t) COSer & operator<<(const T &t)
{ {
@ -737,21 +735,21 @@ public:
if(!hlp) if(!hlp)
return; return;
if(smartVectorMembersSerialization) if(writer->smartVectorMembersSerialization)
{ {
typedef typename boost::remove_const<typename boost::remove_pointer<T>::type>::type TObjectType; typedef typename boost::remove_const<typename boost::remove_pointer<T>::type>::type TObjectType;
typedef typename VectorisedTypeFor<TObjectType>::type VType; typedef typename VectorisedTypeFor<TObjectType>::type VType;
typedef typename VectorizedIDType<TObjectType>::type IDType; typedef typename VectorizedIDType<TObjectType>::type IDType;
if(const auto *info = getVectorisedTypeInfo<VType, IDType>()) if(const auto *info = writer->getVectorisedTypeInfo<VType, IDType>())
{ {
IDType id = getIdFromVectorItem<VType>(*info, data); IDType id = writer->getIdFromVectorItem<VType>(*info, data);
*this << id; *this << id;
if(id != IDType(-1)) //vector id is enough if(id != IDType(-1)) //vector id is enough
return; return;
} }
} }
if(sendStackInstanceByIds) if(writer->sendStackInstanceByIds)
{ {
const bool gotSaved = SaveIfStackInstance<COSer,T>::invoke(*this, data); const bool gotSaved = SaveIfStackInstance<COSer,T>::invoke(*this, data);
if(gotSaved) if(gotSaved)
@ -960,15 +958,15 @@ public:
} }
}; };
class IBinaryReader class IBinaryReader : public virtual CSerializer
{ {
public: public:
virtual int read(void * data, unsigned size) = 0; virtual int read(void * data, unsigned size) = 0;
}; };
class DLL_LINKAGE CLoaderBase : public virtual CSerializer class DLL_LINKAGE CLoaderBase
{ {
private: protected:
IBinaryReader * reader; IBinaryReader * reader;
public: public:
CLoaderBase(IBinaryReader * r): reader(r){}; CLoaderBase(IBinaryReader * r): reader(r){};
@ -1240,24 +1238,24 @@ public:
return; return;
} }
if(smartVectorMembersSerialization) if(reader->smartVectorMembersSerialization)
{ {
typedef typename boost::remove_const<typename boost::remove_pointer<T>::type>::type TObjectType; //eg: const CGHeroInstance * => CGHeroInstance typedef typename boost::remove_const<typename boost::remove_pointer<T>::type>::type TObjectType; //eg: const CGHeroInstance * => CGHeroInstance
typedef typename VectorisedTypeFor<TObjectType>::type VType; //eg: CGHeroInstance -> CGobjectInstance typedef typename VectorisedTypeFor<TObjectType>::type VType; //eg: CGHeroInstance -> CGobjectInstance
typedef typename VectorizedIDType<TObjectType>::type IDType; typedef typename VectorizedIDType<TObjectType>::type IDType;
if(const auto *info = getVectorisedTypeInfo<VType, IDType>()) if(const auto *info = reader->getVectorisedTypeInfo<VType, IDType>())
{ {
IDType id; IDType id;
*this >> id; *this >> id;
if(id != IDType(-1)) if(id != IDType(-1))
{ {
data = static_cast<T>(getVectorItemFromId<VType, IDType>(*info, id)); data = static_cast<T>(reader->getVectorItemFromId<VType, IDType>(*info, id));
return; return;
} }
} }
} }
if(sendStackInstanceByIds) if(reader->sendStackInstanceByIds)
{ {
bool gotLoaded = LoadIfStackInstance<CISer,T>::invoke(* this, data); bool gotLoaded = LoadIfStackInstance<CISer,T>::invoke(* this, data);
if(gotLoaded) if(gotLoaded)
@ -1321,7 +1319,7 @@ public:
if(length > 500000) \ if(length > 500000) \
{ \ { \
logGlobal->warnStream() << "Warning: very big length: " << length;\ logGlobal->warnStream() << "Warning: very big length: " << length;\
reportState(logGlobal); \ reader->reportState(logGlobal); \
}; };
@ -1673,12 +1671,6 @@ public:
oser << t; oser << t;
return * this; return * this;
} }
void addStdVecItems(CGameState *gs, LibClasses *lib = VLC)
{
iser.addStdVecItems(gs, lib);
oser.addStdVecItems(gs, lib);
}
}; };
DLL_LINKAGE std::ostream &operator<<(std::ostream &str, const CConnection &cpc); DLL_LINKAGE std::ostream &operator<<(std::ostream &str, const CConnection &cpc);