Skip to content

Commit

Permalink
Use static function and member to improve performances
Browse files Browse the repository at this point in the history
  • Loading branch information
Teskann committed May 30, 2022
1 parent 5fd845e commit 760f6c1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ your CMakeLists.txt file looks like:

```cmake
project("myapp")
add_executable(${PROJECT_NAME} myapp.cpp)
CPMAddPackage("gh:Teskann/blnscpp#master") # Add this after add_executable / add_library, ...
# Add BLNS cmake package
CPMAddPackage("gh:Teskann/blnscpp#master")
add_executable(${PROJECT_NAME} myapp.cpp)
target_include_directories(${PROJECT_NAME} PRIVATE ${blnscpp_SOURCE_DIR}/include) # Add include path
target_link_libraries(${PROJECT_NAME} PRIVATE lib_blns) # Link the blns library
Expand All @@ -41,8 +42,8 @@ With this setup, you can use blns like so in `myapp.cpp`:
#include <iostream>

int main() {
auto blns = blns::Blns();
for (auto const &ns : blns.getStrings()) {
for (auto const &ns : blns::Blns::getStrings())
{
std::cout << ns << std::endl;
}
}
Expand Down
3 changes: 1 addition & 2 deletions examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#include <iostream>

int main() {
auto blns = blns::Blns{};
for (auto const &ns : blns.getStrings())
for (auto const &ns : blns::Blns::getStrings())
{
std::cout << ns << std::endl;
}
Expand Down
9 changes: 4 additions & 5 deletions include/blns/blns.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ namespace blns
class Blns
{
public:
Blns();

~Blns() = default;

/*
* Returns the list of Naughty Strings
* @return list of naugthy strings
*/
[[nodiscard]] std::vector<std::string> const &getStrings() const;
[[nodiscard]] static std::vector<std::string> const &getStrings() ;

private:
std::vector<std::string> blns;
inline static std::vector<std::string> blns;

static void readFile();
};

}
37 changes: 20 additions & 17 deletions src/blns/blns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@
#include <algorithm>
#include <string>

blns::Blns::Blns() {
std::ifstream blnsFile = std::filesystem::path(__FILE__).parent_path()
/ ".." / ".." / "blns.txt";
std::vector<std::string> const &blns::Blns::getStrings() {
if(blns.empty()) {
readFile();
}
return blns;
}

// If this assertion fails, ensure that you have correctly
// downloaded blns.txt at the root of the project during cmake build
assert(blnsFile.is_open());
void blns::Blns::readFile() {
std::ifstream blnsFile = std::filesystem::path(__FILE__).parent_path()
/ ".." / ".." / "blns.txt";

std::string line;
while(getline(blnsFile, line)) {
// Skip comments in txt file
if(line.rfind('#', 0) != 0) {
blns.push_back(line);
}
}
assert(!blns.empty());
}
// If this assertion fails, ensure that you have correctly
// downloaded blns.txt at the root of the project during cmake build
assert(blnsFile.is_open());

std::vector<std::string> const &blns::Blns::getStrings() const {
return blns;
std::string line;
while(getline(blnsFile, line)) {
// Skip comments in txt file
if(line.rfind('#', 0) != 0) {
blns.push_back(line);
}
}
assert(!blns.empty());
}

0 comments on commit 760f6c1

Please sign in to comment.