Skip to content

Commit

Permalink
support c++23 deducing this
Browse files Browse the repository at this point in the history
  • Loading branch information
etorth committed Dec 3, 2024
1 parent 6d3f3b0 commit 7d45fc9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 68 deletions.
22 changes: 6 additions & 16 deletions client/src/xmlparagraph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,12 @@ class XMLParagraph
}

public:
const auto &leaf(int leafIndex) const
auto & leaf(this auto && self, int leafIndex)
{
if(!leafValid(leafIndex)){
if(!self.leafValid(leafIndex)){
throw fflerror("invalid leaf index: %d", leafIndex);
}
return m_leafList[leafIndex];
}

auto &leaf(int leafIndex)
{
return const_cast<XMLParagraphLeaf &>(static_cast<const XMLParagraph *>(this)->leaf(leafIndex));
return self.m_leafList[leafIndex];
}

public:
Expand All @@ -68,17 +63,12 @@ class XMLParagraph
}

public:
const auto &backLeaf() const
auto & backLeaf(this auto && self)
{
if(m_leafList.empty()){
if(self.m_leafList.empty()){
throw fflerror("no leaf");
}
return m_leafList.back();
}

auto &backLeaf()
{
return const_cast<XMLParagraphLeaf &>(static_cast<const XMLParagraph *>(this)->backLeaf());
return self.m_leafList.back();
}

public:
Expand Down
23 changes: 9 additions & 14 deletions client/src/xmlparagraphleaf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,25 @@ class XMLParagraphLeaf
return self.m_node;
}

int length() const
{
if(type() == LEAF_UTF8STR){
return to_d(utf8CharOff().size());
}
return 1;
}

const std::vector<int> &utf8CharOff() const
auto & utf8CharOff(this auto && self)
{
if(type() != LEAF_UTF8STR){
if(self.type() != LEAF_UTF8STR){
throw fflerror("leaf is not an utf8 string");
}

if(m_utf8CharOff.empty()){
if(self.m_utf8CharOff.empty()){
throw fflerror("utf8 token off doesn't initialized");
}

return m_utf8CharOff;
return self.m_utf8CharOff;
}

std::vector<int> &utf8CharOff()
int length() const
{
return const_cast<std::vector<int> &>(static_cast<const XMLParagraphLeaf *>(this)->utf8CharOff());
if(type() == LEAF_UTF8STR){
return to_d(utf8CharOff().size());
}
return 1;
}

const char *utf8Text() const
Expand Down
18 changes: 4 additions & 14 deletions server/src/actorpool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,25 +584,15 @@ class ActorPool final
}

public:
const MailboxSubBucket & getSubBucket(int bucketId, int subBucketId) const
auto & getSubBucket(this auto && self, int bucketId, int subBucketId)
{
return const_cast<ActorPool *>(this)->getSubBucket(bucketId, subBucketId);
}

MailboxSubBucket & getSubBucket(int bucketId, int subBucketId)
{
return m_bucketList.at(bucketId).subBucketList.at(subBucketId);
return self.m_bucketList.at(bucketId).subBucketList.at(subBucketId);
}

public:
const MailboxSubBucket & getSubBucket(uint64_t uid) const
{
return const_cast<ActorPool *>(this)->getSubBucket(uid);
}

MailboxSubBucket & getSubBucket(uint64_t uid)
auto & getSubBucket(this auto && self, uint64_t uid)
{
return getSubBucket(getBucketID(uid), getSubBucketID(uid));
return self.getSubBucket(self.getBucketID(uid), self.getSubBucketID(uid));
}

private:
Expand Down
33 changes: 9 additions & 24 deletions server/src/servermap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,10 @@ class ServerMap final: public ServerObject
int getMonsterCount(uint32_t);

private:
const auto &getGrid(int x, int y) const
auto & getGrid(this auto && self, int x, int y)
{
fflassert(validC(x, y));
return m_gridList.at(x + y * W());
}

auto &getGrid(int x, int y)
{
return const_cast<MapGrid &>(static_cast<const ServerMap *>(this)->getGrid(x, y));
fflassert(self.validC(x, y));
return self.m_gridList.at(x + y * self.W());
}

private:
Expand Down Expand Up @@ -313,18 +308,18 @@ class ServerMap final: public ServerObject
}

private:
template<std::predicate<int, int> F> bool doCircle(int cx0, int cy0, int r, const F &f)
template<std::predicate<int, int> F> bool doCircle(this auto && self, int cx0, int cy0, int r, const F &f)
{
int doW = 2 * r - 1;
int doH = 2 * r - 1;

int x0 = cx0 - r + 1;
int y0 = cy0 - r + 1;

if((doW > 0) && (doH > 0) && mathf::rectangleOverlapRegion(0, 0, W(), H(), x0, y0, doW, doH)){
if((doW > 0) && (doH > 0) && mathf::rectangleOverlapRegion(0, 0, self.W(), self.H(), x0, y0, doW, doH)){
for(int x = x0; x < x0 + doW; ++x){
for(int y = y0; y < y0 + doH; ++y){
if(validC(x, y)){
if(self.validC(x, y)){
if(mathf::LDistance2(x, y, cx0, cy0) <= (r - 1) * (r - 1)){
if(f(x, y)){
return true;
Expand All @@ -337,17 +332,12 @@ class ServerMap final: public ServerObject
return false;
}

template<std::predicate<int, int> F> bool doCircle(int cx0, int cy0, int r, const F &f) const
template<std::predicate<int, int> F> bool doSquare(this auto && self, int x0, int y0, int doW, int doH, const F &f)
{
return const_cast<ServerMap *>(this)->doCircle(cx0, cy0, r, f);
}

template<std::predicate<int, int> F> bool doSquare(int x0, int y0, int doW, int doH, const F &f)
{
if((doW > 0) && (doH > 0) && mathf::rectangleOverlapRegion(0, 0, W(), H(), x0, y0, doW, doH)){
if((doW > 0) && (doH > 0) && mathf::rectangleOverlapRegion(0, 0, self.W(), self.H(), x0, y0, doW, doH)){
for(int x = x0; x < x0 + doW; ++x){
for(int y = y0; y < y0 + doH; ++y){
if(validC(x, y)){
if(self.validC(x, y)){
if(f(x, y)){
return true;
}
Expand All @@ -358,11 +348,6 @@ class ServerMap final: public ServerObject
return false;
}

template<std::predicate<int, int> F> bool doSquare(int x0, int y0, int doW, int doH, const F &f) const
{
return const_cast<ServerMap *>(this)->doSquare(x0, y0, doW, doH, f);
}

bool DoCenterCircle(int, int, int, bool, const std::function<bool(int, int)> &);
bool DoCenterSquare(int, int, int, int, bool, const std::function<bool(int, int)> &);

Expand Down

0 comments on commit 7d45fc9

Please sign in to comment.