Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview

Artifact ID: eb255721ec5d6358f138c023b8f92a765a8bb3cc85bbf0d839dba911f4a7607f
Page Name:Supplements
Date: 2019-02-03 22:37:08
Original User: luismachuca
Parent: c8f37b29567c96fa72cef2248f7170daed3fb14ce92e02889b87e0bc658e5a91
Content

Cxxomfort Library Supplements

The cxxomfort library offers some specific utilities of its own, not found in C++03 or C++11.

The header cxxomfort/library.hpp> is responsible for adding these features, and automatically included when using #include <cxxomfort/cxxomfort.hpp> as well. All the features thus included are defined in namespace cxxomfort.

Unlike Extras, these features are considered intrinsic to the cxxomfort library: other features are allowed to depend on them, and they do not need to be explicitly included to work.

General index of library utilities:

Supplementals for:
<algorithm> -- <functional> -- <iterator> -- <numeric> -- <random> -- <string> -- <tuple> -- <type_traits> -- <utility>

cxxomfort-specific utilities:
i12n (Initialization of Sequences) -- Foreach Loop Emulation -- array_ref (array-like views) -- fixed_vector (constant-size vector) -- Sequence-related Utilities Literal Strings -- "Local Function" Emulation -- Type Identification -- typesafe_enum (scoped enum-like types)

Array References

#include <cxxomfort/library/array_ref.hpp>

The cxxomfort library implements n3334's array_ref<T> as a Supplement: a reference or view-type for a contiguous sequence of elements. Generated Documentation.

Foreach Loop

#include <cxxomfort/library/foreach.hpp>

"foreach" Loop emulation: allows writing C++11-like foreach loops in C++03 via a reworked implementation of the Artima Foreach utility, which also generates native foreach loops in C++11 onwards, thus resulting in zero overhead.

SomeSequence<string> mystrings;
// fill mystrings somehow
CXXO_FOREACH /string const& s, mystrings) {
    // do stuff with 's'
}

Fixed_vector

#include <cxxomfort/library/fixed_vector.hpp>

fixed_vector<T> - the oft sought for holy grail of a std::vector whose size is fixed at construction as an invariant. Generated Documentation.

fixed_vector<short> fixed(32);
// this vector has exactly 32 elements and can not be resized.

Initialization of Sequence Containers

#include <cxxomfort/library/i12n.hpp>

Initialization of Sequences: a way to use a syntax similar to C++11's { , , , } initialization of containers in various kinda of sequence-like containers, even in C++03.

CXXO_I12N_SEQ (vector<string>, mystrings, { "foo", "bar", "baz" } );
// operate on mystrings
cout<< mystrings[1]<< endl; // foo

Local Function Emulation

#include <cxxomfort/library/localfn.hpp>

Using local functions in template calls: a way to have locally defined functions or functors, which then can be used as arguments for eg.: STL algorithm calls. While one of the big limitations in C++03 is the inability to use local types as arguments to anything templated, this library adds a partial implementation that creates proxy functor objects, where the actual "calling" code resides out of scope, and that produces code near transpartent to C++11 onwards.

// here, X is a tag that identifies the functor type in case one needs to add state or other nontrivial behaviour
CXXO_LOCALFN(X,void(int,int)) (int x0, int x1) {
    std::cout<< '{'<< x0+1<< ','<< x1-1<< '}'<< std::endl;
} 
CXXO_LOCALFN_NAME(X,mi_fn);
function<void(int,int)> f_localfn = mi_fn; // <-- can only exist in the current scope

CXXO_LOCALFN(Neg_,int(int)) (int x) {
    return -x;
    }
CXXO_LOCALFN_NAME(Neg_,negate);
transform( begin(sequence), end(sequence), begin(sequence), negate );

Type Identification

#include <cxxomfort/library/type_name.hpp>
  • template <typename T>
    std::string type_name ()
    (1)
  • std::string typeid_demangle (std::type_info const&) (2)

Given:

(1) a type T passed as a template argument; (2) a function argument resulting from typeid evaluation;

these utilities return information from typeid::name(), except this information is demangled if demangling is available.

Typesafe Enums

#include <cxxomfort/library/typesafe_enum.hpp>

typesafe_enum<T,S> - a typesafe declaration wrapper for enum types, that implements the basic idea behind the enum class... feature of C++11.

Other Features

The following traits are available in cxxomfort/utilities.hpp as assistance when writing generic / quasi-generic code.

is_any_of

Given a type T and a list of types A1, A2, …, A9, the trait is_any_of<T, A1, A2, …, A9> inherits from std::true_type if:

  • T is an exact cv-qualified match for any of the types A1, A2, …, A9.

In both C++03 and C++11, this trait can take up to 10 candidate types.

result_of_function

Given a type T, the trait result_of_function<T> defines the member type ::type, as a type to-be-determined below, if:

  • T is a type signature of a function type eg.: T = R(A1,A2, …). In this case, type: R.
  • T is a pointer type to the above eg.: T = R(*)(A1,A2,…). In this case, type is defined as in the above.
  • (Not implemented yet) T is std::random_device. In this case, type: unsigned int.

In C++03 mode, this trait can recognize function types up to 10 arguments.

integral_of_size

Given a compile-time integer N, with N≥0, this trait defines the member type ::type, as a type to-be-determined-below, if:

  • There exists a native signed integral type I such that sizeof(I)==N. In this case, type: I.

If there is no integral type for the given value of N, instantiation of this template results in an error.

Utility Functions

null_device

This trait defines the static member variable const char* value as the literal string corresponding to the "null device" recognized by the OS in the current compiler.

  • Under POSIX, this is "/dev/null"
  • Under Microsoft Windows, this is "NUL".
  • Under other systems this is left a null-string.

----

C++ Features -- Back to the beginning