nx_metadata_sdk 1.0 Metadata SDK

Macros | Functions
utils.h File Reference
`#include <cstddef>`
`#include <cstring>`
`#include <cstdlib>`
`#include <memory>`
`#include <string>`
`#include <sstream>`
`#include <vector>`

Go to the source code of this file.

## Macros
#define | NX_KIT_API /*empty*/

## Functions
bool | nx::kit::utils::isAsciiPrintable (int c)
std::string | nx::kit::utils::baseName (std::string path)
std::string | nx::kit::utils::getProcessName ()
template<typename T >
std::string | nx::kit::utils::toString (T value)
template<typename... Args>
std::string | nx::kit::utils::format (const std::string &formatStr, Args... args)
bool | nx::kit::utils::fromString (const std::string &s, int *value)
bool | nx::kit::utils::fromString (const std::string &s, double *value)
bool | nx::kit::utils::fromString (const std::string &s, float *value)
const std::vector< std::string > & | nx::kit::utils::getProcessCmdLineArgs ()
size_t | nx::kit::utils::alignUp (size_t value, size_t alignment)
uint8_t * | nx::kit::utils::misalignedPtr (void *data)
template<class MallocFunc >
void * | nx::kit::utils::mallocAligned (size_t size, size_t alignment, MallocFunc mallocFunc)
void * | nx::kit::utils::mallocAligned (size_t size, size_t alignment)
template<class FreeFunc >
void | nx::kit::utils::freeAligned (void *ptr, FreeFunc freeFunc)
void | nx::kit::utils::freeAligned (void *ptr)
std::string | nx::kit::utils::toString (bool b)
std::string | nx::kit::utils::toString (const void *ptr)
std::string | nx::kit::utils::toString (void *ptr)
std::string | nx::kit::utils::toString (std::nullptr_t ptr)
std::string | nx::kit::utils::toString (uint8_t i)
std::string | nx::kit::utils::toString (int8_t i)
std::string | nx::kit::utils::toString (char c)
std::string | nx::kit::utils::toString (const char *s)
std::string | nx::kit::utils::toString (char *s)
std::string | nx::kit::utils::toString (const std::string &s)
std::string | nx::kit::utils::toString (wchar_t w)
std::string | nx::kit::utils::toString (const wchar_t *w)
std::string | nx::kit::utils::toString (wchar_t *w)
std::string | nx::kit::utils::toString (const std::wstring &w)
template<typename P >
std::string | nx::kit::utils::toString (P *ptr)

## Detailed Description

Various utilities. Used by other nx_kit components.

This unit can be compiled in the context of any C++ project. If Qt headers are included before this one, some Qt support is enabled via "#if defined(QT_CORE_LIB)".

## Function Documentation

## ◆ alignUp()

size_t nx::kit::utils::alignUp | ( | size_t | value,
|  | size_t | alignment
| ) |  |
| inline

Aligns value up to alignment boundary.

Parameters

  alignment | If zero, value is returned unchanged.

## ◆ baseName()

NX_KIT_API std::string nx::kit::utils::baseName | ( | std::string | path | ) |

Returns
  Last path component: text after the last path separator. On Windows, possible `<drive>:` prefix is excluded and both `/` and `\` are supported. If path is empty, the result is empty.

## ◆ freeAligned() [1/2]

template<class FreeFunc >

void nx::kit::utils::freeAligned | ( | void * | ptr,
|  | FreeFunc | freeFunc
| ) |  |

Free ptr allocated with a call to mallocAligned().

NOTE: This function is as safe as ::free().

Parameters

  freeFunc | Function with the signature void(void*), which is called to free the memory.

## ◆ freeAligned() [2/2]

void nx::kit::utils::freeAligned | ( | void * | ptr | ) |
| inline

Calls freeAligned() passing standard free() as freeFunc.

## ◆ getProcessCmdLineArgs()

NX_KIT_API const std::vector< std::string > & nx::kit::utils::getProcessCmdLineArgs | ( |  | ) |

Returns
  Command line arguments of the process, cached after the first call. If arguments are not available, then returns a single empty string.

## ◆ getProcessName()

NX_KIT_API std::string nx::kit::utils::getProcessName | ( |  | ) |

Returns
  Process name, without .exe in Windows.

## ◆ mallocAligned() [1/2]

template<class MallocFunc >

void* nx::kit::utils::mallocAligned | ( | size_t | size,
|  | size_t | alignment,
|  | MallocFunc | mallocFunc
| ) |  |

Allocates size bytes of data, aligned to alignment boundary.

NOTE: Allocated memory must be freed with a call to freeAligned(). NOTE: This function is as safe as malloc().

Parameters

  mallocFunc | Function with the signature void*(size_t), which is called to allocate memory.

## ◆ mallocAligned() [2/2]

void* nx::kit::utils::mallocAligned | ( | size_t | size,
|  | size_t | alignment
| ) |  |
| inline

Calls mallocAligned() passing standard malloc() as mallocFunc.

## ◆ misalignedPtr()

uint8_t* nx::kit::utils::misalignedPtr | ( | void * | data | ) |
| inline

Shifts the pointer up to deliberately misalign it to an odd address - intended for tests.

## ◆ toString()

template<typename T >

std::string nx::kit::utils::toString | ( | T | value | ) |

Convert a value to its report-friendly text representation, e.g. a quoted and escaped string. Non-printable chars in a string are represented as hex escape sequences like `\xFF""` - note that the two quotes after it are inserted to indicate the end of the hex number, as in C/C++.