Commit d4c0d267 authored by Brett Simmers's avatar Brett Simmers Committed by Facebook Github Bot 7

Allow const mapped types in folly::AtomicHash(Array|Map)

Summary:
Without this const_cast, it's impossible to insert anything into an
AHA with a const mapped_type, making it effectively useless.

Reviewed By: yfeldblum

Differential Revision: D3405687

fbshipit-source-id: 3ecba19e0e92661c2c537c747b4927176104939f
parent 8cb615a2
...@@ -152,7 +152,11 @@ insertInternal(LookupKeyT key_in, ArgTs&&... vCtorArgs) { ...@@ -152,7 +152,11 @@ insertInternal(LookupKeyT key_in, ArgTs&&... vCtorArgs) {
checkLegalKeyIfKey(key_new); checkLegalKeyIfKey(key_new);
} }
DCHECK(relaxedLoadKey(*cell) == kLockedKey_); DCHECK(relaxedLoadKey(*cell) == kLockedKey_);
new (&cell->second) ValueT(std::forward<ArgTs>(vCtorArgs)...); // A const mapped_type is only constant once constructed, so cast
// away any const for the placement new here.
using mapped = typename std::remove_const<mapped_type>::type;
new (const_cast<mapped*>(&cell->second))
ValueT(std::forward<ArgTs>(vCtorArgs)...);
unlockCell(cell, key_new); // Sets the new key unlockCell(cell, key_new); // Sets the new key
} catch (...) { } catch (...) {
// Transition back to empty key---requires handling // Transition back to empty key---requires handling
......
...@@ -333,3 +333,10 @@ TEST(Aha, LookupAny) { ...@@ -333,3 +333,10 @@ TEST(Aha, LookupAny) {
free(it.first); free(it.first);
} }
} }
using AHAIntCInt = AtomicHashArray<int64_t, const int32_t>;
TEST(Aha, ConstValue) {
auto aha = AHAIntCInt::create(10);
aha->emplace(1, 2);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment