Const correctness improvements:

removed some unnecessary const_casts that remove const.
This commit is contained in:
bubnikv 2020-01-03 16:32:56 +01:00
parent 30f7a2b8e5
commit 9406b50447
13 changed files with 41 additions and 40 deletions

View File

@ -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<char*>(argv_narrow[i].data());
argv_ptrs[i] = argv_narrow[i].data();
// Call the UTF8 main.
return CLI().run(argc, argv_ptrs.data());
}

View File

@ -93,11 +93,11 @@ void AvrDude::priv::unset_handlers()
int AvrDude::priv::run_one(const std::vector<std::string> &args) {
std::vector<char*> c_args { const_cast<char*>(PACKAGE) };
std::vector<const char*> c_args { PACKAGE };
std::string command_line { PACKAGE };
for (const auto &arg : args) {
c_args.push_back(const_cast<char*>(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<std::string> &args) {
message_fn(command_line.c_str(), (unsigned)command_line.size());
const auto res = ::avrdude_main(static_cast<int>(c_args.size()), c_args.data());
const auto res = ::avrdude_main(static_cast<int>(c_args.size()), const_cast<char**>(c_args.data()));
return res;
}

View File

@ -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<char*>(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<std::string> &tokens, t_config_option_keys* extra, t_config_option_keys* keys)
{
std::vector<char*> args;
std::vector<const char*> args;
// push a bogus executable name (argv[0])
args.emplace_back(const_cast<char*>(""));
args.emplace_back("");
for (size_t i = 0; i < tokens.size(); ++ i)
args.emplace_back(const_cast<char *>(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<std::string,std::string> opts;

View File

@ -1779,7 +1779,7 @@ public:
// Command line processing
void read_cli(const std::vector<std::string> &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<t_config_option_key, std::unique_ptr<ConfigOption>>::const_iterator cbegin() const { return options.cbegin(); }
std::map<t_config_option_key, std::unique_ptr<ConfigOption>>::const_iterator cend() const { return options.cend(); }

View File

@ -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<char*>(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<float, 5, 1, Eigen::DontAlign> point(Eigen::Matrix<float, 5, 1, Eigen::DontAlign>::Zero());
char *p = const_cast<char*>(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<char*>(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<char*>(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);

View File

@ -420,7 +420,7 @@ bool loadvector(FILE *pFile, std::vector<std::string> &v)
if (::fread(&len, sizeof(len), 1, pFile) != 1)
return false;
std::string s(" ", len);
if (::fread(const_cast<char*>(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<T> &v)
if (::fread(&len, sizeof(len), 1, pFile) != 1)
return false;
v[i].name.assign(" ", len);
if (::fread(const_cast<char*>(v[i].name.c_str()), 1, len, pFile) != len)
if (::fread(v[i].name.data(), 1, len, pFile) != len)
return false;
}
return true;

View File

@ -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<char*>(str.c_str())); }
static char * strdup(const std::string &str) { return ::semver_strdup(str.data()); }
};

View File

@ -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<char*>(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<wchar_t*>(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 */

View File

@ -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<char*>(line.data()));
char *key = left_trim(line.data());
if (*key == '#')
// Skip a comment line.
continue;

View File

@ -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<char*>(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);

View File

@ -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<char*>(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<char*>(fragment_shader.data()), file_length);
fs.read(fragment_shader.data(), file_length);
if (!fs.good())
return false;

View File

@ -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<DynamicPrintConfig*>(cfg)));
compatible_prints_condition .emplace_back(Preset::compatible_prints_condition(*const_cast<DynamicPrintConfig*>(cfg)));
inherits .emplace_back(Preset::inherits(*const_cast<DynamicPrintConfig*>(cfg)));
// The compatible_prints/printers_condition() returns a reference to configuration key, which may not yet exist.
DynamicPrintConfig &cfg_rw = *const_cast<DynamicPrintConfig*>(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<const ConfigOption*> filament_opts(num_extruders, nullptr);

View File

@ -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<char*>(path.c_str()));
std::string name = basename(path.data());
m_current_drives.push_back(DriveData(name,path));
}
}