Welcome to mirror list, hosted at ThFree Co, Russian Federation.

map_view.hpp « v2.0 « llfio « include - github.com/windirstat/llfio.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 75bab5b0512d41e70475136a8b8beaaf96f1b9c0 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* A typed view of a mapped section
(C) 2017-2018 Niall Douglas <http://www.nedproductions.biz/> (12 commits)
File Created: Aug 2017


Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License in the accompanying file
Licence.txt or at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


Distributed under the Boost Software License, Version 1.0.
    (See accompanying file Licence.txt or copy at
          http://www.boost.org/LICENSE_1_0.txt)
*/

#ifndef LLFIO_MAP_VIEW_HPP
#define LLFIO_MAP_VIEW_HPP

#include "mapped.hpp"

//! \file map_view.hpp Provides typed view of mapped section.

LLFIO_V2_NAMESPACE_BEGIN

/*! \brief Provides a lightweight typed view of a `map_handle`, a `mapped_file_handle`
or a `mapped<T>` suitable for feeding to STL algorithms or the Ranges TS.

This is the correct type to use when passing non-owning views of mapped data
around between functions. Where you wish the view to be (possibly) owning,
you may find the non-lightweight `mapped<T>` of more use.
*/
template <class T> class map_view : public span<T>
{
public:
  //! The extent type.
  using extent_type = typename section_handle::extent_type;
  //! The size type.
  using size_type = typename section_handle::size_type;

public:
  //! Default constructor
  constexpr map_view() {}  // NOLINT

  /*! Implicitly construct a mapped view of the given mapped data.

  \param map The mapped data to take a view upon.
  \param length The number of items to map, use -1 to mean the length of the input view.
  \param offset The item offset into the mapped file handle.
  */
  map_view(mapped<T> &map, size_type length = (size_type) -1, size_type offset = 0)             // NOLINT
  : span<T>(map.begin() + offset, (length == (size_type) -1) ? (map.size() - offset) : length)  // NOLINT
  {
  }
  /*! Construct a mapped view of the given map handle.

  \param mh The map handle to use.
  \param length The number of items to map, use -1 to mean the length of the map handle divided by `sizeof(T)`.
  \param byteoffset The byte offset into the map handle, this does not need to be a multiple of the page size.
  */
  explicit map_view(map_handle &mh, size_type length = (size_type) -1, extent_type byteoffset = 0)                                             // NOLINT
  : span<T>(reinterpret_cast<T *>(mh.address() + byteoffset), (length == (size_type) -1) ? ((mh.length() - byteoffset) / sizeof(T)) : length)  // NOLINT
  {
  }
  /*! Construct a mapped view of the given mapped file handle.

  \param mfh The mapped file handle to take a view upon.
  \param length The number of items to map, use -1 to mean the length of the section handle divided by `sizeof(T)`.
  \param byteoffset The byte offset into the mapped file handle, this does not need to be a multiple of the page size.
  */
  explicit map_view(mapped_file_handle &mfh, size_type length = (size_type) -1, extent_type byteoffset = 0)                                                      // NOLINT
  : span<T>(reinterpret_cast<T *>(mfh.address() + byteoffset), (length == (size_type) -1) ? ((mfh.maximum_extent().value() - byteoffset) / sizeof(T)) : length)  // NOLINT
  {
  }
};

LLFIO_V2_NAMESPACE_END

#endif