TypedIndex.h (1077B)
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 <compare> // std::strong_ordering 11 #include <cstdint> 12 #include <stddef.h> 13 14 namespace js { 15 namespace frontend { 16 17 // TypedIndex allows discrimination in variants between different 18 // index types. Used as a typesafe index for various stencil arrays. 19 template <typename Tag> 20 struct TypedIndex { 21 TypedIndex() = default; 22 constexpr explicit TypedIndex(uint32_t index) : index(index) {}; 23 24 uint32_t index = 0; 25 26 // For Vector::operator[] 27 operator size_t() const { return index; } 28 29 TypedIndex& operator=(size_t idx) { 30 index = idx; 31 return *this; 32 } 33 34 constexpr auto operator<=>(const TypedIndex& other) const = default; 35 }; 36 37 } // namespace frontend 38 } // namespace js 39 40 #endif