Skip to content

Commit

Permalink
Replacing ::new with new throughout the library
Browse files Browse the repository at this point in the history
  • Loading branch information
jrouwe committed Dec 11, 2024
1 parent 2f3f4e2 commit b5bfdc2
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 41 deletions.
22 changes: 11 additions & 11 deletions Jolt/Core/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ class [[nodiscard]] Array : private Allocator
{
for (T *destination_end = inDestination + inCount; inDestination < destination_end; ++inDestination, ++inSource)
{
::new (inDestination) T(std::move(*inSource));
new (inDestination) T(std::move(*inSource));
inSource->~T();
}
}
else
{
for (T *destination = inDestination + inCount - 1, *source = inSource + inCount - 1; destination >= inDestination; --destination, --source)
{
::new (destination) T(std::move(*source));
new (destination) T(std::move(*source));
source->~T();
}
}
Expand Down Expand Up @@ -124,7 +124,7 @@ class [[nodiscard]] Array : private Allocator

if constexpr (!std::is_trivially_constructible<T>())
for (T *element = mElements + mSize, *element_end = mElements + inNewSize; element < element_end; ++element)
::new (element) T;
new (element) T;
mSize = inNewSize;
}

Expand All @@ -137,7 +137,7 @@ class [[nodiscard]] Array : private Allocator
reserve(inNewSize);

for (T *element = mElements + mSize, *element_end = mElements + inNewSize; element < element_end; ++element)
::new (element) T(inValue);
new (element) T(inValue);
mSize = inNewSize;
}

Expand Down Expand Up @@ -187,7 +187,7 @@ class [[nodiscard]] Array : private Allocator
reserve(size_type(std::distance(inBegin, inEnd)));

for (Iterator element = inBegin; element != inEnd; ++element)
::new (&mElements[mSize++]) T(*element);
new (&mElements[mSize++]) T(*element);
}

/// Replace the contents of this array with inList
Expand All @@ -197,7 +197,7 @@ class [[nodiscard]] Array : private Allocator
reserve(size_type(inList.size()));

for (const T &v : inList)
::new (&mElements[mSize++]) T(v);
new (&mElements[mSize++]) T(v);
}

/// Default constructor
Expand Down Expand Up @@ -281,15 +281,15 @@ class [[nodiscard]] Array : private Allocator
grow();

T *element = mElements + mSize++;
::new (element) T(inValue);
new (element) T(inValue);
}

inline void push_back(T &&inValue)
{
grow();

T *element = mElements + mSize++;
::new (element) T(std::move(inValue));
new (element) T(std::move(inValue));
}

/// Construct element at the back of the array
Expand All @@ -299,7 +299,7 @@ class [[nodiscard]] Array : private Allocator
grow();

T *element = mElements + mSize++;
::new (element) T(std::forward<A>(inValue)...);
new (element) T(std::forward<A>(inValue)...);
return *element;
}

Expand Down Expand Up @@ -365,7 +365,7 @@ class [[nodiscard]] Array : private Allocator
move(element_end, element_begin, mSize - first_element);

for (T *element = element_begin; element < element_end; ++element, ++inBegin)
::new (element) T(*inBegin);
new (element) T(*inBegin);

mSize += num_elements;
}
Expand All @@ -383,7 +383,7 @@ class [[nodiscard]] Array : private Allocator
T *element = mElements + first_element;
move(element + 1, element, mSize - first_element);

::new (element) T(inValue);
new (element) T(inValue);
mSize++;
}

Expand Down
2 changes: 1 addition & 1 deletion Jolt/Core/ByteBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ByteBuffer : public ByteBufferVector

// Construct elements
for (Type *d = data, *d_end = data + inSize; d < d_end; ++d)
::new (d) Type;
new (d) Type;

// Return pointer
return data;
Expand Down
4 changes: 2 additions & 2 deletions Jolt/Core/FixedSizeFreeList.inl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ uint32 FixedSizeFreeList<Object>::ConstructObject(Parameters &&... inParameters)
// Allocation successful
JPH_IF_ENABLE_ASSERTS(mNumFreeObjects.fetch_sub(1, memory_order_relaxed);)
ObjectStorage &storage = GetStorage(first_free);
::new (&storage.mObject) Object(std::forward<Parameters>(inParameters)...);
new (&storage.mObject) Object(std::forward<Parameters>(inParameters)...);
storage.mNextFreeObject.store(first_free, memory_order_release);
return first_free;
}
Expand All @@ -97,7 +97,7 @@ uint32 FixedSizeFreeList<Object>::ConstructObject(Parameters &&... inParameters)
// Allocation successful
JPH_IF_ENABLE_ASSERTS(mNumFreeObjects.fetch_sub(1, memory_order_relaxed);)
ObjectStorage &storage = GetStorage(first_free);
::new (&storage.mObject) Object(std::forward<Parameters>(inParameters)...);
new (&storage.mObject) Object(std::forward<Parameters>(inParameters)...);
storage.mNextFreeObject.store(first_free, memory_order_release);
return first_free;
}
Expand Down
8 changes: 4 additions & 4 deletions Jolt/Core/HashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class HashTable
uint index = 0;
for (const uint8 *control = mControl, *control_end = mControl + mMaxSize; control != control_end; ++control, ++index)
if (*control & cBucketUsed)
::new (mData + index) KeyValue(inRHS.mData[index]);
new (mData + index) KeyValue(inRHS.mData[index]);
mSize = inRHS.mSize;
}

Expand Down Expand Up @@ -216,7 +216,7 @@ class HashTable
KeyValue *element = old_data + i;
JPH_IF_ENABLE_ASSERTS(bool inserted =) InsertKey</* InsertAfterGrow= */ true>(HashTableDetail::sGetKey(*element), index);
JPH_ASSERT(inserted);
::new (mData + index) KeyValue(std::move(*element));
new (mData + index) KeyValue(std::move(*element));
element->~KeyValue();
}

Expand Down Expand Up @@ -601,7 +601,7 @@ class HashTable
size_type index;
bool inserted = InsertKey(HashTableDetail::sGetKey(inValue), index);
if (inserted)
::new (mData + index) KeyValue(inValue);
new (mData + index) KeyValue(inValue);
return std::make_pair(iterator(this, index), inserted);
}

Expand Down Expand Up @@ -800,7 +800,7 @@ class HashTable
// There's an empty bucket, move us there
SetControlValue(dst, src_control);
SetControlValue(src, cBucketEmpty);
::new (mData + dst) KeyValue(std::move(mData[src]));
new (mData + dst) KeyValue(std::move(mData[src]));
mData[src].~KeyValue();
break;
}
Expand Down
26 changes: 13 additions & 13 deletions Jolt/Core/Result.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class Result
switch (inRHS.mState)
{
case EState::Valid:
::new (&mResult) Type (inRHS.mResult);
new (&mResult) Type (inRHS.mResult);
break;

case EState::Error:
::new (&mError) String(inRHS.mError);
new (&mError) String(inRHS.mError);
break;

case EState::Invalid:
Expand All @@ -40,11 +40,11 @@ class Result
switch (inRHS.mState)
{
case EState::Valid:
::new (&mResult) Type (std::move(inRHS.mResult));
new (&mResult) Type (std::move(inRHS.mResult));
break;

case EState::Error:
::new (&mError) String(std::move(inRHS.mError));
new (&mError) String(std::move(inRHS.mError));
break;

case EState::Invalid:
Expand All @@ -67,11 +67,11 @@ class Result
switch (inRHS.mState)
{
case EState::Valid:
::new (&mResult) Type (inRHS.mResult);
new (&mResult) Type (inRHS.mResult);
break;

case EState::Error:
::new (&mError) String(inRHS.mError);
new (&mError) String(inRHS.mError);
break;

case EState::Invalid:
Expand All @@ -91,11 +91,11 @@ class Result
switch (inRHS.mState)
{
case EState::Valid:
::new (&mResult) Type (std::move(inRHS.mResult));
new (&mResult) Type (std::move(inRHS.mResult));
break;

case EState::Error:
::new (&mError) String(std::move(inRHS.mError));
new (&mError) String(std::move(inRHS.mError));
break;

case EState::Invalid:
Expand Down Expand Up @@ -137,10 +137,10 @@ class Result
const Type & Get() const { JPH_ASSERT(IsValid()); return mResult; }

/// Set the result value
void Set(const Type &inResult) { Clear(); ::new (&mResult) Type(inResult); mState = EState::Valid; }
void Set(const Type &inResult) { Clear(); new (&mResult) Type(inResult); mState = EState::Valid; }

/// Set the result value (move value)
void Set(Type &&inResult) { Clear(); ::new (&mResult) Type(std::move(inResult)); mState = EState::Valid; }
void Set(Type &&inResult) { Clear(); new (&mResult) Type(std::move(inResult)); mState = EState::Valid; }

/// Check if we had an error
bool HasError() const { return mState == EState::Error; }
Expand All @@ -149,9 +149,9 @@ class Result
const String & GetError() const { JPH_ASSERT(HasError()); return mError; }

/// Set an error value
void SetError(const char *inError) { Clear(); ::new (&mError) String(inError); mState = EState::Error; }
void SetError(const string_view &inError) { Clear(); ::new (&mError) String(inError); mState = EState::Error; }
void SetError(String &&inError) { Clear(); ::new (&mError) String(std::move(inError)); mState = EState::Error; }
void SetError(const char *inError) { Clear(); new (&mError) String(inError); mState = EState::Error; }
void SetError(const string_view &inError) { Clear(); new (&mError) String(inError); mState = EState::Error; }
void SetError(String &&inError) { Clear(); new (&mError) String(std::move(inError)); mState = EState::Error; }

private:
union
Expand Down
14 changes: 7 additions & 7 deletions Jolt/Core/StaticArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ class [[nodiscard]] StaticArray
{
JPH_ASSERT(inList.size() <= N);
for (const T &v : inList)
::new (reinterpret_cast<T *>(&mElements[mSize++])) T(v);
new (reinterpret_cast<T *>(&mElements[mSize++])) T(v);
}

/// Copy constructor
StaticArray(const StaticArray<T, N> &inRHS)
{
while (mSize < inRHS.mSize)
{
::new (&mElements[mSize]) T(inRHS[mSize]);
new (&mElements[mSize]) T(inRHS[mSize]);
++mSize;
}
}
Expand All @@ -61,15 +61,15 @@ class [[nodiscard]] StaticArray
void push_back(const T &inElement)
{
JPH_ASSERT(mSize < N);
::new (&mElements[mSize++]) T(inElement);
new (&mElements[mSize++]) T(inElement);
}

/// Construct element at the back of the array
template <class... A>
void emplace_back(A &&... inElement)
{
JPH_ASSERT(mSize < N);
::new (&mElements[mSize++]) T(std::forward<A>(inElement)...);
new (&mElements[mSize++]) T(std::forward<A>(inElement)...);
}

/// Remove element from the back of the array
Expand Down Expand Up @@ -103,7 +103,7 @@ class [[nodiscard]] StaticArray
JPH_ASSERT(inNewSize <= N);
if constexpr (!std::is_trivially_constructible<T>())
for (T *element = reinterpret_cast<T *>(mElements) + mSize, *element_end = reinterpret_cast<T *>(mElements) + inNewSize; element < element_end; ++element)
::new (element) T;
new (element) T;
if constexpr (!std::is_trivially_destructible<T>())
for (T *element = reinterpret_cast<T *>(mElements) + inNewSize, *element_end = reinterpret_cast<T *>(mElements) + mSize; element < element_end; ++element)
element->~T();
Expand Down Expand Up @@ -232,7 +232,7 @@ class [[nodiscard]] StaticArray

while (mSize < rhs_size)
{
::new (&mElements[mSize]) T(inRHS[mSize]);
new (&mElements[mSize]) T(inRHS[mSize]);
++mSize;
}
}
Expand All @@ -253,7 +253,7 @@ class [[nodiscard]] StaticArray

while (mSize < rhs_size)
{
::new (&mElements[mSize]) T(inRHS[mSize]);
new (&mElements[mSize]) T(inRHS[mSize]);
++mSize;
}
}
Expand Down
6 changes: 3 additions & 3 deletions Jolt/Core/UnorderedMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class UnorderedMap : public HashTable<Key, std::pair<Key, Value>, UnorderedMapDe
bool inserted = this->InsertKey(inKey, index);
value_type &key_value = this->GetElement(index);
if (inserted)
::new (&key_value) value_type(inKey, Value());
new (&key_value) value_type(inKey, Value());
return key_value.second;
}

Expand All @@ -52,7 +52,7 @@ class UnorderedMap : public HashTable<Key, std::pair<Key, Value>, UnorderedMapDe
size_type index;
bool inserted = this->InsertKey(inKey, index);
if (inserted)
::new (&this->GetElement(index)) value_type(std::piecewise_construct, std::forward_as_tuple(inKey), std::forward_as_tuple(std::forward<Args>(inArgs)...));
new (&this->GetElement(index)) value_type(std::piecewise_construct, std::forward_as_tuple(inKey), std::forward_as_tuple(std::forward<Args>(inArgs)...));
return std::make_pair(iterator(this, index), inserted);
}

Expand All @@ -62,7 +62,7 @@ class UnorderedMap : public HashTable<Key, std::pair<Key, Value>, UnorderedMapDe
size_type index;
bool inserted = this->InsertKey(inKey, index);
if (inserted)
::new (&this->GetElement(index)) value_type(std::piecewise_construct, std::forward_as_tuple(std::move(inKey)), std::forward_as_tuple(std::forward<Args>(inArgs)...));
new (&this->GetElement(index)) value_type(std::piecewise_construct, std::forward_as_tuple(std::move(inKey)), std::forward_as_tuple(std::forward<Args>(inArgs)...));
return std::make_pair(iterator(this, index), inserted);
}

Expand Down

0 comments on commit b5bfdc2

Please sign in to comment.