1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Lambda decltype on compile time instead of runtime

This commit is contained in:
nordsoft 2022-09-14 12:06:51 +04:00 committed by Andrii Danylchenko
parent 3d2dc2335b
commit c24fc89fe3
2 changed files with 54 additions and 54 deletions

View File

@ -20,7 +20,6 @@
#include "../../lib/CModHandler.h"
extern boost::thread_specific_ptr<CCallback> cb;
extern boost::thread_specific_ptr<AIGateway> ai;
//extern static const int3 dirs[8];
@ -161,55 +160,6 @@ bool HeroPtr::operator==(const HeroPtr & rhs) const
return h == rhs.get(true);
}
void foreach_tile_pos(std::function<void(const int3 & pos)> foo)
{
// some micro-optimizations since this function gets called a LOT
// callback pointer is thread-specific and slow to retrieve -> read map size only once
int3 mapSize = cb->getMapSize();
for(int i = 0; i < mapSize.x; i++)
{
for(int j = 0; j < mapSize.y; j++)
{
for(int k = 0; k < mapSize.z; k++)
foo(int3(i, j, k));
}
}
}
void foreach_tile_pos(CCallback * cbp, std::function<void(CCallback * cbp, const int3 & pos)> foo)
{
int3 mapSize = cbp->getMapSize();
for(int i = 0; i < mapSize.x; i++)
{
for(int j = 0; j < mapSize.y; j++)
{
for(int k = 0; k < mapSize.z; k++)
foo(cbp, int3(i, j, k));
}
}
}
void foreach_neighbour(const int3 & pos, std::function<void(const int3 & pos)> foo)
{
CCallback * cbp = cb.get(); // avoid costly retrieval of thread-specific pointer
for(const int3 & dir : int3::getDirs())
{
const int3 n = pos + dir;
if(cbp->isInTheMap(n))
foo(pos + dir);
}
}
void foreach_neighbour(CCallback * cbp, const int3 & pos, std::function<void(CCallback * cbp, const int3 & pos)> foo)
{
for(const int3 & dir : int3::getDirs())
{
const int3 n = pos + dir;
if(cbp->isInTheMap(n))
foo(cbp, pos + dir);
}
}
bool CDistanceSorter::operator()(const CGObjectInstance * lhs, const CGObjectInstance * rhs) const
{
const CGPathNode * ln = ai->myCb->getPathsInfo(hero)->getPathInfo(lhs->visitablePos());

View File

@ -69,6 +69,8 @@ const int ALLOWED_ROAMING_HEROES = 8;
extern const float SAFE_ATTACK_CONSTANT;
extern const int GOLD_RESERVE;
extern boost::thread_specific_ptr<CCallback> cb;
enum HeroRole
{
SCOUT = 0,
@ -197,10 +199,58 @@ struct creInfo
};
creInfo infoFromDC(const dwellingContent & dc);
void foreach_tile_pos(std::function<void(const int3 & pos)> foo);
void foreach_tile_pos(CCallback * cbp, std::function<void(CCallback * cbp, const int3 & pos)> foo); // avoid costly retrieval of thread-specific pointer
void foreach_neighbour(const int3 & pos, std::function<void(const int3 & pos)> foo);
void foreach_neighbour(CCallback * cbp, const int3 & pos, std::function<void(CCallback * cbp, const int3 & pos)> foo); // avoid costly retrieval of thread-specific pointer
template<class Func>
void foreach_tile_pos(const Func & foo)
{
// some micro-optimizations since this function gets called a LOT
// callback pointer is thread-specific and slow to retrieve -> read map size only once
int3 mapSize = cb->getMapSize();
for(int i = 0; i < mapSize.x; i++)
{
for(int j = 0; j < mapSize.y; j++)
{
for(int k = 0; k < mapSize.z; k++)
foo(int3(i, j, k));
}
}
}
template<class Func>
void foreach_tile_pos(CCallback * cbp, const Func & foo) // avoid costly retrieval of thread-specific pointer
{
int3 mapSize = cbp->getMapSize();
for(int i = 0; i < mapSize.x; i++)
{
for(int j = 0; j < mapSize.y; j++)
{
for(int k = 0; k < mapSize.z; k++)
foo(cbp, int3(i, j, k));
}
}
}
template<class Func>
void foreach_neighbour(const int3 & pos, const Func & foo)
{
CCallback * cbp = cb.get(); // avoid costly retrieval of thread-specific pointer
for(const int3 & dir : int3::getDirs())
{
const int3 n = pos + dir;
if(cbp->isInTheMap(n))
foo(pos + dir);
}
}
template<class Func>
void foreach_neighbour(CCallback * cbp, const int3 & pos, const Func & foo) // avoid costly retrieval of thread-specific pointer
{
for(const int3 & dir : int3::getDirs())
{
const int3 n = pos + dir;
if(cbp->isInTheMap(n))
foo(cbp, pos + dir);
}
}
bool canBeEmbarkmentPoint(const TerrainTile * t, bool fromWater);
bool isObjectPassable(const CGObjectInstance * obj);