mirror of
https://github.com/thunderbrewhq/common.git
synced 2025-12-12 03:02:29 +00:00
chore: initial commit
This commit is contained in:
commit
69a95ae405
28 changed files with 18574 additions and 0 deletions
54
.clang-format
Normal file
54
.clang-format
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
---
|
||||||
|
AccessModifierOffset: 0
|
||||||
|
AlignConsecutiveAssignments: false
|
||||||
|
AlignConsecutiveDeclarations: false
|
||||||
|
AllowShortBlocksOnASingleLine: Never
|
||||||
|
AllowShortCaseLabelsOnASingleLine: false
|
||||||
|
AllowShortIfStatementsOnASingleLine: Never
|
||||||
|
AlwaysBreakTemplateDeclarations: Yes
|
||||||
|
BreakBeforeBraces: Attach
|
||||||
|
BreakBeforeTernaryOperators: true
|
||||||
|
BreakConstructorInitializers: BeforeComma
|
||||||
|
ColumnLimit: 0
|
||||||
|
CompactNamespaces: false
|
||||||
|
ContinuationIndentWidth: 4
|
||||||
|
Cpp11BracedListStyle: false
|
||||||
|
DeriveLineEnding: false
|
||||||
|
DerivePointerAlignment: false
|
||||||
|
IncludeBlocks: Merge
|
||||||
|
IncludeCategories:
|
||||||
|
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
|
||||||
|
Priority: 2
|
||||||
|
SortPriority: 2
|
||||||
|
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
|
||||||
|
Priority: 3
|
||||||
|
- Regex: "<[[:alnum:].]+>"
|
||||||
|
Priority: 4
|
||||||
|
- Regex: ".*"
|
||||||
|
Priority: 1
|
||||||
|
SortPriority: 0
|
||||||
|
IndentGotoLabels: false
|
||||||
|
IndentWidth: 4
|
||||||
|
MaxEmptyLinesToKeep: 1
|
||||||
|
ObjCBlockIndentWidth: 4
|
||||||
|
ObjCSpaceAfterProperty: false
|
||||||
|
ObjCSpaceBeforeProtocolList: false
|
||||||
|
PointerAlignment: Left
|
||||||
|
SortIncludes: true
|
||||||
|
SpaceAfterCStyleCast: false
|
||||||
|
SpaceAfterLogicalNot: false
|
||||||
|
SpaceAfterTemplateKeyword: true
|
||||||
|
SpaceBeforeAssignmentOperators: true
|
||||||
|
SpaceBeforeCpp11BracedList: true
|
||||||
|
SpaceBeforeCtorInitializerColon: true
|
||||||
|
SpaceBeforeInheritanceColon: true
|
||||||
|
SpaceBeforeParens: ControlStatements
|
||||||
|
SpaceBeforeSquareBrackets: false
|
||||||
|
SpaceInEmptyBlock: false
|
||||||
|
SpacesInAngles: false
|
||||||
|
SpacesInCStyleCastParentheses: false
|
||||||
|
SpacesInConditionalStatement: false
|
||||||
|
Standard: c++11
|
||||||
|
TabWidth: 4
|
||||||
|
UseCRLF: false
|
||||||
|
UseTab: Never
|
||||||
1
.clang-format-ignore
Normal file
1
.clang-format-ignore
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
/vendor
|
||||||
15
.editorconfig
Normal file
15
.editorconfig
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
charset = utf-8
|
||||||
|
end_of_line = lf
|
||||||
|
insert_final_newline = true
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
|
||||||
|
[common/**/*]
|
||||||
|
indent_size = 4
|
||||||
|
indent_style = space
|
||||||
|
|
||||||
|
[test/**/*]
|
||||||
|
indent_size = 4
|
||||||
|
indent_style = space
|
||||||
50
.github/workflows/pr.yml
vendored
Normal file
50
.github/workflows/pr.yml
vendored
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
name: PR
|
||||||
|
|
||||||
|
on: pull_request
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
name: ${{ matrix.config.name }}
|
||||||
|
runs-on: ${{ matrix.config.os }}
|
||||||
|
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
config:
|
||||||
|
- name: Ubuntu Latest (GCC)
|
||||||
|
os: ubuntu-latest
|
||||||
|
build_type: Release
|
||||||
|
test_path: CommonTest
|
||||||
|
cc: gcc
|
||||||
|
cxx: g++
|
||||||
|
|
||||||
|
- name: macOS Latest (Clang)
|
||||||
|
os: macos-latest
|
||||||
|
build_type: Release
|
||||||
|
test_path: CommonTest
|
||||||
|
cc: clang
|
||||||
|
cxx: clang++
|
||||||
|
|
||||||
|
- name: Windows Latest (MSVC)
|
||||||
|
os: windows-latest
|
||||||
|
build_type: Release
|
||||||
|
test_path: Release/CommonTest
|
||||||
|
cc: cl
|
||||||
|
cxx: cl
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
with:
|
||||||
|
submodules: recursive
|
||||||
|
|
||||||
|
- name: Prepare
|
||||||
|
run: mkdir build
|
||||||
|
|
||||||
|
- name: Configure
|
||||||
|
run: cd build && cmake .. -DCMAKE_BUILD_TYPE=${{ matrix.config.build_type }}
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: cmake --build build --config ${{ matrix.config.build_type }}
|
||||||
|
|
||||||
|
- name: Test
|
||||||
|
run: ./build/test/${{ matrix.config.test_path }}
|
||||||
53
.github/workflows/push.yml
vendored
Normal file
53
.github/workflows/push.yml
vendored
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
name: Push
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
name: ${{ matrix.config.name }}
|
||||||
|
runs-on: ${{ matrix.config.os }}
|
||||||
|
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
config:
|
||||||
|
- name: Ubuntu Latest (GCC)
|
||||||
|
os: ubuntu-latest
|
||||||
|
build_type: Release
|
||||||
|
test_path: CommonTest
|
||||||
|
cc: gcc
|
||||||
|
cxx: g++
|
||||||
|
|
||||||
|
- name: macOS Latest (Clang)
|
||||||
|
os: macos-latest
|
||||||
|
build_type: Release
|
||||||
|
test_path: CommonTest
|
||||||
|
cc: clang
|
||||||
|
cxx: clang++
|
||||||
|
|
||||||
|
- name: Windows Latest (MSVC)
|
||||||
|
os: windows-latest
|
||||||
|
build_type: Release
|
||||||
|
test_path: Release/CommonTest
|
||||||
|
cc: cl
|
||||||
|
cxx: cl
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
with:
|
||||||
|
submodules: recursive
|
||||||
|
|
||||||
|
- name: Prepare
|
||||||
|
run: mkdir build
|
||||||
|
|
||||||
|
- name: Configure
|
||||||
|
run: cd build && cmake .. -DCMAKE_BUILD_TYPE=${{ matrix.config.build_type }}
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: cmake --build build --config ${{ matrix.config.build_type }}
|
||||||
|
|
||||||
|
- name: Test
|
||||||
|
run: ./build/test/${{ matrix.config.test_path }}
|
||||||
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
.DS_Store
|
||||||
|
.idea
|
||||||
|
.vscode
|
||||||
|
|
||||||
|
/build
|
||||||
|
/dist
|
||||||
6
.gitmodules
vendored
Normal file
6
.gitmodules
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
[submodule "lib/squall"]
|
||||||
|
path = lib/squall
|
||||||
|
url = https://github.com/whoahq/squall
|
||||||
|
[submodule "lib/system"]
|
||||||
|
path = lib/system
|
||||||
|
url = https://github.com/whoahq/system
|
||||||
25
CMakeLists.txt
Normal file
25
CMakeLists.txt
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
cmake_minimum_required(VERSION 3.1)
|
||||||
|
|
||||||
|
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
|
||||||
|
message(FATAL_ERROR
|
||||||
|
"In-source builds not allowed.
|
||||||
|
Please make a new directory (called a build directory) and run CMake from there.
|
||||||
|
You may need to remove CMakeCache.txt."
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(TARGET common)
|
||||||
|
# Guard for use as transitive dependency
|
||||||
|
return()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Project
|
||||||
|
project(common)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 11)
|
||||||
|
|
||||||
|
include(lib/system/cmake/system.cmake)
|
||||||
|
|
||||||
|
add_subdirectory(lib)
|
||||||
|
add_subdirectory(common)
|
||||||
|
add_subdirectory(test)
|
||||||
24
LICENSE
Normal file
24
LICENSE
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
This is free and unencumbered software released into the public domain.
|
||||||
|
|
||||||
|
Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||||
|
distribute this software, either in source code form or as a compiled
|
||||||
|
binary, for any purpose, commercial or non-commercial, and by any
|
||||||
|
means.
|
||||||
|
|
||||||
|
In jurisdictions that recognize copyright laws, the author or authors
|
||||||
|
of this software dedicate any and all copyright interest in the
|
||||||
|
software to the public domain. We make this dedication for the benefit
|
||||||
|
of the public at large and to the detriment of our heirs and
|
||||||
|
successors. We intend this dedication to be an overt act of
|
||||||
|
relinquishment in perpetuity of all present and future rights to this
|
||||||
|
software under copyright law.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
|
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||||
|
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||||
|
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
|
OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
For more information, please refer to <https://unlicense.org>
|
||||||
49
README.md
Normal file
49
README.md
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
# Common
|
||||||
|
|
||||||
|
Common is an unofficial open source implementation of common utility classes
|
||||||
|
and routines found in World of Warcraft: Wrath of the Lich King circa 2010.
|
||||||
|
These utility classes and routines were not part of the standard libraries
|
||||||
|
used for the game, and are assumed to be specific to World of Warcraft, unlike
|
||||||
|
Storm, Tempest, and so on.
|
||||||
|
|
||||||
|
This project attempts to provide a version of these common utility classes and
|
||||||
|
routines compatible with the final release build of World of Warcraft: Wrath
|
||||||
|
of the Lich King in 2010: 3.3.5a (12340).
|
||||||
|
|
||||||
|
In the spirit of documenting what is presumed to have existed at the time,
|
||||||
|
this project makes every attempt to maintain the canonical names, layouts, and
|
||||||
|
side effects of the original implementation of these common utility classes
|
||||||
|
and routines. At the same time, it attempts to ensure portability and
|
||||||
|
compatibility with modern 64-bit systems.
|
||||||
|
|
||||||
|
## FAQ
|
||||||
|
|
||||||
|
**Why?**
|
||||||
|
|
||||||
|
It's fascinating to explore the development practices used to build a modern
|
||||||
|
major video game.
|
||||||
|
|
||||||
|
**Why 3.3.5a?**
|
||||||
|
|
||||||
|
The game and its libraries have become significantly more complex in the
|
||||||
|
intervening 10+ years. By picking 3.3.5a, it's possible to imagine this
|
||||||
|
implementation will eventually be complete.
|
||||||
|
|
||||||
|
**Will this let me cheat in the game?**
|
||||||
|
|
||||||
|
No. Common is a library of utility classes and routines, none of which are
|
||||||
|
particularly relevant to the anti-cheat measures found in the game.
|
||||||
|
|
||||||
|
**Can I use this in my own development projects?**
|
||||||
|
|
||||||
|
It's probably a bad idea. The original utility classes and routines remain
|
||||||
|
closed source, and this project is in no way official.
|
||||||
|
|
||||||
|
## Legal
|
||||||
|
|
||||||
|
This project is released into the public domain.
|
||||||
|
|
||||||
|
World of Warcraft: Wrath of the Lich King ©2008 Blizzard Entertainment, Inc.
|
||||||
|
All rights reserved. Wrath of the Lich King is a trademark, and World of
|
||||||
|
Warcraft, Warcraft and Blizzard Entertainment are trademarks or registered
|
||||||
|
trademarks of Blizzard Entertainment, Inc. in the U.S. and/or other countries.
|
||||||
19
common/CMakeLists.txt
Normal file
19
common/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
file(GLOB COMMON_SOURCES
|
||||||
|
"*.cpp"
|
||||||
|
"ref/*.cpp"
|
||||||
|
"string/*.cpp"
|
||||||
|
)
|
||||||
|
|
||||||
|
add_library(common STATIC
|
||||||
|
${COMMON_SOURCES}
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(common
|
||||||
|
PUBLIC
|
||||||
|
${PROJECT_SOURCE_DIR}
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(common
|
||||||
|
PUBLIC
|
||||||
|
storm
|
||||||
|
)
|
||||||
6
common/Ref.hpp
Normal file
6
common/Ref.hpp
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef COMMON_REF_HPP
|
||||||
|
#define COMMON_REF_HPP
|
||||||
|
|
||||||
|
#include "common/ref/TRefCnt.hpp"
|
||||||
|
|
||||||
|
#endif
|
||||||
6
common/String.hpp
Normal file
6
common/String.hpp
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef COMMON_STRING_HPP
|
||||||
|
#define COMMON_STRING_HPP
|
||||||
|
|
||||||
|
#include "common/string/RCString.hpp"
|
||||||
|
|
||||||
|
#endif
|
||||||
19
common/ref/TRefCnt.hpp
Normal file
19
common/ref/TRefCnt.hpp
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
#ifndef COMMON_T_REF_CNT_HPP
|
||||||
|
#define COMMON_T_REF_CNT_HPP
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
class TRefCnt {
|
||||||
|
public:
|
||||||
|
// Member variables
|
||||||
|
uint32_t m_refcnt;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
class TRefCntPtr {
|
||||||
|
public:
|
||||||
|
// Member variables
|
||||||
|
T* m_ptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
49
common/string/CStringManager.cpp
Normal file
49
common/string/CStringManager.cpp
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
#include "common/string/CStringManager.hpp"
|
||||||
|
#include <climits>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
|
#include <storm/Memory.hpp>
|
||||||
|
#include <storm/String.hpp>
|
||||||
|
|
||||||
|
CStringManager* CStringManager::s_stringManager;
|
||||||
|
|
||||||
|
CStringManager* CStringManager::Get() {
|
||||||
|
// If the string manager isn't instantiated yet, do so now
|
||||||
|
if (!CStringManager::s_stringManager) {
|
||||||
|
void* m = SMemAlloc(sizeof(CStringManager), __FILE__, __LINE__, 0x0);
|
||||||
|
CStringManager::s_stringManager = m ? new (m) CStringManager() : nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return CStringManager::s_stringManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
CStringManager::CStringManager() {
|
||||||
|
this->m_memory.AllocateBlock(0);
|
||||||
|
memset(this->m_buckets, 0, sizeof(this->m_buckets));
|
||||||
|
}
|
||||||
|
|
||||||
|
char* CStringManager::Add(const char* str) {
|
||||||
|
uint32_t hashval = SStrHashHT(str);
|
||||||
|
uint32_t bucketIdx = hashval % 0x209;
|
||||||
|
auto bucket = this->m_buckets[bucketIdx];
|
||||||
|
|
||||||
|
if (bucket) {
|
||||||
|
auto ele = bucket;
|
||||||
|
|
||||||
|
while (ele) {
|
||||||
|
if (!SStrCmp(str, &ele->str, INT_MAX)) {
|
||||||
|
return &ele->str;
|
||||||
|
}
|
||||||
|
|
||||||
|
ele = ele->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto newEle = this->m_memory.New(str);
|
||||||
|
|
||||||
|
// Insert new element at start
|
||||||
|
newEle->next = this->m_buckets[bucketIdx];
|
||||||
|
this->m_buckets[bucketIdx] = newEle;
|
||||||
|
|
||||||
|
return &newEle->str;
|
||||||
|
}
|
||||||
25
common/string/CStringManager.hpp
Normal file
25
common/string/CStringManager.hpp
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
#ifndef COMMON_STRING_C_STRING_MANAGER_HPP
|
||||||
|
#define COMMON_STRING_C_STRING_MANAGER_HPP
|
||||||
|
|
||||||
|
#include "common/string/CStringMemory.hpp"
|
||||||
|
|
||||||
|
struct CStringElement;
|
||||||
|
|
||||||
|
class CStringManager {
|
||||||
|
public:
|
||||||
|
// Static variables
|
||||||
|
static CStringManager* s_stringManager;
|
||||||
|
|
||||||
|
// Static functions
|
||||||
|
static CStringManager* Get();
|
||||||
|
|
||||||
|
// Member variables
|
||||||
|
CStringElement* m_buckets[521];
|
||||||
|
CStringMemory m_memory;
|
||||||
|
|
||||||
|
// Member functions
|
||||||
|
CStringManager();
|
||||||
|
char* Add(const char* str);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
44
common/string/CStringMemory.cpp
Normal file
44
common/string/CStringMemory.cpp
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
#include "common/string/CStringMemory.hpp"
|
||||||
|
#include <climits>
|
||||||
|
#include <storm/Memory.hpp>
|
||||||
|
#include <storm/String.hpp>
|
||||||
|
|
||||||
|
void CStringMemory::AllocateBlock(size_t blockSize) {
|
||||||
|
// Ensure the block is of a minimum size
|
||||||
|
if (blockSize <= 0x10000) {
|
||||||
|
blockSize = 0x10000;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t index = this->Count();
|
||||||
|
this->GrowToFit(index, 0);
|
||||||
|
|
||||||
|
// Allocate the block
|
||||||
|
void* block = SMemAlloc(blockSize, __FILE__, __LINE__, 0x0);
|
||||||
|
|
||||||
|
// Store the block in the array
|
||||||
|
this->operator[](index) = block;
|
||||||
|
|
||||||
|
this->m_free = reinterpret_cast<uintptr_t>(block);
|
||||||
|
this->m_end = reinterpret_cast<uintptr_t>(block) + blockSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
CStringElement* CStringMemory::New(const char* str) {
|
||||||
|
size_t strLen = SStrLen(str);
|
||||||
|
|
||||||
|
// Aligned to pointer: sizeof(pointer + string)
|
||||||
|
size_t eleSize = (strLen + (sizeof(void*) * 2)) & static_cast<uintptr_t>(-sizeof(void*));
|
||||||
|
|
||||||
|
// Allocation would exceed block end, so allocate a new block
|
||||||
|
if (this->m_free + eleSize > this->m_end) {
|
||||||
|
this->AllocateBlock(eleSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto ele = reinterpret_cast<CStringElement*>(this->m_free);
|
||||||
|
|
||||||
|
// Copy the provided string into string memory
|
||||||
|
SStrCopy(&ele->str, str, INT_MAX);
|
||||||
|
|
||||||
|
this->m_free += eleSize;
|
||||||
|
|
||||||
|
return ele;
|
||||||
|
}
|
||||||
22
common/string/CStringMemory.hpp
Normal file
22
common/string/CStringMemory.hpp
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
#ifndef COMMON_STRING_C_STRING_MEMORY_HPP
|
||||||
|
#define COMMON_STRING_C_STRING_MEMORY_HPP
|
||||||
|
|
||||||
|
#include <storm/Array.hpp>
|
||||||
|
|
||||||
|
struct CStringElement {
|
||||||
|
CStringElement* next;
|
||||||
|
char str;
|
||||||
|
};
|
||||||
|
|
||||||
|
class CStringMemory : public TSGrowableArray<void*> {
|
||||||
|
public:
|
||||||
|
// Member variables
|
||||||
|
uintptr_t m_free;
|
||||||
|
uintptr_t m_end;
|
||||||
|
|
||||||
|
// Member functions
|
||||||
|
void AllocateBlock(size_t blockSize);
|
||||||
|
CStringElement* New(const char* str);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
34
common/string/RCString.cpp
Normal file
34
common/string/RCString.cpp
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
#include "common/string/RCString.hpp"
|
||||||
|
#include "common/string/CStringManager.hpp"
|
||||||
|
#include <storm/String.hpp>
|
||||||
|
|
||||||
|
RCString::RCString()
|
||||||
|
: TRefCnt() {
|
||||||
|
this->Copy(nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void RCString::Copy(const char* source) {
|
||||||
|
if (source) {
|
||||||
|
this->m_str = CStringManager::Get()->Add(source);
|
||||||
|
} else {
|
||||||
|
this->m_str = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void RCString::Get(char* buf, size_t bufSize) const {
|
||||||
|
const char* str = this->GetString();
|
||||||
|
|
||||||
|
if (str) {
|
||||||
|
SStrCopy(buf, str, bufSize);
|
||||||
|
} else {
|
||||||
|
*buf = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* RCString::GetString() const {
|
||||||
|
if (this->m_str) {
|
||||||
|
return this->m_str;
|
||||||
|
} else {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
19
common/string/RCString.hpp
Normal file
19
common/string/RCString.hpp
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
#ifndef COMMON_STRING_RC_STRING_HPP
|
||||||
|
#define COMMON_STRING_RC_STRING_HPP
|
||||||
|
|
||||||
|
#include "common/ref/TRefCnt.hpp"
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
class RCString : public TRefCnt {
|
||||||
|
public:
|
||||||
|
// Member variables
|
||||||
|
char* m_str;
|
||||||
|
|
||||||
|
// Member functions
|
||||||
|
RCString();
|
||||||
|
void Copy(const char* source);
|
||||||
|
void Get(char* buf, size_t bufSize) const;
|
||||||
|
const char* GetString() const;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
2
lib/CMakeLists.txt
Normal file
2
lib/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
add_subdirectory(squall)
|
||||||
|
add_subdirectory(system)
|
||||||
1
lib/squall
Submodule
1
lib/squall
Submodule
|
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 61d2c8d01e35bba0a0a7363f40bb6a81f289b76d
|
||||||
1
lib/system
Submodule
1
lib/system
Submodule
|
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit f886ab5bc35b2e0d968baa8dec3faaccf385fbc3
|
||||||
31
test/CMakeLists.txt
Normal file
31
test/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
file(GLOB_RECURSE TEST_SOURCES "*.cpp")
|
||||||
|
|
||||||
|
if(WHOA_SYSTEM_MAC)
|
||||||
|
set_source_files_properties(${TEST_SOURCES}
|
||||||
|
PROPERTIES COMPILE_FLAGS "-x objective-c++"
|
||||||
|
)
|
||||||
|
|
||||||
|
add_executable(CommonTest ${TEST_SOURCES})
|
||||||
|
|
||||||
|
target_link_libraries(CommonTest
|
||||||
|
PRIVATE
|
||||||
|
common
|
||||||
|
"-framework AppKit"
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(WHOA_SYSTEM_LINUX OR WHOA_SYSTEM_WIN)
|
||||||
|
add_executable(CommonTest ${TEST_SOURCES})
|
||||||
|
|
||||||
|
target_link_libraries(CommonTest
|
||||||
|
PRIVATE
|
||||||
|
common
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
target_include_directories(CommonTest
|
||||||
|
PRIVATE
|
||||||
|
${PROJECT_SOURCE_DIR}
|
||||||
|
)
|
||||||
|
|
||||||
|
install(TARGETS CommonTest DESTINATION "bin")
|
||||||
34
test/String.cpp
Normal file
34
test/String.cpp
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
#include "common/String.hpp"
|
||||||
|
#include "test/Test.hpp"
|
||||||
|
#include <storm/String.hpp>
|
||||||
|
|
||||||
|
TEST_CASE("RCString::Copy", "[string]") {
|
||||||
|
SECTION("copies source string") {
|
||||||
|
auto str = "foo";
|
||||||
|
RCString rcStr;
|
||||||
|
rcStr.Copy(str);
|
||||||
|
REQUIRE(!SStrCmp(str, rcStr.GetString(), STORM_MAX_STR));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("RCString::GetString", "[string]") {
|
||||||
|
SECTION("returns identical pointers for identical source strings") {
|
||||||
|
auto str1 = "foo";
|
||||||
|
auto str2 = "foo";
|
||||||
|
RCString rcStr1;
|
||||||
|
rcStr1.Copy(str1);
|
||||||
|
RCString rcStr2;
|
||||||
|
rcStr2.Copy(str2);
|
||||||
|
REQUIRE(rcStr1.GetString() == rcStr2.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("returns different pointers for different source strings") {
|
||||||
|
auto str1 = "foo1";
|
||||||
|
auto str2 = "foo2";
|
||||||
|
RCString rcStr1;
|
||||||
|
rcStr1.Copy(str1);
|
||||||
|
RCString rcStr2;
|
||||||
|
rcStr2.Copy(str2);
|
||||||
|
REQUIRE(rcStr1.GetString() != rcStr2.GetString());
|
||||||
|
}
|
||||||
|
}
|
||||||
2
test/Test.cpp
Normal file
2
test/Test.cpp
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
#define CATCH_CONFIG_MAIN
|
||||||
|
#include "test/Test.hpp"
|
||||||
1
test/Test.hpp
Normal file
1
test/Test.hpp
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
#include "vendor/catch-2.13.10/catch.hpp"
|
||||||
17976
vendor/catch-2.13.10/catch.hpp
vendored
Normal file
17976
vendor/catch-2.13.10/catch.hpp
vendored
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue