Skip to content

Commit b0a58fc

Browse files
authored
Merge pull request #11 from vinitjames/feature/samples
Feature/samples
2 parents 2d21de6 + 5b7394b commit b0a58fc

File tree

6 files changed

+224
-252
lines changed

6 files changed

+224
-252
lines changed

samples/CMakeLists.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
set(CMAKE_CXX_STANDARD 11)
22
set(CMAKE_CXX_STANDARD_REQUIRED ON)
33

4-
add_executable(sample_circularbuffer sample_circularbuffer.cpp)
5-
target_link_libraries(sample_circularbuffer
4+
add_executable(sample_int_buffer sample_int_buffer.cpp)
5+
target_link_libraries(sample_int_buffer
66
PRIVATE
77
circularbuffer
88
)
99

10-
add_executable(sample_stringbuffer sample_stringbuffer.cpp)
11-
target_link_libraries(sample_stringbuffer
10+
add_executable(sample_string_buffer sample_string_buffer.cpp)
11+
target_link_libraries(sample_string_buffer
12+
PRIVATE
13+
circularbuffer
14+
)
15+
16+
add_executable(sample_custom_buffer sample_custom_buffer.cpp)
17+
target_link_libraries(sample_custom_buffer
1218
PRIVATE
1319
circularbuffer
1420
)

samples/sample_circularbuffer.cpp

Lines changed: 0 additions & 141 deletions
This file was deleted.

samples/sample_custom_buffer.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include "circular_buffer.h"
2+
#include <iostream>
3+
#include <string.h>
4+
#include <vector>
5+
#include <thread>
6+
#include <chrono>
7+
8+
struct custom_struct{
9+
static int count ;
10+
char* bytes =nullptr;
11+
int id = 0;
12+
custom_struct(){
13+
bytes = (char*)malloc(100);
14+
id = count;
15+
std::cout<<"constructing custom_struct: "<<count++<<"\n";
16+
}
17+
18+
custom_struct(const custom_struct& other){
19+
bytes = (char*)malloc(100);
20+
memcpy(bytes, other.bytes,100);
21+
std::cout<<"copy constructor called for custom_struct \n";
22+
}
23+
24+
custom_struct(custom_struct&& other){
25+
bytes = other.bytes;
26+
other.bytes = nullptr;
27+
std::cout<<"move constructor called for custom_struct \n";
28+
}
29+
30+
custom_struct& operator=(const custom_struct& other){
31+
memcpy(bytes, other.bytes,100);
32+
std::cout<<"assignment operator called for custom_struct \n";
33+
return *this;
34+
}
35+
36+
custom_struct& operator=(custom_struct&& other){
37+
delete(bytes);
38+
bytes = other.bytes;
39+
other.bytes = nullptr;
40+
std::cout<<"move assignment operator called for custom_struct \n";
41+
return *this;
42+
}
43+
44+
~custom_struct(){
45+
std::cout<<"destructor called for test struct "<<id<<std::endl;
46+
count--;
47+
free(bytes);
48+
bytes = nullptr;
49+
}
50+
51+
friend std::ostream& operator<<(std::ostream& os, const custom_struct& ts){
52+
return os<<"Printing struct no: "<< ts.id<<"\n";
53+
}
54+
55+
56+
};
57+
58+
int custom_struct::count =0;
59+
60+
61+
int main(int argc, char *argv[])
62+
{
63+
CircularBuffer<custom_struct> custom_buffer(5);
64+
custom_struct element;
65+
for (int i = 0; i < 10; ++i) {
66+
custom_buffer.push_back(element);
67+
}
68+
69+
CircularBuffer<custom_struct> buffermoved = std::move(custom_buffer);
70+
CircularBuffer<custom_struct> buffermoveassigned{10};
71+
buffermoveassigned = std::move(buffermoved);
72+
buffermoveassigned.push_back(std::move(element));
73+
return 0;
74+
}
75+

samples/sample_int_buffer.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include "circular_buffer.h"
2+
#include <iostream>
3+
#include <string.h>
4+
5+
int main(int argc, char *argv[])
6+
{
7+
//Initializing a buffer
8+
CircularBuffer<int> int_buff{5};
9+
10+
//checking buffer empty function
11+
std::cout<<"Calling buffer empty function "<<int_buff.empty()<<"\n";
12+
13+
//Pushing data back into buffer
14+
std::cout<<"Pushing back data in bufffer \n";
15+
int_buff.push_back(5);
16+
int_buff.push_back(2);
17+
int_buff.push_back(1250);
18+
int_buff.push_back(500);
19+
int_buff.push_back(235);
20+
21+
//Length of buffer
22+
std::cout<<"Checking length of buffer "<<int_buff.size()<<"\n";
23+
//Using Front Function, gives the first element
24+
std::cout<<"Checking front function "<<int_buff.front()<<"\n";
25+
//Using Back Function, gives last element
26+
std::cout<<"Checking back function "<<int_buff.back()<<"\n";
27+
//Front and Back can be used for assignment
28+
int_buff.front() = 100;
29+
int_buff.back() = 10;
30+
31+
//Pop Function removes element as per FIFO
32+
std::cout<<"Checking pop_front function\n";
33+
int_buff.pop_front();
34+
std::cout<<"pop front executed new size: "<<int_buff.size()
35+
<<" new front:" <<int_buff.front()<<"\n";
36+
37+
std::cout<<"Checking iterator function\n";
38+
auto it = int_buff.begin();
39+
40+
std::cout<<"Checking deference * operator "<<*it<<"\n";
41+
std::cout<<"Checking deference ++ operator "<<*(++it)<<"\n";
42+
std::cout<<"Checking deference -- operator "<<*(--it)<<"\n";
43+
44+
// Range based for loop can be used like other STL containers
45+
std::cout<<"Checking iterator for loop \n";
46+
for(const auto& it: int_buff)
47+
std::cout<<"Checking range based for loop function "<<it<<"\n";
48+
49+
// for loop with iterators can be used like other STL stile containers
50+
std::cout<<"Checking for loop with iterator \n";
51+
for(auto it = int_buff.begin(); it != int_buff.end(); it++)
52+
std::cout<<"Checking for loop function "<<*it<<"\n";
53+
54+
return 0;
55+
}
56+
57+
58+

samples/sample_string_buffer.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include "circular_buffer.h"
2+
#include <iostream>
3+
#include <string.h>
4+
#include "circular_buffer.h"
5+
#include <iostream>
6+
#include <string.h>
7+
#include <utility>
8+
9+
10+
int main(int argc, char *argv[])
11+
{
12+
13+
CircularBuffer<std::string> string_buff{10};
14+
//checking buffer empty function
15+
std::cout<<"Calling buffer empty function "<<string_buff.empty()<<"\n";
16+
17+
//Pushing data back into buffer
18+
std::cout<<"Pushing back string data in bufffer \n";
19+
for(int i = 0; i<10;i++){
20+
string_buff.push_back(" sample string " + std::to_string(i));
21+
std::cout<<"Pushing back string "<<i<<std::endl;
22+
}
23+
24+
//Length of buffer
25+
std::cout<<"Checking length of buffer "<<string_buff.size()<<"\n";
26+
//Using Front Function, gives the first element
27+
std::cout<<"Checking front function "<<string_buff.front()<<"\n";
28+
//Using Back Function, gives last element
29+
std::cout<<"Checking back function "<<string_buff.back()<<"\n";
30+
//Front and Back can be used for assignment
31+
string_buff.front() = "modified front string";
32+
string_buff.back() = "modified back string";
33+
34+
//Pop Function removes element as per FIFO
35+
std::cout<<"Checking pop_front function\n";
36+
string_buff.pop_front();
37+
std::cout<<"pop front executed new size: "<<string_buff.size()
38+
<<" new front:" <<string_buff.front()<<"\n";
39+
40+
std::cout<<"Checking iterator function\n";
41+
auto it = string_buff.begin();
42+
43+
std::cout<<"Checking deference * operator "<<*it<<"\n";
44+
std::cout<<"Checking deference ++ operator "<<*(++it)<<"\n";
45+
std::cout<<"Checking deference -- operator "<<*(--it)<<"\n";
46+
47+
// Range based for loop can be used like other STL containers
48+
std::cout<<"Checking iterator for loop \n";
49+
for(auto& it: string_buff)
50+
std::cout<<"Checking range based for loop function "<<it<<"\n";
51+
52+
// for loop with iterators can be used like other STL stile containers
53+
std::cout<<"Checking for loop with iterator \n";
54+
for(auto it = string_buff.begin(); it != string_buff.end(); it++)
55+
std::cout<<"Checking for loop function "<<*it<<"\n";
56+
57+
// push_back using move semantics
58+
std::string string_to_move{"This string is moved"};
59+
string_buff.push_back(std::move(string_to_move));
60+
std::cout<<"string moved to buffer new size: "<<string_buff.size()
61+
<<" new back:" <<string_buff.back()<<"\n";
62+
63+
//creating buffer from another buffer
64+
CircularBuffer<std::string> string_buff_copy1{string_buff};
65+
std::cout<<"Buffer Copy Created, copied buffer size "<<string_buff_copy1.size()<<"\n";
66+
67+
//copying buffer
68+
CircularBuffer<std::string> string_buff_copy2{10};
69+
string_buff_copy2 = string_buff;
70+
std::cout<<"Buffer Copy assigned , copied buffer size "<<string_buff_copy2.size()<<"\n";
71+
72+
//move constructing
73+
CircularBuffer<std::string> string_buff_move1{std::move(string_buff)};
74+
std::cout<<"Buffer created with move constructor, moved buffer size "<<string_buff_move1.size()<<"\n";
75+
76+
//move assignment
77+
CircularBuffer<std::string> string_buff_move2{10};
78+
string_buff_move2 = std::move(string_buff_move1);
79+
std::cout<<"Buffer moved with assignment operator, moved buffer size "<<string_buff_move2.size()<<"\n";
80+
81+
}

0 commit comments

Comments
 (0)