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