trying to deal with c++

This commit is contained in:
Timerix22 2022-05-31 14:46:59 +03:00
parent 3cb3dca293
commit 85293d8c32
6 changed files with 64 additions and 19 deletions

View File

@ -12,7 +12,7 @@
<inspection_tool class="EmptyDeclOrStmt" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="GrazieInspection" enabled="false" level="TYPO" enabled_by_default="false" />
<inspection_tool class="LanguageDetectionInspection" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="Misra" enabled="true" level="WARNING" enabled_by_default="true">
<inspection_tool class="Misra" enabled="false" level="WARNING" enabled_by_default="false">
<option name="myMisraCPPChecks" value="clion-misra-cpp2008-*,-clion-misra-cpp2008-11-0-1" />
</inspection_tool>
<inspection_tool class="OCUnusedGlobalDeclaration" enabled="false" level="WARNING" enabled_by_default="false" />

View File

@ -1,40 +1,62 @@
#include "Autoarr2.hpp"
//
//
//
//
//
//
#define MAX_BLOCK_LENGTH_DEFAULT 64
#define ALLOC_N_BLOCKS 1
template<typename T>
Autoarr2<T>::Autoarr2() {
values=NULL;
block_length=0;
blocks_count=0;
max_block_length=MAX_BLOCK_LENGTH_DEFAULT;
}
template<typename T>
T Autoarr2<T>::Get(uint32 index) {
template<typename T>
Autoarr2<T>::Autoarr2(uint16 _max_block_length) : Autoarr2() {
max_block_length=_max_block_length;
}
template<typename T>
T *Autoarr2<T>::GetPtr(uint32 index) {
if(index>=Length) throwcpp(ERR_WRONGINDEX);
return values[index/max_block_length]+index%max_block_length;
}
template<typename T>
T Autoarr2<T>::Get(uint32 index) {
return *GetPtr(index);
}
template<typename T>
void Autoarr2<T>::Set(uint32 index, T value) {
*GetPtr(index)=value;
}
template<typename T>
void Autoarr2<T>::Add(T value) {
if(!values){
values=malloc(ALLOC_N_BLOCKS*sizeof(T*));
goto create_block;
}
else if(block_length==max_block_length){
block_length=0;
create_block:
values=realloc((blocks_count+ALLOC_N_BLOCKS)*sizeof(T*));
blocks_count+=ALLOC_N_BLOCKS;
values[blocks_count]=malloc(max_block_length*sizeof(T));
}
values[blocks_count-1][block_length]=value;
block_length++;
Length++;
}
template<typename T>
Autoarr2<T>::~Autoarr2() {
}
template<typename T>
uint32 Autoarr2<T>::Length() {
for (uint16 i=0;i<blocks_count;i++)
delete[] values[i];
delete[] values;
}

View File

@ -2,17 +2,19 @@
#include "../base/base.h"
template <typename T>
class Autoarr2 {
T** values;
uint16 blocks_count;
uint16 block_length;
T** values;
uint16 max_block_length;
public:
uint32 Length;
Autoarr2();
uint32 Length();
T Get(uint32 index);
explicit Autoarr2(uint16 _max_block_length);
T* GetPtr(uint32 index);
T Get(uint32 index);
void Set(uint32 index, T value);
void Add(T value);
~Autoarr2();

View File

@ -9,6 +9,10 @@ extern "C" {
#include "errors.h"
#include "cptr.h"
#if __cplusplus
#include "errors.hpp"
#endif
// executes codeblock and prints execution time
#ifdef CLOCK_REALTIME // non-standard high-precision clock
#define optime(opname,repeats,codeblock) ({\

9
src/base/errors.cpp Normal file
View File

@ -0,0 +1,9 @@
#include "errors.hpp"
void throwcpp(err_t eid){
throwcpp(errname(eid));
}
void throwcpp(char* emsg){
__EXIT(emsg);
}

8
src/base/errors.hpp Normal file
View File

@ -0,0 +1,8 @@
#pragma once
#ifdef __cplusplus
#include "errors.h"
void throwcpp(err_t eid);
void throwcpp(char* emsg);
#endif