summaryrefslogtreecommitdiff
path: root/deps/v8/src/detachable-vector.h
blob: 4609ebf0e10a37519cee1ed6c982ce5de6bfe49e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_DETACHABLE_VECTOR_H_
#define V8_DETACHABLE_VECTOR_H_

#include <vector>

namespace v8 {
namespace internal {

// This class wraps a std::vector and provides a few of the common member
// functions for accessing the data. It acts as a lazy wrapper of the vector,
// not initiliazing the backing store until push_back() is first called. Two
// extra methods are also provided: free() and detach(), which allow for manual
// control of the backing store. This is currently required for use in the
// HandleScopeImplementer. Any other class should just use a std::vector
// directly.
template <typename T>
class DetachableVector {
 public:
  DetachableVector() : vector_(nullptr) {}

  ~DetachableVector() { delete vector_; }

  void push_back(const T& value) {
    ensureAttached();
    vector_->push_back(value);
  }

  // Free the backing store and clear our reference to it.
  void free() {
    delete vector_;
    vector_ = nullptr;
  }

  // Clear our reference to the backing store. Does not delete it!
  void detach() { vector_ = nullptr; }

  T& at(typename std::vector<T>::size_type i) const { return vector_->at(i); }

  T& back() const { return vector_->back(); }

  T& front() const { return vector_->front(); }

  void pop_back() { vector_->pop_back(); }

  typename std::vector<T>::size_type size() const {
    if (vector_) return vector_->size();
    return 0;
  }

  bool empty() const {
    if (vector_) return vector_->empty();
    return true;
  }

 private:
  std::vector<T>* vector_;

  // Attach a vector backing store if not present.
  void ensureAttached() {
    if (vector_ == nullptr) {
      vector_ = new std::vector<T>();
    }
  }
};

}  // namespace internal
}  // namespace v8

#endif  // V8_DETACHABLE_VECTOR_H_