Skip to main content

Overview

The types namespace provides convenient type aliases for commonly used types in the Drac++ project. These aliases offer shorthand notations for standard library types and follow Rust-like naming conventions. Namespace: draconis::utils::types Header: <Drac++/Utils/Types.hpp>

Integer types

Unsigned integers

AliasUnderlying TypeDescription
u8std::uint8_t8-bit unsigned integer
u16std::uint16_t16-bit unsigned integer
u32std::uint32_t32-bit unsigned integer
u64std::uint64_t64-bit unsigned integer
usizestd::size_tUnsigned size type (result of sizeof)

Signed integers

AliasUnderlying TypeDescription
i8std::int8_t8-bit signed integer
i16std::int16_t16-bit signed integer
i32std::int32_t32-bit signed integer
i64std::int64_t64-bit signed integer
isizestd::ptrdiff_tSigned size type (result of pointer subtraction)

Floating-point types

AliasUnderlying TypeDescription
f32float32-bit floating-point number
f64double64-bit floating-point number

String types

AliasUnderlying TypeDescription
Stringstd::stringOwning, mutable string
WStringstd::wstringOwning, mutable wide string
StringViewstd::string_viewNon-owning view of a string
WStringViewstd::wstring_viewNon-owning view of a wide string
CStrcharSingle character type
PCStrconst char*Pointer to a null-terminated C-style string
WCStrwchar_tSingle wide character type
PWCStrconst wchar_t*Pointer to a null-terminated C-style wide string

Container types

Array

template <typename Tp, usize sz>
using Array = std::array<Tp, sz>
Represents a fixed-size array. Template parameters:
  • Tp - The element type
  • sz - The size of the array

Vec

template <typename Tp>
using Vec = std::vector<Tp>
Represents a dynamic-size array (vector). Template parameters:
  • Tp - The element type

Span

template <typename Tp, usize sz = std::dynamic_extent>
using Span = std::span<Tp, sz>
Represents a non-owning view of a contiguous sequence of elements. Template parameters:
  • Tp - The element type
  • sz - (Optional) The size of the span

Map

template <typename Key, typename Val>
using Map = std::map<Key, Val, std::less<>>
Represents an ordered map (dictionary). Template parameters:
  • Key - The key type
  • Val - The value type

UnorderedMap

template <typename Key, typename Val>
using UnorderedMap = ankerl::unordered_dense::map<Key, Val>
High-performance unordered map using Robin Hood hashing. Template parameters:
  • Key - The key type
  • Val - The value type

Tuple types

Pair

template <typename T1, typename T2>
using Pair = std::pair<T1, T2>
Represents a pair of values. Template parameters:
  • T1 - The type of the first element
  • T2 - The type of the second element

Tuple

template <typename... Ts>
using Tuple = std::tuple<Ts...>
Represents a tuple of values. Template parameters:
  • Ts... - The types of the elements

Smart pointers

SharedPointer

template <typename Tp>
using SharedPointer = std::shared_ptr<Tp>
Manages shared ownership of a dynamically allocated object. Template parameters:
  • Tp - The type of the managed object

UniquePointer

template <typename Tp, typename Dp = std::default_delete<Tp>>
using UniquePointer = std::unique_ptr<Tp, Dp>
Manages unique ownership of a dynamically allocated object. Template parameters:
  • Tp - The type of the managed object
  • Dp - The deleter type (defaults to std::default_delete<Tp>)

Option type

template <typename Tp>
using Option = std::optional<Tp>
Represents a value that may or may not be present. Template parameters:
  • Tp - The type of the potential value

Helper constants and functions

None

inline constexpr std::nullopt_t None = std::nullopt
Represents an empty optional value.

Some

template <typename Tp>
constexpr auto Some(Tp&& value) -> Option<std::remove_reference_t<Tp>>
Helper function to create an Option with a value. Parameters:
  • value - The value to wrap in an Option
Returns: An Option containing the value

Example usage

using namespace draconis::utils::types;

auto findUser(const String& name) -> Option<User> {
  if (name == "admin") {
    return Some(User{"admin", 0});
  }
  return None;
}

auto user = findUser("admin");
if (user) {
  std::cout << "Found: " << user->name << std::endl;
}

Result type

template <typename Tp = Unit, typename Er = error::DracError>
using Result = std::expected<Tp, Er>
Represents a value that can either be a success value of type Tp or an error value of type Er. Template parameters:
  • Tp - The type of the success value (defaults to Unit)
  • Er - The type of the error value (defaults to error::DracError)

Err

template <typename Er = error::DracError>
using Err = std::unexpected<Er>
Used to construct a Result in an error state. Template parameters:
  • Er - The type of the error value (defaults to error::DracError)

Example usage

using namespace draconis::utils::types;

auto divide(f64 a, f64 b) -> Result<f64> {
  if (b == 0.0) {
    return Err(DracError(
      DracErrorCode::InvalidArgument,
      "Division by zero"
    ));
  }
  return a / b;
}

auto result = divide(10.0, 2.0);
if (result) {
  std::cout << "Result: " << *result << std::endl;
} else {
  std::cerr << "Error: " << result.error().message << std::endl;
}

Synchronization types

AliasUnderlying TypeDescription
Mutexstd::mutexMutex type for synchronization
LockGuardstd::lock_guard<Mutex>RAII-style lock guard for mutexes

Function types

Fn

template <typename Tp>
using Fn = std::function<Tp>
Represents a callable object. Template parameters:
  • Tp - The function signature (e.g., void(), int(String))

Future

template <typename Tp>
using Future = std::future<Tp>
Represents a future value from an asynchronous operation. Template parameters:
  • Tp - The type of the value

Special types

AliasUnderlying TypeDescription
UnitvoidRepresents a unit type
RawPointervoid*A type-erased pointer
Exceptionstd::exceptionStandard exception type

Example: combining types

using namespace draconis::utils::types;

// Map of strings to vectors of integers
Map<String, Vec<u32>> myMap;

// Optional result containing a string
Result<Option<String>> getData() {
  auto value = fetchFromDatabase();
  if (!value) {
    return Err(DracError(
      DracErrorCode::NotFound,
      "Data not found"
    ));
  }
  return Some(*value);
}

// Function type for callbacks
Fn<void(const String&)> callback = [](const String& msg) {
  std::cout << msg << std::endl;
};

Build docs developers (and LLMs) love