lintel 0.2.0
Loading...
Searching...
No Matches
Platform.h
Go to the documentation of this file.
1#ifndef LINTEL_CORE_PLATFORM_PLATFORM_H
2#define LINTEL_CORE_PLATFORM_PLATFORM_H
3
4#include <cstdint>
5#include <cstdlib>
6#include <ctime>
7#include <string>
8
9#if defined(_WIN32)
10// Keep the transitive Windows headers from defining the min/max macros: the
11// library uses std::min/std::max throughout, and a consumer that includes
12// <Windows.h> before a lintel header would otherwise see them expand.
13#ifndef NOMINMAX
14#define NOMINMAX
15#endif
16#ifndef WIN32_LEAN_AND_MEAN
17#define WIN32_LEAN_AND_MEAN
18#endif
19#include <process.h>// _getpid
20#else
21#include <sys/types.h>// pid_t
22#include <unistd.h>// getpid
23#endif
24
25namespace lintel {
33using ProcessId = std::int64_t;
34
37{
38#if defined(_WIN32)
39 return static_cast<ProcessId>(::_getpid());
40#else
41 return static_cast<ProcessId>(::getpid());
42#endif
43}
44
54inline bool localTime(std::time_t time, std::tm &out) noexcept
55{
56#if defined(_WIN32)
57 return ::localtime_s(&out, &time) == 0;
58#else
59 return ::localtime_r(&time, &out) != nullptr;
60#endif
61}
62
64inline std::string homeDirectory()
65{
66#if defined(_WIN32)
67 // Windows has no HOME; USERPROFILE is the canonical variable, HOMEDRIVE +
68 // HOMEPATH is the legacy fallback for stripped-down environments.
69 if (const char *profile = std::getenv("USERPROFILE"); profile != nullptr && *profile != '\0') { return profile; }
70 const char *drive = std::getenv("HOMEDRIVE");
71 const char *path = std::getenv("HOMEPATH");
72 if (drive != nullptr && path != nullptr) { return std::string(drive) + path; }
73 return {};
74#else
75 const char *home = std::getenv("HOME");
76 return home != nullptr ? home : std::string{};
77#endif
78}
79
81inline constexpr char pathListSeparator() noexcept
82{
83#if defined(_WIN32)
84 return ';';
85#else
86 return ':';
87#endif
88}
89
90}// namespace lintel
91
92#endif// LINTEL_CORE_PLATFORM_PLATFORM_H
Definition BaseFeature.h:6
bool localTime(std::time_t time, std::tm &out) noexcept
Definition Platform.h:54
std::string homeDirectory()
Definition Platform.h:64
constexpr char pathListSeparator() noexcept
Definition Platform.h:81
std::int64_t ProcessId
Definition Platform.h:33
constexpr std::string_view type_name()
Definition TypeName.h:9
ProcessId currentProcessId() noexcept
Definition Platform.h:36