diff options
| author | Stefan Boberg <[email protected]> | 2023-06-30 11:07:10 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2023-06-30 11:07:10 +0200 |
| commit | 0396f400a97f71bf743d584acc71a0cdbc155566 (patch) | |
| tree | 829f7764157aecf9c198e4b076e5fb1adcfeb13f /src/zencore/include | |
| parent | various zenhttp fixes from sb/proto (diff) | |
| download | zen-0396f400a97f71bf743d584acc71a0cdbc155566.tar.xz zen-0396f400a97f71bf743d584acc71a0cdbc155566.zip | |
* Added Guid::FromString
* Added LoadCompactBinaryObject from file to compactbinaryfile.cpp/h
* Added SaveCompactBinary(BinaryWriter& Ar, ...) functions
* Added ZEN_PLATFORM_NAME define
* Added SystemMetrics functionality to query system properties (see zencore/system.h)
Diffstat (limited to 'src/zencore/include')
| -rw-r--r-- | src/zencore/include/zencore/compactbinary.h | 14 | ||||
| -rw-r--r-- | src/zencore/include/zencore/compactbinarybuilder.h | 4 | ||||
| -rw-r--r-- | src/zencore/include/zencore/compactbinaryfile.h | 18 | ||||
| -rw-r--r-- | src/zencore/include/zencore/filesystem.h | 10 | ||||
| -rw-r--r-- | src/zencore/include/zencore/fmtutils.h | 19 | ||||
| -rw-r--r-- | src/zencore/include/zencore/guid.h | 26 | ||||
| -rw-r--r-- | src/zencore/include/zencore/iobuffer.h | 1 | ||||
| -rw-r--r-- | src/zencore/include/zencore/iohash.h | 3 | ||||
| -rw-r--r-- | src/zencore/include/zencore/string.h | 11 | ||||
| -rw-r--r-- | src/zencore/include/zencore/system.h | 37 | ||||
| -rw-r--r-- | src/zencore/include/zencore/thread.h | 5 | ||||
| -rw-r--r-- | src/zencore/include/zencore/uid.h | 2 | ||||
| -rw-r--r-- | src/zencore/include/zencore/zencore.h | 9 |
13 files changed, 138 insertions, 21 deletions
diff --git a/src/zencore/include/zencore/compactbinary.h b/src/zencore/include/zencore/compactbinary.h index b546f97aa..66e621a8a 100644 --- a/src/zencore/include/zencore/compactbinary.h +++ b/src/zencore/include/zencore/compactbinary.h @@ -5,6 +5,7 @@ #include <zencore/zencore.h> #include <zencore/enumflags.h> +#include <zencore/guid.h> #include <zencore/intmath.h> #include <zencore/iobuffer.h> #include <zencore/iohash.h> @@ -141,13 +142,6 @@ private: uint64_t Ticks; }; -struct Guid -{ - uint32_t A, B, C, D; - - StringBuilderBase& ToString(StringBuilderBase& OutString) const; -}; - ////////////////////////////////////////////////////////////////////////// /** @@ -891,6 +885,8 @@ public: /** @see CbField::CbField */ using CbFieldView::CbFieldView; + using CbFieldView::TryGetSerializedView; + /** Construct an array with no fields. */ ZENCORE_API CbArrayView(); @@ -1410,6 +1406,10 @@ ZENCORE_API CbObject LoadCompactBinaryObject(const CompressedBuffer& Payload); ZENCORE_API CbFieldIterator LoadCompactBinaryFromJson(std::string_view Json, std::string& Error); ZENCORE_API CbFieldIterator LoadCompactBinaryFromJson(std::string_view Json); +ZENCORE_API void SaveCompactBinary(BinaryWriter& Ar, const CbFieldView& Field); +ZENCORE_API void SaveCompactBinary(BinaryWriter& Ar, const CbArrayView& Array); +ZENCORE_API void SaveCompactBinary(BinaryWriter& Ar, const CbObjectView& Object); + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** diff --git a/src/zencore/include/zencore/compactbinarybuilder.h b/src/zencore/include/zencore/compactbinarybuilder.h index 4be8c2ba5..53f00ae4e 100644 --- a/src/zencore/include/zencore/compactbinarybuilder.h +++ b/src/zencore/include/zencore/compactbinarybuilder.h @@ -46,7 +46,7 @@ class BinaryWriter; * * Example: * - * CbObjectRef WriteObject() + * CbObject WriteObject() * { * CbWriter<256> Writer; * Writer.BeginObject(); @@ -60,7 +60,7 @@ class BinaryWriter; * Writer.EndArray(); * * Writer.EndObject(); - * return Writer.Save().AsObjectRef(); + * return Writer.Save().AsObject(); * } */ class CbWriter diff --git a/src/zencore/include/zencore/compactbinaryfile.h b/src/zencore/include/zencore/compactbinaryfile.h new file mode 100644 index 000000000..00c37e941 --- /dev/null +++ b/src/zencore/include/zencore/compactbinaryfile.h @@ -0,0 +1,18 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#include <zencore/compactbinary.h> +#include <zencore/iohash.h> + +#include <filesystem> + +namespace zen { + +struct CbObjectFromFile +{ + CbObject Object; + IoHash Hash; +}; + +CbObjectFromFile LoadCompactBinaryObject(const std::filesystem::path& FilePath); + +} // namespace zen diff --git a/src/zencore/include/zencore/filesystem.h b/src/zencore/include/zencore/filesystem.h index 230d8d1c2..1a582672b 100644 --- a/src/zencore/include/zencore/filesystem.h +++ b/src/zencore/include/zencore/filesystem.h @@ -14,6 +14,7 @@ namespace zen { class IoBuffer; +class CompositeBuffer; /** Delete directory (after deleting any contents) */ @@ -52,10 +53,19 @@ struct FileContents }; ZENCORE_API FileContents ReadStdIn(); + +/** Prepare file for reading + + Note that this generally does not actually read the file contents. Instead it creates an + IoBuffer referencing the file contents so that it may be read at a later time. This is + leveraged to allow sending of data straight from disk cache and other optimizations. + */ ZENCORE_API FileContents ReadFile(std::filesystem::path Path); + ZENCORE_API bool ScanFile(std::filesystem::path Path, uint64_t ChunkSize, std::function<void(const void* Data, size_t Size)>&& ProcessFunc); ZENCORE_API void WriteFile(std::filesystem::path Path, const IoBuffer* const* Data, size_t BufferCount); ZENCORE_API void WriteFile(std::filesystem::path Path, IoBuffer Data); +ZENCORE_API void WriteFile(std::filesystem::path Path, CompositeBuffer Data); struct CopyFileOptions { diff --git a/src/zencore/include/zencore/fmtutils.h b/src/zencore/include/zencore/fmtutils.h index 70867fe72..5c5169cd6 100644 --- a/src/zencore/include/zencore/fmtutils.h +++ b/src/zencore/include/zencore/fmtutils.h @@ -2,6 +2,7 @@ #pragma once +#include <zencore/guid.h> #include <zencore/iohash.h> #include <zencore/string.h> #include <zencore/uid.h> @@ -23,7 +24,7 @@ struct fmt::formatter<zen::IoHash> : formatter<string_view> { zen::IoHash::String_t String; Hash.ToHexString(String); - return formatter<string_view>::format({String, zen::IoHash::StringLength}, ctx); + return fmt::formatter<string_view>::format({String, zen::IoHash::StringLength}, ctx); } }; @@ -35,7 +36,19 @@ struct fmt::formatter<zen::Oid> : formatter<string_view> { zen::StringBuilder<32> String; Id.ToString(String); - return formatter<string_view>::format({String.c_str(), zen::Oid::StringLength}, ctx); + return fmt::formatter<string_view>::format({String.c_str(), zen::Oid::StringLength}, ctx); + } +}; + +template<> +struct fmt::formatter<zen::Guid> : formatter<string_view> +{ + template<typename FormatContext> + auto format(const zen::Guid& Id, FormatContext& ctx) + { + zen::StringBuilder<48> String; + Id.ToString(String); + return fmt::formatter<string_view>::format({String.c_str(), zen::Guid::StringLength}, ctx); } }; @@ -47,6 +60,6 @@ struct fmt::formatter<std::filesystem::path> : formatter<string_view> { zen::ExtendableStringBuilder<128> String; String << Path.u8string(); - return formatter<string_view>::format(String.ToView(), ctx); + return fmt::formatter<string_view>::format(String.ToView(), ctx); } }; diff --git a/src/zencore/include/zencore/guid.h b/src/zencore/include/zencore/guid.h new file mode 100644 index 000000000..751aa945f --- /dev/null +++ b/src/zencore/include/zencore/guid.h @@ -0,0 +1,26 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#pragma once + +#include <zencore/zencore.h> +#include <compare> + +namespace zen { + +class StringBuilderBase; + +struct Guid +{ + uint32_t A, B, C, D; + + static const int StringLength = 36; + typedef char String_t[StringLength + 1]; + + StringBuilderBase& ToString(StringBuilderBase& OutString) const; + static Guid FromString(std::string_view InString); + + inline bool operator==(const Guid& Rhs) const = default; + inline auto operator<=>(const Guid& Rhs) const = default; +}; + +} // namespace zen diff --git a/src/zencore/include/zencore/iobuffer.h b/src/zencore/include/zencore/iobuffer.h index 2f248eb8b..bbc346f9b 100644 --- a/src/zencore/include/zencore/iobuffer.h +++ b/src/zencore/include/zencore/iobuffer.h @@ -414,6 +414,7 @@ public: /** Make sure buffer data is memory resident, but avoid memory mapping data from files */ ZENCORE_API static IoBuffer ReadFromFileMaybe(IoBuffer& InBuffer); + inline static IoBuffer MakeFromMemory(MemoryView Memory) { return IoBuffer(IoBuffer::Wrap, Memory.GetData(), Memory.GetSize()); } inline static IoBuffer MakeCloneFromMemory(const void* Ptr, size_t Sz) { if (Sz) diff --git a/src/zencore/include/zencore/iohash.h b/src/zencore/include/zencore/iohash.h index fd0f4b2a7..649a59c2c 100644 --- a/src/zencore/include/zencore/iohash.h +++ b/src/zencore/include/zencore/iohash.h @@ -42,6 +42,9 @@ struct IoHash return Io; } + // Temporary helper for transitioning APIs + static IoHash FromBLAKE3(const IoHash& Hash) { return Hash; } + static IoHash HashBuffer(const void* data, size_t byteCount); static IoHash HashBuffer(MemoryView Data) { return HashBuffer(Data.GetData(), Data.GetSize()); } static IoHash HashBuffer(const CompositeBuffer& Buffer); diff --git a/src/zencore/include/zencore/string.h b/src/zencore/include/zencore/string.h index ab111ff81..7a44bf781 100644 --- a/src/zencore/include/zencore/string.h +++ b/src/zencore/include/zencore/string.h @@ -306,6 +306,8 @@ public: inline StringBuilderImpl& operator<<(const std::string_view str) { return AppendAscii(str); } inline StringBuilderImpl& operator<<(const std::u8string_view str) { return AppendAscii(str); } + inline void EnsureNulTerminated() const { *m_CurPos = '\0'; } + protected: inline void Init(C* Base, size_t Capacity) { @@ -313,8 +315,6 @@ protected: m_End = Base + Capacity; } - inline void EnsureNulTerminated() const { *m_CurPos = '\0'; } - inline void EnsureCapacity(size_t ExtraRequired) { // precondition: we know the current buffer has enough capacity @@ -607,14 +607,13 @@ ToHexNumber(UnsignedIntegral auto Value, char* OutString) /// <summary> /// Parse hex number string into a value, this formats the number in the correct order for a hexadecimal number /// </summary> -/// <param name="string">Input string</param> -/// <param name="characterCount">Number of characters in string</param> +/// <param name="HexString">Input string</param> /// <param name="OutValue">Pointer to output value</param> /// <returns>true if the input consisted of all valid hexadecimal characters</returns> bool -ParseHexNumber(const std::string HexString, UnsignedIntegral auto& OutValue) +ParseHexNumber(const std::string_view HexString, UnsignedIntegral auto& OutValue) { - return ParseHexNumber(HexString.c_str(), sizeof(OutValue) * 2, (uint8_t*)&OutValue); + return ParseHexNumber(HexString.data(), sizeof(OutValue) * 2, (uint8_t*)&OutValue); } ////////////////////////////////////////////////////////////////////////// diff --git a/src/zencore/include/zencore/system.h b/src/zencore/include/zencore/system.h new file mode 100644 index 000000000..23665fb09 --- /dev/null +++ b/src/zencore/include/zencore/system.h @@ -0,0 +1,37 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#pragma once + +#include <zencore/zencore.h> + +#include <string> + +namespace zen { + +class CbWriter; + +std::string GetMachineName(); +std::string_view GetOperatingSystemName(); +std::string_view GetCpuName(); + +struct SystemMetrics +{ + int CpuCount = 1; + int CoreCount = 1; + int LogicalProcessorCount = 1; + uint64_t SystemMemoryMiB = 0; + uint64_t AvailSystemMemoryMiB = 0; + uint64_t VirtualMemoryMiB = 0; + uint64_t AvailVirtualMemoryMiB = 0; + uint64_t PageFileMiB = 0; + uint64_t AvailPageFileMiB = 0; +}; + +SystemMetrics GetSystemMetrics(); + +void SetCpuCountForReporting(int FakeCpuCount); +SystemMetrics GetSystemMetricsForReporting(); + +void Describe(const SystemMetrics& Metrics, CbWriter& Writer); + +} // namespace zen diff --git a/src/zencore/include/zencore/thread.h b/src/zencore/include/zencore/thread.h index 9ff98de8c..9f2671610 100644 --- a/src/zencore/include/zencore/thread.h +++ b/src/zencore/include/zencore/thread.h @@ -107,6 +107,10 @@ public: ZENCORE_API bool Wait(int TimeoutMs = -1); ZENCORE_API void Close(); +#if ZEN_PLATFORM_WINDOWS + inline void* GetWindowsHandle() { return m_EventHandle; } +#endif + protected: explicit Event(void* EventHandle) : m_EventHandle(EventHandle) {} @@ -181,6 +185,7 @@ public: void AddCount(std::ptrdiff_t Count) { std::atomic_ptrdiff_t Old = Counter.fetch_add(Count); + ZEN_UNUSED(Old); ZEN_ASSERT_SLOW(Old > 0); } diff --git a/src/zencore/include/zencore/uid.h b/src/zencore/include/zencore/uid.h index a356da88d..4a1285416 100644 --- a/src/zencore/include/zencore/uid.h +++ b/src/zencore/include/zencore/uid.h @@ -60,7 +60,7 @@ struct Oid const Oid& Generate(); [[nodiscard]] static Oid FromHexString(const std::string_view String); StringBuilderBase& ToString(StringBuilderBase& OutString) const; - void ToString(char OutString[StringLength]); + void ToString(char OutString[StringLength]) const; [[nodiscard]] static Oid FromMemory(const void* Ptr); auto operator<=>(const Oid& rhs) const = default; diff --git a/src/zencore/include/zencore/zencore.h b/src/zencore/include/zencore/zencore.h index 47ff2ebc2..71bd08e69 100644 --- a/src/zencore/include/zencore/zencore.h +++ b/src/zencore/include/zencore/zencore.h @@ -22,12 +22,15 @@ #ifdef _WIN32 # undef ZEN_PLATFORM_WINDOWS # define ZEN_PLATFORM_WINDOWS 1 +# define ZEN_PLATFORM_NAME "Windows" #elif defined(__linux__) # undef ZEN_PLATFORM_LINUX # define ZEN_PLATFORM_LINUX 1 +# define ZEN_PLATFORM_NAME "Linux" #elif defined(__APPLE__) # undef ZEN_PLATFORM_MAC -# define ZEN_PLATFORM_MAC 1 +# define ZEN_PLATFORM_MAC 1 +# define ZEN_PLATFORM_NAME "MacOS" #endif #if ZEN_PLATFORM_WINDOWS @@ -201,9 +204,11 @@ concept ContiguousRange = true; #ifdef NDEBUG # define ZEN_BUILD_DEBUG 0 # define ZEN_BUILD_RELEASE 1 +# define ZEN_BUILD_NAME "release" #else # define ZEN_BUILD_DEBUG 1 # define ZEN_BUILD_RELEASE 0 +# define ZEN_BUILD_NAME "debug" #endif ////////////////////////////////////////////////////////////////////////// @@ -323,7 +328,7 @@ char (&ZenArrayCountHelper(const T (&)[N]))[N + 1]; #if ZEN_PLATFORM_WINDOWS # define ZEN_EXE_SUFFIX_LITERAL ".exe" #else -# define ZEN_EXE_SUFFIX_LITERAL +# define ZEN_EXE_SUFFIX_LITERAL "" #endif #define ZEN_UNUSED(...) ((void)__VA_ARGS__) |