C++ Integer Queue - a small, dependency-free fixed-capacity FIFO queue of int, implemented as a ring buffer.
Features · Prerequisites · Quick start · API · Build · How to use · Limitations · Download
- Clean, minimal codebase
- Documented
- RAII
- Cross platform
- Dependecy free
- Cmake 3.19+
- A compiler that supports C++17/C++20 (g++ | clang++ | msvc)
Click to expand
#include "IntQueue.hpp"
#include <iostream>
void log(IntQueue &q) {
bool empty ; // Logging
std::cout << "Capacity: " << q.getQueueStoredSize() << std::endl ;
std::cout << "First Item: " << q.getFirstQueueItem(empty) << std::endl ;
if (empty) {
std::cout << "Container is empty" << std::endl ;
}
std::cout << "Last Item: " << q.getLastQueueItem(empty) << std::endl ;
if (empty) {
std::cout << "Container is empty" << std::endl ;
}
}
int main(int argc, const char** argv) {
IntQueue q(5) ;
std::cout << q.getQueueSize() << std::endl ;
// Initializing Container and checking for errors
if (!q.init()) {
std::cout << "Error: " << q.getError() << std::endl ;
return 1 ;
}
std::cout << "Queue Minimum Size Limit: " << q.getMinQueueSize() << std::endl ;
std::cout << "Queue Maximum Size Limit: " << q.getMaxQueueSize() << std::endl ;
for (int i = 10; !q.isFull(); i *= 2) {
// Adding Item to Container
if (!q.addQ(i)) {
std::cout << "Error: " << q.getError() << std::endl ;
}
log(q) ;
}
// Trying to add another item
if (!q.addQ(200)) {
std::cout << "Error: " << q.getError() << std::endl ;
}
// Removing Stored Integers
while (!q.isEmpty()) {
int item ;
if (!q.removeQ(item)) {
std::cout << "Error: " << q.getError() << std::endl ;
}
std::cout << "Removed Item: " << item << std::endl ;
}
// Trying to Remove another item
if (!q.removeQ()) {
std::cout << "Error: " << q.getError() << std::endl ;
}
// Resizing the Container
if (!q.setQueueSize(100)) {
std::cout << "Error: 1" << q.getError() << std::endl ;
}
std::cout << q.getQueueSize() << std::endl ;
return 0 ;
}| Function Name | Description |
|---|---|
bool init() |
Initializes necessary variable including Queue container and heap allocation, returns true on success or failure on false |
bool addQ(int item) |
Adds Integer variable to the container, returns true in success or false if it the container is full |
bool removeQ(int &item) |
Remove the first item in the container and returns the removed item, returns true in success or false if the container is empty |
bool removeQ() |
same as bool removeQ(int &item) but it won't return the removed item |
bool setQueueSize(int size) |
Set how much the container can hold before it gets full, IntQueue::init() should be called after for initializing container capacity, returns true on success or false on failure |
bool isFull() const |
A function to check whether the container is full or not, returns true if it's full or false if there's still room |
bool isEmpty() const |
A function to check whether the container is Empty or not, returns true if it's empty or false if there's still room |
void getError(const char *&info) const |
Points to a fixed buffer size in the container for debugging why previous call failed |
const char *getError() const |
Same as void getError(const char *&info) const but instead it returns a pointer to the error buffer |
int getMaxQueueSize() const |
Returns the Maximum allowed size in the container |
int getMinQueueSize() const |
Returns the Minimum allowed size in the container |
- first make sure you have Prerequisites
- Clone or download the source project
- Open terminal and type this (root project directory) :
cmake -S . -B build -DCMAKE_BUILD_TYPE=Releasecmake --build build- if you wish to have static library, add
-DBUILD_SHARED_LIBS=OFF - install it using
cmake --install buildor also add--prefix="C:/Your/path"
Download or Build the library
in your cmakelists.txt:
find_package(iqueue 1.0.0 CONFIG REQUIRED)
# Linking to your program
target_link_libraries(your_program PRIVATE iqueue::iqueue)
IntQueue container have fixed size capacity, which makes you call initialization after you change the capacity
alternatively you can download the compiled library and use it :
Windows 7+ SP1 64 Bit:
Note
Pre-Compiled versions of library in the release are not Code-Signed, which the library might be Flagged by Anti Virus, if that happen, exclude the folder or file in the settings