This library provides a template-based implementation of a circular queue in C++. The circular_queue class efficiently manages fixed-size collections of any data type and supports features like enqueue, dequeue, peek, and logging with callbacks.
- Generic Template: Supports any data type.
- Fixed Size: Size defined during construction.
- Efficient Memory Management: Implements a circular buffer to reuse space.
- Callback Support: Register optional logger functions for custom event handling.
- Utility Methods: Enqueue, dequeue, peek, clear, and display functionality.
Ensure you have the following:
- C++17 or later.
- A compatible build tool (e.g., GCC, Clang, or MSVC).
-
Clone the repository:
git clone https://github.com/GtechGovind/CQueue.git cd CQueue -
Build the project using CMake:
mkdir build cd build cmake .. make
To include this library in your project:
- Add the
circular_queueheader file to your project. - Include it in your code:
#include "circular_queue.h"
- Link the project if you are using the build from this repository.
If you want to include this project using CMake FetchContent, add the following to your CMakeLists.txt:
include(FetchContent)
FetchContent_Declare(
circular_queue
GIT_REPOSITORY https://github.com/GtechGovind/CQueue.git
GIT_TAG main # or a specific tag
)
FetchContent_MakeAvailable(circular_queue)
add_executable(your_project main.cpp)
target_link_libraries(your_project PRIVATE circular_queue)- Copy the
circular_queue.hfile into your project directory. - Add the include path to your compiler flags, if necessary.
- Include the header file in your code using:
#include "circular_queue.h"
template <typename T>
class circular_queue;explicit circular_queue(int n, std::function<void(const std::string&)> callback = nullptr);- Parameters:
n: Maximum size of the queue.callback: Optional logger for events like enqueue or dequeue.
~circular_queue();- Automatically deallocates memory used by the queue.
bool is_full() const;- Returns:
trueif the queue is full, otherwisefalse.
bool is_empty() const;- Returns:
trueif the queue is empty, otherwisefalse.
void enqueue(T value);- Parameters:
value: The element to add to the queue.
- Throws: Exception if the queue is full.
T dequeue();- Returns: The front element of the queue.
- Throws: Exception if the queue is empty.
T peek();- Returns: The front element without removing it.
- Throws: Exception if the queue is empty.
void display();- Prints the current elements in the queue.
- Note: This function only works if a logger is assigned during construction.
void clear();- Resets the queue by clearing all elements.
Here is a simple example demonstrating the usage of circular_queue:
#include "circular_queue.h"
#include <iostream>
void log_event(const std::string& message) {
std::cout << "[LOG]: " << message << std::endl;
}
int main() {
circular_queue<int> queue(5, log_event);
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.display();
std::cout << "Dequeued: " << queue.dequeue() << std::endl;
queue.display();
std::cout << "Peek: " << queue.peek() << std::endl;
queue.clear();
queue.display();
return 0;
}Output:
[LOG]: Enqueued 10
[LOG]: Enqueued 20
[LOG]: Enqueued 30
Queue: 10 20 30
[LOG]: Dequeued 10
Dequeued: 10
Queue: 20 30
Peek: 20
[LOG]: Queue cleared
Queue:
- Ensure you have Google Test installed.
- Build and run the tests:
mkdir build && cd build cmake -DBUILD_TESTING=ON .. make ctest
- Fork the repository.
- Create a new branch (
git checkout -b feature/YourFeature). - Commit your changes (
git commit -m 'Add some feature'). - Push to the branch (
git push origin feature/YourFeature). - Open a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
- Template-based design inspired by generic programming principles in C++.
- Special thanks to the open-source community for contributions and feedback.