Skip to content

Struct mdbx::slice

ClassList > mdbx > slice

References a data located outside the slice. More...

  • #include <mdbx.h++>

Inherits the following classes: MDBX_val

Inherited by the following classes: mdbx::buffer, mdbx::buffer

Public Types

Type Name
enum size_t @145120015325042373144130173373163042370340165246

Public Functions

Type Name
int128_t as_int128 () const
Reinterprets the slice as a 128-bit signed integer.
int128_t as_int128_adapt () const
Reinterprets the slice as a 128-bit signed integer, adapting the value when the slice is shorter than 16 bytes.
int16_t as_int16 () const
Reinterprets the slice as a 16-bit signed integer.
int16_t as_int16_adapt () const
Reinterprets the slice as a 16-bit signed integer, adapting the value when the slice is shorter than 2 bytes.
int32_t as_int32 () const
Reinterprets the slice as a 32-bit signed integer.
int32_t as_int32_adapt () const
Reinterprets the slice as a 32-bit signed integer, adapting the value when the slice is shorter than 4 bytes.
int64_t as_int64 () const
Reinterprets the slice as a 64-bit signed integer.
int64_t as_int64_adapt () const
Reinterprets the slice as a 64-bit signed integer, adapting the value when the slice is shorter than 8 bytes.
int8_t as_int8 () const
Reinterprets the slice as an 8-bit signed integer.
int8_t as_int8_adapt () const
Reinterprets the slice as an 8-bit signed integer.
POD as_pod () const
Reinterprets the slice content as an object of a standard-layout type.
::std::span< const POD > as_span () const
Returns a read-only std::span view of the referenced data.
::std::span< POD > as_span ()
Returns a mutable std::span view of the referenced data.
::std::basic_string< CHAR, T, ALLOCATOR > as_string (const ALLOCATOR & alloc=ALLOCATOR()) const
Copies the slice content into a std::basic_string .
uint128_t as_uint128 () const
Reinterprets the slice as a 128-bit unsigned integer.
uint128_t as_uint128_adapt () const
Reinterprets the slice as a 128-bit unsigned integer, adapting the value when the slice is shorter than 16 bytes.
uint16_t as_uint16 () const
Reinterprets the slice as a 16-bit unsigned integer.
uint16_t as_uint16_adapt () const
Reinterprets the slice as a 16-bit unsigned integer, adapting the value when the slice is shorter than 2 bytes.
uint32_t as_uint32 () const
Reinterprets the slice as a 32-bit unsigned integer.
uint32_t as_uint32_adapt () const
Reinterprets the slice as a 32-bit unsigned integer, adapting the value when the slice is shorter than 4 bytes.
uint64_t as_uint64 () const
Reinterprets the slice as a 64-bit unsigned integer.
uint64_t as_uint64_adapt () const
Reinterprets the slice as a 64-bit unsigned integer, adapting the value when the slice is shorter than 8 bytes.
uint8_t as_uint8 () const
Reinterprets the slice as an 8-bit unsigned integer.
uint8_t as_uint8_adapt () const
Reinterprets the slice as an 8-bit unsigned integer.
slice & assign (const ::std::basic_string< CHAR, T, ALLOCATOR > & str)
Assigns the slice to reference the contents of the given string.
slice & assign (const ::std::basic_string_view< CHAR, T > & view)
Assigns the slice to reference the contents of the given string view.
slice & assign (::std::basic_string_view< CHAR, T > && view)
Takes over a temporary string view and assigns the slice to its contents.
byte back () noexcept const
Returns the last byte of the referenced data.
::std::span< const byte > bytes () const
Returns a read-only std::span of bytes of the referenced data.
::std::span< byte > bytes ()
Returns a mutable std::span of bytes of the referenced data.
::std::span< const char > chars () const
Returns a read-only std::span of chars of the referenced data.
::std::span< char > chars ()
Returns a mutable std::span of chars of the referenced data.
intptr_t compare (const slice & other) noexcept const
Lexicographical three-way comparison with another slice.
bool contains (const slice & needle) noexcept const
Checks whether the referenced data contains needle as a sub-slice.
byte front () noexcept const
Returns the first byte of the referenced data.
bool is_printable (bool disable_utf8=false) noexcept const
Checks whether the content of the slice is printable.
bool is_valid () noexcept const
Checks the slice is valid: returns false only if it has a null data pointer and a non-zero length.
operator MDBX_val * () noexcept
Returns the pointer to the underlying MDBX_val instance.
operator const MDBX_val * () noexcept const
Returns the pointer to the underlying MDBX_val instance.
constexpr basic_string () const
Copies the slice content into a std::basic_string .
constexpr basic_string_view () noexcept const
Return a string_view that references the same data as this slice.
slice & operator= (const slice &) noexcept
Copy assignment operator.
slice & operator= (const ::std::basic_string_view< CHAR, T > & view)
Assigns the slice to reference the contents of the given string view.
slice & operator= (::std::basic_string_view< CHAR, T > && view)
Takes over a temporary string view and assigns the slice to its contents.
constexpr slice (const char(&) text)
Create a slice that refers to text[0,strlen(text)-1].
constexpr slice (const ::std::basic_string< CHAR, T, A > & str)
Create a slice that refers to the contents of "str".
constexpr slice (const slice & src) noexcept
Copy constructor.
constexpr slice (const ::std::span< POD > & span)
Create a slice that refers to the contents of the given std::span .
constexpr slice (const ::std::basic_string_view< CHAR, T > & sv)
Create a slice that refers to the same contents as "string_view".
slice (::std::basic_string_view< CHAR, T > && sv)
Takes over a temporary string view: the slice refers to its data and the moved-from view is emptied.
::std::basic_string_view< CHAR, T > string_view () noexcept const
Return a string_view that references the same data as this slice.
slice substr (size_t pos, size_t count=npos) const
Returns a sub-slice of [pos, pos+count) clamped to the end.
void swap (::std::basic_string_view< CHAR, T > & view) noexcept
Swaps the slice content with the given string view.
void swap (slice & other) noexcept
Swaps this slice with another one.
::std::optional< POD > try_as_pod () const
Non-throwing counterpart of as_pod() : returnsstd::nullopt when the slice length does not exactly matchsizeof(POD) .

Public Static Functions

Type Name
slice blank_for_put_reserve (size_t length)
Builds a "blank" slice for a MDBX_RESERVE allocation: it has a null data pointer but the requested non-zero length, which the C API fills in on return fromtxn::insert_reserve() ,txn::upsert_reserve() and friends.
slice invalid () noexcept
Builds an invalid slice that has a non-zero length and refers to a null address.
slice null () noexcept
Build a null slice which zero length and refers to null address.
slice wrap (const char(&) text)
Wraps a null-terminated string literal into a slice.
slice wrap (const POD & pod)
Wraps an object of a standard-layout type into a slice.

Protected Functions

Type Name
constexpr slice (size_t invalid_length) noexcept
A special constructor without length checking, for invalid() andblank_for_put_reserve() .

Detailed Description

The slice is similar in many ways to std::string_view, but it implements specific capabilities and manipulates with bytes but not a characters.

Note:

A slice is non-owning: it references data allocated elsewhere and never copies or releases it. The referenced data must remain valid as long as the slice is used. Passing a temporary (e.g. a std::string converted inline in a call argument, like txn.insert (map, mdbx::slice(std::to_string(k)) , ...)) is safe: a temporary lives until the end of the full-expression and libmdbx copies the bytes during the call. In contrast, storing such a slice beyond the statement leaves it dangling.

See also: mdbx::slice

See also: mdbx::buffer Values returned from the table are valid only until a subsequent update operation, or the end of the transaction. Do not modify or free them, they commonly point into the database itself.

Key sizes must be between 0 and mdbx_env_get_maxkeysize() inclusive. The same applies to data sizes in tables with the MDBX_DUPSORT flag. Other data items can in theory be from 0 to MDBX_MAXDATASIZE bytes long.

Note:

The notable difference between MDBX and LMDB is that MDBX support zero length keys.

Public Types Documentation

enum @145120015325042373144130173373163042370340165246

enum mdbx::slice::@145120015325042373144130173373163042370340165246 {
    max_length = MDBX_MAXDATASIZE,
    npos = size_t(-1)
};

Public Functions Documentation

function as_int128

Reinterprets the slice as a 128-bit signed integer.

inline int128_t mdbx::slice::as_int128 () const

Returns:

The value; throws mdbx::bad_value_size on a size mismatch.


function as_int128_adapt

Reinterprets the slice as a 128-bit signed integer, adapting the value when the slice is shorter than 16 bytes.

int128_t mdbx::slice::as_int128_adapt () const

Returns:

The value adapted to the actual slice length.


function as_int16

Reinterprets the slice as a 16-bit signed integer.

inline int16_t mdbx::slice::as_int16 () const

Returns:

The value; throws mdbx::bad_value_size on a size mismatch.


function as_int16_adapt

Reinterprets the slice as a 16-bit signed integer, adapting the value when the slice is shorter than 2 bytes.

int16_t mdbx::slice::as_int16_adapt () const

Returns:

The value adapted to the actual slice length.


function as_int32

Reinterprets the slice as a 32-bit signed integer.

inline int32_t mdbx::slice::as_int32 () const

Returns:

The value; throws mdbx::bad_value_size on a size mismatch.


function as_int32_adapt

Reinterprets the slice as a 32-bit signed integer, adapting the value when the slice is shorter than 4 bytes.

int32_t mdbx::slice::as_int32_adapt () const

Returns:

The value adapted to the actual slice length.


function as_int64

Reinterprets the slice as a 64-bit signed integer.

inline int64_t mdbx::slice::as_int64 () const

Returns:

The value; throws mdbx::bad_value_size on a size mismatch.


function as_int64_adapt

Reinterprets the slice as a 64-bit signed integer, adapting the value when the slice is shorter than 8 bytes.

int64_t mdbx::slice::as_int64_adapt () const

Returns:

The value adapted to the actual slice length.


function as_int8

Reinterprets the slice as an 8-bit signed integer.

inline int8_t mdbx::slice::as_int8 () const

Returns:

The value; throws mdbx::bad_value_size on a size mismatch.


function as_int8_adapt

Reinterprets the slice as an 8-bit signed integer.

int8_t mdbx::slice::as_int8_adapt () const

Returns:

The value adapted to the actual slice length.


function as_pod

Reinterprets the slice content as an object of a standard-layout type.

template<typename POD>
inline POD mdbx::slice::as_pod () const

Returns:

A copy of the object of type POD read from the slice bytes.

Exception:


function as_span [1/2]

Returns a read-only std::span view of the referenced data.

template<typename POD>
inline ::std::span< const POD > mdbx::slice::as_span () const

Returns:

The std::span of the slice content.

Exception:


function as_span [2/2]

Returns a mutable std::span view of the referenced data.

template<typename POD>
inline ::std::span< POD > mdbx::slice::as_span () 

Returns:

The std::span of the slice content.

Exception:


function as_string

Copies the slice content into a std::basic_string .

template<class CHAR, class T, class ALLOCATOR>
inline ::std::basic_string< CHAR, T, ALLOCATOR > mdbx::slice::as_string (
    const ALLOCATOR & alloc=ALLOCATOR()
) const

Parameters:

  • alloc The allocator for the resulting string.

Returns:

The string with a copy of the slice content.


function as_uint128

Reinterprets the slice as a 128-bit unsigned integer.

inline uint128_t mdbx::slice::as_uint128 () const

Returns:

The value; throws mdbx::bad_value_size on a size mismatch.


function as_uint128_adapt

Reinterprets the slice as a 128-bit unsigned integer, adapting the value when the slice is shorter than 16 bytes.

uint128_t mdbx::slice::as_uint128_adapt () const

Returns:

The value adapted to the actual slice length.


function as_uint16

Reinterprets the slice as a 16-bit unsigned integer.

inline uint16_t mdbx::slice::as_uint16 () const

Returns:

The value; throws mdbx::bad_value_size on a size mismatch.


function as_uint16_adapt

Reinterprets the slice as a 16-bit unsigned integer, adapting the value when the slice is shorter than 2 bytes.

uint16_t mdbx::slice::as_uint16_adapt () const

Returns:

The value adapted to the actual slice length.


function as_uint32

Reinterprets the slice as a 32-bit unsigned integer.

inline uint32_t mdbx::slice::as_uint32 () const

Returns:

The value; throws mdbx::bad_value_size on a size mismatch.


function as_uint32_adapt

Reinterprets the slice as a 32-bit unsigned integer, adapting the value when the slice is shorter than 4 bytes.

uint32_t mdbx::slice::as_uint32_adapt () const

Returns:

The value adapted to the actual slice length.


function as_uint64

Reinterprets the slice as a 64-bit unsigned integer.

inline uint64_t mdbx::slice::as_uint64 () const

Returns:

The value; throws mdbx::bad_value_size on a size mismatch.


function as_uint64_adapt

Reinterprets the slice as a 64-bit unsigned integer, adapting the value when the slice is shorter than 8 bytes.

uint64_t mdbx::slice::as_uint64_adapt () const

Returns:

The value adapted to the actual slice length.


function as_uint8

Reinterprets the slice as an 8-bit unsigned integer.

inline uint8_t mdbx::slice::as_uint8 () const

Returns:

The value; throws mdbx::bad_value_size on a size mismatch.


function as_uint8_adapt

Reinterprets the slice as an 8-bit unsigned integer.

uint8_t mdbx::slice::as_uint8_adapt () const

Returns:

The value adapted to the actual slice length.


function assign [1/3]

Assigns the slice to reference the contents of the given string.

template<class CHAR, class T, class ALLOCATOR>
inline slice & mdbx::slice::assign (
    const ::std::basic_string< CHAR, T, ALLOCATOR > & str
) 

Parameters:

  • str The source string.

Returns:

A reference to this instance.


function assign [2/3]

Assigns the slice to reference the contents of the given string view.

template<class CHAR, class T>
inline slice & mdbx::slice::assign (
    const ::std::basic_string_view< CHAR, T > & view
) 

Parameters:

  • view The source string view.

Returns:

A reference to this instance.


function assign [3/3]

Takes over a temporary string view and assigns the slice to its contents.

template<class CHAR, class T>
inline slice & mdbx::slice::assign (
    ::std::basic_string_view< CHAR, T > && view
) 

Parameters:

  • view The source string view to be moved from.

Returns:

A reference to this instance.


function back

Returns the last byte of the referenced data.

inline byte mdbx::slice::back () noexcept const

Returns:

The last byte.

Precondition:

REQUIRES: !empty()


function bytes [1/2]

Returns a read-only std::span of bytes of the referenced data.

inline ::std::span< const byte > mdbx::slice::bytes () const

Returns:

The std::span<const byte > view.


function bytes [2/2]

Returns a mutable std::span of bytes of the referenced data.

inline ::std::span< byte > mdbx::slice::bytes () 

Returns:

The std::span< byte > view.


function chars [1/2]

Returns a read-only std::span of chars of the referenced data.

inline ::std::span< const char > mdbx::slice::chars () const

Returns:

The std::span<const char> view.


function chars [2/2]

Returns a mutable std::span of chars of the referenced data.

inline ::std::span< char > mdbx::slice::chars () 

Returns:

The std::span<char> view.


function compare

Lexicographical three-way comparison with another slice.

inline intptr_t mdbx::slice::compare (
    const slice & other
) noexcept const

Parameters:

  • other The slice to compare with.

Returns:

0 if equal, <0 if this slice is less, >0 if greater.


function contains

Checks whether the referenced data contains needle as a sub-slice.

inline bool mdbx::slice::contains (
    const slice & needle
) noexcept const

Parameters:

  • needle The sub-slice to search for.

Returns:

true if the slice contains the given sub-slice.


function front

Returns the first byte of the referenced data.

inline byte mdbx::slice::front () noexcept const

Returns:

The first byte.

Precondition:

REQUIRES: !empty()


function is_printable

Checks whether the content of the slice is printable.

bool mdbx::slice::is_printable (
    bool disable_utf8=false
) noexcept const

Parameters:

  • disable_utf8 By default if disable_utf8 is false function checks that content bytes are printable ASCII-7 characters or a valid UTF8 sequences. Otherwise, if disable_utf8 is true function checks that content bytes are printable extended 8-bit ASCII codes.

Returns:

true if the content is printable.


function is_valid

Checks the slice is valid: returns false only if it has a null data pointer and a non-zero length.

inline bool mdbx::slice::is_valid () noexcept const

Returns:

true if the slice is valid.


function operator MDBX_val *

Returns the pointer to the underlying MDBX_val instance.

inline mdbx::slice::operator MDBX_val * () noexcept

Returns:

The mutable pointer to this instance as MDBX_val.


function operator const MDBX_val *

Returns the pointer to the underlying MDBX_val instance.

inline mdbx::slice::operator const MDBX_val * () noexcept const

Returns:

The constant pointer to this instance as MDBX_val.


function basic_string

Copies the slice content into a std::basic_string .

template<class CHAR, class T, class ALLOCATOR>
inline explicit constexpr mdbx::slice::basic_string () const

Returns:

The string with a copy of the slice content.


function basic_string_view

Return a string_view that references the same data as this slice.

template<class CHAR, class T>
inline explicit constexpr mdbx::slice::basic_string_view () noexcept const

Returns:

The std::basic_string_view of the slice content.


function operator=

Copy assignment operator.

slice & mdbx::slice::operator= (
    const slice &
) noexcept

Returns:

A reference to this instance.


function operator=

Assigns the slice to reference the contents of the given string view.

template<class CHAR, class T>
inline slice & mdbx::slice::operator= (
    const ::std::basic_string_view< CHAR, T > & view
) 

Parameters:

  • view The source string view.

Returns:

A reference to this instance.


function operator=

Takes over a temporary string view and assigns the slice to its contents.

template<class CHAR, class T>
inline slice & mdbx::slice::operator= (
    ::std::basic_string_view< CHAR, T > && view
) 

Parameters:

  • view The source string view to be moved from.

Returns:

A reference to this instance.


function slice [1/7]

Create a slice that refers to text[0,strlen(text)-1].

template<size_t SIZE>
inline constexpr mdbx::slice::slice (
    const char(&) text
) 

Parameters:

  • text The null-terminated string literal to be referenced.

function slice [2/7]

Create a slice that refers to the contents of "str".

template<class CHAR, class T, class A>
inline explicit constexpr mdbx::slice::slice (
    const ::std::basic_string< CHAR, T, A > & str
) 

Note:

'explicit' to avoid reference to the temporary std::string instance. The slice is non-owning: it references the string's data without copying. Passing a temporary string inline in a call argument is safe (the temporary lives until the end of the full-expression and libmdbx copies the bytes during the call), but storing the resulting slice beyond the statement leaves it dangling — use mdbx::buffer to own a copy.

Parameters:

  • str The string which content is to be referenced.

function slice [3/7]

Copy constructor.

constexpr mdbx::slice::slice (
    const slice & src
) noexcept

Parameters:

  • src The source slice.

function slice [4/7]

Create a slice that refers to the contents of the given std::span .

template<typename POD>
inline constexpr mdbx::slice::slice (
    const ::std::span< POD > & span
) 

Parameters:

  • span The source span of a standard-layout type.

function slice [5/7]

Create a slice that refers to the same contents as "string_view".

template<class CHAR, class T>
inline constexpr mdbx::slice::slice (
    const ::std::basic_string_view< CHAR, T > & sv
) 

Parameters:

  • sv The source string view.

function slice [6/7]

Takes over a temporary string view: the slice refers to its data and the moved-from view is emptied.

template<class CHAR, class T>
inline mdbx::slice::slice (
    ::std::basic_string_view< CHAR, T > && sv
) 

Parameters:

  • sv The source string view to be moved from.

function string_view

Return a string_view that references the same data as this slice.

template<class CHAR, class T>
inline ::std::basic_string_view< CHAR, T > mdbx::slice::string_view () noexcept const

Returns:

The std::basic_string_view of the slice content.


function substr

Returns a sub-slice of [pos, pos+count) clamped to the end.

inline slice mdbx::slice::substr (
    size_t pos,
    size_t count=npos
) const

Parameters:

  • pos The offset of the first byte of the sub-slice.
  • count The number of bytes, npos means "up to the end".

Returns:

The sub-slice.

Exception:

  • std::out_of_range if pos > size().

function swap [1/2]

Swaps the slice content with the given string view.

template<class CHAR, class T>
inline void mdbx::slice::swap (
    ::std::basic_string_view< CHAR, T > & view
) noexcept

Parameters:

  • view The string view to swap with.

function swap [2/2]

Swaps this slice with another one.

inline void mdbx::slice::swap (
    slice & other
) noexcept

Parameters:

  • other The slice to swap with.

function try_as_pod

Non-throwing counterpart of as_pod() : returnsstd::nullopt when the slice length does not exactly matchsizeof(POD) .

template<typename POD>
inline ::std::optional< POD > mdbx::slice::try_as_pod () const

Note:

The returned object is a copy of the referenced bytes, so the slice need not outlive the result (issue #144).

Returns:

The object of type POD or std::nullopt on a size mismatch.


Public Static Functions Documentation

function blank_for_put_reserve

Builds a "blank" slice for a MDBX_RESERVE allocation: it has a null data pointer but the requested non-zero length, which the C API fills in on return fromtxn::insert_reserve() ,txn::upsert_reserve() and friends.

static inline slice mdbx::slice::blank_for_put_reserve (
    size_t length
) 

Since a null pointer with a non-zero length is rejected by the ordinary slice() constructor and assign() (issue #142), this explicit factory is the only way to spell the reserve form; it builds the blank via the special private constructor (like invalid()) but still validates length against MDBX_MAXDATASIZE, so an oversize request throws std::length_error exactly as the old slice(nullptr, N) spelling did.

Parameters:

  • length The requested length of the blank slice.

Returns:

The blank slice for a reserve allocation.

Exception:

  • std::length_error if length exceeds the maximum value size.

function invalid

Builds an invalid slice that has a non-zero length and refers to a null address.

static inline slice mdbx::slice::invalid () noexcept

Returns:

The invalid slice instance.


function null

Build a null slice which zero length and refers to null address.

static inline slice mdbx::slice::null () noexcept

Returns:

The null slice instance.


function wrap [1/2]

Wraps a null-terminated string literal into a slice.

template<size_t SIZE>
static inline slice mdbx::slice::wrap (
    const char(&) text
) 

Parameters:

  • text The string literal to be referenced.

Returns:

The slice referring to the literal without the terminating null.


function wrap [2/2]

Wraps an object of a standard-layout type into a slice.

template<typename POD>
static inline slice mdbx::slice::wrap (
    const POD & pod
) 

Parameters:

  • pod The object to be referenced.

Returns:

The slice referring to the object's bytes.


Protected Functions Documentation

function slice [7/7]

A special constructor without length checking, for invalid() andblank_for_put_reserve() .

inline constexpr mdbx::slice::slice (
    size_t invalid_length
) noexcept

Parameters:

  • invalid_length The length to be set with a null data pointer.

Friends Documentation

friend operator!=

bool mdbx::slice::operator!= (
    const slice & a,
    const slice & b
) 

friend operator<

bool mdbx::slice::operator< (
    const slice & a,
    const slice & b
) 

friend operator<=

bool mdbx::slice::operator<= (
    const slice & a,
    const slice & b
) 

friend operator==

bool mdbx::slice::operator== (
    const slice & a,
    const slice & b
) 

friend operator>

bool mdbx::slice::operator> (
    const slice & a,
    const slice & b
) 

friend operator>=

bool mdbx::slice::operator>= (
    const slice & a,
    const slice & b
) 


The documentation for this class was generated from the following file mdbx.h++