scoped_arena_iterator.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  6. // Use of this source code is governed by a BSD-style license that can be
  7. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  8. #pragma once
  9. #include "table/internal_iterator.h"
  10. #include "port/port.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. class ScopedArenaIterator {
  13. void reset(InternalIterator* iter) ROCKSDB_NOEXCEPT {
  14. if (iter_ != nullptr) {
  15. iter_->~InternalIterator();
  16. }
  17. iter_ = iter;
  18. }
  19. public:
  20. explicit ScopedArenaIterator(InternalIterator* iter = nullptr)
  21. : iter_(iter) {}
  22. ScopedArenaIterator(const ScopedArenaIterator&) = delete;
  23. ScopedArenaIterator& operator=(const ScopedArenaIterator&) = delete;
  24. ScopedArenaIterator(ScopedArenaIterator&& o) ROCKSDB_NOEXCEPT {
  25. iter_ = o.iter_;
  26. o.iter_ = nullptr;
  27. }
  28. ScopedArenaIterator& operator=(ScopedArenaIterator&& o) ROCKSDB_NOEXCEPT {
  29. reset(o.iter_);
  30. o.iter_ = nullptr;
  31. return *this;
  32. }
  33. InternalIterator* operator->() { return iter_; }
  34. InternalIterator* get() { return iter_; }
  35. void set(InternalIterator* iter) { reset(iter); }
  36. InternalIterator* release() {
  37. assert(iter_ != nullptr);
  38. auto* res = iter_;
  39. iter_ = nullptr;
  40. return res;
  41. }
  42. ~ScopedArenaIterator() {
  43. reset(nullptr);
  44. }
  45. private:
  46. InternalIterator* iter_;
  47. };
  48. } // namespace ROCKSDB_NAMESPACE