first implementation, badly, of allocator
This commit is contained in:
6
Makefile
6
Makefile
@@ -19,11 +19,13 @@ LIBS =
|
||||
INCLUDES = -I$(D_HEADERS)
|
||||
|
||||
D_SRCS = srcs
|
||||
SRCS = main.cpp
|
||||
SRCS = main.cpp \
|
||||
ftvector.cpp
|
||||
|
||||
D_HEADERS = headers
|
||||
HEADERS = colors.h \
|
||||
tests.h
|
||||
tests.h \
|
||||
ftvector.hpp
|
||||
|
||||
D_OBJS = builds
|
||||
OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o)
|
||||
|
||||
34
headers/ftvector.hpp
Normal file
34
headers/ftvector.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef FTVECTOR_HPP
|
||||
# define FTVECTOR_HPP
|
||||
|
||||
# include "colors.h"
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
# include <memory> // std::allocator
|
||||
|
||||
class ftvector {
|
||||
|
||||
public:
|
||||
|
||||
ftvector();
|
||||
ftvector( ftvector const & src );
|
||||
~ftvector();
|
||||
ftvector & operator=( ftvector const & rhs );
|
||||
|
||||
unsigned int size() const;
|
||||
void push_back(const int & element);
|
||||
|
||||
private:
|
||||
|
||||
unsigned int _size;
|
||||
int * _mem_ptr;
|
||||
std::allocator<int> _allocator;
|
||||
|
||||
// static std::string const ftvector::_bar;
|
||||
|
||||
};
|
||||
|
||||
//std::ostream & operator<<(std::ostream & o, ftvector const & rhs);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
# define TESTS_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
# define TEST(s) \
|
||||
{\
|
||||
|
||||
16
main.cpp
16
main.cpp
@@ -4,9 +4,24 @@
|
||||
#include "tests.h"
|
||||
|
||||
#include <vector>
|
||||
#include "ftvector.hpp"
|
||||
|
||||
int main() {
|
||||
|
||||
TEST(vector::vector (constructor))
|
||||
{
|
||||
ftvector myvector;
|
||||
int myint[] = {12434, -2432, 12, 5345, 23, 0, -4, 387, 8432, -934723, 1};
|
||||
int size = sizeof(myint) / sizeof(myint[0]);
|
||||
|
||||
for (int i = 0; i < size; i++)
|
||||
myvector.push_back (myint[i]);
|
||||
|
||||
std::cout << "myvector stores " << int(myvector.size()) << " numbers.\n";
|
||||
}
|
||||
TESTEND
|
||||
|
||||
/*
|
||||
TEST(vector::vector (constructor))
|
||||
{
|
||||
// constructors used in the same order as described above:
|
||||
@@ -445,6 +460,7 @@ int main() {
|
||||
myvector.get_allocator().deallocate(p,5);
|
||||
}
|
||||
TESTEND
|
||||
*/
|
||||
|
||||
|
||||
// execute tests and print them :
|
||||
|
||||
90
srcs/ftvector.cpp
Normal file
90
srcs/ftvector.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ftvector.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/06/01 15:39:26 by simplonco #+# #+# */
|
||||
/* Updated: 2022/06/01 15:39:57 by simplonco ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ftvector.hpp"
|
||||
|
||||
#define COPLIEN_COLOR B_CYAN
|
||||
|
||||
/*********************************************
|
||||
* CONSTRUCTORS
|
||||
*********************************************/
|
||||
|
||||
ftvector::ftvector() :
|
||||
_size(0) {
|
||||
// std::cout << COPLIEN_COLOR "ftvector constructor" RESET "\n";
|
||||
_allocator = std::allocator<int>();
|
||||
return;
|
||||
}
|
||||
|
||||
ftvector::ftvector( ftvector const & src ) {
|
||||
// std::cout << COPLIEN_COLOR "ftvector copy constructor" RESET "\n";
|
||||
*this = src;
|
||||
return;
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* DESTRUCTORS
|
||||
*********************************************/
|
||||
|
||||
ftvector::~ftvector() {
|
||||
// std::cout << COPLIEN_COLOR "ftvector destructor" RESET "\n";
|
||||
return;
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* OPERATORS
|
||||
*********************************************/
|
||||
|
||||
ftvector & ftvector::operator=( ftvector const & rhs ) {
|
||||
// Base::operator=(rhs);
|
||||
if ( this != &rhs )
|
||||
{
|
||||
_size = rhs.size();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
//std::ostream & operator<<(std::ostream & o, ftvector const & rhs)
|
||||
//{
|
||||
// o << rhs.getFoo();
|
||||
// return (o);
|
||||
//}
|
||||
|
||||
/*********************************************
|
||||
* ACCESSORS
|
||||
*********************************************/
|
||||
|
||||
unsigned int ftvector::size() const {return _size;}
|
||||
|
||||
/*********************************************
|
||||
* PUBLIC MEMBER FUNCTIONS
|
||||
*********************************************/
|
||||
|
||||
void ftvector::push_back(const int & element) {
|
||||
_mem_ptr = _allocator.allocate(1);
|
||||
_allocator.construct(_mem_ptr, element);
|
||||
_size++;
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* NESTED CLASS
|
||||
*********************************************/
|
||||
|
||||
//void ftvector::Class::function() {}
|
||||
|
||||
/*********************************************
|
||||
* STATICS
|
||||
*********************************************/
|
||||
|
||||
//std::string const ftvector::_bar = "bar";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user