ByteLength.h 842 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Types.h>
  8. #include <AK/Variant.h>
  9. namespace JS {
  10. class ByteLength {
  11. public:
  12. static ByteLength auto_() { return { Auto {} }; }
  13. static ByteLength detached() { return { Detached {} }; }
  14. ByteLength(u32 length)
  15. : m_length(length)
  16. {
  17. }
  18. bool is_auto() const { return m_length.has<Auto>(); }
  19. bool is_detached() const { return m_length.has<Detached>(); }
  20. u32 length() const
  21. {
  22. VERIFY(m_length.has<u32>());
  23. return m_length.get<u32>();
  24. }
  25. private:
  26. struct Auto { };
  27. struct Detached { };
  28. using Length = Variant<Auto, Detached, u32>;
  29. ByteLength(Length length)
  30. : m_length(move(length))
  31. {
  32. }
  33. Length m_length;
  34. };
  35. }