Skip to content

Commit 3ff0b30

Browse files
committed
Improved C++ Flow
1 parent 149ef19 commit 3ff0b30

File tree

3 files changed

+39
-92
lines changed

3 files changed

+39
-92
lines changed

application/src/Application.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ int main(int argc, char **argv) {
1313
using namespace dotnamecpp::logging;
1414
using namespace dotnamecpp::utils;
1515

16+
constexpr int runDurationSeconds = 5;
17+
1618
try {
1719
// Parse command-line options
1820
cxxopts::Options options(appName, "DotName C++ Application");
@@ -42,35 +44,32 @@ int main(int argc, char **argv) {
4244
.appPrefix = appName};
4345
auto logger = UtilsFactory::createLogger(LoggerType::Console, loggerConfig);
4446

45-
// Log application start
46-
logger->infoStream() << appName << " running...";
47+
#if __cplusplus >= 202002L
48+
// Available in C++20 and later
49+
logger->infoWithLocation(appName + " started ...");
50+
#else
51+
logger->infoStream() << appName << " started ...";
52+
#endif
4753

48-
// Initialize assets
4954
auto assetManager = UtilsFactory::createAssetManager(execPathResult.value(), appName);
50-
5155
if (!assetManager->validate()) {
5256
logger->errorStream() << "Failed to validate assets: " << assetManager->getAssetsPath();
5357
return EXIT_FAILURE;
5458
}
5559

56-
logger->infoStream() << "Assets initialized: " << assetManager->getAssetsPath();
57-
5860
// Initialize library
5961
auto library = std::make_unique<v1::DotNameLib>(logger, assetManager);
62+
if (!library->isInitialized()) {
63+
logger->errorStream() << "Library initialization failed";
64+
return EXIT_FAILURE;
65+
}
6066

61-
#if __cplusplus >= 202002L
62-
// Available in C++20 and later
63-
logger->infoWithLocation("Library initialized successfully");
64-
#else
65-
logger->infoStream() << "Library initialized successfully";
66-
#endif
67-
68-
if (!library->run(5)) {
67+
if (!library->run(runDurationSeconds)) {
6968
logger->errorStream() << "Failed to run library";
7069
return EXIT_FAILURE;
7170
}
7271

73-
logger->infoStream() << appName << " shutting down";
72+
logger->infoStream() << appName << " ... shutting down";
7473
return EXIT_SUCCESS;
7574

7675
} catch (const std::exception &e) {

include/DotNameLib/DotNameLib.hpp

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#pragma once
22

3-
#include <DotNameLib/version.h> // first configuration will create this file
3+
#include <DotNameLib/version.h> // cmake configuration will generate this file
44
#include <Utils/UtilsFactory.hpp>
5+
56
#include <atomic>
6-
#include <filesystem>
77
#include <memory>
88
#include <string>
99

@@ -28,29 +28,15 @@ namespace dotnamecpp::v1 {
2828
~DotNameLib();
2929

3030
/**
31-
* @brief Copy construction is deleted
31+
* @brief Copy and move operations are deleted
32+
*
33+
* This class manages unique resources
34+
* and should not be copied or moved.
3235
*/
3336
DotNameLib(const DotNameLib &other) = delete;
34-
35-
/**
36-
* @brief Copy assignment is deleted
37-
*/
3837
DotNameLib &operator=(const DotNameLib &other) = delete;
39-
40-
/**
41-
* @brief Move constructor
42-
*
43-
* @param other Object to move from
44-
*/
45-
DotNameLib(DotNameLib &&other) noexcept;
46-
47-
/**
48-
* @brief Move assignment operator
49-
*
50-
* @param other Object to move from
51-
* @return Reference to this object
52-
*/
53-
DotNameLib &operator=(DotNameLib &&other) noexcept;
38+
DotNameLib(DotNameLib &&other) = delete;
39+
DotNameLib &operator=(DotNameLib &&other) = delete;
5440

5541
// ============================================================================
5642
// Main API
@@ -80,27 +66,26 @@ namespace dotnamecpp::v1 {
8066
// ============================================================================
8167

8268
/**
83-
* @brief Check if the library is properly initialized
69+
* @brief Check if the library is initialized
8470
*
85-
* @return true if initialized and ready to use
86-
* @return false if initialization failed
71+
* @return true
72+
* @return false
8773
*/
8874
[[nodiscard]]
8975
bool isInitialized() const noexcept;
9076

9177
/**
92-
* @brief Get the assets directory path
78+
* @brief Get the Asset Manager object
9379
*
94-
* @return const std::filesystem::path& Path to assets
80+
* @return const std::shared_ptr<dotnamecpp::assets::IAssetManager>&
9581
*/
9682
[[nodiscard]]
97-
const std::filesystem::path &getAssetsPath() const noexcept;
83+
const std::shared_ptr<dotnamecpp::assets::IAssetManager> &getAssetManager() const noexcept;
9884

9985
private:
10086
const std::string libName_ = "DotNameLib v." DOTNAMELIB_VERSION;
10187
std::shared_ptr<dotnamecpp::logging::ILogger> logger_;
10288
std::shared_ptr<dotnamecpp::assets::IAssetManager> assetManager_;
103-
std::filesystem::path assetsPath_;
10489
bool isInitialized_ = false;
10590
std::atomic<bool> shouldStop_{false};
10691
};

src/DotNameLib.cpp

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ namespace dotnamecpp::v1 {
1414
return;
1515
}
1616

17-
logger_->infoStream() << libName_ << " initialized ...";
17+
// just an example of asset usage
1818
const auto logoPath = assetManager_->resolveAsset("DotNameCppLogo.svg");
19-
if (assetManager_->assetExists("DotNameCppLogo.svg")) {
20-
logger_->debugStream() << "Logo: " << logoPath << " found";
21-
} else {
19+
if (!assetManager_->assetExists("DotNameCppLogo.svg")) {
2220
logger_->warningStream() << "Logo not found: " << logoPath;
2321
}
2422

@@ -29,51 +27,16 @@ namespace dotnamecpp::v1 {
2927
// └──────────────────────────────────────────────────────────────────┘
3028

3129
isInitialized_ = true;
32-
logger_->infoStream() << libName_ << " initialized successfully.";
30+
logger_->infoStream() << libName_ << " initialized ...";
3331
}
3432

3533
DotNameLib::~DotNameLib() {
3634
if (isInitialized_) {
37-
// Ensure graceful shutdown
3835
stop();
39-
logger_->infoStream() << libName_ << " destructed";
36+
logger_->infoStream() << libName_ << " ... destructed";
4037
} else {
41-
logger_->infoStream() << libName_ << " (not initialized) destructed";
42-
}
43-
}
44-
45-
DotNameLib::DotNameLib(DotNameLib &&other) noexcept
46-
: logger_(std::move(other.logger_)), assetManager_(std::move(other.assetManager_)),
47-
assetsPath_(std::move(other.assetsPath_)), isInitialized_(other.isInitialized_),
48-
shouldStop_(other.shouldStop_.load()) {
49-
other.isInitialized_ = false;
50-
other.shouldStop_.store(false);
51-
if (logger_) {
52-
logger_->infoStream() << libName_ << " move constructed";
53-
}
54-
}
55-
56-
DotNameLib &DotNameLib::operator=(DotNameLib &&other) noexcept {
57-
if (this != &other) {
58-
// Stop current instance
59-
if (isInitialized_) {
60-
stop();
61-
}
62-
63-
// Move all members
64-
logger_ = std::move(other.logger_);
65-
assetManager_ = std::move(other.assetManager_);
66-
assetsPath_ = std::move(other.assetsPath_);
67-
isInitialized_ = other.isInitialized_;
68-
shouldStop_.store(other.shouldStop_.load());
69-
70-
other.isInitialized_ = false;
71-
other.shouldStop_.store(false);
72-
if (logger_) {
73-
logger_->infoStream() << libName_ << " move assigned";
74-
}
38+
logger_->infoStream() << libName_ << " ... (not initialized) destructed";
7539
}
76-
return *this;
7740
}
7841

7942
bool DotNameLib::run(int durationSeconds) {
@@ -92,11 +55,9 @@ namespace dotnamecpp::v1 {
9255
// │ Example: Start services, process data, etc. │
9356
// └──────────────────────────────────────────────────────────────────┘
9457

95-
logger_->infoStream() << libName_ << " started successfully";
96-
9758
// Run for specified duration
9859
if (durationSeconds > 0) {
99-
logger_->infoStream() << "Running for " << durationSeconds << " seconds...";
60+
logger_->infoStream() << "MOCK BUSINESS LOGIC running for " << durationSeconds << " seconds ...";
10061
for (int i = 0; i < durationSeconds && !shouldStop_.load(); ++i) {
10162
std::this_thread::sleep_for(std::chrono::seconds(1));
10263
}
@@ -106,7 +67,6 @@ namespace dotnamecpp::v1 {
10667
} else {
10768
logger_->infoStream() << libName_ << " finished after " << durationSeconds << " seconds";
10869
}
109-
stop();
11070
} else {
11171
logger_->infoStream() << "Running indefinitely. Call stop() to terminate.";
11272
constexpr int pollIntervalMs = 100;
@@ -129,7 +89,7 @@ namespace dotnamecpp::v1 {
12989
return;
13090
}
13191

132-
logger_->infoStream() << "Stopping " << libName_ << "...";
92+
logger_->infoStream() << "Stopping " << libName_ << " ...";
13393
shouldStop_.store(true);
13494

13595
// ┌──────────────────────────────────────────────────────────────────┐
@@ -140,6 +100,9 @@ namespace dotnamecpp::v1 {
140100
}
141101

142102
bool DotNameLib::isInitialized() const noexcept { return isInitialized_; }
143-
const std::filesystem::path &DotNameLib::getAssetsPath() const noexcept { return assetsPath_; }
103+
const std::shared_ptr<dotnamecpp::assets::IAssetManager> &
104+
DotNameLib::getAssetManager() const noexcept {
105+
return assetManager_;
106+
}
144107

145108
} // namespace dotnamecpp::v1

0 commit comments

Comments
 (0)