Elementa v8.0.0
Minimalistic library for any C++ application (C++11 and up)
Loading...
Searching...
No Matches
Fundamental types extensions

Description


Diverse extensions of fundamental types.

Author
Juan-Antonio Fernandez-Madrigal. http://jafma.net
Date
2018-2019
Collaboration diagram for Fundamental types extensions:

INTEGRAL TYPES EXTENSIONS

enum class  elementa::base::endian { little = 0 , big = 1 , native = little }
 Endianness of the target machine, as defined in the C++20 standard. More...
 
enum class  elementa::base::SignSpec : uint8_t { kSigned = 0 , kUnsigned }
 Defines the possible sign of a fundamental (integral) type. More...
 
using elementa::base::sizebits_t = unsigned
 Type able to index all bits in the longest int. type in the target machine. More...
 
using elementa::base::sizebytes_t = unsigned
 Type able to index all bytes in the longest int. type in the target machine. More...
 
using elementa::base::LongestUnsigned = unsigned long long
 Longest unsigned integral type in the target machine. More...
 
using elementa::base::LongestInt = long long
 Longest signed integral type in the target machine. More...
 
using elementa::base::LongestReal = long double
 Longest real type. More...
 
template<sizebits_t NBITS, SignSpec SIGN>
using elementa::base::ExactInteger = typename std::conditional<((NBITS==8) &&(SIGN==SignSpec::kSigned)), int8_t, typename std::conditional<((NBITS==8) &&(SIGN==SignSpec::kUnsigned)), uint8_t, typename std::conditional<((NBITS==16) &&(SIGN==SignSpec::kSigned)), int16_t, typename std::conditional<((NBITS==16) &&(SIGN==SignSpec::kUnsigned)), uint16_t, typename std::conditional<((NBITS==32) &&(SIGN==SignSpec::kSigned) &&(kMaxBitsInt > 16)), int32_t, typename std::conditional<((NBITS==32) &&(SIGN==SignSpec::kUnsigned) &&(kMaxBitsInt > 16)), uint32_t, typename std::conditional<((NBITS==64) &&(SIGN==SignSpec::kSigned) &&(kMaxBitsInt > 32)), int64_t, typename std::conditional<((NBITS==64) &&(SIGN==SignSpec::kUnsigned) &&(kMaxBitsInt > 32)), uint64_t, void >::type >::type >::type >::type >::type >::type >::type >::type
 To get an int. type with given characteristics of number of bits and sign. More...
 
constexpr sizebits_t elementa::base::kMaxBitsInt = 16
 Maximum number of bits in an integer type in the target machine. More...
 
constexpr sizebits_t elementa::base::kLeastSizeForBits (sizebits_t nbs)
 Shortest integral type for holding a number of bits in the target machine. More...
 
template<typename I >
constexpr I elementa::base::kNBitsSet (sizebits_t n)
 Compute an integral constant with its N least-significant bits set. More...
 
uint8_t elementa::base::len_for_bits (uint8_t nbits)
 Maximum number of digits needed for the decimal form of NBITS binary numbers.
 
template<typename T >
sizebits_t elementa::base::count_bits (T v)
 Count the number of bits that are set in an integral value. More...
 
template<typename T , sizebits_t N = sizeof(T)*CHAR_BIT>
sizebits_t elementa::base::most_significant_bit (T b)
 Return the index of the MSb set in B, or the number of bits in T if none. More...
 
template<typename PT >
const PT * elementa::base::kPtrPlusBytes (const PT *p, size_t boff)
 Add a number of bytes to the address of a pointer without changing its type. More...
 
LongestUnsigned elementa::base::to_number (const void *p)
 Convert a pointer address to a number.
 
int elementa::base::to_number (char n)
 Convert a char into a number (useful for printing, for ex.).
 
unsigned elementa::base::to_number (unsigned char n)
 Convert an unsigned char into a number (useful for printing, for ex.).
 

Typedef Documentation

◆ sizebits_t

using elementa::base::sizebits_t = typedef unsigned

#include <elementa/base/fundamentaltypes.h>

Type able to index all bits in the longest int. type in the target machine.

This type is not signed, just like size_t is defined as not signed in the C++ standard.

Note
- We use unsigned instead of a shorter type (e.g., uint8_t) for easier output to STL streams.

Definition at line 102 of file fundamentaltypes.h.

◆ sizebytes_t

using elementa::base::sizebytes_t = typedef unsigned

#include <elementa/base/fundamentaltypes.h>

Type able to index all bytes in the longest int. type in the target machine.

This type is not signed, just like size_t is defined as not signed in the C++ standard.

Note
- We use unsigned instead of a shorter type (e.g., uint8_t) for easier output to STL streams.

Definition at line 111 of file fundamentaltypes.h.

◆ LongestUnsigned

using elementa::base::LongestUnsigned = typedef unsigned long long

#include <elementa/base/fundamentaltypes.h>

Longest unsigned integral type in the target machine.

This type is useful, for example, to receive an integral argument that can have any length. For indicating a size in memory, use size_t instead: the standard states, in fact, that any object in memory that occupies more than size_t is ill formed.

Definition at line 118 of file fundamentaltypes.h.

◆ LongestInt

using elementa::base::LongestInt = typedef long long

#include <elementa/base/fundamentaltypes.h>

Longest signed integral type in the target machine.

Definition at line 121 of file fundamentaltypes.h.

◆ LongestReal

using elementa::base::LongestReal = typedef long double

#include <elementa/base/fundamentaltypes.h>

Longest real type.

Definition at line 124 of file fundamentaltypes.h.

◆ ExactInteger

template<sizebits_t NBITS, SignSpec SIGN>
using elementa::base::ExactInteger = typedef typename std::conditional< ((NBITS == 8) && (SIGN==SignSpec::kSigned)), int8_t, typename std::conditional< ((NBITS == 8) && (SIGN==SignSpec::kUnsigned)), uint8_t, typename std::conditional< ((NBITS == 16) && (SIGN==SignSpec::kSigned)), int16_t, typename std::conditional< ((NBITS == 16) && (SIGN==SignSpec::kUnsigned)), uint16_t, typename std::conditional< ((NBITS == 32) && (SIGN==SignSpec::kSigned) && (kMaxBitsInt > 16)), int32_t, typename std::conditional< ((NBITS == 32) && (SIGN==SignSpec::kUnsigned) && (kMaxBitsInt > 16)), uint32_t, typename std::conditional< ((NBITS == 64) && (SIGN==SignSpec::kSigned) && (kMaxBitsInt > 32)), int64_t, typename std::conditional< ((NBITS == 64) && (SIGN==SignSpec::kUnsigned) && (kMaxBitsInt > 32)), uint64_t, void >::type >::type >::type >::type >::type >::type >::type >::type

#include <elementa/base/fundamentaltypes.h>

To get an int. type with given characteristics of number of bits and sign.

This is useful in those cases where a program needs those characteristics but they are not fixed yet when the programmer writes the code.

Definition at line 191 of file fundamentaltypes.h.

Enumeration Type Documentation

◆ endian

enum class elementa::base::endian
strong

#include <elementa/base/fundamentaltypes.h>

Endianness of the target machine, as defined in the C++20 standard.

Defined here since this machine has no c++20 support. See https://en.cppreference.com/w/cpp/types/endian This class indicates the endianness of all scalar types:

  • If all scalar types are little-endian, std::endian::native equals std::endian::little
  • If all scalar types are big-endian, std::endian::native equals std::endian::big

Corner case platforms are also supported:

  • If all scalar types have sizeof equal to 1, endianness does not matter and all three values, std::endian::little, std::endian::big, and std::endian::native are the same.
  • If the platform uses mixed endian, std::endian::native does not equal either std::endian::big nor std::endian::little.

Definition at line 74 of file fundamentaltypes.h.

◆ SignSpec

enum class elementa::base::SignSpec : uint8_t
strong

#include <elementa/base/fundamentaltypes.h>

Defines the possible sign of a fundamental (integral) type.

Definition at line 134 of file fundamentaltypes.h.

Function Documentation

◆ kLeastSizeForBits()

constexpr sizebits_t elementa::base::kLeastSizeForBits ( sizebits_t  nbs)
constexpr

#include <elementa/base/fundamentaltypes.h>

Shortest integral type for holding a number of bits in the target machine.

Return the number of bits of the shortest integral type existing in the target machine that at least is capable of holding NBS bits. Return 0 if the requested number of bits cannot be held in any target machine integral type.

Definition at line 171 of file fundamentaltypes.h.

◆ kNBitsSet()

template<typename I >
constexpr I elementa::base::kNBitsSet ( sizebits_t  n)
constexpr

#include <elementa/base/fundamentaltypes.h>

Compute an integral constant with its N least-significant bits set.

Template Parameters
Iis a type that must be long enough to hold n+1 bits.

Definition at line 226 of file fundamentaltypes.h.

◆ count_bits()

template<typename T >
sizebits_t elementa::base::count_bits ( v)

#include <elementa/base/fundamentaltypes.h>

Count the number of bits that are set in an integral value.

Definition at line 248 of file fundamentaltypes.h.

◆ most_significant_bit()

template<typename T , sizebits_t N = sizeof(T)*CHAR_BIT>
sizebits_t elementa::base::most_significant_bit ( b)

#include <elementa/base/fundamentaltypes.h>

Return the index of the MSb set in B, or the number of bits in T if none.

Definition at line 267 of file fundamentaltypes.h.

◆ kPtrPlusBytes()

template<typename PT >
const PT * elementa::base::kPtrPlusBytes ( const PT *  p,
size_t  boff 
)

#include <elementa/base/fundamentaltypes.h>

Add a number of bytes to the address of a pointer without changing its type.

Template Parameters
PTis the pointed type, not the pointer type

Definition at line 290 of file fundamentaltypes.h.

Variable Documentation

◆ kMaxBitsInt

constexpr sizebits_t elementa::base::kMaxBitsInt = 16
constexpr

#include <elementa/base/fundamentaltypes.h>

Maximum number of bits in an integer type in the target machine.

Note
- The C++ standard assumes 16 as the number of bits of the shortest possible int type in any machine (even short int is guaranteed to be at least 16 bits).

Definition at line 156 of file fundamentaltypes.h.