Compare commits
4 Commits
ab09914434
...
e0c9e2f551
Author | SHA1 | Date | |
---|---|---|---|
e0c9e2f551 | |||
27b3f4182a | |||
24cb2afb8e | |||
dfaa03647f |
@ -46,6 +46,7 @@ add_subdirectory(src/myx/qt)
|
||||
|
||||
# Примеры
|
||||
if(MYXLIB_BUILD_EXAMPLES OR MYXLIB_BUILD_EXAMPLES_HO)
|
||||
add_subdirectory(examples/base)
|
||||
add_subdirectory(examples/filesystem)
|
||||
add_subdirectory(examples/qt)
|
||||
endif()
|
||||
|
77
examples/base/01_endian/CMakeLists.txt
Normal file
77
examples/base/01_endian/CMakeLists.txt
Normal file
@ -0,0 +1,77 @@
|
||||
# Название основной цели в текущем каталоге
|
||||
set(TRGT example-endian-minimal)
|
||||
|
||||
# Список файлов исходных текстов
|
||||
set(TRGT_cpp ${CMAKE_CURRENT_SOURCE_DIR}/endian.cpp)
|
||||
|
||||
if(MYXLIB_BUILD_EXAMPLES)
|
||||
# Путь поиска библиотек внутри проекта
|
||||
link_directories(${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
|
||||
|
||||
# Цель для создания исполняемого файла
|
||||
add_executable(${TRGT} ${TRGT_cpp} ${TRGT_qrc})
|
||||
common_target_properties(${TRGT})
|
||||
|
||||
# Создание цели для проверки утилитой clang-tidy
|
||||
add_clang_tidy_check(${TRGT} ${TRGT_cpp})
|
||||
|
||||
# Создание цели для проверки утилитой clang-analyze
|
||||
add_clang_analyze_check(${TRGT} ${TRGT_cpp})
|
||||
|
||||
# Создание цели для проверки утилитой clazy
|
||||
add_clazy_check(${TRGT} ${TRGT_cpp})
|
||||
|
||||
# Создание цели для проверки утилитой pvs-studio
|
||||
add_pvs_check(${TRGT})
|
||||
|
||||
# Создание цели для автоматического форматирования кода
|
||||
add_format_sources(${TRGT} ${TRGT_cpp})
|
||||
|
||||
# Qt5
|
||||
target_include_directories(${TRGT} PRIVATE ${CMAKE_SOURCE_DIR}/src)
|
||||
target_include_directories(${TRGT} SYSTEM PUBLIC ${Qt5Core_INCLUDE_DIRS})
|
||||
|
||||
target_include_directories(${TRGT} SYSTEM PRIVATE ${CMAKE_SOURCE_DIR}/src)
|
||||
add_dependencies(${TRGT} base)
|
||||
|
||||
target_link_libraries(${TRGT} base_static)
|
||||
target_link_libraries(${TRGT} Qt5::Core)
|
||||
target_link_libraries(${TRGT} Threads::Threads)
|
||||
|
||||
# Имя выходного файла для цели
|
||||
set_target_properties(${TRGT} PROPERTIES OUTPUT_NAME endian-minimal)
|
||||
|
||||
add_sanitizers(${TRGT})
|
||||
|
||||
cotire(${TRGT})
|
||||
|
||||
add_dependencies(${TRGT} create_auxilary_symlinks)
|
||||
|
||||
# Правила для установки
|
||||
install(TARGETS ${TRGT} COMPONENT examples RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
|
||||
endif()
|
||||
|
||||
if(MYXLIB_BUILD_EXAMPLES_HO)
|
||||
# Цель для создания исполняемого файла
|
||||
add_executable(${TRGT}-ho ${TRGT_cpp} ${TRGT_qrc})
|
||||
common_target_properties(${TRGT}-ho)
|
||||
|
||||
target_include_directories(${TRGT}-ho PRIVATE ${CMAKE_SOURCE_DIR}/src)
|
||||
target_include_directories(${TRGT}-ho SYSTEM PUBLIC ${Qt5Core_INCLUDE_DIRS})
|
||||
|
||||
add_dependencies(${TRGT}-ho base-header-only)
|
||||
|
||||
target_link_libraries(${TRGT}-ho Qt5::Core)
|
||||
target_link_libraries(${TRGT}-ho Threads::Threads)
|
||||
|
||||
# Имя выходного файла для цели
|
||||
set_target_properties(${TRGT}-ho PROPERTIES OUTPUT_NAME endian-minimal-ho)
|
||||
|
||||
add_sanitizers(${TRGT}-ho)
|
||||
cotire(${TRGT}-ho)
|
||||
|
||||
add_dependencies(${TRGT}-ho create_auxilary_symlinks)
|
||||
|
||||
# Правила для установки
|
||||
install(TARGETS ${TRGT}-ho COMPONENT examples RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
|
||||
endif()
|
25
examples/base/01_endian/endian.cpp
Normal file
25
examples/base/01_endian/endian.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
#include <myx/base/config.hpp>
|
||||
#include <myx/base/endian_types.hpp>
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
#define CMLIB_PROJECT_NAME "myxlib"
|
||||
|
||||
namespace MB = myx::base;
|
||||
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
beint64 bi = 1;
|
||||
int64_t bii = reinterpret_cast< int64_t* >( &bi )[0];
|
||||
qDebug() << hex << bi << bii;
|
||||
|
||||
leint64 li = 1;
|
||||
int64_t lii = reinterpret_cast< int64_t* >( &li )[0];
|
||||
qDebug() << hex << li << lii;
|
||||
|
||||
return( 0 );
|
||||
} // main
|
1
examples/base/CMakeLists.txt
Normal file
1
examples/base/CMakeLists.txt
Normal file
@ -0,0 +1 @@
|
||||
add_subdirectory(01_endian)
|
@ -48,4 +48,4 @@ int main( int argc, char** argv )
|
||||
qDebug() << "userLogDirectory : " << paths.userLogDirectory();
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
} // main
|
||||
|
@ -10,6 +10,7 @@ set(TRGT_cpp
|
||||
set(TRGT_hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/config.hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/limits.hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/span.hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/endian_types.hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/enum_bitmask_operations.hpp)
|
||||
|
||||
|
@ -3,6 +3,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <myx/base/span.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
@ -11,10 +13,7 @@
|
||||
#include <QDebug>
|
||||
#endif
|
||||
|
||||
// Both LittleEndianType and BigEndianType are manufactured primitives
|
||||
// that are essentially immutable except for the assignment operators.
|
||||
// All math logic happens outside of the class in the native format
|
||||
// and an internal set/swap method is used to store the data.
|
||||
// Основа здесь: https://github.com/tatewake/endian-template/
|
||||
|
||||
namespace myx {
|
||||
|
||||
@ -24,44 +23,20 @@ template< typename T >
|
||||
class EndianTypesBase
|
||||
{
|
||||
protected:
|
||||
#if _MSC_VER
|
||||
#if defined ( _MSC_VER )
|
||||
#pragma warning ( push )
|
||||
#endif
|
||||
static T swapBytes( const T& b )
|
||||
{
|
||||
T n;
|
||||
switch ( sizeof( T ) )
|
||||
{
|
||||
case 8: // 64-bit
|
||||
( reinterpret_cast< uint8_t* >( &n ) )[0] = ( reinterpret_cast< const uint8_t* >( &b ) )[7];
|
||||
( reinterpret_cast< uint8_t* >( &n ) )[1] = ( reinterpret_cast< const uint8_t* >( &b ) )[6];
|
||||
( reinterpret_cast< uint8_t* >( &n ) )[2] = ( reinterpret_cast< const uint8_t* >( &b ) )[5];
|
||||
( reinterpret_cast< uint8_t* >( &n ) )[3] = ( reinterpret_cast< const uint8_t* >( &b ) )[4];
|
||||
( reinterpret_cast< uint8_t* >( &n ) )[4] = ( reinterpret_cast< const uint8_t* >( &b ) )[3];
|
||||
( reinterpret_cast< uint8_t* >( &n ) )[5] = ( reinterpret_cast< const uint8_t* >( &b ) )[2];
|
||||
( reinterpret_cast< uint8_t* >( &n ) )[6] = ( reinterpret_cast< const uint8_t* >( &b ) )[1];
|
||||
( reinterpret_cast< uint8_t* >( &n ) )[7] = ( reinterpret_cast< const uint8_t* >( &b ) )[0];
|
||||
break;
|
||||
case 4: // 32-bit
|
||||
( reinterpret_cast< uint8_t* >( &n ) )[0] = ( reinterpret_cast< const uint8_t* >( &b ) )[3];
|
||||
( reinterpret_cast< uint8_t* >( &n ) )[1] = ( reinterpret_cast< const uint8_t* >( &b ) )[2];
|
||||
( reinterpret_cast< uint8_t* >( &n ) )[2] = ( reinterpret_cast< const uint8_t* >( &b ) )[1];
|
||||
( reinterpret_cast< uint8_t* >( &n ) )[3] = ( reinterpret_cast< const uint8_t* >( &b ) )[0];
|
||||
break;
|
||||
case 2: // 16-bit
|
||||
( reinterpret_cast< uint8_t* >( &n ) )[0] = ( reinterpret_cast< const uint8_t* >( &b ) )[1];
|
||||
( reinterpret_cast< uint8_t* >( &n ) )[1] = ( reinterpret_cast< const uint8_t* >( &b ) )[0];
|
||||
break;
|
||||
default:
|
||||
assert( 0 ); // Endian swap is only defined for 2, 4, and 8-byte types
|
||||
break;
|
||||
} // switch
|
||||
|
||||
myx::base::span< uint8_t > sn( reinterpret_cast< uint8_t* >( &n ), sizeof( T ) );
|
||||
myx::base::span< const uint8_t > sb( reinterpret_cast< const uint8_t* >( &b ), sizeof( T ) );
|
||||
std::reverse_copy( sb.begin(), sb.end(), sn.begin() );
|
||||
return( n );
|
||||
} // swapBytes
|
||||
|
||||
|
||||
#if _MSC_VER
|
||||
#if defined( _MSC_VER )
|
||||
#pragma warning ( pop )
|
||||
#endif
|
||||
}; // class EndianTypesBase
|
||||
@ -87,7 +62,8 @@ public:
|
||||
// 1. Set an OE from a PE
|
||||
// 2. Get a PE from an OE
|
||||
// Storage in
|
||||
explicit LittleEndianType( const T& b ) :
|
||||
// NOLINTNEXTLINE
|
||||
LittleEndianType( const T& b ) :
|
||||
m_data( transform( b ) )
|
||||
{}
|
||||
template < typename U > explicit LittleEndianType( U const& b ) :
|
||||
@ -121,12 +97,11 @@ public:
|
||||
friend std::istream& operator >>( std::istream& in, LittleEndianType& b ) { T val; in >> val; b = val; return( in ); }
|
||||
std::string toStdString() { return( std::to_string( transform( m_data ) ) ); }
|
||||
#ifdef QT_CORE_LIB
|
||||
friend QDebug& operator <<( QDebug& out, const LittleEndianType b ) { out << T( b ); return( out ); }
|
||||
friend QDebug& operator <<( QDebug& out, const LittleEndianType< T > b ) { out << T( b ); return( out ); }
|
||||
QString toQString() { return( QString::number( transform( m_data ) ) ); }
|
||||
#endif
|
||||
}; // class LittleEndianType
|
||||
|
||||
|
||||
template< typename T >
|
||||
class BigEndianType : public EndianTypesBase< T >
|
||||
{
|
||||
@ -148,7 +123,8 @@ public:
|
||||
// 1. Set an OE from a PE
|
||||
// 2. Get a PE from an OE
|
||||
// Storage in
|
||||
explicit BigEndianType( const T& b ) :
|
||||
// NOLINTNEXTLINE
|
||||
BigEndianType( const T& b ) :
|
||||
m_data( transform( b ) )
|
||||
{}
|
||||
template < typename U > explicit BigEndianType( U const& b ) :
|
||||
@ -182,7 +158,7 @@ public:
|
||||
friend std::istream& operator >>( std::istream& in, BigEndianType& b ) { T val; in >> val; b = val; return( in ); }
|
||||
std::string toStdString() { return( std::to_string( transform( m_data ) ) ); }
|
||||
#ifdef QT_CORE_LIB
|
||||
friend QDebug& operator <<( QDebug& out, const BigEndianType b ) { out << T( b ); return( out ); }
|
||||
friend QDebug& operator <<( QDebug& out, const BigEndianType< T > b ) { out << T( b ); return( out ); }
|
||||
QString toQString() { return( QString::number( transform( m_data ) ) ); }
|
||||
#endif
|
||||
}; // class BigEndianType
|
||||
|
243
src/myx/base/span.hpp
Normal file
243
src/myx/base/span.hpp
Normal file
@ -0,0 +1,243 @@
|
||||
//
|
||||
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// Official repository: https://github.com/boostorg/beast
|
||||
//
|
||||
|
||||
#ifndef MYX_BASE_SPAN_HPP_
|
||||
#define MYX_BASE_SPAN_HPP_
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
namespace myx {
|
||||
|
||||
namespace base {
|
||||
|
||||
template< typename ... Ts > struct make_void { typedef void type;}; // NOLINT
|
||||
template< typename ... Ts > using void_t = typename make_void< Ts... >::type; // NOLINT
|
||||
|
||||
template< class T, class E, class = void >
|
||||
struct is_contiguous_container : std::false_type {}; // NOLINT
|
||||
|
||||
template< class T, class E >
|
||||
struct is_contiguous_container< T, E, void_t<
|
||||
decltype(
|
||||
std::declval< std::size_t& >() = std::declval< T const& >().size(),
|
||||
std::declval< E*& >() = std::declval< T& >().data() ),
|
||||
typename std::enable_if<
|
||||
std::is_same<
|
||||
typename std::remove_cv< E >::type,
|
||||
typename std::remove_cv<
|
||||
typename std::remove_pointer<
|
||||
decltype( std::declval< T& >().data() )
|
||||
>::type
|
||||
>::type
|
||||
>::value
|
||||
>::type > > : std::true_type
|
||||
{};
|
||||
|
||||
|
||||
/** A range of bytes expressed as a ContiguousContainer
|
||||
|
||||
This class implements a non-owning reference to a storage
|
||||
area of a certain size and having an underlying integral
|
||||
type with size of 1.
|
||||
|
||||
@tparam T The type pointed to by span iterators
|
||||
*/
|
||||
template< class T >
|
||||
class span // NOLINT
|
||||
{
|
||||
T* m_data = nullptr;
|
||||
std::size_t m_size = 0;
|
||||
|
||||
public:
|
||||
/// The type of value, including cv qualifiers
|
||||
using element_type = T;
|
||||
|
||||
/// The type of value of each span element
|
||||
using value_type = typename std::remove_const< T >::type;
|
||||
|
||||
/// The type of integer used to index the span
|
||||
using index_type = std::ptrdiff_t;
|
||||
|
||||
/// A pointer to a span element
|
||||
using pointer = T*;
|
||||
|
||||
/// A reference to a span element
|
||||
using reference = T&;
|
||||
|
||||
/// The iterator used by the container
|
||||
using iterator = pointer;
|
||||
|
||||
/// The const pointer used by the container
|
||||
using const_pointer = T const*;
|
||||
|
||||
/// The const reference used by the container
|
||||
using const_reference = T const&;
|
||||
|
||||
/// The const iterator used by the container
|
||||
using const_iterator = const_pointer;
|
||||
|
||||
/// Constructor
|
||||
span() = default;
|
||||
|
||||
/// Constructor
|
||||
span( span const& ) = default;
|
||||
|
||||
/// Assignment
|
||||
span& operator=( span const& ) = default;
|
||||
|
||||
/** Constructor
|
||||
|
||||
@param data A pointer to the beginning of the range of elements
|
||||
|
||||
@param size The number of elements pointed to by `data`
|
||||
*/
|
||||
span( T* data, std::size_t size ) :
|
||||
m_data( data ),
|
||||
m_size( size )
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/** Constructor
|
||||
|
||||
@param container The container to construct from
|
||||
*/
|
||||
template< class ContiguousContainer,
|
||||
class = typename std::enable_if<
|
||||
is_contiguous_container<
|
||||
ContiguousContainer, T >::value >::type
|
||||
>
|
||||
explicit
|
||||
span( ContiguousContainer&& container ) :
|
||||
m_data( container.data() ),
|
||||
m_size( container.size() )
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
template< class CharT, class Traits, class Allocator >
|
||||
explicit
|
||||
span( std::basic_string< CharT, Traits, Allocator >& s ) :
|
||||
m_data( &s[0] ),
|
||||
m_size( s.size() )
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
template< class CharT, class Traits, class Allocator >
|
||||
explicit
|
||||
span( std::basic_string< CharT, Traits, Allocator > const& s ) :
|
||||
m_data( s.data() ),
|
||||
m_size( s.size() )
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/** Assignment
|
||||
|
||||
@param container The container to assign from
|
||||
*/
|
||||
template< class ContiguousContainer >
|
||||
typename std::enable_if< is_contiguous_container<
|
||||
ContiguousContainer, T >::value,
|
||||
span& >::type
|
||||
operator=( ContiguousContainer&& container )
|
||||
{
|
||||
m_data = container.data();
|
||||
m_size = container.size();
|
||||
return( *this );
|
||||
}
|
||||
|
||||
|
||||
template< class CharT, class Traits, class Allocator >
|
||||
span&
|
||||
operator=( std::basic_string<
|
||||
CharT, Traits, Allocator >& s )
|
||||
{
|
||||
m_data = &s[0];
|
||||
m_size = s.size();
|
||||
return( *this );
|
||||
}
|
||||
|
||||
|
||||
template< class CharT, class Traits, class Allocator >
|
||||
span&
|
||||
operator=( std::basic_string<
|
||||
CharT, Traits, Allocator > const& s )
|
||||
{
|
||||
m_data = s.data();
|
||||
m_size = s.size();
|
||||
return( *this );
|
||||
}
|
||||
|
||||
|
||||
/// Returns `true` if the span is empty
|
||||
bool
|
||||
empty() const
|
||||
{
|
||||
return( m_size == 0 );
|
||||
}
|
||||
|
||||
|
||||
/// Returns a pointer to the beginning of the span
|
||||
T*
|
||||
data() const
|
||||
{
|
||||
return( m_data );
|
||||
}
|
||||
|
||||
|
||||
/// Returns the number of elements in the span
|
||||
std::size_t
|
||||
size() const
|
||||
{
|
||||
return( m_size );
|
||||
}
|
||||
|
||||
|
||||
/// Returns an iterator to the beginning of the span
|
||||
iterator
|
||||
begin() const
|
||||
{
|
||||
return( m_data );
|
||||
}
|
||||
|
||||
|
||||
/// Returns an iterator to the beginning of the span
|
||||
const_iterator
|
||||
cbegin() const
|
||||
{
|
||||
return( m_data );
|
||||
}
|
||||
|
||||
|
||||
/// Returns an iterator to one past the end of the span
|
||||
iterator
|
||||
end() const
|
||||
{
|
||||
return( m_data + m_size );
|
||||
}
|
||||
|
||||
|
||||
/// Returns an iterator to one past the end of the span
|
||||
const_iterator
|
||||
cend() const
|
||||
{
|
||||
return( m_data + m_size );
|
||||
}
|
||||
}; // class span
|
||||
|
||||
} // namespace base
|
||||
|
||||
} // namespace myx
|
||||
|
||||
#endif // ifndef MYX_BASE_SPAN_HPP_
|
Loading…
Reference in New Issue
Block a user