Mountain  1.0.0
Simple C++ 2D Game Framework
utils.hpp File Reference

Defines general utility functions. More...

#include <filesystem>
#include <functional>
#include <thread>
#include <magic_enum/magic_enum.hpp>
#include <Maths/quaternion.hpp>
#include <Maths/vector2.hpp>
#include <Maths/vector3.hpp>
#include "Mountain/core.hpp"
#include "Mountain/utils/concepts.hpp"
#include "Mountain/utils/meta_programming.hpp"
#include "Mountain/utils/pointer.hpp"
#include "Mountain/utils/utils.inl"
+ Include dependency graph for utils.hpp:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

 Utils
 Namespace containing utility functions that don't belong anywhere else.
 

Macros

#define TO_STRING(x)   #x
 
#define STRINGIFY(x)   TO_STRING(x)
 

Typedefs

template<typename T >
using Mountain::Utils::ProjectionFunc = T(*)(T)
 

Enumerations

enum  TrimOptions : uint8_t { None = 0, Start = 1 << 0, End = 1 << 1, Both = Start | End }
 
enum  Type : uint8_t {
  Linear, SineIn, SineOut, SineInOut,
  QuadIn, QuadOut, QuadInOut, CubicIn,
  CubicOut, CubicInOut, QuartIn, QuartOut,
  QuartInOut, QuintIn, QuintOut, QuintInOut,
  ExpoIn, ExpoOut, ExpoInOut, CircIn,
  CircOut, CircInOut, BackIn, BackOut,
  BackInOut, ElasticIn, ElasticOut, ElasticInOut,
  BounceIn, BounceOut, BounceInOut
}
 

Functions

template<typename PtrT , Concepts::IntegralT IntT>
constexpr PtrT * Mountain::Utils::IntToPointer (IntT number)
 Converts a integral number to a valid pointer without illegal size operations. More...
 
template<typename T >
size_t Mountain::Utils::GetTypeHash ()
 Gets the hash code of a specified type. More...
 
template<typename T >
size_t Mountain::Utils::GetTypeHash (const T *ptr)
 Gets the hash code of a specified polymorphic pointer type. More...
 
MOUNTAIN_API void Mountain::Utils::AlignImGuiCursor (float_t objectWidth, float_t alignment=0.5f)
 Horizontally aligns the cursor of ImGui to be centered around a specific portion of the available space. More...
 
MOUNTAIN_API std::string Mountain::Utils::HumanizeString (const std::string &str)
 Humanizes the provided string. More...
 
MOUNTAIN_API std::string Mountain::Utils::HumanizeVariableName (const std::string &str)
 Humanizes the provided variable name. More...
 
MOUNTAIN_API constexpr std::string Mountain::Utils::RemoveNamespaces (const std::string &str)
 Removes the namespaces indicators from the provided string. More...
 
MOUNTAIN_API constexpr const char_t * Mountain::Utils::RemoveNamespaces (const char_t *str)
 Removes the namespaces indicators from the provided string. More...
 
MOUNTAIN_API constexpr float_t Mountain::Utils::RemapValue (float_t oldValue, Vector2 oldRange, Vector2 newRange)
 Remaps a value from one range to another. More...
 
MOUNTAIN_API constexpr size_t Mountain::Utils::RemapValue (size_t oldValue, Vector2i oldRange, Vector2i newRange)
 Remaps a value from one range to another. More...
 
MOUNTAIN_API float_t Mountain::Utils::NormalizeAngle (float_t angle)
 Normalizes an angle (clamps its value between 0 and 2 * PI) More...
 
MOUNTAIN_API Vector3 Mountain::Utils::NormalizeAngles (Vector3 angles)
 Normalizes a set of 3 angles in a Vector3 (clamps their value between 0 and 2 * PI) More...
 
MOUNTAIN_API Vector3 Mountain::Utils::GetQuaternionEulerAngles (const Quaternion &rot)
 Converts a quaternion to its euler angle representation. More...
 
template<typename T , typename U >
Pointer< T > Mountain::Utils::DynamicPointerCast (const Pointer< U > &value)
 Equivalent of a dynamic_cast for Pointer "Pointers". More...
 
MOUNTAIN_API void Mountain::Utils::OpenInExplorer (const std::filesystem::path &path)
 Opens the specified path in the file explorer. More...
 
MOUNTAIN_API void Mountain::Utils::OpenInExplorer (const std::filesystem::path &path, bool_t isFile)
 Opens the specified path in the file explorer. More...
 
MOUNTAIN_API void Mountain::Utils::OpenFile (const std::filesystem::path &filepath)
 Opens the specified file on the user's computer. More...
 
template<std::ranges::input_range Container, typename T >
bool_t Mountain::Utils::ArrayContains (const Container &container, T element)
 Returns whether an array contains an element.
 
template<std::ranges::input_range Container>
bool_t Mountain::Utils::StringArrayContains (const Container &container, const std::string &element)
 Returns whether a string array contains an element using Utils::StringEqualsIgnoreCase.
 
MOUNTAIN_API bool_t Mountain::Utils::StringEqualsIgnoreCase (std::string_view a, std::string_view b)
 Checks if two strings are equal, case-insensitive.
 
MOUNTAIN_API bool_t Mountain::Utils::StringContainsIgnoreCase (std::string_view a, std::string_view b)
 Checks if a string contains another one, case-insensitive.
 
template<typename Ret , typename... Args>
constexpr size_t Mountain::Utils::FunctionAddress (std::function< Ret(Args...)> f)
 Gets the address of a function. More...
 
MOUNTAIN_API int32_t Mountain::Utils::TerminalCommand (const std::string &command, bool_t asynchronous=true)
 
MOUNTAIN_API void Mountain::Utils::CreateEmptyFile (const std::filesystem::path &path)
 
MOUNTAIN_API void Mountain::Utils::SetThreadName (std::thread &thread, const std::wstring &name)
 
template<typename R , typename... Args>
Mountain::Utils::CallSafe (const std::function< R(Args...)> &function, Args &&... args)
 
MOUNTAIN_API std::wstring Mountain::Utils::NarrowToWide (std::string_view str)
 
MOUNTAIN_API std::string Mountain::Utils::WideToNarrow (std::wstring_view str)
 
MOUNTAIN_API std::string Mountain::Utils::ToLower (std::string_view str)
 
MOUNTAIN_API std::string Mountain::Utils::ToUpper (std::string_view str)
 
MOUNTAIN_API std::pair< float_t, std::string_view > Mountain::Utils::ByteSizeUnit (int64_t size)
 
MOUNTAIN_API std::string Mountain::Utils::GetBuiltinShadersPath ()
 
MOUNTAIN_API std::string Mountain::Utils::GetBuiltinAssetsPath ()
 
MOUNTAIN_API std::string Mountain::Utils::Trim (std::string_view str, TrimOptions options=TrimOptions::Both)
 
MOUNTAIN_API std::string Mountain::Utils::GetLine (const std::string &str, size_t lineIndex)
 
MOUNTAIN_API uint16_t Mountain::Utils::Concat16 (uint8_t right, uint8_t left)
 
MOUNTAIN_API uint32_t Mountain::Utils::Concat32 (uint8_t right0, uint8_t right1, uint8_t left0, uint8_t left1)
 
template<uint64_t Offset, uint64_t Count>
constexpr uint64_t Mountain::Utils::GetBits (const uint64_t value)
 
template<Concepts::EnumT T>
constexpr Meta::Flags< T > Mountain::Utils::ToFlags (T enumValue)
 

Variables

template<typename T >
constexpr ProjectionFunc< T > Mountain::Utils::Identity = [](T t) { return t; }
 

Detailed Description

Defines general utility functions.

Definition in file utils.hpp.

Function Documentation

◆ AlignImGuiCursor()

MOUNTAIN_API void Mountain::Utils::AlignImGuiCursor ( float_t  objectWidth,
float_t  alignment = 0.5f 
)

Horizontally aligns the cursor of ImGui to be centered around a specific portion of the available space.

Parameters
objectWidthWidth of the element to align
alignmentIn window alignment, 0.5f by default to center the object

◆ DynamicPointerCast()

template<typename T , typename U >
Pointer<T> Mountain::Utils::DynamicPointerCast ( const Pointer< U > &  value)

Equivalent of a dynamic_cast for Pointer "Pointers".

This function first checks if the given Pointer is nullptr, and returns nullptr if so. It then checks if one type is derived from the other, and returns the cast result if so. This actually performs a reinterpret_cast under the hood. If all other conditions failed, the function returns nullptr.

Template Parameters
TThe type to dynamically cast to.
UThe type to dynamically cast from.
Parameters
valueThe Pointer to cast from.
Returns
A null Pointer if the cast failed. Otherwise, the cast result.

◆ FunctionAddress()

template<typename Ret , typename... Args>
constexpr size_t Mountain::Utils::FunctionAddress ( std::function< Ret(Args...)>  f)

Gets the address of a function.

Template Parameters
RetFunction return type
ArgsFunction arguments types
Parameters
fFunction
Returns
Address

◆ GetQuaternionEulerAngles()

MOUNTAIN_API Vector3 Mountain::Utils::GetQuaternionEulerAngles ( const Quaternion rot)

Converts a quaternion to its euler angle representation.

Parameters
rotQuaternion to convert
Returns
Euler representation

◆ GetTypeHash() [1/2]

template<typename T >
size_t Mountain::Utils::GetTypeHash ( )

Gets the hash code of a specified type.

Template Parameters
TType
Returns
Hash

◆ GetTypeHash() [2/2]

template<typename T >
size_t Mountain::Utils::GetTypeHash ( const T ptr)

Gets the hash code of a specified polymorphic pointer type.

Template Parameters
TType
Parameters
ptrPolymorphic pointer
Returns
Hash

◆ HumanizeString()

MOUNTAIN_API std::string Mountain::Utils::HumanizeString ( const std::string &  str)

Humanizes the provided string.

The process converts a PascalCase styled word to a humanized version that puts spaces between each words and adds an uppercase at the very beginning

e.g. RequiresUIReloadAttribute will become Requires UI Reload Attribute

Parameters
strString to humanize
Returns
Result

◆ HumanizeVariableName()

MOUNTAIN_API std::string Mountain::Utils::HumanizeVariableName ( const std::string &  str)

Humanizes the provided variable name.

The process converts a m_PascalCase styled word to a humanized version that puts spaces between each word and adds an uppercase at the very beginning, it also removes the m_ prefix

e.g. m_ShouldChange will become Should Change

Parameters
strString to humanize
Returns
Result

◆ IntToPointer()

template<typename PtrT , Concepts::IntegralT IntT>
constexpr PtrT* Mountain::Utils::IntToPointer ( IntT  number)

Converts a integral number to a valid pointer without illegal size operations.

Template Parameters
PtrTType of the pointer
IntTType of the number, must be integral
Parameters
numberNumber to convert
Returns
Pointer representation of the number

◆ NormalizeAngle()

MOUNTAIN_API float_t Mountain::Utils::NormalizeAngle ( float_t  angle)

Normalizes an angle (clamps its value between 0 and 2 * PI)

Parameters
angleAngle to normalize
Returns
Normalized representation

◆ NormalizeAngles()

MOUNTAIN_API Vector3 Mountain::Utils::NormalizeAngles ( Vector3  angles)

Normalizes a set of 3 angles in a Vector3 (clamps their value between 0 and 2 * PI)

Parameters
anglesVector3 of angles to normalize
Returns
Normalized representation

◆ OpenFile()

MOUNTAIN_API void Mountain::Utils::OpenFile ( const std::filesystem::path &  filepath)

Opens the specified file on the user's computer.

Parameters
filepathFile system path

◆ OpenInExplorer() [1/2]

MOUNTAIN_API void Mountain::Utils::OpenInExplorer ( const std::filesystem::path &  path)

Opens the specified path in the file explorer.

Parameters
pathFile system path

◆ OpenInExplorer() [2/2]

MOUNTAIN_API void Mountain::Utils::OpenInExplorer ( const std::filesystem::path &  path,
bool_t  isFile 
)

Opens the specified path in the file explorer.

Parameters
pathFile system path
isFileWhether path is a file or a directory

◆ RemapValue() [1/2]

MOUNTAIN_API constexpr float_t Mountain::Utils::RemapValue ( float_t  oldValue,
Vector2  oldRange,
Vector2  newRange 
)

Remaps a value from one range to another.

e.g., the number 5 in the range [0;10] will become .5 if remapped to the range [0;1]

Parameters
oldValueValue
oldRangeOld range
newRangeNew range
Returns
New value

◆ RemapValue() [2/2]

MOUNTAIN_API constexpr size_t Mountain::Utils::RemapValue ( size_t  oldValue,
Vector2i  oldRange,
Vector2i  newRange 
)

Remaps a value from one range to another.

e.g., the number 5 in the range [0;10] will become 1 if remapped to the range [0;2]

Parameters
oldValueValue
oldRangeOld range
newRangeNew range
Returns
New value

◆ RemoveNamespaces() [1/2]

MOUNTAIN_API constexpr std::string Mountain::Utils::RemoveNamespaces ( const std::string &  str)

Removes the namespaces indicators from the provided string.

e.g. Mountain::MyClass will become MyClass

Parameters
strString to modify
Returns
Result

◆ RemoveNamespaces() [2/2]

MOUNTAIN_API constexpr const char_t* Mountain::Utils::RemoveNamespaces ( const char_t *  str)

Removes the namespaces indicators from the provided string.

e.g. Mountain::MyClass will become MyClass

Parameters
strString to modify
Returns
Result