Skip to content

mid.cpp: Replace std::to_string with compatible solution#176

Merged
mywave82 merged 2 commits into
adplug:masterfrom
dmitrysmagin:to_string_fix
Mar 26, 2023
Merged

mid.cpp: Replace std::to_string with compatible solution#176
mywave82 merged 2 commits into
adplug:masterfrom
dmitrysmagin:to_string_fix

Conversation

@dmitrysmagin

Copy link
Copy Markdown
Contributor

Trivia: older mingw/gcc 4.8.x versions don't have std::to_string implementation.
And since to_string is used only in one sole case in mid.cpp to show the midi_type value, it can be safely replaced with compatible solution.
'midi_type' can hold 0, 1 or 2 only, so we should care only about these values.

Why bother? There are still some cross-toolchains that are nailed to old gcc versions that won't be ever updated. The rest of the code is absolutely 4.8.x compliant, so why not make it slightly more compatible?

@mywave82

Copy link
Copy Markdown
Contributor

last alternative would be to have a sub switch with different text?
"type 0 - single track"
"type 1 - multi track"
"type 2 - multi pattern"

@dmitrysmagin

Copy link
Copy Markdown
Contributor Author

I think, showing just 'Type 0' or 'Type 1' would be enough. Any other objections?

@Malvineous

Copy link
Copy Markdown
Member

Type 2 files definitely exist although rare, so you'd need to be able to handle that too.

If you want to do it in a true cross-platform manner, in a way that will work regardless of which character set is in use, you'll probably have to use string streams.

I used this macro in Camoto, you are welcome to borrow it:

#include <sstream>
/**
 * String creation macro (with iostream syntax).
 * 
 * Useful for assigning strings in constructor initialisation lists.
 *
 * @code
 * std::string str = createString("The value is " << iValue);
 * @endcode
 */
#define createString(a) \
        (static_cast<const std::ostringstream&>(std::ostringstream() << a).str())

Then you would write:

return createString("General MIDI (type " << midi_type << ")");

It's probably only worth doing this though if there are a number of different places you can use it, across multiple files. It's probably overkill for just a single use.

@sagamusix

Copy link
Copy Markdown
Contributor

Maybe a compromise that doesn't assume ASCII but also doesn't pull in stringstream would be std::string(1, '0' + midi_type) which doesn't hardcode '0' as 0x30 and thus is also a bit more understandable.

BTW, the readme still mentions that AdPlug is "currently tested" with GCC 4.1, which evidently cannot be true. Maybe that information should be removed...

@mywave82

Copy link
Copy Markdown
Contributor

tweak from @sagamusix is more readable, and with a comment perhaps about why we do it.

return std::string("General MIDI (type " + std::string(1, '0' + midi_type) + ")"); // avoid using std::to_string(midi_type) which is c++11 ```

I also added a issue about that testing currently does not test gcc 4.1, this detail probably has been missing since adplug was migrated to github

@dmitrysmagin

dmitrysmagin commented Mar 26, 2023

Copy link
Copy Markdown
Contributor Author

I've updated the PR as @mywave82 suggested.
If being more specific, std::to_string should be avoided not because it's c++11, but because its implementation was broken/missing for a long time specifically in mingw32, but later fixed in mingw64 only, afaik.

https://stackoverflow.com/questions/12975341/to-string-is-not-a-member-of-std-says-g-mingw

@mywave82 mywave82 merged commit 9cf8835 into adplug:master Mar 26, 2023
@mywave82

Copy link
Copy Markdown
Contributor

Thank you for the patience

mywave82 added a commit to mywave82/adplug that referenced this pull request May 7, 2023
mywave82 added a commit to mywave82/adplug that referenced this pull request May 7, 2023
mywave82 added a commit to mywave82/adplug that referenced this pull request May 7, 2023
mywave82 added a commit that referenced this pull request May 11, 2023
* We need a more complete implementation of nullptr in c++98
* Using #include <cstdint> is a c++11 feature, using #include <stdint.h> works in all C++ versions
* non-static data member initializers only available from c++11
@dmitrysmagin dmitrysmagin deleted the to_string_fix branch August 29, 2024 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants