-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom queue example, more readme
- Loading branch information
Showing
5 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
add_subdirectory(everlog) | ||
add_subdirectory(custom_queue_example) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
if (NOT TARGET farbot) | ||
include(FetchContent) | ||
|
||
FetchContent_Declare(farbot | ||
GIT_REPOSITORY https://github.com/hogliux/farbot | ||
GIT_TAG 0416705394720c12f0d02e55c144e4f69bb06912 | ||
) | ||
# Note we do not "MakeAvailable" here, because farbot does not fully work via FetchContent | ||
if(NOT farbot_POPULATED) | ||
FetchContent_Populate(farbot) | ||
endif() | ||
add_library(farbot INTERFACE) | ||
add_library(farbot::farbot ALIAS farbot) | ||
|
||
target_include_directories(farbot INTERFACE | ||
$<BUILD_INTERFACE:${farbot_SOURCE_DIR}/include> | ||
$<INSTALL_INTERFACE:include> | ||
) | ||
endif() | ||
|
||
add_executable(custom_queue_example | ||
customqueuemain.cpp | ||
) | ||
|
||
target_link_libraries(custom_queue_example | ||
PRIVATE | ||
rtlog::rtlog | ||
farbot::farbot | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <farbot/fifo.hpp> | ||
#include <rtlog/rtlog.h> | ||
|
||
template <typename T> class FarbotMPSCQueueWrapper { | ||
farbot::fifo<T, | ||
farbot::fifo_options::concurrency::single, // Consumer | ||
farbot::fifo_options::concurrency::multiple, // Producer | ||
farbot::fifo_options::full_empty_failure_mode:: | ||
return_false_on_full_or_empty, // consumer_failure_mode | ||
farbot::fifo_options::full_empty_failure_mode:: | ||
overwrite_or_return_default> // producer_failure_mode | ||
|
||
mQueue; | ||
|
||
public: | ||
using value_type = T; | ||
|
||
FarbotMPSCQueueWrapper(int capacity) : mQueue(capacity) {} | ||
|
||
bool try_enqueue(T &&item) { return mQueue.push(std::move(item)); } | ||
bool try_dequeue(T &item) { return mQueue.pop(item); } | ||
}; | ||
|
||
struct LogData {}; | ||
|
||
std::atomic<size_t> gSequenceNumber{0}; | ||
|
||
int main() { | ||
rtlog::Logger<LogData, 128, 128, gSequenceNumber, FarbotMPSCQueueWrapper> | ||
logger; | ||
logger.Log({}, "Hello, World!"); | ||
|
||
logger.PrintAndClearLogQueue( | ||
[](const LogData &data, size_t sequenceNumber, const char *fstring, ...) { | ||
(void)data; | ||
std::array<char, 128> buffer; | ||
|
||
va_list args; | ||
va_start(args, fstring); | ||
vsnprintf(buffer.data(), buffer.size(), fstring, args); | ||
va_end(args); | ||
|
||
printf("{%zu} %s\n", sequenceNumber, buffer.data()); | ||
}); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters