Compare commits

..

No commits in common. "e0c9e2f551b47d49caea79629aeea10be96b0e8b" and "ab09914434475bfdfe22518dc964cf086062bbbb" have entirely different histories.

8 changed files with 39 additions and 363 deletions

View File

@ -46,7 +46,6 @@ 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()

View File

@ -1,77 +0,0 @@
# Название основной цели в текущем каталоге
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()

View File

@ -1,25 +0,0 @@
#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

View File

@ -1 +0,0 @@
add_subdirectory(01_endian)

View File

@ -48,4 +48,4 @@ int main( int argc, char** argv )
qDebug() << "userLogDirectory : " << paths.userLogDirectory();
return( 0 );
} // main
}

View File

@ -10,7 +10,6 @@ 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)

View File

@ -3,8 +3,6 @@
#pragma once
#include <myx/base/span.hpp>
#include <cassert>
#include <cstdint>
#include <iostream>
@ -13,7 +11,10 @@
#include <QDebug>
#endif
// Основа здесь: https://github.com/tatewake/endian-template/
// 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.
namespace myx {
@ -23,20 +24,44 @@ template< typename T >
class EndianTypesBase
{
protected:
#if defined ( _MSC_VER )
#if _MSC_VER
#pragma warning ( push )
#endif
static T swapBytes( const T& b )
{
T n;
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() );
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
return( n );
} // swapBytes
#if defined( _MSC_VER )
#if _MSC_VER
#pragma warning ( pop )
#endif
}; // class EndianTypesBase
@ -62,8 +87,7 @@ public:
// 1. Set an OE from a PE
// 2. Get a PE from an OE
// Storage in
// NOLINTNEXTLINE
LittleEndianType( const T& b ) :
explicit LittleEndianType( const T& b ) :
m_data( transform( b ) )
{}
template < typename U > explicit LittleEndianType( U const& b ) :
@ -97,11 +121,12 @@ 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< T > b ) { out << T( b ); return( out ); }
friend QDebug& operator <<( QDebug& out, const LittleEndianType 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 >
{
@ -123,8 +148,7 @@ public:
// 1. Set an OE from a PE
// 2. Get a PE from an OE
// Storage in
// NOLINTNEXTLINE
BigEndianType( const T& b ) :
explicit BigEndianType( const T& b ) :
m_data( transform( b ) )
{}
template < typename U > explicit BigEndianType( U const& b ) :
@ -158,7 +182,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< T > b ) { out << T( b ); return( out ); }
friend QDebug& operator <<( QDebug& out, const BigEndianType b ) { out << T( b ); return( out ); }
QString toQString() { return( QString::number( transform( m_data ) ) ); }
#endif
}; // class BigEndianType

View File

@ -1,243 +0,0 @@
//
// 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_