Skip to content

Commit f5837f8

Browse files
authored
Codechange: Replace AllocArray with vector<unique_ptr<>>. (#59)
Removes manual memory management and obfuscation.
1 parent 2f10596 commit f5837f8

File tree

4 files changed

+11
-86
lines changed

4 files changed

+11
-86
lines changed

src/allocarray.h

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

src/info.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int makeint(U8 low, S8 high)
2424
return combined;
2525
}
2626

27-
void read_file(std::istream&in,int infover,int grfcontversion,AllocArray<Sprite>&sprites);
27+
void read_file(std::istream&in,int infover,int grfcontversion,std::vector<std::unique_ptr<Sprite>>&sprites);
2828

2929
nfe_map nfo_escapes;
3030

src/info.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
#include <stdio.h>
66
#include <string>
7+
#include <memory>
78

89
#include "pcxsprit.h"
910
#include "pngsprit.h"
1011
#include "sprites.h"
1112
#include "nfosprite.h"
12-
#include "allocarray.h"
1313

1414
class inforeader {
1515
public:
@@ -32,7 +32,7 @@ class inforeader {
3232

3333
std::string imgname;
3434
int *colourmap;
35-
AllocArray<Sprite> nfofile;
35+
std::vector<std::unique_ptr<Sprite>> nfofile;
3636
private:
3737
pcxread* MakeReader()const;
3838
};

src/readinfo.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Version 7: Add backslash escapes
4040
#include<sstream>
4141
#include<iomanip>
4242
#include<cstdio>
43+
#include<memory>
4344

4445

4546
// grfcodec requires boost::date_time for its processing of the \wYMD and
@@ -48,7 +49,6 @@ Version 7: Add backslash escapes
4849
using namespace boost::gregorian;
4950

5051
#include"nfosprite.h"
51-
#include"allocarray.h"
5252
#include"inlines.h"
5353

5454
extern int _quiet;
@@ -65,14 +65,14 @@ const char *depths[DEPTHS] = { "8bpp", "32bpp", "mask" };
6565
if(true){\
6666
if(buffer!=""){\
6767
checkspriteno();\
68-
sprites.push_back(Pseudo(sprites.size(),infover,grfcontversion,buffer,claimed_size));\
68+
sprites.push_back(std::make_unique<Pseudo>(sprites.size(),infover,grfcontversion,buffer,claimed_size));\
6969
buffer="";\
7070
}\
7171
spriteno=temp;\
7272
}else\
7373
(void(0))
7474

75-
void read_file(std::istream&in,int infover,int grfcontversion,AllocArray<Sprite>&sprites){
75+
void read_file(std::istream&in,int infover,int grfcontversion,std::vector<std::unique_ptr<Sprite>>&sprites){
7676
std::string sprite,datapart,buffer;
7777

7878
int temp=-1,spriteno=-1,claimed_size=1;
@@ -95,7 +95,7 @@ void read_file(std::istream&in,int infover,int grfcontversion,AllocArray<Sprite>
9595
getline(eat_white(spritestream.ignore()),datapart);
9696
strip_trailing_white(datapart);
9797
checkspriteno();
98-
sprites.push_back(Include(datapart));
98+
sprites.push_back(std::make_unique<Include>(datapart));
9999
}else{
100100
flush_buffer();
101101
eat_white(spritestream>>claimed_size);
@@ -115,13 +115,15 @@ void read_file(std::istream&in,int infover,int grfcontversion,AllocArray<Sprite>
115115
flush_buffer();
116116
checkspriteno();
117117
if (peeked!='|') {
118-
sprites.push_back(Real());
118+
sprites.push_back(std::make_unique<Real>());
119119
} else {
120120
do {
121121
datapart.erase(0, 1);
122122
} while (isspace(datapart[0]));
123123
}
124-
((Real*)sprites.last())->AddSprite(sprites.size()-1,infover,datapart);
124+
Real *r = dynamic_cast<Real *>(sprites.back().get());
125+
if (r == nullptr) throw Sprite::unparseable("internal error, expected Real sprite", sprites.size() - 1);
126+
r->AddSprite(sprites.size()-1,infover,datapart);
125127
}
126128
}
127129
}

0 commit comments

Comments
 (0)