bit7z 4.1.0
A C++ library for interfacing with the 7-zip shared libs.
Loading...
Searching...
No Matches
bittypes.hpp
1/*
2 * bit7z - A C++ static library to interface with the 7-zip shared libraries.
3 * Copyright (c) Riccardo Ostani - All Rights Reserved.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
8 */
9
10#ifndef BITTYPES_HPP
11#define BITTYPES_HPP
12
13// Must be included here since the user might have manually enabled a BIT7Z_* compilation option
14// by uncommenting the corresponding macro define in bitdefines.hpp.
15#include "bitdefines.hpp" // For BIT7Z_* macros.
16
17#ifdef BIT7Z_REGEX_MATCHING
18#include <regex>
19#endif
20
21#include <cstddef>
22#include <string>
23#include <type_traits>
24#include <vector>
25
26namespace bit7z {
27
31#ifdef BIT7Z_USE_STD_BYTE
32#if __cpp_lib_byte
33using byte_t = std::byte;
34#else
35enum class byte_t : unsigned char {}; //same as std::byte_t
36#endif
37#else
38using byte_t = unsigned char;
39#endif
40
42using buffer_t = std::vector< byte_t >;
43using index_t = std::ptrdiff_t; //like gsl::index (https://github.com/microsoft/GSL)
44
45template< class Char >
46struct StringTraits;
47
48template<>
49struct StringTraits< char > {
50 template< class T >
51 static auto convertToString( T value ) -> std::string {
52 return std::to_string( value );
53 }
54};
55
56template<>
57struct StringTraits< wchar_t > {
58 template< class T >
59 static auto convertToString( T value ) -> std::wstring {
60 return std::to_wstring( value );
61 }
62};
64
70
75#ifdef _WIN32
77#define BIT7Z_NATIVE_STRING_( str ) L##str
78#define BIT7Z_NATIVE_STRING( str ) BIT7Z_NATIVE_STRING_( str )
79#else
81#define BIT7Z_NATIVE_STRING( str ) str
82#endif
83
87using native_char = native_string::value_type;
88
92#if defined( BIT7Z_USE_NATIVE_STRING ) && defined( _WIN32 ) // Windows with native strings
93using tchar = wchar_t;
94#define BIT7Z_STRING( str ) BIT7Z_NATIVE_STRING_( str )
95#else // Unix, and Windows with non-native strings
96using tchar = char;
97#define BIT7Z_STRING( str ) str
98#endif
99
105
106#ifdef BIT7Z_REGEX_MATCHING
112#endif
113
121template< typename T, typename = typename std::enable_if< std::is_arithmetic< T >::value, T >::type >
123 return StringTraits< tchar >::convertToString( arg );
124}
125
139#if defined( _WIN32 ) && !defined( BIT7Z_USE_NATIVE_STRING )
140BIT7Z_NODISCARD
141auto to_tstring( const native_string& str ) -> tstring;
142
143BIT7Z_NODISCARD
144BIT7Z_ALWAYS_INLINE
145auto to_tstring( tstring&& str ) -> tstring {
146 return std::move( str );
147}
148
149BIT7Z_NODISCARD
150BIT7Z_ALWAYS_INLINE
151auto to_tstring( const tstring& str ) -> const tstring& {
152 return str;
153}
154#else
155BIT7Z_NODISCARD
156BIT7Z_ALWAYS_INLINE
158 return std::move( str );
159}
160
161BIT7Z_NODISCARD
162BIT7Z_ALWAYS_INLINE
163auto to_tstring( const native_string& str ) -> const tstring& {
164 return str;
165}
166#endif
168
176template< typename T, typename = typename std::enable_if< std::is_arithmetic< T >::value, T >::type >
178#ifdef _WIN32
179 return std::to_wstring( arg );
180#else
181 return std::to_string( arg );
182#endif
183}
184
196BIT7Z_NODISCARD
197BIT7Z_ALWAYS_INLINE
199 return std::move( str );
200}
201
202BIT7Z_NODISCARD
203BIT7Z_ALWAYS_INLINE
204auto to_native_string( const native_string& str ) -> const native_string& {
205 return str;
206}
207
208#ifndef _WIN32
209BIT7Z_NODISCARD
211#elif !defined( BIT7Z_USE_NATIVE_STRING )
212BIT7Z_NODISCARD
213auto to_native_string( const tstring& str ) -> native_string;
214#endif
216
220template< typename Enum >
222
230template< typename Enum >
231constexpr auto to_underlying( Enum enum_value ) noexcept -> underlying_type_t< Enum > {
232 return static_cast< underlying_type_t< Enum > >( enum_value );
233}
234
238template< typename From, typename To >
241
242} // namespace bit7z
243
244#endif // BITTYPES_HPP
T move(T... args)
The main namespace of the bit7z library.
Definition bit7zlibrary.hpp:29
auto to_native_string(T arg) -> native_string
Converts an arithmetic value to a native string.
Definition bittypes.hpp:177
unsigned char byte_t
A type representing a byte.
Definition bittypes.hpp:38
std::wstring sevenzip_string
The string type used internally by 7-Zip.
Definition bittypes.hpp:69
std::basic_regex< tchar > tregex
Definition bittypes.hpp:111
constexpr auto to_underlying(Enum enum_value) noexcept -> underlying_type_t< Enum >
Converts an enumerator to its underlying integer value.
Definition bittypes.hpp:231
char tchar
Definition bittypes.hpp:96
native_string::value_type native_char
The character type of the system's native string type.
Definition bittypes.hpp:87
std::basic_string< tchar > tstring
Definition bittypes.hpp:104
std::integral_constant< bool, std::is_constructible< To, From >::value && !std::is_convertible< From, To >::value > is_explicitly_convertible
Type trait that is true if and only if From is explicitly (but not implicitly) convertible to To.
Definition bittypes.hpp:239
typename std::underlying_type< Enum >::type underlying_type_t
Alias for the underlying integer type of the given enumeration type.
Definition bittypes.hpp:221
std::string native_string
Native string type of the system.
Definition bittypes.hpp:80
auto to_tstring(T arg) -> std::basic_string< tchar >
Converts an arithmetic value to a tstring.
Definition bittypes.hpp:122
T to_string(T... args)
T to_wstring(T... args)