class DataStorage_t
{
public:
DataStorage_t();//create an object of deafault size
DataStorage_t(int _capacity);//create the object with user define size
//beware if << failes a I'll throw ALLOCATION_FAILED
virtual DataStorage_t& operator << ( const int _data2ins );//insert to storage data
//beware if << failes a I'll throw ALLOCATION_FAILED
virtual DataStorage_t& operator >> ( int _data2get );//extract from srorage data
//m_position is changes, thus it is not a const function
//beware if >> failes a I'll throw OUT_OF_RANGE
//PUBLIC FUNCTIONS
int Getpossition()const { return m_possition; } //get the position to r/w from
void Setpossition(int val) { m_possition = val; } //set the position to r/w from
int Getcapacity()const { return m_capacity; } //get the amount of data object can hold
int GetactualSize()const { return m_actualSize; } // get the amount of data writen on object
virtual bool XpndValue( int _bytes2add ); //add more memory to the buffer return false on failure
protected:
private:
int m_possition; //the plase the user want to read or write from
int m_capacity; //the amount of storage that class provides
int m_actualSize;//the amount of data writen on the object in byte units
Byte_t* m_buffer; //vector of bytes to store the data
//private function
void InitializeDataStorage(int _capacity);//called only from CTOR and allocate data for the buffer;
};
#endif // DATASTORAGE_T_H
cpp file
#include "string.h"
#include "DataStorage_t.h"
const int defaultBlockSize = 64;
//default growth size of the page I use it also for the deafult size
//defualt CTOR with deafult size
DataStorage_t::DataStorage_t():
m_possition(0),
m_actualSize(0),
m_capacity(defaultBlockSize),
m_buffer(0)//this is a pointer set to NULL for allocation check
{
InitializeDataStorage(defaultBlockSize);
}
//CTOR with user provide size
DataStorage_t::DataStorage_t(int _capacity):
m_possition(0),
m_actualSize(0),
m_capacity(_capacity),
m_buffer(0)
{
InitializeDataStorage(_capacity);
}
DataStorage_t::~DataStorage_t()
{
delete[] m_buffer;
}
//write data into the data storage
DataStorage_t& DataStorage_t::operator <<( const int _data2ins )
{
// 1. varify that there is enough space
if( m_capacity - m_possition < sizeof( _data2ins ))
//need allocation
{
if(!XpndValue(sizeof( _data2ins )))//allcation failed
{
throw ALLOCTION_FAILED;
}
}
//2. Write the data on the buffer
memcpy ( m_buffer + m_possition , &_data2ins , sizeof(_data2ins) );
//4. Update the actuall size if m_position passed it
if( m_possition > m_actualSize )
{
m_actualSize = m_possition;
}
//5. return the current object for further writing
return *this;
}
DataStorage_t& DataStorage_t::operator >>( int _data2get )
{
//1.check that the m_possition is not over the actuall size
// ... or that the data rquested is not bigger then the m_actualSize - m_possition
// ... the later is sufficient
if( m_possition + sizeof(_data2get) > m_actualSize )
{
throw OUT_OF_RANGE;
}
//2. copying the data into _data2ins
memcpy ( &_data2get , m_buffer + m_possition , sizeof(_data2get) );
//private functions
//allocate given capacity cells into the bit vector this function is called only from CTOR
void DataStorage_t::InitializeDataStorage(int _capacity)
{
m_buffer = new Byte_t[_capacity];
if(!m_buffer)// allocate initial capacity and check for allocation status
{
throw ALLOCTION_FAILED;
}
}
this is the main
#include <iostream>
#include "DataStorage_t.h"
using namespace std;
int main()
{
DataStorage_t data();
int n=4;
data<< n ;
int num;
data >> num;
cout << num;
return 0;
}
compiler
/home/student/DataStorage/main.cpp||In function ‘int main()’:|
/home/student/DataStorage/main.cpp|11|error: invalid operands of types ‘DataStorage_t()’ and ‘int’ to binary ‘operator<<’|
/home/student/DataStorage/main.cpp|13|error: invalid operands of types ‘DataStorage_t()’ and ‘int’ to binary ‘operator>>’|
||=== Build finished: 2 errors, 0 warnings ===|