File: span.hpp

package info (click to toggle)
sleuthkit 4.12.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,608 kB
  • sloc: ansic: 143,795; cpp: 52,225; java: 37,892; xml: 2,416; python: 1,076; perl: 874; makefile: 439; sh: 184
file content (53 lines) | stat: -rwxr-xr-x 1,193 bytes parent folder | download | duplicates (3)
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
#pragma once

#include <cstddef>
#include <iterator>
#include <stdexcept>
#include <type_traits>

// TODO(JTS): We really should just use gsl::span

template <typename T>
class span {
  // TODO(JTS): Finish this to make it useful
 public:
  using element_type = T;
  using index_type = size_t;
  using pointer = T*;
  using reference = T&;
  using value = std::remove_cv_t<T>;

 protected:
  pointer _storage{};
  index_type _count{};

 public:
  span() = default;
  span(std::nullptr_t) noexcept : span() {}

  span(pointer p, index_type n) noexcept : _storage{p}, _count{n} {}

  template <size_t N>
  span(element_type (&arr)[N]) noexcept : span(arr, N) {}

  span(pointer first, pointer last) noexcept
      : span(first, std::distance(first, last)) {}

  index_type count() const noexcept { return _count; }

  bool valid() const noexcept { return _storage != nullptr; }
  pointer data() const noexcept { return _storage; }
};

struct memory_view : public span<char> {
  using span::span;

  template <typename T>
  T* as() const {
    // if ((size_t) count() < sizeof(T)) {
    //  throw std::runtime_error("not enough space");
    //}

    return reinterpret_cast<T*>(_storage);
  }
};