From 9406b5044753cff3ed20ec28f6c5b6067e6fd5a3 Mon Sep 17 00:00:00 2001 From: bubnikv Date: Fri, 3 Jan 2020 16:32:56 +0100 Subject: [PATCH] Const correctness improvements: removed some unnecessary const_casts that remove const. --- src/PrusaSlicer.cpp | 2 +- src/avrdude/avrdude-slic3r.cpp | 6 ++-- src/libslic3r/Config.cpp | 35 ++++++++++++------------ src/libslic3r/Config.hpp | 2 +- src/libslic3r/Format/AMF.cpp | 8 +++--- src/libslic3r/Format/objparser.cpp | 4 +-- src/libslic3r/Semver.hpp | 2 +- src/libslic3r/utils.cpp | 4 +-- src/slic3r/Config/Version.cpp | 2 +- src/slic3r/GUI/Camera.cpp | 2 +- src/slic3r/GUI/GLShader.cpp | 4 +-- src/slic3r/GUI/PresetBundle.cpp | 8 ++++-- src/slic3r/GUI/RemovableDriveManager.cpp | 2 +- 13 files changed, 41 insertions(+), 40 deletions(-) diff --git a/src/PrusaSlicer.cpp b/src/PrusaSlicer.cpp index f708679c1..d755f0b2e 100644 --- a/src/PrusaSlicer.cpp +++ b/src/PrusaSlicer.cpp @@ -691,7 +691,7 @@ extern "C" { for (size_t i = 0; i < argc; ++ i) argv_narrow.emplace_back(boost::nowide::narrow(argv[i])); for (size_t i = 0; i < argc; ++ i) - argv_ptrs[i] = const_cast(argv_narrow[i].data()); + argv_ptrs[i] = argv_narrow[i].data(); // Call the UTF8 main. return CLI().run(argc, argv_ptrs.data()); } diff --git a/src/avrdude/avrdude-slic3r.cpp b/src/avrdude/avrdude-slic3r.cpp index b561cd843..6dbb84226 100644 --- a/src/avrdude/avrdude-slic3r.cpp +++ b/src/avrdude/avrdude-slic3r.cpp @@ -93,11 +93,11 @@ void AvrDude::priv::unset_handlers() int AvrDude::priv::run_one(const std::vector &args) { - std::vector c_args { const_cast(PACKAGE) }; + std::vector c_args { PACKAGE }; std::string command_line { PACKAGE }; for (const auto &arg : args) { - c_args.push_back(const_cast(arg.data())); + c_args.push_back(arg.c_str()); command_line.push_back(' '); command_line.append(arg); } @@ -107,7 +107,7 @@ int AvrDude::priv::run_one(const std::vector &args) { message_fn(command_line.c_str(), (unsigned)command_line.size()); - const auto res = ::avrdude_main(static_cast(c_args.size()), c_args.data()); + const auto res = ::avrdude_main(static_cast(c_args.size()), const_cast(c_args.data())); return res; } diff --git a/src/libslic3r/Config.cpp b/src/libslic3r/Config.cpp index a81e9521a..bcd017278 100644 --- a/src/libslic3r/Config.cpp +++ b/src/libslic3r/Config.cpp @@ -630,39 +630,38 @@ size_t ConfigBase::load_from_gcode_string(const char* str) return 0; // Walk line by line in reverse until a non-configuration key appears. - char *data_start = const_cast(str); + const char *data_start = str; // boost::nowide::ifstream seems to cook the text data somehow, so less then the 64k of characters may be retrieved. - char *end = data_start + strlen(str); + const char *end = data_start + strlen(str); size_t num_key_value_pairs = 0; for (;;) { // Extract next line. for (--end; end > data_start && (*end == '\r' || *end == '\n'); --end); if (end == data_start) break; - char *start = end; - *(++end) = 0; + const char *start = end ++; for (; start > data_start && *start != '\r' && *start != '\n'; --start); if (start == data_start) break; // Extracted a line from start to end. Extract the key = value pair. - if (end - (++start) < 10 || start[0] != ';' || start[1] != ' ') + if (end - (++ start) < 10 || start[0] != ';' || start[1] != ' ') break; - char *key = start + 2; + const char *key = start + 2; if (!(*key >= 'a' && *key <= 'z') || (*key >= 'A' && *key <= 'Z')) // A key must start with a letter. break; - char *sep = strchr(key, '='); - if (sep == nullptr || sep[-1] != ' ' || sep[1] != ' ') + const char *sep = key; + for (; sep != end && *sep != '='; ++ sep) ; + if (sep == end || sep[-1] != ' ' || sep[1] != ' ') break; - char *value = sep + 2; + const char *value = sep + 2; if (value > end) break; - char *key_end = sep - 1; + const char *key_end = sep - 1; if (key_end - key < 3) break; - *key_end = 0; // The key may contain letters, digits and underscores. - for (char *c = key; c != key_end; ++c) + for (const char *c = key; c != key_end; ++ c) if (!((*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z') || (*c >= '0' && *c <= '9') || *c == '_')) { key = nullptr; break; @@ -670,7 +669,7 @@ size_t ConfigBase::load_from_gcode_string(const char* str) if (key == nullptr) break; try { - this->set_deserialize(key, value); + this->set_deserialize(std::string(key, key_end), std::string(value, end)); ++num_key_value_pairs; } catch (UnknownOptionException & /* e */) { @@ -760,15 +759,15 @@ ConfigOption* DynamicConfig::optptr(const t_config_option_key &opt_key, bool cre void DynamicConfig::read_cli(const std::vector &tokens, t_config_option_keys* extra, t_config_option_keys* keys) { - std::vector args; + std::vector args; // push a bogus executable name (argv[0]) - args.emplace_back(const_cast("")); + args.emplace_back(""); for (size_t i = 0; i < tokens.size(); ++ i) - args.emplace_back(const_cast(tokens[i].c_str())); - this->read_cli(int(args.size()), &args[0], extra, keys); + args.emplace_back(tokens[i].c_str()); + this->read_cli(int(args.size()), args.data(), extra, keys); } -bool DynamicConfig::read_cli(int argc, char** argv, t_config_option_keys* extra, t_config_option_keys* keys) +bool DynamicConfig::read_cli(int argc, const char* const argv[], t_config_option_keys* extra, t_config_option_keys* keys) { // cache the CLI option => opt_key mapping std::map opts; diff --git a/src/libslic3r/Config.hpp b/src/libslic3r/Config.hpp index c49dd134e..87a79d93f 100644 --- a/src/libslic3r/Config.hpp +++ b/src/libslic3r/Config.hpp @@ -1779,7 +1779,7 @@ public: // Command line processing void read_cli(const std::vector &tokens, t_config_option_keys* extra, t_config_option_keys* keys = nullptr); - bool read_cli(int argc, char** argv, t_config_option_keys* extra, t_config_option_keys* keys = nullptr); + bool read_cli(int argc, const char* const argv[], t_config_option_keys* extra, t_config_option_keys* keys = nullptr); std::map>::const_iterator cbegin() const { return options.cbegin(); } std::map>::const_iterator cend() const { return options.cend(); } diff --git a/src/libslic3r/Format/AMF.cpp b/src/libslic3r/Format/AMF.cpp index 847ac6f5c..5b65d8fa2 100644 --- a/src/libslic3r/Format/AMF.cpp +++ b/src/libslic3r/Format/AMF.cpp @@ -683,7 +683,7 @@ void AMFParserContext::endElement(const char * /* name */) config->set_deserialize(opt_key, m_value[1]); } else if (m_path.size() == 3 && m_path[1] == NODE_TYPE_OBJECT && m_object && strcmp(opt_key, "layer_height_profile") == 0) { // Parse object's layer height profile, a semicolon separated list of floats. - char *p = const_cast(m_value[1].c_str()); + char *p = m_value[1].data(); for (;;) { char *end = strchr(p, ';'); if (end != nullptr) @@ -698,7 +698,7 @@ void AMFParserContext::endElement(const char * /* name */) // Parse object's layer height profile, a semicolon separated list of floats. unsigned char coord_idx = 0; Eigen::Matrix point(Eigen::Matrix::Zero()); - char *p = const_cast(m_value[1].c_str()); + char *p = m_value[1].data(); for (;;) { char *end = strchr(p, ';'); if (end != nullptr) @@ -718,7 +718,7 @@ void AMFParserContext::endElement(const char * /* name */) else if (m_path.size() == 5 && m_path[1] == NODE_TYPE_OBJECT && m_path[3] == NODE_TYPE_RANGE && m_object && strcmp(opt_key, "layer_height_range") == 0) { // Parse object's layer_height_range, a semicolon separated doubles. - char* p = const_cast(m_value[1].c_str()); + char* p = m_value[1].data(); char* end = strchr(p, ';'); *end = 0; @@ -1010,7 +1010,7 @@ bool load_amf(const char* path, DynamicPrintConfig* config, Model* model, bool c return false; std::string zip_mask(2, '\0'); - file.read(const_cast(zip_mask.data()), 2); + file.read(zip_mask.data(), 2); file.close(); return (zip_mask == "PK") ? load_amf_archive(path, config, model, check_version) : load_amf_file(path, config, model); diff --git a/src/libslic3r/Format/objparser.cpp b/src/libslic3r/Format/objparser.cpp index c805667f8..28f2fdbbd 100644 --- a/src/libslic3r/Format/objparser.cpp +++ b/src/libslic3r/Format/objparser.cpp @@ -420,7 +420,7 @@ bool loadvector(FILE *pFile, std::vector &v) if (::fread(&len, sizeof(len), 1, pFile) != 1) return false; std::string s(" ", len); - if (::fread(const_cast(s.c_str()), 1, len, pFile) != len) + if (::fread(s.data(), 1, len, pFile) != len) return false; v.push_back(std::move(s)); } @@ -442,7 +442,7 @@ bool loadvectornameidx(FILE *pFile, std::vector &v) if (::fread(&len, sizeof(len), 1, pFile) != 1) return false; v[i].name.assign(" ", len); - if (::fread(const_cast(v[i].name.c_str()), 1, len, pFile) != len) + if (::fread(v[i].name.data(), 1, len, pFile) != len) return false; } return true; diff --git a/src/libslic3r/Semver.hpp b/src/libslic3r/Semver.hpp index 172391077..24ca74f83 100644 --- a/src/libslic3r/Semver.hpp +++ b/src/libslic3r/Semver.hpp @@ -149,7 +149,7 @@ private: Semver(semver_t ver) : ver(ver) {} static semver_t semver_zero() { return { 0, 0, 0, nullptr, nullptr }; } - static char * strdup(const std::string &str) { return ::semver_strdup(const_cast(str.c_str())); } + static char * strdup(const std::string &str) { return ::semver_strdup(str.data()); } }; diff --git a/src/libslic3r/utils.cpp b/src/libslic3r/utils.cpp index b7155ddc8..36653cdaf 100644 --- a/src/libslic3r/utils.cpp +++ b/src/libslic3r/utils.cpp @@ -535,7 +535,7 @@ std::string encode_path(const char *src) // Convert a wide string to a local code page. int size_needed = ::WideCharToMultiByte(0, 0, wstr_src.data(), (int)wstr_src.size(), nullptr, 0, nullptr, nullptr); std::string str_dst(size_needed, 0); - ::WideCharToMultiByte(0, 0, wstr_src.data(), (int)wstr_src.size(), const_cast(str_dst.data()), size_needed, nullptr, nullptr); + ::WideCharToMultiByte(0, 0, wstr_src.data(), (int)wstr_src.size(), str_dst.data(), size_needed, nullptr, nullptr); return str_dst; #else /* WIN32 */ return src; @@ -552,7 +552,7 @@ std::string decode_path(const char *src) // Convert the string encoded using the local code page to a wide string. int size_needed = ::MultiByteToWideChar(0, 0, src, len, nullptr, 0); std::wstring wstr_dst(size_needed, 0); - ::MultiByteToWideChar(0, 0, src, len, const_cast(wstr_dst.data()), size_needed); + ::MultiByteToWideChar(0, 0, src, len, wstr_dst.data(), size_needed); // Convert a wide string to utf8. return boost::nowide::narrow(wstr_dst.c_str()); #else /* WIN32 */ diff --git a/src/slic3r/Config/Version.cpp b/src/slic3r/Config/Version.cpp index fcebf88d1..2104a6eea 100644 --- a/src/slic3r/Config/Version.cpp +++ b/src/slic3r/Config/Version.cpp @@ -205,7 +205,7 @@ size_t Index::load(const boost::filesystem::path &path) #endif ++ idx_line; // Skip the initial white spaces. - char *key = left_trim(const_cast(line.data())); + char *key = left_trim(line.data()); if (*key == '#') // Skip a comment line. continue; diff --git a/src/slic3r/GUI/Camera.cpp b/src/slic3r/GUI/Camera.cpp index bb514e888..fa7a3be21 100644 --- a/src/slic3r/GUI/Camera.cpp +++ b/src/slic3r/GUI/Camera.cpp @@ -338,7 +338,7 @@ void Camera::debug_render() const float fov = (float)get_fov(); float gui_scale = (float)get_gui_scale(); - ImGui::InputText("Type", const_cast(type.data()), type.length(), ImGuiInputTextFlags_ReadOnly); + ImGui::InputText("Type", type.data(), type.length(), ImGuiInputTextFlags_ReadOnly); ImGui::Separator(); ImGui::InputFloat3("Position", position.data(), "%.6f", ImGuiInputTextFlags_ReadOnly); ImGui::InputFloat3("Target", target.data(), "%.6f", ImGuiInputTextFlags_ReadOnly); diff --git a/src/slic3r/GUI/GLShader.cpp b/src/slic3r/GUI/GLShader.cpp index 11f109fd5..c31076060 100644 --- a/src/slic3r/GUI/GLShader.cpp +++ b/src/slic3r/GUI/GLShader.cpp @@ -142,7 +142,7 @@ bool GLShader::load_from_file(const char* fragment_shader_filename, const char* int file_length = (int)vs.tellg(); vs.seekg(0, vs.beg); std::string vertex_shader(file_length, '\0'); - vs.read(const_cast(vertex_shader.data()), file_length); + vs.read(vertex_shader.data(), file_length); if (!vs.good()) return false; @@ -156,7 +156,7 @@ bool GLShader::load_from_file(const char* fragment_shader_filename, const char* file_length = (int)fs.tellg(); fs.seekg(0, fs.beg); std::string fragment_shader(file_length, '\0'); - fs.read(const_cast(fragment_shader.data()), file_length); + fs.read(fragment_shader.data(), file_length); if (!fs.good()) return false; diff --git a/src/slic3r/GUI/PresetBundle.cpp b/src/slic3r/GUI/PresetBundle.cpp index 3ede7a4e0..219d7b89d 100644 --- a/src/slic3r/GUI/PresetBundle.cpp +++ b/src/slic3r/GUI/PresetBundle.cpp @@ -560,9 +560,11 @@ DynamicPrintConfig PresetBundle::full_fff_config() const while (filament_configs.size() < num_extruders) filament_configs.emplace_back(&this->filaments.first_visible().config); for (const DynamicPrintConfig *cfg : filament_configs) { - compatible_printers_condition.emplace_back(Preset::compatible_printers_condition(*const_cast(cfg))); - compatible_prints_condition .emplace_back(Preset::compatible_prints_condition(*const_cast(cfg))); - inherits .emplace_back(Preset::inherits(*const_cast(cfg))); + // The compatible_prints/printers_condition() returns a reference to configuration key, which may not yet exist. + DynamicPrintConfig &cfg_rw = *const_cast(cfg); + compatible_printers_condition.emplace_back(Preset::compatible_printers_condition(cfg_rw)); + compatible_prints_condition .emplace_back(Preset::compatible_prints_condition(cfg_rw)); + inherits .emplace_back(Preset::inherits(cfg_rw)); } // Option values to set a ConfigOptionVector from. std::vector filament_opts(num_extruders, nullptr); diff --git a/src/slic3r/GUI/RemovableDriveManager.cpp b/src/slic3r/GUI/RemovableDriveManager.cpp index 353337e47..9ef31748d 100644 --- a/src/slic3r/GUI/RemovableDriveManager.cpp +++ b/src/slic3r/GUI/RemovableDriveManager.cpp @@ -325,7 +325,7 @@ void RemovableDriveManager::inspect_file(const std::string &path, const std::str { if(pw->pw_name == username) { - std::string name = basename(const_cast(path.c_str())); + std::string name = basename(path.data()); m_current_drives.push_back(DriveData(name,path)); } }