f2e-spec icon indicating copy to clipboard operation
f2e-spec copied to clipboard

Using C++11 facilities to clean up code and Boost dependencies

Open Tronic opened this issue 12 years ago • 38 comments

We can reduce but not entirely remove dependency on Boost by replacing parts of it with stdlib equivalents. This bug is a tracker for features to replace, and for discussion about compiler compatibility.

  • [ ] boost/algorithm/string.hpp: No stdlib equivalents
  • [ ] boost/bind.hpp: std::bind in functional
  • [ ] boost/circular_buffer.hpp: No stdlib equivalent
  • [ ] boost/cstdint.hpp: Now in stdlib (cstdint)
  • [ ] boost/date_time.hpp: Partial support from stdlib (chrono)
  • [ ] boost/filesystem.hpp: C++ still doesn't know of files :/
  • [ ] boost/foreach.hpp: C++11 built-in range-based for!
  • [ ] boost/format.hpp: No direct stdlib equivalent but see std::to_string(num)
  • [ ] boost/function.hpp: std::function (functional)
  • [ ] boost/iostreams/stream_buffer.hpp: ???
  • [ ] boost/lexical_cast.hpp: See std::to_string and std::stoi etc. (string)
  • [ ] boost/noncopyable.hpp: C++11 deleted functions (but noncopyable is cleaner in code)
  • [ ] boost/program_options.hpp: Never gonna get into stdlib
  • [ ] boost/ptr_container/: Not in C++11 and probably not in C++14 either.
  • [ ] boost/regex.hpp: The last time I tried (quite recently), the libstdc++ implementation was unusably incomplete.
  • [ ] boost/smart_ptr/scoped_ptr.hpp: std::unique_ptr in memory (also replaces std::auto_ptr)
  • [ ] boost/smart_ptr/shared_ptr.hpp: std::unique_ptr (if shared_ptr was only used for calling a free function of a C lib), std::shared_ptr if the resource is actually shared, C++11 move semantics instead if a resourse is only being moved.
  • [ ] boost/thread/: std::thread and stdlib mutexes (no idea if stdlib implementation is good yet or if all necessary lock types are included or not)
  • [ ] boost/variant.hpp: Didn't make it into C++11

We don't currently use Boost.ASIO directly but likely will, soon. Sockets didn't make it into C++11.

Tronic avatar May 20 '13 15:05 Tronic

LLVM folks also have an automatic converter tool: http://blog.llvm.org/2013/04/status-of-c11-migrator.html

Tronic avatar May 20 '13 15:05 Tronic

Nice work on the list. Since we can't get rid of the whole boost, I don't consider this a very high priority. However, it could still be nice to reduce the amount of compiled boost components we depend on, namely thread, date_time and possibly regex (if I didn't miss anything).

I'll dump here links to C++11 support tables.

tapio avatar May 20 '13 17:05 tapio

so, this means I should think of an alternative to boost:asio lib for webserver implementation (I was thinking about using that one, but I won't start implementation before 0.8 release) since we're trying to ditch boost libraries?

nieknooijens avatar May 20 '13 17:05 nieknooijens

We aren't trying to ditch Boost, nor can we, as seen on the list. Keeping any library deps as light as possible is a good idea, that's why I made the list (Boost was used as a reference for the C++11 standard and thus it shares a lot of the functionality that is now part of the standard library). Boost.ASIO is the most well known and most heavily tested C++ socket library, so using it makes sense. Only if we switched to another platform library (e.g. Qt), should we consider using something else, IMO.

Tronic avatar May 20 '13 18:05 Tronic

I'm going to apply cpp11migrate on master tonight. Please notify me and push any local changes that you may have done because a huge conflict will be created otherwise.

Tronic avatar May 20 '13 18:05 Tronic

This tool is pretty impressive. Could have removed the empty line, though ;)

Original:

       for (Paths::const_iterator it = paths.begin(); it != paths.end(); ++it) {
               fs::path p = *it;
               p /= filename;

Migrated:

       for (auto p : paths) {

               p /= filename;

Tronic avatar May 20 '13 21:05 Tronic

Commit done. Feel free to hack again :)

Tronic avatar May 20 '13 23:05 Tronic

shouldn't we just close this?

nieknooijens avatar May 28 '13 07:05 nieknooijens

Nothing has yet been done to reduce dependency on Boost (it is actually being increased in fspath branch). cpp11migrate only did some general syntax clean-up.

tapio avatar May 28 '13 09:05 tapio

I had problems compiling using boost 1.53 (1.49 works fine) it keeps telling me :-1: error: No rule to make target /usr/lib/libboost_thread-mt.so', needed bygame/performous'. Stop. but libboost-thread-1.53 is installed!! i'm reverting back to 1.49 for now....

nieknooijens avatar May 29 '13 10:05 nieknooijens

@nieknooijens: I too get errors like that sometimes when upgrading libraries. They go away by deleting the build folder and starting from scratch.

tapio avatar May 29 '13 10:05 tapio

Deleting CMakeCache.txt is usually enough. This also happens when switching between torrent and master branches because (for some reason, probably no good) they have different FindBoost scripts.

Tronic avatar May 29 '13 19:05 Tronic

Apparently mingw lacks thread support, so we need to stick with Boost.Thread on that for now.

Tronic avatar Jun 18 '13 00:06 Tronic

Is this bug still relevant or should it be closed?

nieknooijens avatar Aug 21 '14 17:08 nieknooijens

The stdthread branch replaces Boost xtime/date_time, threads/locks, bind, function, and the BOOST_FOREACH macro with C++11 equivalents, and others such as cstdint are already implemented in master. Work still remains to be done. Most notable things include smart pointers, lexical_cast/format and regex.

Tronic avatar Aug 28 '14 07:08 Tronic

c++ does know about files!!! http://en.cppreference.com/w/cpp/experimental/fs/directory_iterator/directory_iterator

nieknooijens avatar Sep 07 '14 08:09 nieknooijens

shouldn't we replace boost::shared_ptr with std::shared_ptr ??

nieknooijens avatar Sep 13 '14 13:09 nieknooijens

If you need a lightweight c++ library to replace part or whole Boost, look at Poco

zosrothko avatar Oct 31 '17 09:10 zosrothko

slowly we're getting there: https://github.com/performous/performous/pull/339

Baklap4 avatar Mar 19 '18 23:03 Baklap4

Slowly getting there by replacing boost::shared_ptrs and boost::thread -> std::thread: #347

Baklap4 avatar Mar 29 '18 20:03 Baklap4

Used @Tronic 's list of items to check of the items we have done already to get a more detailed status into this cleanup session:

  • [ ] boost/algorithm/string.hpp: No stdlib equivalents
  • [x] boost/bind.hpp: std::bind in functional
  • [x] boost/circular_buffer.hpp: No stdlib equivalent
  • [x] boost/cstdint.hpp: Now in stdlib (cstdint)
  • [x] boost/date_time.hpp: Partial support from stdlib (chrono)
  • [x] boost/filesystem.hpp: C++ still doesn't know of files :/
  • [x] boost/foreach.hpp: C++11 built-in range-based for!
  • [ ] boost/format.hpp: No direct stdlib equivalent but see std::to_string(num)
  • [x] boost/function.hpp: std::function (functional)
  • [ ] boost/iostreams/stream_buffer.hpp: ???
  • [x] boost/lexical_cast.hpp: See std::to_string and std::stoi etc. (string)
  • [x] boost/noncopyable.hpp: C++11 deleted functions (but noncopyable is cleaner in code)
  • [ ] boost/program_options.hpp: Never gonna get into stdlib
  • [x] boost/ptr_container/: LK: There's many of these in code base still, but as I said below, they're not needed anymore.
  • [x] boost/regex.hpp: The last time I tried (quite recently), the libstdc++ implementation was unusably incomplete.
  • [x] boost/smart_ptr/scoped_ptr.hpp: std::unique_ptr in memory (also replaces std::auto_ptr)
  • [x] boost/smart_ptr/shared_ptr.hpp: std::unique_ptr (if shared_ptr was only used for calling a free function of a C lib), std::shared_ptr if the resource is actually shared, C++11 move semantics instead if a resourse is only being moved.
  • [x] boost/thread/: std::thread and stdlib mutexes (no idea if stdlib implementation is good yet or if all necessary lock types are included or not)
  • [x] boost/variant.hpp: Didn't make it into C++11

Baklap4 avatar Oct 15 '18 18:10 Baklap4

I replaced some boost::function calls with the std version in my GL branch. Also, ptr_container is not really needed anymore because now stl containers can contain smart pointers (at least one instance of which I also fixed in the GL branch, see ShaderMap)

Lord-Kamina avatar Oct 15 '18 18:10 Lord-Kamina

@Lord-Kamina please cross off what you think is already fixed i'm currently checking master upon these

Baklap4 avatar Oct 15 '18 18:10 Baklap4

Also, correct me if I'm wrong but we're actually targetting c++14 now. No?

Edit: @Baklap4 Updated with things not appearing in a project-wide search of the GL branch.

Lord-Kamina avatar Oct 15 '18 18:10 Lord-Kamina

As far as master branch i've ticked off all aswell with an incoming merge request for some easy to fix shizzle

Baklap4 avatar Oct 15 '18 18:10 Baklap4

BTW. Here's the specific relevant commit you can use as a base if you want to tackle the remaining ptr_map and ptr_vectors. If not I'll do them tomorrow.

https://github.com/performous/performous/commit/c234b7934d67b4e01ca35ea7e0a2ac17310cb863

Lord-Kamina avatar Oct 15 '18 21:10 Lord-Kamina

Thanks it's not all ptr_maps and vectors though. I'm atm busy with tackling the cachemap. I fixed all the ptr_vectors already

Baklap4 avatar Oct 15 '18 21:10 Baklap4

@Lord-Kamina some weird stuff happens if i cherry-pick that specific commit within audio.cc hmmm

Baklap4 avatar Oct 15 '18 22:10 Baklap4

Yeah; don't. I just linked it to show an example of how I solved it. Have you had time to finish checking that PR?

Lord-Kamina avatar Oct 15 '18 22:10 Lord-Kamina

TODO

  • [x] boost/format.hpp can be replaced with the much faster: fmtlib/fmt -> #714
  • [ ] boost/iostreams/stream_buffer.hpp can be replaced with std::fstream since we don't do fancy stuff with the streams. So this is basicly a lot of bloat for a simple read stream command.
  • [ ] boost/algorithm/string.hpp can probably be replaced with stl functionality as it's just manipulating strings.

This would leave us with just boost-program-options as a dependency on boost. Yet it does add another (but faster) dependency: fmtlib

Baklap4 avatar Apr 04 '22 12:04 Baklap4