Skip to content

Commit b727c1d

Browse files
authored
Merge pull request #5847 from ab9rf/use-std-array
changes required as part of changing codegen to use `std::array`
2 parents c3ae0c3 + fe95b29 commit b727c1d

10 files changed

Lines changed: 86 additions & 67 deletions

File tree

library/DataStatics.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ namespace {
2222
DFHack::VersionInfo *global_table_ = DFHack::Core::getInstance().vinfo.get(); \
2323
void * tmp_;
2424

25-
#define INIT_GLOBAL_FUNCTION_ITEM(type,name) \
26-
if (global_table_->getAddress(#name,tmp_)) name = (type*)tmp_;
25+
#define INIT_GLOBAL_FUNCTION_ITEM(name, ...) \
26+
if (global_table_->getAddress(#name,tmp_)) name = (__VA_ARGS__*)tmp_;
2727

2828
#define TID(type) (&identity_traits< type >::identity)
2929

library/include/DataIdentity.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,11 @@ namespace df
713713
static const container_identity *get();
714714
};
715715

716+
template<class T, size_t sz> struct identity_traits<std::array<T, sz>>
717+
{
718+
static const container_identity* get();
719+
};
720+
716721
template<class T> struct identity_traits<std::vector<T> > {
717722
static const container_identity *get();
718723
};
@@ -797,6 +802,13 @@ namespace df
797802
return &identity;
798803
}
799804

805+
template<class T, size_t sz>
806+
inline const container_identity* identity_traits<std::array<T,sz>>::get()
807+
{
808+
static const buffer_container_identity identity(sz, identity_traits<T>::get());
809+
return &identity;
810+
}
811+
800812
template<class T>
801813
inline const container_identity *identity_traits<std::vector<T> >::get() {
802814
using container = std::vector<T>;

library/include/df/custom/tile_bitmask.methods.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ inline uint16_t &operator[] (int y)
66
}
77
void clear()
88
{
9-
memset(bits,0,sizeof(bits));
9+
bits.fill(0);
1010
}
1111
void set_all()
1212
{
13-
memset(bits,0xFF,sizeof(bits));
13+
bits.fill(-1);
1414
}
1515
inline bool getassignment( const df::coord2d &xy )
1616
{

library/include/modules/MapCache.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ struct BiomeInfo {
6868
int16_t layer_stone[MAX_LAYERS];
6969
};
7070

71-
typedef uint8_t t_veintype[16][16];
72-
typedef df::tiletype t_tilearr[16][16];
71+
using t_veintype = arr40d<uint8_t>;
72+
using t_tilearr = arr40d<df::tiletype>;
7373

7474
class BlockInfo
7575
{

library/include/modules/Maps.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,28 +126,32 @@ enum BiomeOffset {
126126
*/
127127
typedef df::block_flags t_blockflags;
128128

129+
template <typename T>
130+
using arr40d = std::array<std::array<T, 16>, 16>;
131+
129132
/**
130133
* 16x16 array of tile types
131134
* \ingroup grp_maps
132135
*/
133-
typedef df::tiletype tiletypes40d [16][16];
136+
using tiletypes40d = arr40d<df::tiletype>;
134137
/**
135138
* 16x16 array used for squashed block materials
136139
* \ingroup grp_maps
137140
*/
138-
typedef int16_t t_blockmaterials [16][16];
141+
using t_blockmaterials = arr40d<int16_t>;
139142
/**
140143
* 16x16 array of designation flags
141144
* \ingroup grp_maps
142145
*/
143146
typedef df::tile_designation t_designation;
144-
typedef t_designation designations40d [16][16];
147+
using designations40d = arr40d<t_designation>;
148+
145149
/**
146150
* 16x16 array of occupancy flags
147151
* \ingroup grp_maps
148152
*/
149153
typedef df::tile_occupancy t_occupancy;
150-
typedef t_occupancy occupancies40d [16][16];
154+
using occupancies40d = arr40d<t_occupancy>;
151155
/**
152156
* array of 16 biome indexes valid for the block
153157
* \ingroup grp_maps
@@ -157,7 +161,7 @@ typedef uint8_t biome_indices40d [9];
157161
* 16x16 array of temperatures
158162
* \ingroup grp_maps
159163
*/
160-
typedef uint16_t t_temperatures [16][16];
164+
using t_temperatures = arr40d<uint16_t>;
161165

162166
/**
163167
* Index a tile array by a 2D coordinate, clipping it to mod 16.

library/modules/MapCache.cpp

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ const BiomeInfo MapCache::biome_stub = {
9696
-1, -1, -1, -1, -1, -1, -1, -1 }
9797
};
9898

99-
#define COPY(a,b) memcpy(&a,&b,sizeof(a))
100-
10199
MapExtras::Block::Block(MapCache *parent, DFCoord _bcoord) :
102100
parent(parent),
103101
designated_tiles{}
@@ -123,20 +121,19 @@ void MapExtras::Block::init()
123121

124122
if(block)
125123
{
126-
COPY(designation, block->designation);
127-
COPY(occupancy, block->occupancy);
128-
129-
COPY(temp1, block->temperature_1);
130-
COPY(temp2, block->temperature_2);
124+
designation = block->designation;
125+
occupancy = block->occupancy;
126+
temp1 = block->temperature_1;
127+
temp2 = block->temperature_2;
131128

132129
valid = true;
133130
}
134131
else
135132
{
136-
memset(designation,0,sizeof(designation));
137-
memset(occupancy,0,sizeof(occupancy));
138-
memset(temp1,0,sizeof(temp1));
139-
memset(temp2,0,sizeof(temp2));
133+
designation.fill({});
134+
occupancy.fill({});
135+
temp1.fill({});
136+
temp2.fill({});
140137
}
141138
}
142139

@@ -198,10 +195,10 @@ void MapExtras::Block::init_tiles(bool basemat)
198195
MapExtras::Block::TileInfo::TileInfo()
199196
{
200197
dirty_raw.clear();
201-
memset(raw_tiles,0,sizeof(raw_tiles));
198+
raw_tiles.fill({});
202199
ice_info = NULL;
203200
con_info = NULL;
204-
memset(base_tiles,0,sizeof(base_tiles));
201+
base_tiles.fill({});
205202
}
206203

207204
MapExtras::Block::TileInfo::~TileInfo()
@@ -218,24 +215,33 @@ void MapExtras::Block::TileInfo::init_iceinfo()
218215
ice_info = new IceInfo();
219216
}
220217

218+
template <typename T>
219+
constexpr T arr40d_neg1() {
220+
T tmp{};
221+
std::remove_reference_t<decltype(tmp[0])> tmp2{};
222+
tmp2.fill(-1);
223+
tmp.fill(tmp2);
224+
return tmp;
225+
};
226+
221227
void MapExtras::Block::TileInfo::init_coninfo()
222228
{
223229
if (con_info)
224230
return;
225231

226232
con_info = new ConInfo();
227233
con_info->constructed.clear();
228-
COPY(con_info->tiles, base_tiles);
229-
memset(con_info->mat_type, -1, sizeof(con_info->mat_type));
230-
memset(con_info->mat_index, -1, sizeof(con_info->mat_index));
234+
con_info->tiles = base_tiles;
235+
con_info->mat_type = arr40d_neg1<t_blockmaterials>();
236+
con_info->mat_index = arr40d_neg1<t_blockmaterials>();
231237
}
232238

233239
MapExtras::Block::BasematInfo::BasematInfo()
234240
{
235241
vein_dirty.clear();
236-
memset(mat_type,0,sizeof(mat_type));
237-
memset(mat_index,-1,sizeof(mat_index));
238-
memset(veinmat,-1,sizeof(veinmat));
242+
mat_type.fill({});
243+
mat_index = arr40d_neg1<t_blockmaterials>();
244+
veinmat = arr40d_neg1<t_blockmaterials>();
239245
}
240246

241247
bool MapExtras::Block::setFlagAt(df::coord2d p, df::tile_designation::Mask mask, bool set)
@@ -481,7 +487,7 @@ void MapExtras::Block::ParseTiles(TileInfo *tiles)
481487
tiletypes40d icetiles;
482488
BlockInfo::SquashFrozenLiquids(block, icetiles);
483489

484-
COPY(tiles->raw_tiles, block->tiletype);
490+
tiles->raw_tiles = block->tiletype;
485491

486492
for (int x = 0; x < 16; x++)
487493
{
@@ -598,7 +604,7 @@ void MapExtras::Block::WriteTiles(TileInfo *tiles)
598604

599605
if (tiles->ice_info && tiles->ice_info->dirty.has_assignments())
600606
{
601-
df::tiletype (*newtiles)[16] = (tiles->con_info ? tiles->con_info->tiles : tiles->base_tiles);
607+
auto newtiles = (tiles->con_info ? tiles->con_info->tiles : tiles->base_tiles);
602608

603609
for (int i = block->block_events.size()-1; i >= 0; i--)
604610
{
@@ -646,8 +652,8 @@ void MapExtras::Block::ParseBasemats(TileInfo *tiles, BasematInfo *bmats)
646652

647653
info.prepare(this);
648654

649-
COPY(bmats->veinmat, info.veinmats);
650-
COPY(bmats->veintype, info.veintype);
655+
bmats->veinmat = info.veinmats;
656+
bmats->veintype = info.veintype;
651657

652658
for (int x = 0; x < 16; x++)
653659
{
@@ -779,7 +785,7 @@ bool MapExtras::Block::Write ()
779785

780786
if(dirty_designations)
781787
{
782-
COPY(block->designation, designation);
788+
block->designation = designation;
783789
block->flags.bits.designated = true;
784790
block->dsgn_check_cooldown = 0;
785791
dirty_designations = false;
@@ -798,13 +804,13 @@ bool MapExtras::Block::Write ()
798804
}
799805
if(dirty_temperatures)
800806
{
801-
COPY(block->temperature_1, temp1);
802-
COPY(block->temperature_2, temp2);
807+
block->temperature_1 = temp1;
808+
block->temperature_2 = temp2;
803809
dirty_temperatures = false;
804810
}
805811
if(dirty_occupancies)
806812
{
807-
COPY(block->occupancy, occupancy);
813+
block->occupancy = occupancy;
808814
dirty_occupancies = false;
809815
}
810816
return true;
@@ -1034,8 +1040,8 @@ void MapExtras::BlockInfo::SquashVeins(df::map_block *mb, t_blockmaterials & mat
10341040
{
10351041
std::vector <df::block_square_event_mineralst *> veins;
10361042
Maps::SortBlockEvents(mb,&veins);
1037-
memset(materials,-1,sizeof(materials));
1038-
memset(veintype, 0, sizeof(t_veintype));
1043+
materials = arr40d_neg1<t_blockmaterials>();
1044+
veintype.fill({});
10391045

10401046
for (uint32_t x = 0;x<16;x++) for (uint32_t y = 0; y< 16;y++)
10411047
{
@@ -1054,7 +1060,7 @@ void MapExtras::BlockInfo::SquashFrozenLiquids(df::map_block *mb, tiletypes40d &
10541060
{
10551061
std::vector <df::block_square_event_frozen_liquidst *> ices;
10561062
Maps::SortBlockEvents(mb,NULL,&ices);
1057-
memset(frozen,0,sizeof(frozen));
1063+
frozen.fill({});
10581064
for (uint32_t x = 0; x < 16; x++) for (uint32_t y = 0; y < 16; y++)
10591065
{
10601066
for (size_t i = 0; i < ices.size(); i++)
@@ -1089,7 +1095,7 @@ void MapExtras::BlockInfo::SquashGrass(df::map_block *mb, t_blockmaterials &mate
10891095
{
10901096
std::vector<df::block_square_event_grassst*> grasses;
10911097
Maps::SortBlockEvents(mb, NULL, NULL, NULL, &grasses);
1092-
memset(materials,-1,sizeof(materials));
1098+
materials = arr40d_neg1<t_blockmaterials>();
10931099
for (uint32_t x = 0; x < 16; x++) for (uint32_t y = 0; y < 16; y++)
10941100
{
10951101
int amount = 0;

library/modules/Maps.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ int32_t Maps::addMaterialSpatter (df::coord pos, int16_t mat, int32_t matg, df::
756756
spatter->mat_type = mat;
757757
spatter->mat_index = matg;
758758
spatter->mat_state = state;
759-
memset(spatter->amount, 0, sizeof(spatter->amount));
759+
spatter->amount.fill({});
760760
spatter->min_temperature = spatter->max_temperature = 60001;
761761

762762
uint16_t melt = matinfo.material->heat.melting_point;
@@ -876,8 +876,8 @@ int32_t Maps::addItemSpatter (df::coord pos, df::item_type i_type, int16_t i_sub
876876
spatter->mattype = i_subcat1;
877877
spatter->matindex = i_subcat2;
878878
spatter->print_variant = print_variant;
879-
memset(spatter->amount, 0, sizeof(spatter->amount));
880-
memset(spatter->flag, 0, sizeof(spatter->flag));
879+
spatter->amount.fill({});
880+
spatter->flag.fill({});
881881
spatter->min_temperature = spatter->max_temperature = 60001;
882882

883883
if (Items::usesStandardMaterial(i_type))
@@ -1569,14 +1569,10 @@ void Maps::addBlockColumns(int32_t new_height)
15691569

15701570
// Copy other potentially important metadata from prior air
15711571
// layer
1572-
std::memcpy(air_block->lighting, last_air_block->lighting,
1573-
sizeof(air_block->lighting));
1574-
std::memcpy(air_block->temperature_1, last_air_block->temperature_1,
1575-
sizeof(air_block->temperature_1));
1576-
std::memcpy(air_block->temperature_2, last_air_block->temperature_2,
1577-
sizeof(air_block->temperature_2));
1578-
std::memcpy(air_block->region_offset, last_air_block->region_offset,
1579-
sizeof(air_block->region_offset));
1572+
air_block->lighting = last_air_block->lighting;
1573+
air_block->temperature_1 = last_air_block->temperature_1;
1574+
air_block->temperature_2 = last_air_block->temperature_2;
1575+
air_block->region_offset = last_air_block->region_offset;
15801576

15811577
// Create tile designations to inform lighting and
15821578
// outside markers
@@ -1598,9 +1594,9 @@ void Maps::addBlockColumns(int32_t new_height)
15981594
continue;
15991595
}
16001596
df::block_column_print_infost* glyphs = new df::block_column_print_infost;
1601-
std::ranges::copy(std::array{0,1,2,3}, glyphs->x);
1602-
std::ranges::copy(std::array{0,0,0,0}, glyphs->y);
1603-
std::ranges::copy(std::array{'e','x','p','^'}, glyphs->tile);
1597+
glyphs->x = {0,1,2,3};
1598+
glyphs->y = {0,0,0,0};
1599+
glyphs->tile = {'e','x','p','^'};
16041600
column->unmined_glyphs.push_back(glyphs);
16051601
}
16061602
return true;

library/xml

plugins/remotefortressreader/remotefortressreader.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,16 @@ DFhackCExport command_result plugin_onupdate(color_ostream &out)
304304
return CR_OK;
305305
}
306306

307-
uint16_t fletcher16(uint8_t const *data, size_t bytes)
307+
uint16_t fletcher16(const void *data_, size_t bytes)
308308
{
309+
auto data = static_cast<const std::byte*>(data_);
309310
uint16_t sum1 = 0xff, sum2 = 0xff;
310311

311312
while (bytes) {
312313
size_t tlen = bytes > 20 ? 20 : bytes;
313314
bytes -= tlen;
314315
do {
315-
sum2 += sum1 += *data++;
316+
sum2 += sum1 += static_cast<uint8_t>(*data++);
316317
} while (--tlen);
317318
sum1 = (sum1 & 0xff) + (sum1 >> 8);
318319
sum2 = (sum2 & 0xff) + (sum2 >> 8);
@@ -335,7 +336,7 @@ void ConvertDfColor(int16_t index, RemoteFortressReader::ColorDefinition * out)
335336
out->set_blue(gps->uccolor[index][2]);
336337
}
337338

338-
void ConvertDfColor(int16_t in[3], RemoteFortressReader::ColorDefinition * out)
339+
void ConvertDfColor(std::array<int16_t,3>& in, RemoteFortressReader::ColorDefinition * out)
339340
{
340341
int index = in[0] | (8 * in[2]);
341342
ConvertDfColor(index, out);
@@ -623,7 +624,7 @@ static command_result CheckHashes(color_ostream &stream, const EmptyMessage *in)
623624
for (size_t i = 0; i < world->map.map_blocks.size(); i++)
624625
{
625626
df::map_block * block = world->map.map_blocks[i];
626-
fletcher16((uint8_t*)(block->tiletype), 16 * 16 * sizeof(df::enums::tiletype::tiletype));
627+
fletcher16((block->tiletype).data(), 16 * 16 * sizeof(df::enums::tiletype::tiletype));
627628
}
628629
clock_t end = clock();
629630
double elapsed_secs = double(end - start) / CLOCKS_PER_SEC;
@@ -654,7 +655,7 @@ bool IsTiletypeChanged(DFCoord pos)
654655
uint16_t hash;
655656
df::map_block * block = Maps::getBlock(pos);
656657
if (block)
657-
hash = fletcher16((uint8_t*)(block->tiletype), 16 * 16 * (sizeof(df::enums::tiletype::tiletype)));
658+
hash = fletcher16((block->tiletype).data(), 16 * 16 * (sizeof(df::enums::tiletype::tiletype)));
658659
else
659660
hash = 0;
660661
if (hashes[pos] != hash)
@@ -672,7 +673,7 @@ bool IsDesignationChanged(DFCoord pos)
672673
uint16_t hash;
673674
df::map_block * block = Maps::getBlock(pos);
674675
if (block)
675-
hash = fletcher16((uint8_t*)(block->designation), 16 * 16 * (sizeof(df::tile_designation)));
676+
hash = fletcher16((block->designation).data(), 16 * 16 * (sizeof(df::tile_designation)));
676677
else
677678
hash = 0;
678679
if (waterHashes[pos] != hash)

0 commit comments

Comments
 (0)