Debian Codegraph @

js/src/frontend/TypedIndex.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef frontend_TypedIndex_h
8 #define frontend_TypedIndex_h
9
10 #include <cstdint>
11 #include <stddef.h>
12
13 namespace js {
14 namespace frontend {
15
16 // TypedIndex allows discrimination in variants between different
17 // index types. Used as a typesafe index for various stencil arrays.
18 template <typename Tag>
19 struct TypedIndex {
20 TypedIndex() = default;
21 constexpr explicit TypedIndex(uint32_t index) : index(index) {};
22
23 uint32_t index = 0;
24
25 // For Vector::operator[]
26 operator size_t() const { return index; }
27
28 TypedIndex& operator=(size_t idx) {
29 index = idx;
30 return *this;
31 }
32
33 bool operator<(TypedIndex other) const { return index < other.index; }
34 bool operator<=(TypedIndex other) const { return index <= other.index; }
35 bool operator>(TypedIndex other) const { return index > other.index; }
36 bool operator>=(TypedIndex other) const { return index >= other.index; }
37 };
38
39 } // namespace frontend
40 } // namespace js
41
42 #endif