mirror of
https://github.com/intrepidcs/libicsneo.git
synced 2026-08-05 09:28:40 +02:00
Optional: Move optional-lite to the include/third-party directory
This fixes the use case of adding the include folder manually to an outside project.
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
#include "nonstd/optional.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
using nonstd::optional;
|
||||
using nonstd::nullopt;
|
||||
|
||||
optional<int> to_int( char const * const text )
|
||||
{
|
||||
char * pos = NULL;
|
||||
const int value = static_cast<int>( strtol( text, &pos, 0 ) );
|
||||
|
||||
return pos == text ? nullopt : optional<int>( value );
|
||||
}
|
||||
|
||||
int main( int argc, char * argv[] )
|
||||
{
|
||||
const char * text = argc > 1 ? argv[1] : "42";
|
||||
|
||||
optional<int> oi = to_int( text );
|
||||
|
||||
if ( oi ) std::cout << "'" << text << "' is " << *oi << std::endl;
|
||||
else std::cout << "'" << text << "' isn't a number" << std::endl;
|
||||
}
|
||||
|
||||
// cl -nologo -W3 -EHsc -I../include to_int.cpp && to_int x1
|
||||
// g++ -Wall -Wextra -std=c++03 -I../include -o to_int.exe to_int.cpp && to_int x1
|
||||
@@ -0,0 +1,36 @@
|
||||
#include "nonstd/optional.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using nonstd::optional;
|
||||
|
||||
struct V
|
||||
{
|
||||
int v;
|
||||
|
||||
V( int v )
|
||||
: v( v ) {}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
try
|
||||
{
|
||||
int x = 42;
|
||||
// V s; // V has no default constructor
|
||||
V t(x); // Ok
|
||||
|
||||
std::vector< optional<V> > v(3);
|
||||
v[0] = t;
|
||||
|
||||
std::cout << "v[0].value().v: " << v[0].value().v << "\n";
|
||||
std::cout << "v[1].value().v: " << v[1].value().v << "\n";
|
||||
}
|
||||
catch( std::exception const & e )
|
||||
{
|
||||
std::cout << "Error: " << e.what() << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
// cl -nologo -W3 -EHsc -I../include 00-nodefltctor.cpp && 00-nodefltctor
|
||||
@@ -0,0 +1,43 @@
|
||||
#include "nonstd/any.hpp"
|
||||
#include "nonstd/optional.hpp"
|
||||
#include "nonstd/variant.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <string>
|
||||
|
||||
using namespace nonstd;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string hello = "hello, world";
|
||||
{
|
||||
optional< int > var;
|
||||
|
||||
assert( ! var );
|
||||
|
||||
var = 7 ; assert( *var == 7 );
|
||||
var = 7 ; assert( var.value() == 7 );
|
||||
}{
|
||||
any var;
|
||||
|
||||
assert( ! var.has_value() );
|
||||
|
||||
var = 'v' ; assert( any_cast<char>( var ) == 'v' );
|
||||
var = 7 ; assert( any_cast<int >( var ) == 7 );
|
||||
var = 42L ; assert( any_cast<long>( var ) == 42L );
|
||||
var = hello; assert( any_cast<std::string>( var ) == hello );
|
||||
}{
|
||||
variant< char, int, long, std::string > var;
|
||||
|
||||
assert( ! var.valueless_by_exception() );
|
||||
|
||||
assert( get< 0 >( var ) == char() );
|
||||
var = 'v' ; assert( get<char>( var ) == 'v' );
|
||||
var = 7 ; assert( get<int >( var ) == 7 );
|
||||
var = 42L ; assert( get<long>( var ) == 42L );
|
||||
var = hello; assert( get<std::string>( var ) == hello );
|
||||
}
|
||||
}
|
||||
|
||||
// cl -nologo -EHsc -I../../any-lite/include -I../../optional-lite/include -I../../variant-lite/include 04-any-optional-variant.cpp && 04-any-optional-variant
|
||||
// g++ -Wall -I../../any-lite/include -I../../optional-lite/include -I../../variant-lite/include -o 04-any-optional-variant 04-any-optional-variant.cpp && 04-any-optional-variant
|
||||
@@ -0,0 +1,13 @@
|
||||
#include "nonstd/optional.hpp"
|
||||
|
||||
using nonstd::optional;
|
||||
|
||||
int main()
|
||||
{
|
||||
optional<int> v;
|
||||
|
||||
v.value(); // asserts (normally throws)
|
||||
}
|
||||
|
||||
// cl -nologo -I../include 05-no-exceptions.cpp && 05-no-exceptions
|
||||
// g++ -Wall -fno-exceptions -I../include -o 05-no-exceptions 05-no-exceptions.cpp && 05-no-exceptions
|
||||
@@ -0,0 +1,38 @@
|
||||
cxx_binary(
|
||||
name = '01-to_int',
|
||||
srcs = [
|
||||
'01-to_int.cpp',
|
||||
],
|
||||
compiler_flags = [
|
||||
'-std=c++11',
|
||||
],
|
||||
deps = [
|
||||
'//:optional-lite',
|
||||
],
|
||||
)
|
||||
|
||||
cxx_binary(
|
||||
name = '02-nodefltctor',
|
||||
srcs = [
|
||||
'02-nodefltctor.cpp',
|
||||
],
|
||||
compiler_flags = [
|
||||
'-std=c++11',
|
||||
],
|
||||
deps = [
|
||||
'//:optional-lite',
|
||||
],
|
||||
)
|
||||
|
||||
cxx_binary(
|
||||
name = '04-any-optional-variant',
|
||||
srcs = [
|
||||
'04-any-optional-variant.cpp',
|
||||
],
|
||||
compiler_flags = [
|
||||
'-std=c++11',
|
||||
],
|
||||
deps = [
|
||||
'//:optional-lite',
|
||||
],
|
||||
)
|
||||
@@ -0,0 +1,60 @@
|
||||
# Copyright 2017-2018 by Martin Moene
|
||||
#
|
||||
# https://github.com/martinmoene/optional-lite
|
||||
#
|
||||
# Distributed under the Boost Software License, Version 1.0.
|
||||
# (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
if( NOT DEFINED CMAKE_MINIMUM_REQUIRED_VERSION )
|
||||
cmake_minimum_required( VERSION 3.5 FATAL_ERROR )
|
||||
endif()
|
||||
|
||||
project( example LANGUAGES CXX )
|
||||
|
||||
set( unit_name "optional" )
|
||||
set( PACKAGE ${unit_name}-lite )
|
||||
set( PROGRAM ${unit_name}-lite )
|
||||
|
||||
message( STATUS "Subproject '${PROJECT_NAME}', examples '${PROGRAM}-*'")
|
||||
|
||||
set( OPTIONS "" )
|
||||
|
||||
if( MSVC )
|
||||
message( STATUS "Matched: MSVC")
|
||||
|
||||
set( BASE_OPTIONS -W3 )
|
||||
set( EXCEPTIONS_OPTIONS ${BASE_OPTIONS} -EHsc )
|
||||
set( NO_EXCEPTIONS_OPTIONS ${BASE_OPTIONS} )
|
||||
|
||||
elseif( CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang" )
|
||||
message( STATUS "CompilerId: '${CMAKE_CXX_COMPILER_ID}'")
|
||||
|
||||
set( BASE_OPTIONS -Wall -Wextra -Wconversion -Wsign-conversion -Wno-missing-braces -fno-elide-constructors )
|
||||
set( EXCEPTIONS_OPTIONS ${BASE_OPTIONS} )
|
||||
set( NO_EXCEPTIONS_OPTIONS -fno-exceptions )
|
||||
|
||||
elseif( CMAKE_CXX_COMPILER_ID MATCHES "Intel" )
|
||||
# as is
|
||||
message( STATUS "Matched: Intel")
|
||||
else()
|
||||
# as is
|
||||
message( STATUS "Matched: nothing")
|
||||
endif()
|
||||
|
||||
function( make_target name no_exceptions )
|
||||
add_executable ( ${PROGRAM}-${name} ${name}.cpp )
|
||||
target_link_libraries ( ${PROGRAM}-${name} PRIVATE ${PACKAGE} )
|
||||
if ( no_exceptions )
|
||||
target_compile_options ( ${PROGRAM}-${name} PRIVATE ${NO_EXCEPTIONS_OPTIONS} )
|
||||
else()
|
||||
target_compile_options ( ${PROGRAM}-${name} PRIVATE ${EXCEPTIONS_OPTIONS} )
|
||||
endif()
|
||||
|
||||
endfunction()
|
||||
|
||||
make_target( 01-to_int FALSE )
|
||||
make_target( 02-nodefltctor FALSE )
|
||||
# 04-any-optional-variant also requires variant-lite.
|
||||
make_target( 05-no-exceptions TRUE )
|
||||
|
||||
# end of file
|
||||
Reference in New Issue
Block a user