Add stmp::make_unique

This template function is a polyfill of `std::make_unique`, which is
unavailable in GCC 4.9 (should we even be supporting this old thing?).
This commit is contained in:
yvt 2019-07-20 16:17:10 +09:00
parent 1b67c4eff0
commit 6e262ea6ec
No known key found for this signature in database
GPG Key ID: 48F2768FA8D07C92
8 changed files with 22 additions and 18 deletions

View File

@ -341,8 +341,8 @@ namespace spades {
spec.samples = (int)s_ysrBufferSize;
spec.channels = 2;
sdlAudioDevice = std::unique_ptr<SdlAudioDevice>(
new SdlAudioDevice(nullptr, SDL_FALSE, spec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE));
sdlAudioDevice = stmp::make_unique<SdlAudioDevice>(nullptr, SDL_FALSE, spec,
SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
YsrContext::InitParam param;
param.maxPolyphonics = s_maxPolyphonics;

View File

@ -380,7 +380,7 @@ namespace spades {
if (cg_debugCorpse) {
if (name == "p" && down) {
Player &victim = world->GetLocalPlayer().value();
std::unique_ptr<Corpse> corp{new Corpse(*renderer, *map, victim)};
auto corp = stmp::make_unique<Corpse>(*renderer, *map, victim);
corp->AddImpulse(victim.GetFront() * 32.f);
corpses.emplace_back(std::move(corp));

View File

@ -838,7 +838,7 @@ namespace spades {
// create ragdoll corpse
if (cg_ragdoll && victim.GetTeamId() < 2) {
std::unique_ptr<Corpse> corp{new Corpse(*renderer, *map, victim)};
auto corp = stmp::make_unique<Corpse>(*renderer, *map, victim);
if (&victim == world->GetLocalPlayer())
lastMyCorpse = corp.get();

View File

@ -47,7 +47,7 @@ namespace spades {
void Run() override {
SPADES_MARK_FUNCTION();
std::unique_ptr<Result> result{new Result()};
auto result = stmp::make_unique<Result>();
try {
DeflateStream inflate(&*rawDataReader, CompressModeDecompress, false);

View File

@ -940,9 +940,9 @@ namespace spades {
default: SPRaise("Received invalid weapon: %d", weapon);
}
std::unique_ptr<Player> p{new Player(*GetWorld(), pId, wType, team,
savedPlayerPos[pId],
GetWorld()->GetTeam(team).color)};
auto p = stmp::make_unique<Player>(*GetWorld(), pId, wType, team,
savedPlayerPos[pId],
GetWorld()->GetTeam(team).color);
p->SetHeldBlockColor(color);
// p->SetOrientation(savedPlayerFront[pId]);
switch (tool) {
@ -1030,10 +1030,9 @@ namespace spades {
default: SPRaise("Received invalid weapon: %d", weapon);
}
std::unique_ptr<Player> p{new Player(*GetWorld(), pId, wType, team,
savedPlayerPos[pId],
GetWorld()->GetTeam(team).color)};
auto p =
stmp::make_unique<Player>(*GetWorld(), pId, wType, team, savedPlayerPos[pId],
GetWorld()->GetTeam(team).color);
p->SetPosition(pos);
GetWorld()->SetPlayer(pId, std::move(p));
@ -1166,7 +1165,7 @@ namespace spades {
int mode = reader.ReadByte();
if (mode == 0) {
// CTF
std::unique_ptr<CTFGameMode> mode{new CTFGameMode()};
auto mode = stmp::make_unique<CTFGameMode>();
CTFGameMode::Team &mt1 = mode->GetTeam(0);
CTFGameMode::Team &mt2 = mode->GetTeam(1);
@ -1208,7 +1207,7 @@ namespace spades {
GetWorld()->SetMode(std::move(mode));
} else {
// TC
std::unique_ptr<TCGameMode> mode{new TCGameMode(*GetWorld())};
auto mode = stmp::make_unique<TCGameMode>(*GetWorld());
int numTer = reader.ReadByte();
for (int i = 0; i < numTer; i++) {

View File

@ -795,7 +795,7 @@ namespace spades {
vel += GetVelocty();
if (this == world.GetLocalPlayer()) {
std::unique_ptr<Grenade> gren{new Grenade(world, muzzle, vel, fuse)};
auto gren = stmp::make_unique<Grenade>(world, muzzle, vel, fuse);
if (world.GetListener())
world.GetListener()->PlayerThrewGrenade(*this, *gren);
world.AddGrenade(std::move(gren));

View File

@ -383,4 +383,9 @@ namespace stmp {
template <class Fn, class T> function<T, Fn> make_fn(T &&inner) {
return function<T, Fn>(std::move(inner));
}
/** Polyfill of `std::make_unique` for compilers which don't support it. */
template <class T, class... Args> std::unique_ptr<T> make_unique(Args &&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
} // namespace stmp

View File

@ -119,7 +119,7 @@ namespace spades {
void ProcessResponse() {
Json::Reader reader;
Json::Value root;
std::unique_ptr<MainScreenServerList> resp{new MainScreenServerList()};
auto resp = stmp::make_unique<MainScreenServerList>();
if (reader.parse(buffer, root, false)) {
for (Json::Value::iterator it = root.begin(); it != root.end(); ++it) {
@ -164,11 +164,11 @@ namespace spades {
SPRaise("Failed to create cURL object.");
}
} catch (std::exception &ex) {
std::unique_ptr<MainScreenServerList> lst{new MainScreenServerList()};
auto lst = stmp::make_unique<MainScreenServerList>();
lst->message = ex.what();
ReturnResult(std::move(lst));
} catch (...) {
std::unique_ptr<MainScreenServerList> lst{new MainScreenServerList()};
auto lst = stmp::make_unique<MainScreenServerList>();
lst->message = "Unknown error.";
ReturnResult(std::move(lst));
}