Pārlūkot izejas kodu

Minimize includes

Kajetan Johannes Hammerle 1 nedēļu atpakaļ
vecāks
revīzija
a33a57268b

+ 17 - 7
CMakeLists.txt

@@ -3,9 +3,13 @@ project(core CXX)
 
 set(CMAKE_CXX_STANDARD 23)
 
-set(SRC_MODULES
-    "modules/Array.cppm"
+set(PRIVATE_MODULES
+    "src/ErrorSimulator.cppm"
+)
+
+set(PUBLIC_MODULES
     "modules/AlignedData.cppm"
+    "modules/Array.cppm"
     "modules/ArrayList.cppm"
     "modules/BitArray.cppm"
     "modules/Box.cppm"
@@ -22,11 +26,13 @@ set(SRC_MODULES
     "modules/Math.cppm"
     "modules/Matrix.cppm"
     "modules/Meta.cppm"
+    "modules/New.cppm"
     "modules/Plane.cppm"
     "modules/Quaternion.cppm"
     "modules/Queue.cppm"
     "modules/Random.cppm"
     "modules/ReadLine.cppm"
+    "modules/Std.cppm"
     "modules/Terminal.cppm"
     "modules/TerminalConstants.cppm"
     "modules/Test.cppm"
@@ -120,7 +126,6 @@ else()
         set(LINK_OPTIONS ${COMPILE_OPTIONS})
     endif()
     set(LOG_LEVEL 4)
-    list(APPEND SRC "src/ErrorSimulator.cpp")
 endif()
 
 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
@@ -136,9 +141,14 @@ target_compile_options(core PUBLIC
     -fdiagnostics-color=always
 )
 target_sources(core PUBLIC
-    FILE_SET CXX_MODULES
-    BASE_DIRS modules
-    FILES ${SRC_MODULES}
+    FILE_SET public_modules
+    TYPE CXX_MODULES
+    FILES ${PUBLIC_MODULES}
+)
+target_sources(core PUBLIC
+    FILE_SET private_modules
+    TYPE CXX_MODULES
+    FILES ${PRIVATE_MODULES}
 )
 target_compile_definitions(core
     PRIVATE LOG_LEVEL=${LOG_LEVEL}
@@ -147,7 +157,7 @@ target_compile_definitions(core
 target_link_libraries(core
     PRIVATE m ${LINK_OPTIONS}
 )
-install(TARGETS core FILE_SET CXX_MODULES DESTINATION modules)
+install(TARGETS core FILE_SET public_modules DESTINATION modules)
 
 add_executable(test ${SRC_TESTS})
 target_sources(test PUBLIC

+ 2 - 3
modules/ArrayList.cppm

@@ -1,16 +1,15 @@
 module;
 
 #include <cassert>
-#include <new>
 
 export module Core.ArrayList;
 
+export import Core.New;
+
 import Core.AlignedData;
 import Core.Meta;
 import Core.Types;
 
-export using ::operator new;
-
 export namespace Core {
     template<typename T, size_t N>
     class ArrayList final {

+ 2 - 1
modules/HashedString.cppm

@@ -1,12 +1,13 @@
 module;
 
 #include <cstdio>
-#include <cstring>
 
 export module Core.HashedString;
 
 export import Core.Types;
 
+import Core.Std;
+
 export namespace Core {
     struct StringHash {
         size_t length = 0;

+ 1 - 1
modules/List.cppm

@@ -1,11 +1,11 @@
 module;
 
 #include <cassert>
-#include <new>
 
 export module Core.List;
 
 export import Core.Utility;
+export import Core.New;
 
 import Core.AlignedData;
 import Core.Math;

+ 1 - 1
modules/Logger.cppm

@@ -1,13 +1,13 @@
 module;
 
 #include <cstdio>
-#include <source_location>
 
 export module Core.Logger;
 
 import Core.TerminalConstants;
 import Core.Meta;
 import Core.ToString;
+import Core.Std;
 
 #define SOURCE const std::source_location& sl = std::source_location::current()
 

+ 11 - 0
modules/New.cppm

@@ -0,0 +1,11 @@
+module;
+
+#include <new>
+
+export module Core.New;
+
+export namespace std {
+    using std::set_new_handler;
+}
+
+export using ::operator new;

+ 2 - 3
modules/Queue.cppm

@@ -1,17 +1,16 @@
 module;
 
 #include <cassert>
-#include <new>
 
 export module Core.Queue;
 
+export import Core.New;
+
 import Core.AlignedData;
 import Core.ToString;
 import Core.Meta;
 import Core.Utility;
 
-export using ::operator new;
-
 export namespace Core {
     template<typename T, size_t N>
     class Queue final {

+ 36 - 0
modules/Std.cppm

@@ -0,0 +1,36 @@
+module;
+
+#include <cctype>
+#include <clocale>
+#include <cmath>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <source_location>
+
+export module Core.Std;
+
+export namespace std {
+    using std::source_location;
+}
+
+export using ::fflush;
+export using ::fputs;
+export using ::free;
+export using ::iscntrl;
+export using ::memcmp;
+export using ::memcpy;
+export using ::memset;
+export using ::puts;
+export using ::setlocale;
+export using ::sincosf;
+export using ::sqrtf;
+export using ::stdout;
+export using ::strcmp;
+export using ::strdup;
+export using ::strstr;
+export using ::tanf;
+export using ::malloc;
+export using ::realloc;
+export using ::exit;
+export inline auto CORE_LC_ALL = LC_ALL;

+ 5 - 7
modules/Test.cppm

@@ -1,13 +1,11 @@
 module;
 
-#include <cstdio>
-#include <source_location>
-
 export module Core.Test;
 
 import Core.Logger;
 import Core.TerminalConstants;
 import Core.ToString;
+import Core.Std;
 
 #define SOURCE const std::source_location& l = std::source_location::current()
 
@@ -30,13 +28,13 @@ export namespace Core {
     }
 
     template<typename T>
-    bool testTrue(const T& actual) {
-        return test<T>(true, actual);
+    bool testTrue(const T& actual, SOURCE) {
+        return test<T>(true, actual, l);
     }
 
     template<typename T>
-    bool testFalse(const T& actual) {
-        return test<T>(false, actual);
+    bool testFalse(const T& actual, SOURCE) {
+        return test<T>(false, actual, l);
     }
 
     bool testString(const char* wanted, const char* actual, SOURCE);

+ 5 - 0
modules/Thread.cppm

@@ -1,10 +1,15 @@
 module;
 
+#include <atomic>
 #include <mutex>
 #include <thread>
 
 export module Core.Thread;
 
+export namespace std {
+    using std::atomic_bool;
+}
+
 export namespace Core {
     class Mutex final {
         std::mutex mutex{};

+ 3 - 3
modules/Utility.cppm

@@ -1,11 +1,11 @@
 module;
 
-#include <cstddef>
-#include <source_location>
-
 export module Core.Utility;
 
+export import Core.Types;
+
 import Core.Meta;
+import Core.Std;
 
 #define SOURCE const std::source_location& l = std::source_location::current()
 

+ 1 - 2
modules/Vector.cppm

@@ -1,7 +1,5 @@
 module;
 
-#include <cmath>
-
 export module Core.Vector;
 
 export import Core.Types;
@@ -9,6 +7,7 @@ export import Core.Types;
 import Core.Math;
 import Core.Meta;
 import Core.ToString;
+import Core.Std;
 
 export namespace Core {
     template<size_t N, typename T>

+ 1 - 1
src/BitArray.cpp

@@ -1,7 +1,6 @@
 module;
 
 #include <cassert>
-#include <cstring>
 
 module Core.BitArray;
 
@@ -9,6 +8,7 @@ import Core.ToString;
 import Core.Math;
 import Core.Meta;
 import Core.Utility;
+import Core.Std;
 
 using Core::BitArray;
 

+ 2 - 0
src/Box.cpp

@@ -4,6 +4,8 @@ module;
 
 module Core.Box;
 
+import Core.Std;
+
 using Core::Box;
 
 Box::Box(const Vector3& min_, const Vector3& max_) : min(min_), max(max_) {

+ 1 - 4
src/Buffer.cpp

@@ -1,12 +1,9 @@
-module;
-
-#include <cstring>
-
 module Core.Buffer;
 
 import Core.Math;
 import Core.Utility;
 import Core.Meta;
+import Core.Std;
 
 Core::Buffer::Buffer() : length(0), capacity(0), buffer(nullptr) {
 }

+ 4 - 4
src/Clock.cpp

@@ -3,10 +3,10 @@ module;
 #include <chrono>
 #include <thread>
 
-#include "ErrorSimulator.hpp"
-
 module Core.Clock;
 
+import ErrorSimulator;
+
 using Core::Clock;
 
 Clock::Clock() : index(0), last(0), sum(0), time({}) {
@@ -35,7 +35,7 @@ float Core::Clock::getUpdatesPerSecond() const {
 
 bool Clock::sleepNanos(i64 nanos) {
     try {
-        FAIL_STEP_THROW();
+        debugThrow();
         std::this_thread::sleep_for(std::chrono::nanoseconds(nanos));
     } catch(...) {
         return true;
@@ -49,7 +49,7 @@ bool Clock::sleepMillis(i64 millis) {
 
 i64 Clock::getNanos(void) {
     try {
-        FAIL_STEP_THROW();
+        debugThrow();
         using namespace std::chrono;
         return duration_cast<nanoseconds>(
                    high_resolution_clock::now().time_since_epoch())

+ 1 - 3
src/CustomNewDelete.cpp

@@ -1,7 +1,5 @@
-#include <cstddef>
-#include <source_location>
-
 import Core.Utility;
+import Core.Std;
 
 #ifdef CHECK_MEMORY
 void* operator new(size_t count) {

+ 0 - 8
src/ErrorSimulator.cpp

@@ -1,8 +0,0 @@
-#ifdef ERROR_SIMULATOR
-
-#include "ErrorSimulator.hpp"
-
-int failStep = -1;
-int failStepThrow = -1;
-
-#endif

+ 30 - 0
src/ErrorSimulator.cppm

@@ -0,0 +1,30 @@
+module;
+
+#include <stdexcept>
+
+export module ErrorSimulator;
+
+#ifdef ERROR_SIMULATOR
+export extern int failStep;
+export extern int failStepThrow;
+int failStep = -1;
+int failStepThrow = -1;
+#endif
+
+#define FAIL_STEP (--failStep == 0)
+
+export inline void debugThrow() {
+#ifdef ERROR_SIMULATOR
+    if(--failStepThrow == 0) {
+        throw std::runtime_error("not a real error");
+    }
+#endif
+}
+
+export inline bool doFailStep() {
+#ifdef ERROR_SIMULATOR
+    return --failStep == 0;
+#else
+    return false;
+#endif
+}

+ 0 - 26
src/ErrorSimulator.hpp

@@ -1,26 +0,0 @@
-#ifndef CORE_ERROR_SIMULATOR_HPP
-#define CORE_ERROR_SIMULATOR_HPP
-
-#include <stdexcept>
-
-#ifdef ERROR_SIMULATOR
-extern int failStep;
-extern int failStepThrow;
-
-#define FAIL_STEP (--failStep == 0)
-
-inline void debugThrow() {
-    if(--failStepThrow == 0) {
-        throw std::runtime_error("not a real error");
-    }
-}
-
-#define FAIL_STEP_THROW() debugThrow()
-#else
-#define FAIL_STEP false
-#define FAIL_STEP_THROW() \
-    do {                  \
-    } while(false)
-#endif
-
-#endif

+ 7 - 8
src/File.cpp

@@ -1,34 +1,33 @@
 module;
 
-#include <stdio.h>
-
-#include "ErrorSimulator.hpp"
+#include <cstdio>
 
 module Core.File;
 
 import Core.Logger;
+import ErrorSimulator;
 
 using Core::LogLevel;
 
 static bool readOpenFile(FILE* file, Core::List<char>& f, const char* path) {
-    if(FAIL_STEP || fseek(file, 0, SEEK_END)) {
+    if(doFailStep() || fseek(file, 0, SEEK_END)) {
         Core::report(LogLevel::ERROR, "cannot seek file end of '#'", path);
         return true;
     }
     long l = ftell(file);
-    if(FAIL_STEP || l < 0) {
+    if(doFailStep() || l < 0) {
         Core::report(LogLevel::ERROR, "cannot tell file position of '#'", path);
         return true;
     }
     size_t size = static_cast<size_t>(l);
     f.resize(size + 1);
-    if(FAIL_STEP || fseek(file, 0, SEEK_SET)) {
+    if(doFailStep() || fseek(file, 0, SEEK_SET)) {
         Core::report(LogLevel::ERROR, "cannot seek file start of '#'", path);
         return true;
     }
     size_t read = fread(&f[0], 1, size, file);
     f.getLast() = 0;
-    if(FAIL_STEP || read != size) {
+    if(doFailStep() || read != size) {
         Core::report(
             LogLevel::ERROR, "expected to read # bytes from '#' but read #",
             size, path, read);
@@ -44,7 +43,7 @@ bool Core::readFile(List<char>& f, const char* path) {
         return true;
     }
     bool r = readOpenFile(file, f, path);
-    if(FAIL_STEP || fclose(file)) {
+    if(doFailStep() || fclose(file)) {
         report(LogLevel::ERROR, "cannot close file '#'", path);
         r = true;
     }

+ 2 - 4
src/Frustum.cpp

@@ -1,9 +1,7 @@
-module;
-
-#include <cmath>
-
 module Core.Frustum;
 
+import Core.Std;
+
 using Core::Frustum;
 using V3 = Core::Vector3;
 using V4 = Core::Vector4;

+ 0 - 4
src/Logger.cpp

@@ -1,7 +1,3 @@
-module;
-
-#include <source_location>
-
 module Core.Logger;
 
 Core::LogLevel Core::logLevel = Core::LogLevel::DEBUG;

+ 1 - 4
src/Matrix.cpp

@@ -1,10 +1,7 @@
-module;
-
-#include <math.h>
-
 module Core.Matrix;
 
 import Core.ToString;
+import Core.Std;
 
 Core::Matrix::Matrix() {
     unit();

+ 2 - 0
src/Plane.cpp

@@ -4,6 +4,8 @@ module;
 
 module Core.Plane;
 
+import Core.Std;
+
 using Core::Plane;
 
 Plane::Plane() : abc(), d(0) {

+ 2 - 2
src/Quaternion.cpp

@@ -1,11 +1,11 @@
 module;
 
-#include <math.h>
-#include <stdio.h>
+#include <cstdio>
 
 module Core.Quaternion;
 
 import Core.Math;
+import Core.Std;
 
 Core::Quaternion::Quaternion() : v(0.0f, 0.0f, 0.0f, 1.0f) {
 }

+ 0 - 4
src/Random.cpp

@@ -1,7 +1,3 @@
-module;
-
-#include <cstring>
-
 module Core.Random;
 
 using Core::Random;

+ 2 - 4
src/ReadLine.cpp

@@ -1,9 +1,6 @@
 module;
 
-#include <atomic>
-
-#include <ctype.h>
-#include <stdio.h>
+#include <cstdio>
 
 module Core.ReadLine;
 
@@ -16,6 +13,7 @@ import Core.Terminal;
 import Core.TerminalConstants;
 import Core.Types;
 import Core.Unicode;
+import Core.Std;
 
 static constexpr size_t HISTORY_LENGTH = 10;
 static constexpr size_t CONSOLE_BUFFER_SIZE = 256;

+ 1 - 0
src/Terminal.cpp

@@ -12,6 +12,7 @@ import Core.TerminalConstants;
 import Core.Array;
 import Core.Logger;
 import Core.Unicode;
+import Core.Std;
 
 #define ESC "\33["
 #define esc(s) fputs(ESC s, stdout)

+ 3 - 6
src/Test.cpp

@@ -1,15 +1,12 @@
 module;
 
-#include <source_location>
-
-#include <inttypes.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include <cstdio>
 
 module Core.Test;
+
 import Core.Logger;
 import Core.Utility;
+import Core.Std;
 
 typedef struct {
     char* file;

+ 5 - 6
src/Thread.cpp

@@ -2,12 +2,11 @@ module;
 
 #include <thread>
 
-#include "ErrorSimulator.hpp"
-
 module Core.Thread;
 
 import Core.Logger;
 import Core.Meta;
+import ErrorSimulator;
 
 using Core::Mutex;
 using Core::MutexGuard;
@@ -15,7 +14,7 @@ using Core::Thread;
 
 void Mutex::lock() noexcept {
     try {
-        FAIL_STEP_THROW();
+        debugThrow();
         mutex.lock();
     } catch(std::exception& e) {
         Core::report(LogLevel::ERROR, "Could not lock mutex: #", e.what());
@@ -24,7 +23,7 @@ void Mutex::lock() noexcept {
 
 void Mutex::unlock() noexcept {
     try {
-        FAIL_STEP_THROW();
+        debugThrow();
         mutex.unlock();
     } catch(std::exception& e) {
         Core::report(LogLevel::ERROR, "Could not unlock mutex: #", e.what());
@@ -67,7 +66,7 @@ bool Thread::start(Function f, void* p) {
         return true;
     }
     try {
-        FAIL_STEP_THROW();
+        debugThrow();
         thread = std::thread([f, p]() { f(p); });
     } catch(std::exception& e) {
         Core::report(LogLevel::ERROR, "Could not start thread: #", e.what());
@@ -79,7 +78,7 @@ bool Thread::start(Function f, void* p) {
 bool Thread::join() {
     if(thread.joinable()) {
         try {
-            FAIL_STEP_THROW();
+            debugThrow();
             thread.join();
         } catch(std::exception& e) {
             Core::report(LogLevel::ERROR, "Could not join thread: #", e.what());

+ 2 - 0
src/ToString.cpp

@@ -4,6 +4,8 @@ module;
 
 module Core.ToString;
 
+import Core.Std;
+
 #define TO_STRING(type, format)                        \
     size_t Core::toString(type v, char* s, size_t n) { \
         int e = snprintf(s, n, format, v);             \

+ 2 - 5
src/Utility.cpp

@@ -1,17 +1,14 @@
 module;
 
 #include <cstdio>
-#include <cstdlib>
-#include <cstring>
-#include <ctime>
-#include <new>
-#include <source_location>
 
 module Core.Utility;
 
 import Core.Logger;
 import Core.Thread;
 import Core.Types;
+import Core.Std;
+import Core.New;
 
 static Core::ExitHandler exitHandler = nullptr;
 static void* exitData = nullptr;

+ 2 - 4
src/Vector.cpp

@@ -1,9 +1,7 @@
-module;
-
-#include <math.h>
-
 module Core.Vector;
 
+import Core.Std;
+
 void Core::setAngles(Vector3& v, float lengthAngle, float widthAngle) {
     float sWidth = 0.0f;
     float cWidth = 0.0f;

+ 2 - 5
test/Main.cpp

@@ -1,11 +1,8 @@
-#include <clocale>
-#include <cstdio>
-#include <cstring>
-
 import Tests;
 import Core.Logger;
 import Core.Test;
 import Core.Utility;
+import Core.Std;
 
 static void finalize() {
     Core::finalizeTests();
@@ -32,7 +29,7 @@ int main(int argAmount, const char** args) {
         puts("light;testData/readLineTest");
         return 0;
     }
-    setlocale(LC_ALL, "en_US.utf8");
+    setlocale(CORE_LC_ALL, "en_US.utf8");
     bool light = false;
     for(int i = 0; i < argAmount; i++) {
         if(strcmp(args[i], "light") == 0) {

+ 1 - 4
test/modules/BufferTests.cpp

@@ -1,13 +1,10 @@
-module;
-
-#include <cstring>
-
 module Tests;
 
 import Core.Buffer;
 import Core.Test;
 import Core.Types;
 import Core.Meta;
+import Core.Std;
 
 static constexpr size_t SIZE_TYPES =
     sizeof(int) + sizeof(long) + sizeof(float) + sizeof(double);

+ 1 - 4
test/modules/ClockTests.cpp

@@ -1,12 +1,9 @@
-module;
-
-#include "../../src/ErrorSimulator.hpp"
-
 module Tests;
 
 import Core.Clock;
 import Core.Test;
 import Core.Types;
+import ErrorSimulator;
 
 using Core::Clock;
 

+ 2 - 5
test/modules/FileTests.cpp

@@ -1,15 +1,12 @@
 module;
 
-#include <cstring>
-#include <source_location>
-
-#include "../../src/ErrorSimulator.hpp"
-
 module Tests;
 
 import Core.File;
 import Core.Logger;
 import Core.Test;
+import Core.Std;
+import ErrorSimulator;
 
 static int failIndex = 0;
 static const char* fails[] = {

+ 2 - 1
test/modules/QueueTests.cpp

@@ -1,9 +1,10 @@
 module;
 
-#include <stdio.h>
+#include <cstdio>
 
 module Tests;
 
+import Core.Std;
 import Core.Queue;
 import Core.Test;
 import Core.Types;

+ 2 - 3
test/modules/ReadLineTests.cpp

@@ -1,15 +1,14 @@
 module;
 
 #include <cstdio>
-#include <source_location>
-
-#include "../../src/ErrorSimulator.hpp"
 
 module Tests;
 
 import Core.Clock;
 import Core.ReadLine;
 import Core.Test;
+import Core.Std;
+import ErrorSimulator;
 
 static void testStringError(
     const char* s,

+ 2 - 1
test/modules/TerminalTests.cpp

@@ -1,9 +1,10 @@
 module;
 
-#include <stdio.h>
+#include <cstdio>
 
 module Tests;
 
+import Core.Std;
 import Core.Clock;
 import Core.Logger;
 import Core.Terminal;

+ 1 - 4
test/modules/ThreadTests.cpp

@@ -1,12 +1,9 @@
-module;
-
-#include "../../src/ErrorSimulator.hpp"
-
 module Tests;
 
 import Core.Test;
 import Core.Thread;
 import Core.Meta;
+import ErrorSimulator;
 
 static int runDone = 0;
 

+ 1 - 4
test/modules/UtilityTests.cpp

@@ -1,7 +1,3 @@
-module;
-
-#include <cstring>
-
 module Tests;
 
 import Core.Test;
@@ -9,6 +5,7 @@ import Core.ToString;
 import Core.Utility;
 import Core.Meta;
 import Core.Types;
+import Core.Std;
 
 static void testPopCount() {
     Core::test(4, Core::popCount(0xF));

+ 1 - 2
test/modules/VectorTests.cpp

@@ -1,10 +1,9 @@
 module;
 
-#include <cmath>
-
 module Tests;
 
 import Core.Test;
+import Core.Std;
 import Core.Vector;
 import Core.ToString;