Snake/SnakeServer/ClientProtocol/factorynetobjects.h

41 lines
956 B
C
Raw Normal View History

2019-03-05 00:48:32 +03:00
#ifndef FACTORYNETOBJECTS_H
#define FACTORYNETOBJECTS_H
#include "clientprotocol_global.h"
2019-04-29 14:39:48 +03:00
#include "basenetworkobject.h"
#include <QMap>
#include <typeinfo>
2019-03-05 00:48:32 +03:00
namespace ClientProtocol {
2019-04-29 14:39:48 +03:00
extern QMap<quint8, BaseNetworkObject*> map;
extern QMap<quint8, NetworkClassSize> types_sizes;
2019-03-05 00:48:32 +03:00
class CLIENTPROTOCOLSHARED_EXPORT FactoryNetObjects {
2019-04-29 14:39:48 +03:00
private:
2019-03-05 00:48:32 +03:00
public:
FactoryNetObjects() = delete;
2019-04-29 14:39:48 +03:00
static BaseNetworkObject *build(quint8 type);
2019-04-30 16:29:36 +03:00
static BaseNetworkObject *build(quint8 type, const QByteArray& array);
2019-04-29 14:39:48 +03:00
static NetworkClassSize getSize(quint8 type);
static bool isRegisteredType(quint8 type);
static bool isInited();
2019-04-20 15:22:00 +03:00
2019-04-29 14:39:48 +03:00
template<typename T>
static bool regType(quint8 id) {
if (map.contains(id)) {
return false;
2019-03-05 00:48:32 +03:00
}
2019-04-29 14:39:48 +03:00
auto tempObj = new T();
map.insert(id, tempObj);
types_sizes.insert(id, tempObj->classSize());
return true;
2019-03-05 00:48:32 +03:00
}
};
}
#endif // FACTORYNETOBJECTS_H