Monero
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
1// Copyright (c) 2018-2022, The Monero Project
2
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without modification, are
6// permitted provided that the following conditions are met:
7//
8// 1. Redistributions of source code must retain the above copyright notice, this list of
9// conditions and the following disclaimer.
10//
11// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12// of conditions and the following disclaimer in the documentation and/or other
13// materials provided with the distribution.
14//
15// 3. Neither the name of the copyright holder nor the names of its contributors may be
16// used to endorse or promote products derived from this software without specific
17// prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#pragma once
29
30#include <cstddef>
31#include <cstring>
32#include <lmdb.h>
33#include <type_traits>
34#include <utility>
35
36#include "span.h"
37
41#define MONERO_FIELD(obj, field) \
42 obj , decltype(std::declval<obj>().field) , offsetof(obj, field)
43
45#define MONERO_SORT_BY(obj, field) \
46 &::lmdb::less< \
47 lmdb::native_type<decltype(std::declval<obj>().field)>, \
48 offsetof(obj, field) \
49 >
50
52#define MONERO_COMPARE(obj, field) \
53 &::lmdb::compare< \
54 decltype(std::declval<obj>().field), \
55 offsetof(obj, field) \
56 >
57
58namespace lmdb
59{
61 template<typename T>
62 struct identity
63 {
64 using type = T;
65 };
66
75 template<typename T>
76 using native_type = typename std::conditional<
77 std::is_enum<T>::value, std::underlying_type<T>, identity<T>
78 >::type::type;
79
81 template<typename T, typename U = typename std::underlying_type<T>::type>
82 inline constexpr U to_native(T value) noexcept
83 {
84 return U(value);
85 }
86
88 template<typename T>
89 inline MDB_val to_val(T&& value) noexcept
90 {
91 // lmdb does not touch user data, so const_cast is acceptable
92 static_assert(!std::is_rvalue_reference<T&&>(), "cannot use temporary value");
93 void const* const temp = reinterpret_cast<void const*>(std::addressof(value));
94 return MDB_val{sizeof(value), const_cast<void*>(temp)};
95 }
96
99 {
100 return {static_cast<const std::uint8_t*>(value.mv_data), value.mv_size};
101 }
102
111 template<typename T, std::size_t offset = 0>
112 inline int less(MDB_val const* left, MDB_val const* right) noexcept
113 {
114 static_assert(std::is_trivially_copyable<T>(), "memcpy will not work");
115 if (!left || !right || left->mv_size < sizeof(T) + offset || right->mv_size < sizeof(T) + offset)
116 {
117 assert("invalid use of custom comparison" == 0);
118 return -1;
119 }
120
121 T left_val;
122 T right_val;
123 std::memcpy(std::addressof(left_val), static_cast<char*>(left->mv_data) + offset, sizeof(T));
124 std::memcpy(std::addressof(right_val), static_cast<char*>(right->mv_data) + offset, sizeof(T));
125 return left_val < right_val ? -1 : bool(right_val < left_val);
126 }
127
136 template<typename T, std::size_t offset = 0>
137 inline int compare(MDB_val const* left, MDB_val const* right) noexcept
138 {
139 static_assert(std::is_standard_layout<T>() && alignof(T) == 1, "memcmp will not work");
140 if (!left || !right || left->mv_size < sizeof(T) + offset || right->mv_size < sizeof(T) + offset)
141 {
142 assert("invalid use of custom comparison" == 0);
143 return -1;
144 }
145 return std::memcmp(
146 static_cast<char*>(left->mv_data) + offset,
147 static_cast<char*>(right->mv_data) + offset,
148 sizeof(T)
149 );
150 }
151} // lmdb
Non-owning sequence of data. Does not deep copy.
Definition span.h:55
#define const
Definition ipfrdr.c:80
Lightning memory-mapped database library.
Definition compare.py:1
Definition database.cpp:46
constexpr epee::span< const std::uint8_t > to_byte_span(MDB_val value) noexcept
Definition util.h:98
int less(MDB_val const *left, MDB_val const *right) noexcept
Definition util.h:112
constexpr U to_native(T value) noexcept
Definition util.h:82
MDB_val to_val(T &&value) noexcept
Definition util.h:89
typename std::conditional< std::is_enum< T >::value, std::underlying_type< T >, identity< T > >::type::type native_type
Definition util.h:76
const GenericPointer< typename T::ValueType > T2 value
Definition pointer.h:1225
Generic structure used for passing keys and data in and out of the database.
Definition lmdb.h:286
Prevent instantiation of std::underlying_type<T> when T is not enum.
Definition util.h:63
T type
Definition util.h:64
#define T(x)