• Stars
    star
    123
  • Rank 289,033 (Top 6 %)
  • Language
    C++
  • License
    Boost Software Li...
  • Created almost 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

any lite - A C++17-like any, a type-safe container for single values of any type for C++98, C++11 and later in a single-file header-only library

any-lite: A single-file header-only version of a C++17-like any, a type-safe container for single values of any type for C++98, C++11 and later

Language License Build Status Build status Version download Conan Try it online Try it on godbolt online

Contents

Example usage

#include "nonstd/any.hpp"

#include <cassert>
#include <string>

using namespace nonstd;

int main()
{
    std::string hello = "hello, world";

    any var;

    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 );
}

Compile and run

prompt> g++ -Wall -I../include -o 01-basic 01-basic.cpp && 01-basic

In a nutshell

any lite is a single-file header-only library to represent a type-safe container for single values of any type. The library aims to provide a C++17-like any for use with C++98 and later. If available, std::any is used.

Features and properties of any lite are ease of installation (single header), freedom of dependencies other than the standard library. any lite shares the approach to in-place tags with expected-lite, optional-lite and with variant-lite and these libraries can be used together.

Limitations of any lite are the absence of small-object optimization: all contained objects are dynamically allocated. Move construction, move assignment and emplacement require C++11 and are not supported when compiling under C++98.

License

any lite is distributed under the Boost Software License.

Dependencies

any lite has no other dependencies than the C++ standard library.

Installation

any lite is a single-file header-only library. Put any.hpp in the include folder directly into the project source tree or somewhere reachable from your project.

Synopsis

Contents

Types and values in namespace nonstd

Purpose Type / value Notes
Type-safe container class any  
Error reporting class bad_any_cast  
In-place construction struct in_place_tag  
  in_place select type or index for in-place construction
  in_place_type select type for in-place construction
 (variant) in_place_index select index for in-place construction
  nonstd_lite_in_place_type_t( T) macro for alias template in_place_type_t<T>
 (variant) nonstd_lite_in_place_index_t( T ) macro for alias template in_place_index_t<T>

Interface of any lite

Kind Std Method Result
Construction   any() default-construct
    any( any const & rhs ) copy-construct from other any
  C++11 any( any && rhs ) noexcept move-construct from other any
  C++11 template< class ValueType >
any( ValueType && value ) noexcept
move-assign from value
  C++11 template< class T >
explicit any( in_place_type_t<T>, Args&&... args )
in-place-construct type T
  C++11 template< class T, class U, class... Args >
explicit any( in_place_type_t<T>, std::initializer_list<U> il, Args&&... args )
in-place-construct type T
  <C++11 template< class ValueType >
any( ValueType const & value )
copy-assign from value
    ~any() destroy current object
Assignment   any & operator=( any const & rhs ) copy-assign from other
  C++11 any & operator=( any && rhs ) noexcept move-assign from other
  C++11 template< class ValueType, ...>
any & operator=( ValueType && rhs )
(move-)assign from value
  <C++11 template< class ValueType >
any & operator=( ValueType const & rhs )
copy-assign from value
Modifiers C++11 template< class T, class... Args >
void emplace( Args && ... args )
emplace type T
  C++11 template< class T, class U, class... Args >
void emplace( std::initializer_list<U> il, Args&&... args )
emplace type T
    void reset() noexcept destroy contained object
    void swap( any & rhs ) noexcept exchange with other any
Observers   bool has_value() const noexcept contains an object
    const std::type_info & type() const noexcept Type of contained object

Algorithms for any lite

Kind Std Function Result
Create C++11 template< class T, class ...Args >
any make_any( Args&& ...args )
in-place construct
  C++11 template< class T, class U, class ...Args >
any make_any( std::initializer_list<U> il, Args&& ...args )
in-place construct
Access   T any_cast<T>( any const & ) obtained value
    T any_cast<T>( any & ) obtained value
  C++11 T any_cast<T>( any && ) obtained value
    T const * any_cast<T>( any const * ) pointer to obtained value
    T * any_cast<T>( any * ) pointer to obtained value
Swap   void swap( any & x, any & y ) exchange contents

Configuration

Tweak header

If the compiler supports __has_include(), any lite supports the tweak header mechanism. Provide your tweak header as nonstd/any.tweak.hpp in a folder in the include-search-path. In the tweak header, provide definitions as documented below, like #define any_CPLUSPLUS 201103L.

Standard selection macro

-Dany_CPLUSPLUS=199711L
Define this macro to override the auto-detection of the supported C++ standard, if your compiler does not set the __cpluplus macro correctly.

Select std::any or nonstd::any

At default, any lite uses std::any if it is available and lets you use it via namespace nonstd. You can however override this default and explicitly request to use std::any or any lite's nonstd::any as nonstd::any via the following macros.

-Dany_CONFIG_SELECT_ANY=any_ANY_DEFAULT
Define this to any_ANY_STD to select std::any as nonstd::any. Define this to any_ANY_NONSTD to select nonstd::any as nonstd::any. Default is undefined, which has the same effect as defining to any_ANY_DEFAULT.

Disable exceptions

-Dany_CONFIG_NO_EXCEPTIONS=0 Define this to 1 if you want to compile without exceptions. If not defined, the header tries and detect if exceptions have been disabled (e.g. via -fno-exceptions). Default is undefined.

Reported to work with

The table below mentions the compiler versions any lite is reported to work with.

OS Compiler Versions
Windows Clang/LLVM ?
  GCC 5.2.0
  Visual C++
(Visual Studio)
8 (2005), 10 (2010), 11 (2012),
12 (2013), 14 (2015)
GNU/Linux Clang/LLVM 3.5.0
  GCC 4.8.4
OS X ? ?

Building the tests

To build the tests you need:

The lest test framework is included in the test folder.

The following steps assume that the any lite source code has been cloned into a directory named c:\any-lite.

Buck

any-lite> buck run test/

CMake

  1. Create a directory for the build outputs for a particular architecture. Here we use c:\any-lite\build-win-x86-vc10.

     ~> cd c:\any-lite
     any-lite> md build-win-x86-vc10
     any-lite> cd build-win-x86-vc10
    
  2. Configure CMake to use the compiler of your choice (run cmake --help for a list).

     any-lite\build> cmake -G "Visual Studio 10 2010" ..
    
  3. Build the test suite in the Debug configuration (alternatively use Release).

     any-lite\build> cmake --build . --config Debug
    
  4. Run the test suite.

     any-lite\build> ctest -V -C Debug
    

All tests should pass, indicating your platform is supported and you are ready to use any lite.

Other implementations of any

  • Isabella Muerte. MNMLSTC Core (C++11).
  • Kevlin Henney. Boost.Any. Safe, generic container for single values of different value types. 2001.

Notes and References

[1] CppReference. Any.

[2] ISO/IEC WG21. N4606, section 20.8 Storage for any type. July 2016.

[3] Beman Dawes and Kevlin Henney. N3508: Any Library Proposal (Revision 2). January 2013.

[4] Kevlin Henney. Boost.Any. Safe, generic container for single values of different value types. 2001.

[5] Kevlin Henney. Valued Conversions (PDF). C++ report, July, August 2000.

[6] Kevlin Henney. Substitutability. Principles, Idioms and Techniques for C++ (PDF). Presented at JaCC, Oxford, 16th September 1999.

[7] Kevlin Henney. Idioms. Breaking the Language Barrier (PDF). Presented at the ACCU's C and C++ European Developers Forum, the Oxford Union, Oxford, UK, 12th September 1998.

Appendix

A.1 Compile-time information

The version of any lite is available via tag [.version]. The following tags are available for information on the compiler and on the C++ standard library used: [.compiler], [.stdc++], [.stdlanguage] and [.stdlibrary].

A.2 Any lite test specification

any: Allows to default construct any
any: Allows to copy-construct from any
any: Allows to move-construct from any (C++11)
any: Allows to copy-construct from literal value
any: Allows to copy-construct from const value
any: Allows to copy-construct from lvalue references
any: Allows to move-construct from value (C++11)
any: Allows to in-place construct from literal value (C++11)
any: Allows to in-place copy-construct from value (C++11)
any: Allows to in-place move-construct from value (C++11)
any: Allows to in-place copy-construct from initializer-list (C++11)
any: Allows to in-place move-construct from initializer-list (C++11)
any: Allows to copy-assign from any
any: Allows to move-assign from any (C++11)
any: Allows to copy-assign from literal value
any: Allows to copy-assign from value
any: Allows to move-assign from value (C++11)
any: Allows to copy-emplace content (C++11)
any: Allows to move-emplace content (C++11)
any: Allows to copy-emplace content from intializer-list (C++11)
any: Allows to move-emplace content from intializer-list (C++11)
any: Allows to reset content
any: Allows to swap with other any (member)
any: Allows to inspect if any contains a value
any: Allows to obtain type_info of any's content
swap: Allows to swap with other any (non-member)
make_any: Allows to in-place copy-construct any from arguments (C++11)
make_any: Allows to in-place move-construct any from arguments (C++11)
make_any: Allows to in-place copy-construct any from initializer-list and arguments (C++11)
make_any: Allows to in-place move-construct any from initializer-list and arguments (C++11)
any_cast: Allows to obtain any's content by value (any const &)
any_cast: Allows to obtain any's content by value (any &)
any_cast: Allows to obtain any's content by value (any &&)
any_cast: Allows to obtain any's content by pointer (any const *)
any_cast: Allows to obtain any's content by pointer (any *)
any_cast: Throws bad_any_cast if requested type differs from content type (any const &)
any_cast: Throws bad_any_cast if requested type differs from content type (any &)
any_cast: Throws bad_any_cast if requested type differs from content type (any &&)
any_cast: Throws bad_any_cast with non-empty what()
tweak header: reads tweak header if supported [tweak]

More Repositories

1

span-lite

span lite - A C++20-like span for C++98, C++11 and later in a single-file header-only library
C++
495
star
2

optional-lite

optional lite - A C++17-like optional, a nullable object for C++98, C++11 and later in a single-file header-only library
C++
393
star
3

lest

A modern, C++11-native, single-file header-only, tiny framework for unit-tests, TDD and BDD (includes C++98 variant)
C++
366
star
4

string-view-lite

string_view lite - A C++17-like string_view for C++98, C++11 and later in a single-file header-only library
C++
364
star
5

expected-lite

expected lite - Expected objects in C++11 and later in a single-file header-only library
C++
274
star
6

variant-lite

variant lite - A C++17-like variant, a type-safe union for C++98, C++11 and later in a single-file header-only library
C++
230
star
7

ring-span-lite

ring-span lite - A C++yy-like ring_span type for C++98, C++11 and later in a single-file header-only library
C++
136
star
8

PhysUnits-CT-Cpp11

A small C++11, C++14 header-only library for compile-time dimensional analysis and unit/quantity manipulation and conversion
C++
77
star
9

nonstd-lite

Parent of *-lite repositories, a migration path to post-C++11 features for pre-C++11 environments
Batchfile
68
star
10

clue

A tiny single-file header-only C++ logging framework
C++
56
star
11

jthread-lite

C++20's jthread for C++11 and later in a single-file header-only library
C++
53
star
12

expected-dark

Expected objects for C++11 and later (and later perhaps C++98 )
C++
52
star
13

byte-lite

byte lite - A C++17-like byte type for C++98, C++11 and later in a single-file header-only library
C++
51
star
14

value-ptr-lite

value-ptr-lite - A C++ smart-pointer with value semantics for C++98, C++11 and later in a single-file header-only library
C++
44
star
15

observer-ptr-lite

observer-ptr - An observer_ptr for C++98 and later in a single-file header-only library (Extensions for Library Fundamentals, v2, v3)
C++
36
star
16

scope-lite

A migration path to C++ library extensions scope_exit, scope_fail, scope_success, unique_resource
C++
34
star
17

type-lite

type - Strong types for C++98, C++11 and later in a single-file header-only library
C++
34
star
18

bit-lite

bit-lite - C++20 bit operations for C++98 and later in a single-file header-only library
C++
33
star
19

martin-moene.blogspot.com

Code from my Blog
C++
30
star
20

invoke-lite

A single-file header-only version of C++17-like invoke() for C++98, C++11 and later
C++
29
star
21

catch-lest-other-comparison

Tabularised feature comparison between Catch, doctest and lest C++ test frameworks
23
star
22

kalman-estimator

kalman-estimator - a Kalman estimator in C++
C++
22
star
23

string-lite

String facilities for C++98 and later - a library in search of its identity.
C++
19
star
24

optional-bare

optional bare - A simple version of a C++17-like optional for default-constructible, copyable types, for C++98 and later in a single-file header-only library
C++
18
star
25

spike-expected

expected: kind of optional?
HTML
16
star
26

status-value-lite

status-value - A class for status and optional value for C++11 and later, C++98 variant provided in a single-file header-only library
C++
14
star
27

atomic-lite

atomic lite - a C++11 atomic operations library for C++98 and later
C++
12
star
28

PhysUnits-RT

A C++ header-only library for run-time dimensional analysis and unit/quantity manipulation and conversion
C++
11
star
29

PhysUnits-CT

A C++ header-only library for compile-time dimensional analysis and unit/quantity manipulation and conversion
C++
11
star
30

WholeValue

Whole value idiom made easy in C++
C++
8
star
31

optional-fun-lite

optional-fun lite - Functional algorithms for optional (lite) for C++98, C++11 and later in a single-file header-only library
C++
6
star
32

EngFormat-Cpp

C++ based Engineering Notation Formatter
C++
6
star
33

indirect-value-lite

indirect_value lite – An indirect value-type for C++11 and later in a single-file header-only library (p1950)
C++
6
star
34

ACCUConf2016

Slides and other materials from ACCU Conference 2016
5
star
35

array_view2d

A simple 2D view on an array or vector for C++98 and C++11
C++
5
star
36

clamp

Limit a value or a range of values to fall between two extremes
C++
4
star
37

boolean-lite

boolean lite: A strong boolean type for C++98 and later
C++
4
star
38

svn-churn

A simple Python script to determine file churn and fix count of a Subversion repository.
Python
3
star
39

active-lite

Active objects
CMake
2
star
40

clamp-proposal

Propose clamp for C++ standard
CSS
2
star
41

kalman-estimator-ada

kalman-estimator - a Kalman estimator in Ada, sibling of Kalman estimator in C++
Makefile
2
star
42

nonstd-lite-project

Stuff common to nonstd lite repositories
CMake
2
star
43

Test-Normalised

1
star
44

bibliography

My bookshelf
TeX
1
star
45

hamlest

Matchers for lest
C++
1
star
46

wordindex

wordindex: create a linenumber cross-referenced list of words
C++
1
star
47

Test-NonNormalised

1
star
48

wordindex-ranged

wordindex: create a linenumber cross-referenced list of words using C++20 std::ranges
C++
1
star
49

testdox

TestDox - generate a readable overview of test case names from the specified files.
Hack
1
star