Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(sample_circularbuffer sample_circularbuffer.cpp)
target_link_libraries(sample_circularbuffer
add_executable(sample_int_buffer sample_int_buffer.cpp)
target_link_libraries(sample_int_buffer
PRIVATE
circularbuffer
)

add_executable(sample_stringbuffer sample_stringbuffer.cpp)
target_link_libraries(sample_stringbuffer
add_executable(sample_string_buffer sample_string_buffer.cpp)
target_link_libraries(sample_string_buffer
PRIVATE
circularbuffer
)

add_executable(sample_custom_buffer sample_custom_buffer.cpp)
target_link_libraries(sample_custom_buffer
PRIVATE
circularbuffer
)
141 changes: 0 additions & 141 deletions samples/sample_circularbuffer.cpp

This file was deleted.

75 changes: 75 additions & 0 deletions samples/sample_custom_buffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "circular_buffer.h"
#include <iostream>
#include <string.h>
#include <vector>
#include <thread>
#include <chrono>

struct custom_struct{
static int count ;
char* bytes =nullptr;
int id = 0;
custom_struct(){
bytes = (char*)malloc(100);
id = count;
std::cout<<"constructing custom_struct: "<<count++<<"\n";
}

custom_struct(const custom_struct& other){
bytes = (char*)malloc(100);
memcpy(bytes, other.bytes,100);
std::cout<<"copy constructor called for custom_struct \n";
}

custom_struct(custom_struct&& other){
bytes = other.bytes;
other.bytes = nullptr;
std::cout<<"move constructor called for custom_struct \n";
}

custom_struct& operator=(const custom_struct& other){
memcpy(bytes, other.bytes,100);
std::cout<<"assignment operator called for custom_struct \n";
return *this;
}

custom_struct& operator=(custom_struct&& other){
delete(bytes);
bytes = other.bytes;
other.bytes = nullptr;
std::cout<<"move assignment operator called for custom_struct \n";
return *this;
}

~custom_struct(){
std::cout<<"destructor called for test struct "<<id<<std::endl;
count--;
free(bytes);
bytes = nullptr;
}

friend std::ostream& operator<<(std::ostream& os, const custom_struct& ts){
return os<<"Printing struct no: "<< ts.id<<"\n";
}


};

int custom_struct::count =0;


int main(int argc, char *argv[])
{
CircularBuffer<custom_struct> custom_buffer(5);
custom_struct element;
for (int i = 0; i < 10; ++i) {
custom_buffer.push_back(element);
}

CircularBuffer<custom_struct> buffermoved = std::move(custom_buffer);
CircularBuffer<custom_struct> buffermoveassigned{10};
buffermoveassigned = std::move(buffermoved);
buffermoveassigned.push_back(std::move(element));
return 0;
}

58 changes: 58 additions & 0 deletions samples/sample_int_buffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "circular_buffer.h"
#include <iostream>
#include <string.h>

int main(int argc, char *argv[])
{
//Initializing a buffer
CircularBuffer<int> int_buff{5};

//checking buffer empty function
std::cout<<"Calling buffer empty function "<<int_buff.empty()<<"\n";

//Pushing data back into buffer
std::cout<<"Pushing back data in bufffer \n";
int_buff.push_back(5);
int_buff.push_back(2);
int_buff.push_back(1250);
int_buff.push_back(500);
int_buff.push_back(235);

//Length of buffer
std::cout<<"Checking length of buffer "<<int_buff.size()<<"\n";
//Using Front Function, gives the first element
std::cout<<"Checking front function "<<int_buff.front()<<"\n";
//Using Back Function, gives last element
std::cout<<"Checking back function "<<int_buff.back()<<"\n";
//Front and Back can be used for assignment
int_buff.front() = 100;
int_buff.back() = 10;

//Pop Function removes element as per FIFO
std::cout<<"Checking pop_front function\n";
int_buff.pop_front();
std::cout<<"pop front executed new size: "<<int_buff.size()
<<" new front:" <<int_buff.front()<<"\n";

std::cout<<"Checking iterator function\n";
auto it = int_buff.begin();

std::cout<<"Checking deference * operator "<<*it<<"\n";
std::cout<<"Checking deference ++ operator "<<*(++it)<<"\n";
std::cout<<"Checking deference -- operator "<<*(--it)<<"\n";

// Range based for loop can be used like other STL containers
std::cout<<"Checking iterator for loop \n";
for(const auto& it: int_buff)
std::cout<<"Checking range based for loop function "<<it<<"\n";

// for loop with iterators can be used like other STL stile containers
std::cout<<"Checking for loop with iterator \n";
for(auto it = int_buff.begin(); it != int_buff.end(); it++)
std::cout<<"Checking for loop function "<<*it<<"\n";

return 0;
}



81 changes: 81 additions & 0 deletions samples/sample_string_buffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "circular_buffer.h"
#include <iostream>
#include <string.h>
#include "circular_buffer.h"
#include <iostream>
#include <string.h>
#include <utility>


int main(int argc, char *argv[])
{

CircularBuffer<std::string> string_buff{10};
//checking buffer empty function
std::cout<<"Calling buffer empty function "<<string_buff.empty()<<"\n";

//Pushing data back into buffer
std::cout<<"Pushing back string data in bufffer \n";
for(int i = 0; i<10;i++){
string_buff.push_back(" sample string " + std::to_string(i));
std::cout<<"Pushing back string "<<i<<std::endl;
}

//Length of buffer
std::cout<<"Checking length of buffer "<<string_buff.size()<<"\n";
//Using Front Function, gives the first element
std::cout<<"Checking front function "<<string_buff.front()<<"\n";
//Using Back Function, gives last element
std::cout<<"Checking back function "<<string_buff.back()<<"\n";
//Front and Back can be used for assignment
string_buff.front() = "modified front string";
string_buff.back() = "modified back string";

//Pop Function removes element as per FIFO
std::cout<<"Checking pop_front function\n";
string_buff.pop_front();
std::cout<<"pop front executed new size: "<<string_buff.size()
<<" new front:" <<string_buff.front()<<"\n";

std::cout<<"Checking iterator function\n";
auto it = string_buff.begin();

std::cout<<"Checking deference * operator "<<*it<<"\n";
std::cout<<"Checking deference ++ operator "<<*(++it)<<"\n";
std::cout<<"Checking deference -- operator "<<*(--it)<<"\n";

// Range based for loop can be used like other STL containers
std::cout<<"Checking iterator for loop \n";
for(auto& it: string_buff)
std::cout<<"Checking range based for loop function "<<it<<"\n";

// for loop with iterators can be used like other STL stile containers
std::cout<<"Checking for loop with iterator \n";
for(auto it = string_buff.begin(); it != string_buff.end(); it++)
std::cout<<"Checking for loop function "<<*it<<"\n";

// push_back using move semantics
std::string string_to_move{"This string is moved"};
string_buff.push_back(std::move(string_to_move));
std::cout<<"string moved to buffer new size: "<<string_buff.size()
<<" new back:" <<string_buff.back()<<"\n";

//creating buffer from another buffer
CircularBuffer<std::string> string_buff_copy1{string_buff};
std::cout<<"Buffer Copy Created, copied buffer size "<<string_buff_copy1.size()<<"\n";

//copying buffer
CircularBuffer<std::string> string_buff_copy2{10};
string_buff_copy2 = string_buff;
std::cout<<"Buffer Copy assigned , copied buffer size "<<string_buff_copy2.size()<<"\n";

//move constructing
CircularBuffer<std::string> string_buff_move1{std::move(string_buff)};
std::cout<<"Buffer created with move constructor, moved buffer size "<<string_buff_move1.size()<<"\n";

//move assignment
CircularBuffer<std::string> string_buff_move2{10};
string_buff_move2 = std::move(string_buff_move1);
std::cout<<"Buffer moved with assignment operator, moved buffer size "<<string_buff_move2.size()<<"\n";

}
Loading