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 
10 #include "table/internal_iterator.h"
11 #include "port/port.h"
12 
13 namespace ROCKSDB_NAMESPACE {
14 class ScopedArenaIterator {
15 
reset(InternalIterator * iter)16   void reset(InternalIterator* iter) ROCKSDB_NOEXCEPT {
17     if (iter_ != nullptr) {
18       iter_->~InternalIterator();
19     }
20     iter_ = iter;
21   }
22 
23  public:
24 
25   explicit ScopedArenaIterator(InternalIterator* iter = nullptr)
iter_(iter)26       : iter_(iter) {}
27 
28   ScopedArenaIterator(const ScopedArenaIterator&) = delete;
29   ScopedArenaIterator& operator=(const ScopedArenaIterator&) = delete;
30 
ScopedArenaIterator(ScopedArenaIterator && o)31   ScopedArenaIterator(ScopedArenaIterator&& o) ROCKSDB_NOEXCEPT {
32     iter_ = o.iter_;
33     o.iter_ = nullptr;
34   }
35 
36   ScopedArenaIterator& operator=(ScopedArenaIterator&& o) ROCKSDB_NOEXCEPT {
37     reset(o.iter_);
38     o.iter_ = nullptr;
39     return *this;
40   }
41 
42   InternalIterator* operator->() { return iter_; }
get()43   InternalIterator* get() { return iter_; }
44 
set(InternalIterator * iter)45   void set(InternalIterator* iter) { reset(iter); }
46 
release()47   InternalIterator* release() {
48     assert(iter_ != nullptr);
49     auto* res = iter_;
50     iter_ = nullptr;
51     return res;
52   }
53 
~ScopedArenaIterator()54   ~ScopedArenaIterator() {
55     reset(nullptr);
56   }
57 
58  private:
59   InternalIterator* iter_;
60 };
61 }  // namespace ROCKSDB_NAMESPACE
62