Skip to content

Commit

Permalink
Get rid of easily removable uses of const_cast
Browse files Browse the repository at this point in the history
  • Loading branch information
rune-scape committed Sep 23, 2024
1 parent a84c480 commit 37f56f7
Show file tree
Hide file tree
Showing 71 changed files with 347 additions and 308 deletions.
2 changes: 1 addition & 1 deletion core/math/a_star.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ int64_t AStar3D::get_available_point_id() const {
while (points.has(cur_new_id)) {
cur_new_id++;
}
const_cast<int64_t &>(last_free_id) = cur_new_id;
last_free_id = cur_new_id;
}

return last_free_id;
Expand Down
2 changes: 1 addition & 1 deletion core/math/a_star.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class AStar3D : public RefCounted {
}
};

int64_t last_free_id = 0;
mutable int64_t last_free_id = 0;
uint64_t pass = 1;

OAHashMap<int64_t, Point *> points;
Expand Down
6 changes: 3 additions & 3 deletions core/object/ref_counted.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class Ref {

template <typename T_Other>
void operator=(const Ref<T_Other> &p_from) {
RefCounted *refb = const_cast<RefCounted *>(static_cast<const RefCounted *>(p_from.ptr()));
RefCounted *refb = static_cast<RefCounted *>(p_from.ptr());
if (!refb) {
unref();
return;
Expand Down Expand Up @@ -173,7 +173,7 @@ class Ref {

template <typename T_Other>
Ref(const Ref<T_Other> &p_from) {
RefCounted *refb = const_cast<RefCounted *>(static_cast<const RefCounted *>(p_from.ptr()));
RefCounted *refb = static_cast<RefCounted *>(p_from.ptr());
if (!refb) {
unref();
return;
Expand Down Expand Up @@ -252,7 +252,7 @@ struct PtrToArg<Ref<T>> {
return Ref<T>();
}
// p_ptr points to a RefCounted object
return Ref<T>(const_cast<T *>(*reinterpret_cast<T *const *>(p_ptr)));
return Ref<T>(*reinterpret_cast<T *const *>(p_ptr));
}

typedef Ref<T> EncodeT;
Expand Down
4 changes: 2 additions & 2 deletions core/os/condition_variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ class ConditionVariable {
public:
template <typename BinaryMutexT>
_ALWAYS_INLINE_ void wait(const MutexLock<BinaryMutexT> &p_lock) const {
condition.wait(const_cast<THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> &>(p_lock._get_lock()));
condition.wait(p_lock._get_lock());
}

template <int Tag>
_ALWAYS_INLINE_ void wait(const MutexLock<SafeBinaryMutex<Tag>> &p_lock) const {
condition.wait(const_cast<THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> &>(p_lock.mutex._get_lock()));
condition.wait(p_lock.mutex._get_lock());
}

_ALWAYS_INLINE_ void notify_one() const {
Expand Down
6 changes: 3 additions & 3 deletions core/templates/rid_owner.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class RID_Alloc : public RID_AllocBase {
return _allocate_rid();
}

_FORCE_INLINE_ T *get_or_null(const RID &p_rid, bool p_initialize = false) {
_FORCE_INLINE_ T *get_or_null(const RID &p_rid, bool p_initialize = false) const {
if (p_rid == RID()) {
return nullptr;
}
Expand Down Expand Up @@ -381,7 +381,7 @@ class RID_PtrOwner {
alloc.initialize_rid(p_rid, p_ptr);
}

_FORCE_INLINE_ T *get_or_null(const RID &p_rid) {
_FORCE_INLINE_ T *get_or_null(const RID &p_rid) const {
T **ptr = alloc.get_or_null(p_rid);
if (unlikely(!ptr)) {
return nullptr;
Expand Down Expand Up @@ -447,7 +447,7 @@ class RID_Owner {
alloc.initialize_rid(p_rid, p_ptr);
}

_FORCE_INLINE_ T *get_or_null(const RID &p_rid) {
_FORCE_INLINE_ T *get_or_null(const RID &p_rid) const {
return alloc.get_or_null(p_rid);
}

Expand Down
2 changes: 1 addition & 1 deletion core/variant/method_ptrcall.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ MAKE_PTRARG_BY_REFERENCE(Variant);
template <typename T>
struct PtrToArg<T *> {
_FORCE_INLINE_ static T *convert(const void *p_ptr) {
return likely(p_ptr) ? const_cast<T *>(*reinterpret_cast<T *const *>(p_ptr)) : nullptr;
return likely(p_ptr) ? *reinterpret_cast<T *const *>(p_ptr) : nullptr;
}
typedef Object *EncodeT;
_FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) {
Expand Down
2 changes: 1 addition & 1 deletion core/variant/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ void Variant::reference(const Variant &p_variant) {
}
}

_get_obj().obj = const_cast<Object *>(p_variant._get_obj().obj);
_get_obj().obj = p_variant._get_obj().obj;
_get_obj().id = p_variant._get_obj().id;
} break;
case CALLABLE: {
Expand Down
2 changes: 1 addition & 1 deletion editor/editor_help_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ void EditorHelpSearch::Runner::_match_method_name_and_push_back(Vector<DocData::
(term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
(term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
(term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
method.doc = const_cast<DocData::MethodDoc *>(&p_methods[i]);
method.doc = &p_methods[i];
r_match_methods->push_back(method);
}
}
Expand Down
8 changes: 4 additions & 4 deletions editor/editor_resource_preview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ void EditorResourcePreviewGenerator::_bind_methods() {
EditorResourcePreviewGenerator::EditorResourcePreviewGenerator() {
}

void EditorResourcePreviewGenerator::DrawRequester::request_and_wait(RID p_viewport) const {
void EditorResourcePreviewGenerator::DrawRequester::request_and_wait(RID p_viewport) {
Callable request_vp_update_once = callable_mp(RS::get_singleton(), &RS::viewport_set_update_mode).bind(p_viewport, RS::VIEWPORT_UPDATE_ONCE);

if (EditorResourcePreview::get_singleton()->is_threaded()) {
RS::get_singleton()->connect(SNAME("frame_pre_draw"), request_vp_update_once, Object::CONNECT_ONE_SHOT);
RS::get_singleton()->request_frame_drawn_callback(callable_mp(const_cast<EditorResourcePreviewGenerator::DrawRequester *>(this), &EditorResourcePreviewGenerator::DrawRequester::_post_semaphore));
RS::get_singleton()->request_frame_drawn_callback(callable_mp(this, &EditorResourcePreviewGenerator::DrawRequester::_post_semaphore));

semaphore.wait();
} else {
Expand All @@ -119,13 +119,13 @@ void EditorResourcePreviewGenerator::DrawRequester::request_and_wait(RID p_viewp
}
}

void EditorResourcePreviewGenerator::DrawRequester::abort() const {
void EditorResourcePreviewGenerator::DrawRequester::abort() {
if (EditorResourcePreview::get_singleton()->is_threaded()) {
semaphore.post();
}
}

Variant EditorResourcePreviewGenerator::DrawRequester::_post_semaphore() const {
Variant EditorResourcePreviewGenerator::DrawRequester::_post_semaphore() {
semaphore.post();
return Variant(); // Needed because of how the callback is used.
}
Expand Down
6 changes: 3 additions & 3 deletions editor/editor_resource_preview.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ class EditorResourcePreviewGenerator : public RefCounted {
class DrawRequester : public Object {
Semaphore semaphore;

Variant _post_semaphore() const;
Variant _post_semaphore();

public:
void request_and_wait(RID p_viewport) const;
void abort() const;
void request_and_wait(RID p_viewport);
void abort();
};

public:
Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/dedicated_server_export_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Ref<Resource> DedicatedServerExportPlugin::_customize_resource(const Ref<Resourc

if (p_resource.is_valid() && current_export_mode == EditorExportPreset::MODE_FILE_STRIP && p_resource->has_method("create_placeholder")) {
Callable::CallError err;
Ref<Resource> result = const_cast<Resource *>(p_resource.ptr())->callp("create_placeholder", nullptr, 0, err);
Ref<Resource> result = p_resource->callp("create_placeholder", nullptr, 0, err);
if (err.error == Callable::CallError::CALL_OK) {
return result;
}
Expand Down
6 changes: 3 additions & 3 deletions editor/plugins/editor_preview_plugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class EditorMaterialPreviewPlugin : public EditorResourcePreviewGenerator {
RID light_instance2;
RID camera;
RID camera_attributes;
DrawRequester draw_requester;
mutable DrawRequester draw_requester;

public:
virtual bool handles(const String &p_type) const override;
Expand Down Expand Up @@ -144,7 +144,7 @@ class EditorMeshPreviewPlugin : public EditorResourcePreviewGenerator {
RID light_instance2;
RID camera;
RID camera_attributes;
DrawRequester draw_requester;
mutable DrawRequester draw_requester;

public:
virtual bool handles(const String &p_type) const override;
Expand All @@ -162,7 +162,7 @@ class EditorFontPreviewPlugin : public EditorResourcePreviewGenerator {
RID viewport_texture;
RID canvas;
RID canvas_item;
DrawRequester draw_requester;
mutable DrawRequester draw_requester;

public:
virtual bool handles(const String &p_type) const override;
Expand Down
4 changes: 2 additions & 2 deletions editor/plugins/tiles/tiles_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ TileMapEditorPlugin *tile_map_plugin_singleton = nullptr;
TileSetEditorPlugin *tile_set_plugin_singleton = nullptr;

void TilesEditorUtils::_preview_frame_started() {
RS::get_singleton()->request_frame_drawn_callback(callable_mp(const_cast<TilesEditorUtils *>(this), &TilesEditorUtils::_pattern_preview_done));
RS::get_singleton()->request_frame_drawn_callback(callable_mp(this, &TilesEditorUtils::_pattern_preview_done));
}

void TilesEditorUtils::_pattern_preview_done() {
Expand Down Expand Up @@ -130,7 +130,7 @@ void TilesEditorUtils::_thread() {
// Add the viewport at the last moment to avoid rendering too early.
callable_mp((Node *)EditorNode::get_singleton(), &Node::add_child).call_deferred(viewport, false, Node::INTERNAL_MODE_DISABLED);

RS::get_singleton()->connect(SNAME("frame_pre_draw"), callable_mp(const_cast<TilesEditorUtils *>(this), &TilesEditorUtils::_preview_frame_started), Object::CONNECT_ONE_SHOT);
RS::get_singleton()->connect(SNAME("frame_pre_draw"), callable_mp(this, &TilesEditorUtils::_preview_frame_started), Object::CONNECT_ONE_SHOT);

pattern_preview_done.wait();

Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/visual_shader_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2263,7 +2263,7 @@ void VisualShaderEditor::_update_options_menu() {
if ((add_options[i].func != current_func && add_options[i].func != -1) || !_is_available(add_options[i].mode)) {
continue;
}
const_cast<AddOption &>(add_options[i]).temp_idx = i; // save valid id
add_options[i].temp_idx = i; // save valid id
if (add_options[i].is_custom) {
custom_options.push_back(add_options[i]);
} else {
Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/visual_shader_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ class VisualShaderEditor : public ShaderEditor {
bool highend = false;
bool is_custom = false;
bool is_native = false;
int temp_idx = 0;
mutable int temp_idx = 0;

AddOption(const String &p_name = String(), const String &p_category = String(), const String &p_type = String(), const String &p_description = String(), const Vector<Variant> &p_ops = Vector<Variant>(), int p_return_type = -1, int p_mode = -1, int p_func = -1, bool p_highend = false) {
name = p_name;
Expand Down
4 changes: 2 additions & 2 deletions modules/enet/enet_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ Error ENetConnection::dtls_server_setup(const Ref<TLSOptions> &p_options) {
#ifdef GODOT_ENET
ERR_FAIL_NULL_V_MSG(host, ERR_UNCONFIGURED, "The ENetConnection instance isn't currently active.");
ERR_FAIL_COND_V(p_options.is_null() || !p_options->is_server(), ERR_INVALID_PARAMETER);
return enet_host_dtls_server_setup(host, const_cast<TLSOptions *>(p_options.ptr())) ? FAILED : OK;
return enet_host_dtls_server_setup(host, p_options.ptr()) ? FAILED : OK;
#else
ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "ENet DTLS support not available in this build.");
#endif
Expand All @@ -296,7 +296,7 @@ Error ENetConnection::dtls_client_setup(const String &p_hostname, const Ref<TLSO
#ifdef GODOT_ENET
ERR_FAIL_NULL_V_MSG(host, ERR_UNCONFIGURED, "The ENetConnection instance isn't currently active.");
ERR_FAIL_COND_V(p_options.is_null() || p_options->is_server(), ERR_INVALID_PARAMETER);
return enet_host_dtls_client_setup(host, p_hostname.utf8().get_data(), const_cast<TLSOptions *>(p_options.ptr())) ? FAILED : OK;
return enet_host_dtls_client_setup(host, p_hostname.utf8().get_data(), p_options.ptr()) ? FAILED : OK;
#else
ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "ENet DTLS support not available in this build.");
#endif
Expand Down
4 changes: 2 additions & 2 deletions modules/gdscript/gdscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1801,7 +1801,7 @@ bool GDScriptInstance::get(const StringName &p_name, Variant &r_ret) const {
const Variant *args[1] = { &name };

Callable::CallError err;
Variant ret = const_cast<GDScriptFunction *>(E->value)->call(const_cast<GDScriptInstance *>(this), (const Variant **)args, 1, err);
Variant ret = E->value->call(const_cast<GDScriptInstance *>(this), (const Variant **)args, 1, err);
if (err.error == Callable::CallError::CALL_OK && ret.get_type() != Variant::NIL) {
r_ret = ret;
return true;
Expand Down Expand Up @@ -1860,7 +1860,7 @@ void GDScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const
HashMap<StringName, GDScriptFunction *>::ConstIterator E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._get_property_list);
if (E) {
Callable::CallError err;
Variant ret = const_cast<GDScriptFunction *>(E->value)->call(const_cast<GDScriptInstance *>(this), nullptr, 0, err);
Variant ret = E->value->call(const_cast<GDScriptInstance *>(this), nullptr, 0, err);
if (err.error == Callable::CallError::CALL_OK) {
ERR_FAIL_COND_MSG(ret.get_type() != Variant::ARRAY, "Wrong type for _get_property_list, must be an array of dictionaries.");

Expand Down
4 changes: 2 additions & 2 deletions modules/gdscript/gdscript_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3058,9 +3058,9 @@ void GDScriptCompiler::convert_to_initializer_type(Variant &p_variant, const GDS
if (member_t.is_hard_type() && init_t.is_hard_type() &&
member_t.kind == GDScriptParser::DataType::BUILTIN && init_t.kind == GDScriptParser::DataType::BUILTIN) {
if (Variant::can_convert_strict(init_t.builtin_type, member_t.builtin_type)) {
Variant *v = &p_node->initializer->reduced_value;
const Variant *v = &p_node->initializer->reduced_value;
Callable::CallError ce;
Variant::construct(member_t.builtin_type, p_variant, const_cast<const Variant **>(&v), 1, ce);
Variant::construct(member_t.builtin_type, p_variant, &v, 1, ce);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions modules/gdscript/gdscript_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2593,7 +2593,7 @@ static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContex
switch (base_type.kind) {
case GDScriptParser::DataType::CLASS:
if (base_type.class_type->has_function(p_method)) {
const GDScriptParser::FunctionNode *method = base_type.class_type->get_member(p_method).function;
GDScriptParser::FunctionNode *method = base_type.class_type->get_member(p_method).function;
if (!is_static || method->is_static) {
if (method->get_datatype().is_set() && !method->get_datatype().is_variant()) {
r_type.type = method->get_datatype();
Expand All @@ -2604,7 +2604,7 @@ static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContex
const GDScriptParser::ExpressionNode *last_returned_value = nullptr;
GDScriptParser::CompletionContext c = p_context;
c.current_class = base_type.class_type;
c.current_function = const_cast<GDScriptParser::FunctionNode *>(method);
c.current_function = method;
c.current_suite = method->body;

_find_last_return_in_block(c, last_return_line, &last_returned_value);
Expand Down
10 changes: 5 additions & 5 deletions modules/godot_physics_3d/godot_physics_server_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void GodotPhysicsServer3D::space_set_active(RID p_space, bool p_active) {
}

bool GodotPhysicsServer3D::space_is_active(RID p_space) const {
const GodotSpace3D *space = space_owner.get_or_null(p_space);
GodotSpace3D *space = space_owner.get_or_null(p_space);
ERR_FAIL_NULL_V(space, false);

return active_spaces.has(space);
Expand Down Expand Up @@ -1638,8 +1638,8 @@ void GodotPhysicsServer3D::step(real_t p_step) {
island_count = 0;
active_objects = 0;
collision_pairs = 0;
for (const GodotSpace3D *E : active_spaces) {
stepper->step(const_cast<GodotSpace3D *>(E), p_step);
for (GodotSpace3D *E : active_spaces) {
stepper->step(E, p_step);
island_count += E->get_island_count();
active_objects += E->get_active_objects();
collision_pairs += E->get_collision_pairs();
Expand All @@ -1659,8 +1659,8 @@ void GodotPhysicsServer3D::flush_queries() {

uint64_t time_beg = OS::get_singleton()->get_ticks_usec();

for (const GodotSpace3D *E : active_spaces) {
GodotSpace3D *space = const_cast<GodotSpace3D *>(E);
for (GodotSpace3D *E : active_spaces) {
GodotSpace3D *space = E;
space->call_queries();
}

Expand Down
2 changes: 1 addition & 1 deletion modules/godot_physics_3d/godot_physics_server_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class GodotPhysicsServer3D : public PhysicsServer3D {
bool flushing_queries = false;

GodotStep3D *stepper = nullptr;
HashSet<const GodotSpace3D *> active_spaces;
HashSet<GodotSpace3D *> active_spaces;

mutable RID_PtrOwner<GodotShape3D, true> shape_owner;
mutable RID_PtrOwner<GodotSpace3D, true> space_owner;
Expand Down
2 changes: 1 addition & 1 deletion modules/godot_physics_3d/godot_shape_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void GodotShape3D::configure(const AABB &p_aabb) {
aabb = p_aabb;
configured = true;
for (const KeyValue<GodotShapeOwner3D *, int> &E : owners) {
GodotShapeOwner3D *co = const_cast<GodotShapeOwner3D *>(E.key);
GodotShapeOwner3D *co = E.key;
co->_shape_changed();
}
}
Expand Down
6 changes: 3 additions & 3 deletions modules/godot_physics_3d/godot_step_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void GodotStep3D::_populate_island(GodotBody3D *p_body, LocalVector<GodotBody3D
}

for (const KeyValue<GodotConstraint3D *, int> &E : p_body->get_constraint_map()) {
GodotConstraint3D *constraint = const_cast<GodotConstraint3D *>(E.key);
GodotConstraint3D *constraint = E.key;
if (constraint->get_island_step() == _step) {
continue; // Already processed.
}
Expand Down Expand Up @@ -88,8 +88,8 @@ void GodotStep3D::_populate_island(GodotBody3D *p_body, LocalVector<GodotBody3D
void GodotStep3D::_populate_island_soft_body(GodotSoftBody3D *p_soft_body, LocalVector<GodotBody3D *> &p_body_island, LocalVector<GodotConstraint3D *> &p_constraint_island) {
p_soft_body->set_island_step(_step);

for (const GodotConstraint3D *E : p_soft_body->get_constraints()) {
GodotConstraint3D *constraint = const_cast<GodotConstraint3D *>(E);
for (GodotConstraint3D *E : p_soft_body->get_constraints()) {
GodotConstraint3D *constraint = E;
if (constraint->get_island_step() == _step) {
continue; // Already processed.
}
Expand Down
Loading

0 comments on commit 37f56f7

Please sign in to comment.