Skip to content

Commit

Permalink
chore(Any): Apply suggestions from code review.
Browse files Browse the repository at this point in the history
  • Loading branch information
matejk committed Dec 19, 2024
1 parent e33bfdd commit e78f3a6
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions Foundation/include/Poco/Any.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ union Placeholder
// Forces to use optimised memset internally
// https://travisdowns.github.io/blog/2020/01/20/zero.html
std::fill(std::begin(holder), std::end(holder), static_cast<char>(0));
setAllocation(Allocation::EMPTY);
setAllocation(Allocation::POCO_ANY_EMPTY);
}

~Placeholder()
Expand All @@ -104,12 +104,12 @@ union Placeholder

bool isEmpty() const
{
return holder[SizeV] == Allocation::EMPTY;
return holder[SizeV] == Allocation::POCO_ANY_EMPTY;
}

bool isLocal() const
{
return holder[SizeV] == Allocation::LOCAL;
return holder[SizeV] == Allocation::POCO_ANY_LOCAL;
}

template<typename T, typename V,
Expand All @@ -118,7 +118,7 @@ union Placeholder
{
erase();
new (reinterpret_cast<PlaceholderT*>(holder)) T(value);
setAllocation(Allocation::LOCAL);
setAllocation(Allocation::POCO_ANY_LOCAL);
return reinterpret_cast<PlaceholderT*>(holder);
}

Expand All @@ -128,7 +128,7 @@ union Placeholder
{
erase();
pHolder = new T(value);
setAllocation(Allocation::EXTERNAL);
setAllocation(Allocation::POCO_ANY_EXTERNAL);
return pHolder;
}

Expand All @@ -144,10 +144,11 @@ union Placeholder
using AlignerType = std::max_align_t;
static_assert(sizeof(AlignerType) <= SizeV + 1, "Aligner type is bigger than the actual storage, so SizeV should be made bigger otherwise you simply waste unused memory.");

enum Allocation : unsigned char {
EMPTY = 0,
LOCAL = 1,
EXTERNAL = 2
enum Allocation : unsigned char
{
POCO_ANY_EMPTY = 0,
POCO_ANY_LOCAL = 1,
POCO_ANY_EXTERNAL = 2
};

void setAllocation(Allocation state) const
Expand All @@ -159,15 +160,15 @@ union Placeholder
{
switch (holder[SizeV])
{
case Allocation::EMPTY:
case Allocation::POCO_ANY_EMPTY:
break;
case Allocation::LOCAL:
case Allocation::POCO_ANY_LOCAL:
{
auto* h { reinterpret_cast<PlaceholderT*>(holder) };
h->~PlaceholderT();
}
break;
case Allocation::EXTERNAL:
case Allocation::POCO_ANY_EXTERNAL:
{
auto* h { pHolder };
pHolder = nullptr;
Expand All @@ -183,7 +184,7 @@ union Placeholder
// Force to use optimised memset internally
std::fill(std::begin(holder), std::end(holder), static_cast<char>(0));
}
setAllocation(Allocation::EMPTY);
setAllocation(Allocation::POCO_ANY_EMPTY);
}

mutable unsigned char holder[SizeV+1];
Expand Down Expand Up @@ -279,8 +280,11 @@ class Any
}

~Any() = default;
/// Destructor. If Any is locally held, calls ValueHolder destructor;
/// otherwise, deletes the placeholder from the heap.
/// Destructor.
/// Small Object Optimization mode behavior:
/// Invokes the Placeholder destructor, which calls the
/// ValueHolder destructor explicitly (locally held Any), or
/// deletes the ValueHolder heap memory (heap-allocated Any).

Any& swap(Any& other) noexcept
/// Swaps the content of the two Anys.
Expand Down

0 comments on commit e78f3a6

Please sign in to comment.