1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
#ifndef ALA_TALE_H
#define ALA_TALE_H
#include<cstddef>
#include<cstring>
#include<cstdlib>
#include<algorithm>
template<typename unkwn,unkwn v>
struct defv{
unkwn value;
defv():value(v){};
defv(unkwn p):value(p){};
};
class tale{
public:
//properties
size_t _length;
char* _buffer;
defv<bool,false> _tmp;
//method
const char*gets(){
return(const char*)_buffer;
};
const size_t length(){
return(const size_t)_length;
};
const bool empty(){
return _length==0;
};
//operator
tale&operator+(const char item){//here I create temporary "tale" class
tale*res=new tale[1];
*res+=*this;
*res+=item;
res->_tmp.value=true;
if(_tmp.value){
delete&item;
};
//std::swap(this->_buffer,res->_buffer);
//std::swap(this->_buffer,res->_buffer);
return*res;
};
tale&operator+=(tale&item){//here I add and trying to remove temporary "tale" class
const size_t len=item.length();
if(len==0)return*this;
_buffer=(char*)realloc(_buffer,_length+len+1);
memcpy(&_buffer[_length],item.gets(),len);
_buffer[_length+=len]=0;
if(item._tmp.value){
delete[]item._buffer;
delete&item;
};
return*this;
};
tale&operator+=(const char*item){
const size_t len=strlen(item);
if(len==0)return*this;
_buffer=(char*)realloc(_buffer,_length+len+1);
memcpy(&_buffer[_length],item,len);
_buffer[_length+=len]=0;
return*this;
};
tale&operator+=(const char item){
if(item==0)return*this;
_buffer=(char*)realloc(_buffer,_length+2);
_buffer[_length]=item;
_buffer[_length+=1]=0;
return*this;
};
tale&operator=(tale&item){//here I add and trying to remove temporary "tale" class
delete[]_buffer;
_length=item.length();
_buffer=new char[_length+1];
_buffer[_length]=0;
memcpy(_buffer,item.gets(),_length);
if(item._tmp.value){
delete[]item._buffer;
delete&item;
};
return*this;
};
tale&operator=(const char*item){
delete[]_buffer;
_length=strlen(item);
_buffer=new char[_length+1];
_buffer[_length]=0;
memcpy(_buffer,item,_length);
return*this;
};
tale&operator=(const char item){
delete[]_buffer;
_length=1;
_buffer=new char[_length+1];
*_buffer=item;
_buffer[_length]=0;
return*this;
};
//constructor
tale(const tale&item){
_length=item._length;
_buffer=new char[_length+1];
_buffer[_length]=0;
memcpy(_buffer,item._buffer,_length);
};
tale(const char*item){
_length=strlen(item);
_buffer=new char[_length+1];
_buffer[_length]=0;
memcpy(_buffer,item,_length);
};
tale(const char item){
_length=1;
_buffer=new char[_length+1];
*_buffer=item;
_buffer[_length]=0;
};
tale(){
_length=0;
_buffer=new char[_length+1];
};
//destructor
~tale(){
delete[]_buffer;
};
};
#endif
|