ugmisc 0.2-76
Miscellaneous C++ header library
Loading...
Searching...
No Matches
ugmisc
Author
Larry Chips
Note
This documentation was generated from the source, and may contain much of that source, which is MIT licensed.

Miscellaneous C++ utilities.

A small C++ header library providing miscellaneous utilities, compatible with C++17 and all later versions. The repository is hosted at Codeberg.

Sample programs will soon be available, to demonstrate library usage.

Luxuries provided by ugmisc

Named member detection

New reflection features coming to C++26 will make most likely use cases of member.hpp obsolete.

The macro defined in member.hpp allows types to be declared which can find a named member of a type or object, or in a sequence of types or objects.

Useful for implementing traits class templates, like the standard library's allocator_traits, which reflect their type parameters' member types or methods if they happen to have them.

Here is a little example:

UGMISC_DECL_MEMBER_ACCESS(member_foo, foo); // Declares type member_foo.
auto foo_val = ugmisc::member_value<member_foo>{}(x, y).get(sum, 1, 2);
#define UGMISC_DECL_MEMBER_ACCESS(TNAME, NAME)
Definition member.hpp:165

The value of foo_val will be the first of the following which is valid:

  1. x.foo
  2. y.foo
  3. sum(1, 2)

There are also ways to use your new member_foo accessor type to find and call a static or non-static method which accepts certain arguments, or to find a member type from a sequence of types.

In all cases a fallback default can be used. See member.hpp for a full explanation.

Type lists

The typelist.hpp header defines a type_list template, which defines lists of types, and a number of templates for manipulating them.

List of types can be concatenated, and sliced. They can have some of their types filtered out, or converted into other types.

Integer type finder

The int_finder.hpp header allows a suitable integer type to be found based on the minimum number of bits required, or an exact number, or a value that needs encoding.

Support for the standard types such as uint16_t is built in, but the user can easily add support for extra builtin types, or their own classes which act like ints. The support works if the integer radix is 2, but the number of digits doesn't have to be a power of 2 (or power of 2 minus 1 for signed ints), as long as the type supports ordinary bitwise operations and has a specialisation of std::numeric_limits.

Bit ops

The bitops.hpp header provides C++17 equivalents of the C++20 bit counting functions.

If those functions are available from the standard library, the functions here just forward their arguments to them. Otherwise they use masking operations to home in on the result.

Todo
Add support for bit ops of custom integer types if those are not supported by the standard library, even if the standard library bit ops are available for built in types.

Feature tests

The features.hpp header is mainly there to support the other headers. In addition to a macro for each feature that may be needed by those headers, a static constexpr bool member of the Features class is defined.

Most of the features are simply the features that are indicated by __cpp_* macros, but some have to be guessed. The header should just work, but to make sure, you can do some configure-time tests using a build system, an example of which is described here.