Skip to content

Commit

Permalink
Clean up const_cast
Browse files Browse the repository at this point in the history
  • Loading branch information
rune-scape committed Aug 1, 2024
1 parent 440f1ea commit 6e95acc
Show file tree
Hide file tree
Showing 76 changed files with 357 additions and 318 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 @@ -119,7 +119,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 @@ -168,7 +168,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 @@ -247,7 +247,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
2 changes: 1 addition & 1 deletion core/os/condition_variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ 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.lock));
condition.wait(p_lock.lock);
}

_ALWAYS_INLINE_ void notify_one() const {
Expand Down
2 changes: 1 addition & 1 deletion core/os/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ template <typename MutexT>
class MutexLock {
friend class ConditionVariable;

THREADING_NAMESPACE::unique_lock<typename MutexT::StdMutexType> lock;
mutable THREADING_NAMESPACE::unique_lock<typename MutexT::StdMutexType> lock;

public:
explicit MutexLock(const MutexT &p_mutex) :
Expand Down
2 changes: 1 addition & 1 deletion core/os/pool_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ bool PoolAllocator::is_locked(ID p_mem) const {
}

mt_lock();
const Entry *e = const_cast<PoolAllocator *>(this)->get_entry(p_mem);
const Entry *e = get_entry(p_mem);
if (!e) {
mt_unlock();
ERR_PRINT("!e");
Expand Down
2 changes: 1 addition & 1 deletion core/os/safe_binary_mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ template <int Tag>
class MutexLock<SafeBinaryMutex<Tag>> {
friend class ConditionVariable;

THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> lock;
mutable THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> lock;

public:
_ALWAYS_INLINE_ explicit MutexLock(const SafeBinaryMutex<Tag> &p_mutex) :
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
4 changes: 2 additions & 2 deletions core/variant/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ bool Variant::is_null() const {
}

bool Variant::initialize_ref(Object *p_object) {
RefCounted *ref_counted = const_cast<RefCounted *>(static_cast<const RefCounted *>(p_object));
RefCounted *ref_counted = static_cast<RefCounted *>(p_object);
if (!ref_counted->init_ref()) {
return false;
}
Expand Down Expand Up @@ -1182,7 +1182,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 drivers/windows/dir_access_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ uint64_t DirAccessWindows::get_space_left() {
}

String DirAccessWindows::get_filesystem_type() const {
String path = fix_path(const_cast<DirAccessWindows *>(this)->get_current_dir());
String path = fix_path(get_current_dir());

int unit_end = path.find(":");
ERR_FAIL_COND_V(unit_end == -1, String());
Expand Down
12 changes: 6 additions & 6 deletions editor/editor_help_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ bool EditorHelpSearch::Runner::_phase_match_classes() {
signal.name = _all_terms_in_name(class_doc->signals[i].name);
signal.keyword = _match_keywords_in_all_terms(class_doc->signals[i].keywords);
if (signal.name || !signal.keyword.is_empty()) {
signal.doc = const_cast<DocData::MethodDoc *>(&class_doc->signals[i]);
signal.doc = &class_doc->signals[i];
match.signals.push_back(signal);
}
}
Expand All @@ -516,7 +516,7 @@ bool EditorHelpSearch::Runner::_phase_match_classes() {
constant.name = _all_terms_in_name(class_doc->constants[i].name);
constant.keyword = _match_keywords_in_all_terms(class_doc->constants[i].keywords);
if (constant.name || !constant.keyword.is_empty()) {
constant.doc = const_cast<DocData::ConstantDoc *>(&class_doc->constants[i]);
constant.doc = &class_doc->constants[i];
match.constants.push_back(constant);
}
}
Expand All @@ -527,7 +527,7 @@ bool EditorHelpSearch::Runner::_phase_match_classes() {
property.name = _all_terms_in_name(class_doc->properties[i].name);
property.keyword = _match_keywords_in_all_terms(class_doc->properties[i].keywords);
if (property.name || !property.keyword.is_empty()) {
property.doc = const_cast<DocData::PropertyDoc *>(&class_doc->properties[i]);
property.doc = &class_doc->properties[i];
match.properties.push_back(property);
}
}
Expand All @@ -538,7 +538,7 @@ bool EditorHelpSearch::Runner::_phase_match_classes() {
theme_property.name = _all_terms_in_name(class_doc->theme_properties[i].name);
theme_property.keyword = _match_keywords_in_all_terms(class_doc->theme_properties[i].keywords);
if (theme_property.name || !theme_property.keyword.is_empty()) {
theme_property.doc = const_cast<DocData::ThemeItemDoc *>(&class_doc->theme_properties[i]);
theme_property.doc = &class_doc->theme_properties[i];
match.theme_properties.push_back(theme_property);
}
}
Expand All @@ -549,7 +549,7 @@ bool EditorHelpSearch::Runner::_phase_match_classes() {
annotation.name = _all_terms_in_name(class_doc->annotations[i].name);
annotation.keyword = _match_keywords_in_all_terms(class_doc->annotations[i].keywords);
if (annotation.name || !annotation.keyword.is_empty()) {
annotation.doc = const_cast<DocData::MethodDoc *>(&class_doc->annotations[i]);
annotation.doc = &class_doc->annotations[i];
match.annotations.push_back(annotation);
}
}
Expand Down Expand Up @@ -716,7 +716,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
2 changes: 1 addition & 1 deletion editor/editor_help_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class EditorHelpSearch::Runner : public RefCounted {

template <typename T>
struct MemberMatch {
T *doc = nullptr;
const T *doc = nullptr;
bool name = false;
String keyword;
};
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 @@ -2155,7 +2155,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 @@ -364,7 +364,7 @@ class VisualShaderEditor : public VBoxContainer {
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 @@ -1793,7 +1793,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 @@ -1852,7 +1852,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 @@ -3048,9 +3048,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 @@ -2589,7 +2589,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 @@ -2600,7 +2600,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
Loading

0 comments on commit 6e95acc

Please sign in to comment.