Skip to content

Commit 644a6bf

Browse files
committed
Add 'Tweak header' support via "nonstd/span.tweak.hpp"
See: Tweak-header, A New Approach to Build-Time Library Configuration, by Colby Pike aka vector-of-bool. Oct 4, 2020 https://vector-of-bool.github.io/2020/10/04/lib-configuration.html See also nonstd-lite/nonstd-lite#44
1 parent 7165baf commit 644a6bf

File tree

8 files changed

+39
-7
lines changed

8 files changed

+39
-7
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ To construct a span from a container with compilers that cannot constrain such a
192192
Configuration
193193
-------------
194194

195+
### Tweak header
196+
197+
If the compiler supports [`__has_include()`](https://en.cppreference.com/w/cpp/preprocessor/include), *span lite* supports the [tweak header](https://vector-of-bool.github.io/2020/10/04/lib-configuration.html) mechanism. Provide your *tweak header* as `nonstd/span.tweak.hpp` in a folder in the include-search-path. In the tweak header, provide definitions as documented below, like `#define span_CONFIG_NO_EXCEPTIONS 1`.
198+
195199
### Standard selection macro
196200

197201
\-D<b>span\_CPLUSPLUS</b>=199711L
@@ -433,6 +437,7 @@ span<>: Allows to construct from a const C-array with size via decay to pointer
433437
span<>: Allows to construct from a std::initializer_list<> (C++11)
434438
span<>: Allows to construct from a std::array<> (C++11)
435439
span<>: Allows to construct from a std::array<> with const data (C++11, span_FEATURE_CONSTR..._ELEMENT_TYPE=1)
440+
span<>: Allows to construct from an empty std::array<> (C++11)
436441
span<>: Allows to construct from a container (std::vector<>)
437442
span<>: Allows to tag-construct from a container (std::vector<>)
438443
span<>: Allows to tag-construct from a const container (std::vector<>)
@@ -505,4 +510,5 @@ tuple_size<>: Allows to obtain the number of elements via std::tuple_size<> (C++
505510
tuple_element<>: Allows to obtain an element via std::tuple_element<> (C++11)
506511
tuple_element<>: Allows to obtain an element via std::tuple_element_t<> (C++11)
507512
get<I>(spn): Allows to access an element via std::get<>()
513+
tweak header: reads tweak header if supported [tweak]
508514
```

include/nonstd/span.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@
2626
#define span_SPAN_NONSTD 1
2727
#define span_SPAN_STD 2
2828

29+
// tweak header support:
30+
31+
#ifdef __has_include
32+
# if __has_include(<nonstd/span.tweak.hpp>)
33+
# include <nonstd/span.tweak.hpp>
34+
# endif
35+
#define span_HAVE_TWEAK_HEADER 1
36+
#else
37+
#define span_HAVE_TWEAK_HEADER 0
38+
//# pragma message("span.hpp: Note: Tweak header not supported.")
39+
#endif
40+
41+
// span selection and configuration:
42+
2943
#define span_HAVE( feature ) ( span_HAVE_##feature )
3044

3145
#ifndef span_CONFIG_SELECT_SPAN

test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ set( unit_name "span" )
1515
set( PACKAGE ${unit_name}-lite )
1616
set( PROGRAM ${unit_name}-lite )
1717
set( SOURCES ${unit_name}-main.t.cpp ${unit_name}.t.cpp )
18+
set( TWEAKD "." )
1819

1920
message( STATUS "Subproject '${PROJECT_NAME}', programs '${PROGRAM}-*'")
2021

@@ -139,6 +140,7 @@ function( make_target target std )
139140
message( STATUS "Make target: '${std}'" )
140141

141142
add_executable ( ${target} ${SOURCES} ${HDRPATH} )
143+
target_include_directories( ${target} PRIVATE ${TWEAKD} )
142144
target_link_libraries ( ${target} PRIVATE ${PACKAGE} )
143145
target_compile_options ( ${target} PRIVATE ${OPTIONS} )
144146
target_compile_definitions( ${target} PRIVATE ${DEFINITIONS} )

test/nonstd/span.tweak.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#define SPAN_TWEAK_VALUE 42

test/span.t.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,7 @@ CASE( "tuple_element<>: Allows to obtain an element via std::tuple_element<> (C+
15511551
#if span_HAVE( STRUCT_BINDING )
15521552
using S = span<int,3>;
15531553
using T = std::tuple_element<0, S>::type;
1554-
1554+
15551555
EXPECT( (std::is_same<T, int>::value) );
15561556

15571557
static_assert( std::is_same<T, int>::value, "std::tuple_element<0, S>::type fails" );
@@ -1565,7 +1565,7 @@ CASE( "tuple_element<>: Allows to obtain an element via std::tuple_element_t<> (
15651565
#if span_HAVE( STRUCT_BINDING ) && span_CPP11_140
15661566
using S = span<int,3>;
15671567
using T = std::tuple_element_t<0, S>;
1568-
1568+
15691569
EXPECT( (std::is_same<T, int>::value) );
15701570

15711571
static_assert( std::is_same<T, int>::value, "std::tuple_element_t<0, S> fails" );
@@ -1606,9 +1606,9 @@ CASE( "get<I>(spn): Allows to access an element via std::get<>()" )
16061606

16071607
// static_assert( std::get< 1 >( vc ) == 2, "std::tuple_element<I>(spn) fails" );
16081608
}
1609-
1609+
16101610
SECTION("rvalue")
1611-
{
1611+
{
16121612
EXPECT( std::get< 1 >( std::move(vna) ) == 2 );
16131613
EXPECT( std::get< 1 >( std::move(vnb) ) == 2 );
16141614
EXPECT( std::get< 1 >( std::move(vca) ) == 2 );
@@ -1715,4 +1715,13 @@ CASE( "[hide][issue-3: same()]" )
17151715
#endif // span_FEATURE_TO_STD( MAKE_SPAN )
17161716
}
17171717

1718+
CASE( "tweak header: reads tweak header if supported " "[tweak]" )
1719+
{
1720+
#if span_HAVE_TWEAK_HEADER
1721+
EXPECT( SPAN_TWEAK_VALUE == 42 );
1722+
#else
1723+
EXPECT( !!"Tweak header is not available (span_HAVE_TWEAK_HEADER: 0)." );
1724+
#endif
1725+
}
1726+
17181727
// end of file

test/t.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ set CppCoreCheckInclude=^
5151
set byte_lite=^
5252
-Dspan_BYTE_LITE_HEADER=\"../../byte-lite/include/nonstd/byte.hpp\"
5353

54-
cl -W3 -EHsc %std% %unit_select% %unit_contract% %unit_config% %msvc_defines% %byte_lite% -I"%CppCoreCheckInclude%" -I../include %unit%-main.t.cpp %unit%.t.cpp && %unit%-main.t.exe
54+
cl -W3 -EHsc %std% %unit_select% %unit_contract% %unit_config% %msvc_defines% %byte_lite% -I"%CppCoreCheckInclude%" -I../include -I. %unit%-main.t.cpp %unit%.t.cpp && %unit%-main.t.exe
5555
endlocal & goto :EOF
5656

5757
:: subroutines:

test/tc.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ rem -flto / -fwhole-program
5050
set optflags=-O2
5151
set warnflags=-Wall -Wextra -Wpedantic -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded -Wno-missing-noreturn -Wno-documentation-unknown-command -Wno-documentation-deprecated-sync -Wno-documentation -Wno-weak-vtables -Wno-missing-prototypes -Wno-missing-variable-declarations -Wno-exit-time-destructors -Wno-global-constructors
5252

53-
"%clang%" -m32 -std=%std% %optflags% %warnflags% %unit_select% %unit_contract% %unit_config% %byte_lite% -fms-compatibility-version=19.00 -isystem "%VCInstallDir%include" -isystem "%WindowsSdkDir_71A%include" -I../include -o %unit%-main.t.exe %unit%-main.t.cpp %unit%.t.cpp && %unit%-main.t.exe
53+
"%clang%" -m32 -std=%std% %optflags% %warnflags% %unit_select% %unit_contract% %unit_config% %byte_lite% -fms-compatibility-version=19.00 -isystem "%VCInstallDir%include" -isystem "%WindowsSdkDir_71A%include" -I../include -I. -o %unit%-main.t.exe %unit%-main.t.cpp %unit%.t.cpp && %unit%-main.t.exe
5454
endlocal & goto :EOF
5555

5656
:: subroutines:

test/tg.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ rem -flto / -fwhole-program
5151
set optflags=-O2
5252
set warnflags=-Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wno-padded -Wno-missing-noreturn
5353

54-
%gpp% -std=%std% %optflags% %warnflags% %unit_select% %unit_contract% %unit_config% %byte_lite% -o %unit%-main.t.exe -I../include %unit%-main.t.cpp %unit%.t.cpp && %unit%-main.t.exe
54+
%gpp% -std=%std% %optflags% %warnflags% %unit_select% %unit_contract% %unit_config% %byte_lite% -o %unit%-main.t.exe -I../include -I. %unit%-main.t.cpp %unit%.t.cpp && %unit%-main.t.exe
5555

5656
endlocal & goto :EOF
5757

0 commit comments

Comments
 (0)