Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dc1bcce

Browse files
committed
constify identities
1 parent 5e5ca96 commit dc1bcce

17 files changed

Lines changed: 549 additions & 545 deletions

library/DataDefs.cpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -42,44 +42,44 @@ distribution.
4242
using namespace DFHack;
4343

4444

45-
void *type_identity::do_allocate_pod() {
45+
void *type_identity::do_allocate_pod() const {
4646
size_t sz = byte_size();
4747
void *p = malloc(sz);
4848
memset(p, 0, sz);
4949
return p;
5050
}
5151

52-
void type_identity::do_copy_pod(void *tgt, const void *src) {
52+
void type_identity::do_copy_pod(void *tgt, const void *src) const {
5353
memmove(tgt, src, byte_size());
5454
};
5555

56-
bool type_identity::do_destroy_pod(void *obj) {
56+
bool type_identity::do_destroy_pod(void *obj) const {
5757
free(obj);
5858
return true;
5959
}
6060

61-
void *type_identity::allocate() {
61+
void *type_identity::allocate() const {
6262
if (can_allocate())
6363
return do_allocate();
6464
else
6565
return NULL;
6666
}
6767

68-
bool type_identity::copy(void *tgt, const void *src) {
68+
bool type_identity::copy(void *tgt, const void *src) const {
6969
if (can_allocate() && tgt && src)
7070
return do_copy(tgt, src);
7171
else
7272
return false;
7373
}
7474

75-
bool type_identity::destroy(void *obj) {
75+
bool type_identity::destroy(void *obj) const {
7676
if (can_allocate() && obj)
7777
return do_destroy(obj);
7878
else
7979
return false;
8080
}
8181

82-
void *enum_identity::do_allocate() {
82+
void *enum_identity::do_allocate() const {
8383
size_t sz = byte_size();
8484
void *p = malloc(sz);
8585
memcpy(p, &first_item_value, std::min(sz, sizeof(int64_t)));
@@ -92,11 +92,11 @@ void *enum_identity::do_allocate() {
9292
* initialized by the loader in the initial mmap.
9393
*/
9494
compound_identity *compound_identity::list = NULL;
95-
std::vector<compound_identity*> compound_identity::top_scope;
95+
std::vector<const compound_identity*> compound_identity::top_scope;
9696

9797
compound_identity::compound_identity(size_t size, TAllocateFn alloc,
98-
compound_identity *scope_parent, const char *dfhack_name)
99-
: constructed_identity(size, alloc), dfhack_name(dfhack_name), scope_parent(scope_parent)
98+
const compound_identity *scope_parent, const char *dfhack_name)
99+
: constructed_identity(size, alloc), dfhack_name(dfhack_name), scope_parent(const_cast<compound_identity*>(scope_parent)) // fixme
100100
{
101101
next = list; list = this;
102102
}
@@ -109,7 +109,7 @@ void compound_identity::doInit(Core *)
109109
top_scope.push_back(this);
110110
}
111111

112-
std::string compound_identity::getFullName()
112+
const std::string compound_identity::getFullName() const
113113
{
114114
if (scope_parent)
115115
return scope_parent->getFullName() + "." + getName();
@@ -131,19 +131,19 @@ void compound_identity::Init(Core *core)
131131
}
132132

133133
bitfield_identity::bitfield_identity(size_t size,
134-
compound_identity *scope_parent, const char *dfhack_name,
134+
const compound_identity *scope_parent, const char *dfhack_name,
135135
int num_bits, const bitfield_item_info *bits)
136136
: compound_identity(size, NULL, scope_parent, dfhack_name), bits(bits), num_bits(num_bits)
137137
{
138138
}
139139

140140
enum_identity::enum_identity(size_t size,
141-
compound_identity *scope_parent, const char *dfhack_name,
142-
type_identity *base_type,
141+
const compound_identity *scope_parent, const char *dfhack_name,
142+
const type_identity *base_type,
143143
int64_t first_item_value, int64_t last_item_value,
144144
const char *const *keys,
145145
const ComplexData *complex,
146-
const void *attrs, struct_identity *attr_type)
146+
const void *attrs, const struct_identity *attr_type)
147147
: compound_identity(size, NULL, scope_parent, dfhack_name),
148148
keys(keys), complex(complex),
149149
first_item_value(first_item_value), last_item_value(last_item_value),
@@ -158,7 +158,7 @@ enum_identity::enum_identity(size_t size,
158158
}
159159
}
160160

161-
enum_identity::enum_identity(enum_identity *base_enum, type_identity *override_base_type)
161+
enum_identity::enum_identity(const enum_identity *base_enum, const type_identity *override_base_type)
162162
: enum_identity(override_base_type->byte_size(), base_enum->getScopeParent(),
163163
base_enum->getName(), override_base_type, base_enum->first_item_value,
164164
base_enum->last_item_value, base_enum->keys, base_enum->complex,
@@ -177,10 +177,10 @@ enum_identity::ComplexData::ComplexData(std::initializer_list<int64_t> values)
177177
}
178178

179179
struct_identity::struct_identity(size_t size, TAllocateFn alloc,
180-
compound_identity *scope_parent, const char *dfhack_name,
181-
struct_identity *parent, const struct_field_info *fields)
180+
const compound_identity *scope_parent, const char *dfhack_name,
181+
const struct_identity *parent, const struct_field_info *fields)
182182
: compound_identity(size, alloc, scope_parent, dfhack_name),
183-
parent(parent), has_children(false), fields(fields)
183+
parent(const_cast<struct_identity*>(parent)), has_children(false), fields(fields)
184184
{
185185
}
186186

@@ -194,7 +194,7 @@ void struct_identity::doInit(Core *core)
194194
}
195195
}
196196

197-
bool struct_identity::is_subclass(struct_identity *actual)
197+
bool struct_identity::is_subclass(const struct_identity *actual) const
198198
{
199199
if (!has_children && actual != this)
200200
return false;
@@ -205,42 +205,42 @@ bool struct_identity::is_subclass(struct_identity *actual)
205205
return false;
206206
}
207207

208-
std::string pointer_identity::getFullName()
208+
const std::string pointer_identity::getFullName() const
209209
{
210210
return (target ? target->getFullName() : std::string("void")) + "*";
211211
}
212212

213-
std::string container_identity::getFullName(type_identity *item)
213+
const std::string container_identity::getFullName(const type_identity *item) const
214214
{
215215
return '<' + (item ? item->getFullName() : std::string("void")) + '>';
216216
}
217217

218-
std::string ptr_container_identity::getFullName(type_identity *item)
218+
const std::string ptr_container_identity::getFullName(const type_identity *item) const
219219
{
220220
return '<' + (item ? item->getFullName() : std::string("void")) + std::string("*>");
221221
}
222222

223-
std::string bit_container_identity::getFullName(type_identity *)
223+
const std::string bit_container_identity::getFullName(const type_identity *) const
224224
{
225225
return "<bool>";
226226
}
227227

228-
std::string df::buffer_container_identity::getFullName(type_identity *item)
228+
const std::string df::buffer_container_identity::getFullName(const type_identity *item) const
229229
{
230230
return (item ? item->getFullName() : std::string("void")) +
231231
(size > 0 ? stl_sprintf("[%d]", size) : std::string("[]"));
232232
}
233233

234-
union_identity::union_identity(size_t size, TAllocateFn alloc,
234+
union_identity::union_identity(size_t size, const TAllocateFn alloc,
235235
compound_identity *scope_parent, const char *dfhack_name,
236236
struct_identity *parent, const struct_field_info *fields)
237237
: struct_identity(size, alloc, scope_parent, dfhack_name, parent, fields)
238238
{
239239
}
240240

241-
virtual_identity::virtual_identity(size_t size, TAllocateFn alloc,
241+
virtual_identity::virtual_identity(size_t size, const TAllocateFn alloc,
242242
const char *dfhack_name, const char *original_name,
243-
virtual_identity *parent, const struct_field_info *fields,
243+
const virtual_identity *parent, const struct_field_info *fields,
244244
bool is_plugin)
245245
: struct_identity(size, alloc, NULL, dfhack_name, parent, fields), original_name(original_name),
246246
vtable_ptr(NULL), is_plugin(is_plugin)
@@ -462,7 +462,7 @@ void DFHack::flagarrayToString(std::vector<std::string> *pvec, const void *p,
462462
}
463463
}
464464

465-
static const struct_field_info *find_union_tag_candidate(struct_identity *structure, const struct_field_info *union_field)
465+
static const struct_field_info *find_union_tag_candidate(const struct_identity *structure, const struct_field_info *union_field)
466466
{
467467
if (union_field->extra && union_field->extra->union_tag_field)
468468
{
@@ -502,7 +502,7 @@ static const struct_field_info *find_union_tag_candidate(struct_identity *struct
502502
return nullptr;
503503
}
504504

505-
const struct_field_info *DFHack::find_union_tag(struct_identity *structure, const struct_field_info *union_field)
505+
const struct_field_info *DFHack::find_union_tag(const struct_identity *structure, const struct_field_info *union_field)
506506
{
507507
CHECK_NULL_POINTER(structure);
508508
CHECK_NULL_POINTER(union_field);
@@ -538,7 +538,7 @@ const struct_field_info *DFHack::find_union_tag(struct_identity *structure, cons
538538
return nullptr;
539539
}
540540

541-
auto container_type = static_cast<container_identity *>(union_field->type);
541+
auto container_type = static_cast<const container_identity *>(union_field->type);
542542
if (container_type->getFullName(nullptr) != "vector<void>" ||
543543
!container_type->getItemType() ||
544544
container_type->getItemType()->type() != IDTYPE_UNION)
@@ -555,7 +555,7 @@ const struct_field_info *DFHack::find_union_tag(struct_identity *structure, cons
555555
return nullptr;
556556
}
557557

558-
auto tag_container_type = static_cast<container_identity *>(tag_candidate->type);
558+
auto tag_container_type = static_cast<const container_identity *>(tag_candidate->type);
559559
if (tag_container_type->getFullName(nullptr) == "vector<void>" &&
560560
tag_container_type->getItemType() &&
561561
tag_container_type->getItemType()->type() == IDTYPE_ENUM)

library/DataIdentity.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
namespace df {
1717
#define NUMBER_IDENTITY_TRAITS(category, type, name) \
18-
category##_identity<type> identity_traits<type>::identity(name);
18+
const category##_identity<type> identity_traits<type>::identity(name);
1919
#define INTEGER_IDENTITY_TRAITS(type, name) NUMBER_IDENTITY_TRAITS(integer, type, name)
2020
#define FLOAT_IDENTITY_TRAITS(type) NUMBER_IDENTITY_TRAITS(float, type, #type)
2121
#define OPAQUE_IDENTITY_TRAITS_NAME(name, ...) \
22-
opaque_identity identity_traits<__VA_ARGS__ >::identity(sizeof(__VA_ARGS__), allocator_noassign_fn<__VA_ARGS__ >, name)
22+
const opaque_identity identity_traits<__VA_ARGS__ >::identity(sizeof(__VA_ARGS__), allocator_noassign_fn<__VA_ARGS__ >, name)
2323
#define OPAQUE_IDENTITY_TRAITS(...) OPAQUE_IDENTITY_TRAITS_NAME(#__VA_ARGS__, __VA_ARGS__ )
2424

2525
INTEGER_IDENTITY_TRAITS(char, "char");
@@ -36,14 +36,14 @@ namespace df {
3636
FLOAT_IDENTITY_TRAITS(float);
3737
FLOAT_IDENTITY_TRAITS(double);
3838

39-
bool_identity identity_traits<bool>::identity;
40-
stl_string_identity identity_traits<std::string>::identity;
41-
ptr_string_identity identity_traits<char*>::identity;
42-
ptr_string_identity identity_traits<const char*>::identity;
43-
pointer_identity identity_traits<void*>::identity;
44-
stl_ptr_vector_identity identity_traits<std::vector<void*> >::identity;
45-
stl_bit_vector_identity identity_traits<std::vector<bool> >::identity;
46-
bit_array_identity identity_traits<BitArray<int> >::identity;
39+
const bool_identity identity_traits<bool>::identity;
40+
const stl_string_identity identity_traits<std::string>::identity;
41+
const ptr_string_identity identity_traits<char*>::identity;
42+
const ptr_string_identity identity_traits<const char*>::identity;
43+
const pointer_identity identity_traits<void*>::identity;
44+
const stl_ptr_vector_identity identity_traits<std::vector<void*> >::identity;
45+
const stl_bit_vector_identity identity_traits<std::vector<bool> >::identity;
46+
const bit_array_identity identity_traits<BitArray<int> >::identity;
4747

4848
OPAQUE_IDENTITY_TRAITS(std::condition_variable);
4949
OPAQUE_IDENTITY_TRAITS(std::fstream);
@@ -57,8 +57,8 @@ namespace df {
5757
OPAQUE_IDENTITY_TRAITS(std::variant<std::string, std::function<void()> >);
5858
OPAQUE_IDENTITY_TRAITS(std::weak_ptr<df::widget_container>);
5959

60-
buffer_container_identity buffer_container_identity::base_instance;
60+
const buffer_container_identity buffer_container_identity::base_instance;
6161

62-
DFHACK_EXPORT stl_container_identity<std::vector<int32_t> > stl_vector_int32_t_identity("vector", identity_traits<int32_t>::get());
63-
DFHACK_EXPORT stl_container_identity<std::vector<int16_t> > stl_vector_int16_t_identity("vector", identity_traits<int16_t>::get());
62+
DFHACK_EXPORT const stl_container_identity<std::vector<int32_t> > stl_vector_int32_t_identity("vector", identity_traits<int32_t>::get());
63+
DFHACK_EXPORT const stl_container_identity<std::vector<int16_t> > stl_vector_int16_t_identity("vector", identity_traits<int16_t>::get());
6464
}

library/LuaTools.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,12 @@ int DFHack::Lua::PushPosXY(lua_State *state, const df::coord2d &pos)
226226
* Public DF object reference handling API
227227
*/
228228

229-
void DFHack::Lua::PushDFObject(lua_State *state, type_identity *type, void *ptr)
229+
void DFHack::Lua::PushDFObject(lua_State *state, const type_identity *type, void *ptr)
230230
{
231231
push_object_internal(state, type, ptr, false);
232232
}
233233

234-
void *DFHack::Lua::GetDFObject(lua_State *state, type_identity *type, int val_index, bool exact_type)
234+
void *DFHack::Lua::GetDFObject(lua_State *state, const type_identity *type, int val_index, bool exact_type)
235235
{
236236
return get_object_internal(state, type, val_index, exact_type, false);
237237
}
@@ -248,7 +248,7 @@ static void check_valid_ptr_index(lua_State *state, int val_index)
248248
}
249249

250250
static void signal_typeid_error(color_ostream *out, lua_State *state,
251-
type_identity *type, const char *msg,
251+
const type_identity *type, const char *msg,
252252
int val_index, bool perr, bool signal)
253253
{
254254
std::string typestr = type ? type->getFullName() : "any pointer";
@@ -273,7 +273,7 @@ static void signal_typeid_error(color_ostream *out, lua_State *state,
273273
}
274274

275275

276-
void *DFHack::Lua::CheckDFObject(lua_State *state, type_identity *type, int val_index, bool exact_type)
276+
void *DFHack::Lua::CheckDFObject(lua_State *state, const type_identity *type, int val_index, bool exact_type)
277277
{
278278
check_valid_ptr_index(state, val_index);
279279

0 commit comments

Comments
 (0)