Skip to content

Commit

Permalink
[aot] Replace std::exchange with local implementation for C++11 (#7170)
Browse files Browse the repository at this point in the history
Issue: #

### Brief Summary

Just noticed that `std::exchange` doesn't exists in C++11.
  • Loading branch information
PENGUINLIONG authored Jan 16, 2023
1 parent fa3a0c2 commit 04c8d2a
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions c_api/include/taichi/cpp/taichi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ struct templ2dtype<double> {
static const TiDataType value = TI_DATA_TYPE_F64;
};

template <typename T, typename U>
T exchange(T &storage, U &&value) {
T out = std::move(storage);
storage = (T)std::move(value);
return out;
}

template <typename THandle>
THandle move_handle(THandle &handle) {
THandle out = std::move(handle);
Expand Down Expand Up @@ -213,8 +220,8 @@ class Memory {
Memory(Memory &&b)
: runtime_(detail::move_handle(b.runtime_)),
memory_(detail::move_handle(b.memory_)),
size_(std::exchange(b.size_, 0)),
should_destroy_(std::exchange(b.should_destroy_, false)) {
size_(detail::exchange(b.size_, 0)),
should_destroy_(detail::exchange(b.should_destroy_, false)) {
}
Memory(TiRuntime runtime, TiMemory memory, size_t size, bool should_destroy)
: runtime_(runtime),
Expand All @@ -231,8 +238,8 @@ class Memory {
destroy();
runtime_ = detail::move_handle(b.runtime_);
memory_ = detail::move_handle(b.memory_);
size_ = std::exchange(b.size_, 0);
should_destroy_ = std::exchange(b.should_destroy_, false);
size_ = detail::exchange(b.size_, 0);
should_destroy_ = detail::exchange(b.should_destroy_, false);
return *this;
}

Expand Down Expand Up @@ -304,9 +311,9 @@ class NdArray {
NdArray(const NdArray<T> &) = delete;
NdArray(NdArray<T> &&b)
: memory_(std::move(b.memory_)),
ndarray_(std::exchange(b.ndarray_, {})),
elem_count_(std::exchange(b.elem_count_, 1)),
scalar_count_(std::exchange(b.scalar_count_, 1)) {
ndarray_(detail::exchange(b.ndarray_, TiNdArray{})),
elem_count_(detail::exchange(b.elem_count_, 1)),
scalar_count_(detail::exchange(b.scalar_count_, 1)) {
}
NdArray(Memory &&memory, const TiNdArray &ndarray)
: memory_(std::move(memory)),
Expand All @@ -332,9 +339,9 @@ class NdArray {
NdArray<T> &operator=(NdArray<T> &&b) {
destroy();
memory_ = std::move(b.memory_);
ndarray_ = std::exchange(b.ndarray_, {});
elem_count_ = std::exchange(b.elem_count_, 1);
scalar_count_ = std::exchange(b.scalar_count_, 1);
ndarray_ = detail::exchange(b.ndarray_, TiNdArray{});
elem_count_ = detail::exchange(b.elem_count_, 1);
scalar_count_ = detail::exchange(b.scalar_count_, 1);
return *this;
}

Expand Down Expand Up @@ -441,7 +448,7 @@ class Image {
Image(Image &&b)
: runtime_(detail::move_handle(b.runtime_)),
image_(detail::move_handle(b.image_)),
should_destroy_(std::exchange(b.should_destroy_, false)) {
should_destroy_(detail::exchange(b.should_destroy_, false)) {
}
Image(TiRuntime runtime, TiImage image, bool should_destroy)
: runtime_(runtime), image_(image), should_destroy_(should_destroy) {
Expand All @@ -455,7 +462,7 @@ class Image {
destroy();
runtime_ = detail::move_handle(b.runtime_);
image_ = detail::move_handle(b.image_);
should_destroy_ = std::exchange(b.should_destroy_, false);
should_destroy_ = detail::exchange(b.should_destroy_, false);
return *this;
}

Expand Down Expand Up @@ -753,7 +760,7 @@ class AotModule {
AotModule(AotModule &&b)
: runtime_(detail::move_handle(b.runtime_)),
aot_module_(detail::move_handle(b.aot_module_)),
should_destroy_(std::exchange(b.should_destroy_, false)) {
should_destroy_(detail::exchange(b.should_destroy_, false)) {
}
AotModule(TiRuntime runtime, TiAotModule aot_module, bool should_destroy)
: runtime_(runtime),
Expand All @@ -768,7 +775,7 @@ class AotModule {
AotModule &operator=(AotModule &&b) {
runtime_ = detail::move_handle(b.runtime_);
aot_module_ = detail::move_handle(b.aot_module_);
should_destroy_ = std::exchange(b.should_destroy_, false);
should_destroy_ = detail::exchange(b.should_destroy_, false);
return *this;
}

Expand Down Expand Up @@ -984,9 +991,9 @@ class Runtime {
}
Runtime(const Runtime &) = delete;
Runtime(Runtime &&b)
: arch_(std::exchange(b.arch_, TI_ARCH_MAX_ENUM)),
: arch_(detail::exchange(b.arch_, TI_ARCH_MAX_ENUM)),
runtime_(detail::move_handle(b.runtime_)),
should_destroy_(std::exchange(b.should_destroy_, false)) {
should_destroy_(detail::exchange(b.should_destroy_, false)) {
}
Runtime(TiArch arch, uint32_t device_index = 0)
: arch_(arch),
Expand All @@ -1002,9 +1009,9 @@ class Runtime {

Runtime &operator=(const Runtime &) = delete;
Runtime &operator=(Runtime &&b) {
arch_ = std::exchange(b.arch_, TI_ARCH_MAX_ENUM);
arch_ = detail::exchange(b.arch_, TI_ARCH_MAX_ENUM);
runtime_ = detail::move_handle(b.runtime_);
should_destroy_ = std::exchange(b.should_destroy_, false);
should_destroy_ = detail::exchange(b.should_destroy_, false);
return *this;
}

Expand Down

0 comments on commit 04c8d2a

Please sign in to comment.