Permalink
Browse files

Fixes for C++11 compatibility.

  • Loading branch information...
1 parent e7e0850 commit c6ef90e5e5403c3c4162a487341361fdebeeb57d @rakshasa committed Nov 17, 2011
View
1 configure.ac
@@ -36,6 +36,7 @@ TORRENT_ENABLE_DEBUG
TORRENT_ENABLE_EXTRA_DEBUG
TORRENT_ENABLE_WERROR
TORRENT_ENABLE_TR1
+TORRENT_ENABLE_CXX11
TORRENT_DISABLE_IPV6
View
24 scripts/checks.m4
@@ -381,6 +381,30 @@ AC_DEFUN([TORRENT_CHECK_TR1], [
AC_LANG_POP(C++)
])
+AC_DEFUN([TORRENT_CHECK_CXX11], [
+ AC_LANG_PUSH(C++)
+ AC_MSG_CHECKING(for C++11 support)
+
+ AC_COMPILE_IFELSE(
+ [[#include <functional>
+ #include <unordered_map>
+ class Foo;
+ typedef std::unordered_map<Foo*, int> Bar;
+
+ union test { Bar b1; };
+ ]],
+ [
+ AC_MSG_RESULT(yes)
+ AC_DEFINE(HAVE_CXX11, 1, Define to 1 if your C++ compiler has support for C++11.)
+ ],
+ [
+ AC_MSG_RESULT(no)
+ ]
+ )
+
+ AC_LANG_POP(C++)
+])
+
AC_DEFUN([TORRENT_WITH_FASTCGI], [
AC_ARG_WITH(fastcgi,
[ --with-fastcgi=PATH Enable FastCGI RPC support. (DO NOT USE)],
View
15 scripts/common.m4
@@ -295,3 +295,18 @@ AC_DEFUN([TORRENT_ENABLE_TR1], [
TORRENT_CHECK_TR1()
])
])
+
+AC_DEFUN([TORRENT_ENABLE_CXX11], [
+ AC_ARG_ENABLE(std_c++11,
+ [ --disable-std_c++11 disable check for support for C++11 [[default=enable]]],
+ [
+ if test "$enableval" = "yes"; then
+ TORRENT_CHECK_CXX11()
+ else
+ AC_MSG_CHECKING(for C++11 support)
+ AC_MSG_RESULT(disabled)
+ fi
+ ],[
+ TORRENT_CHECK_CXX11()
+ ])
+])
View
6 src/dht/dht_hash_map.h
@@ -189,12 +189,6 @@ class DhtTrackerList : public std::map<HashString, DhtTracker*> {
};
#endif // HAVE_TR1
-inline
-DhtNode* DhtNodeList::add_node(DhtNode* n) {
- insert(std::make_pair<const HashString*, DhtNode*>(n, n));
- return n;
-}
-
}
#endif
View
2 src/download/available_list.cc
@@ -91,9 +91,7 @@ AvailableList::erase(const rak::socket_address& sa) {
iterator itr = std::find(begin(), end(), sa);
if (itr != end()) {
- value_type tmp = *itr;
*itr = back();
-
pop_back();
}
}
View
2 src/manager.cc
@@ -59,7 +59,7 @@
#include "manager.h"
-namespace std { using namespace tr1; }
+namespace tr1 { using namespace std::tr1; }
namespace torrent {
View
4 src/protocol/extensions.cc
@@ -401,8 +401,8 @@ ProtocolExtension::send_metadata_piece(size_t piece) {
// These messages will be rare, so we'll just build the
// metadata here instead of caching it uselessly.
char* buffer = new char[metadataSize];
- object_buffer_t result = object_write_bencode_c(object_write_to_buffer, NULL, object_buffer_t(buffer, buffer + metadataSize),
- &(*manager->download_manager()->find(m_download->info()))->bencode()->get_key("info"));
+ object_write_bencode_c(object_write_to_buffer, NULL, object_buffer_t(buffer, buffer + metadataSize),
+ &(*manager->download_manager()->find(m_download->info()))->bencode()->get_key("info"));
// data: { "msg_type" => 1, "piece" => ..., "total_size" => ... } followed by piece data (outside of dictionary)
size_t length = piece == pieceEnd - 1 ? m_download->info()->metadata_size() % metadata_piece_size : metadata_piece_size;
View
52 src/torrent/object.h
@@ -248,6 +248,57 @@ class LIBTORRENT_EXPORT Object {
uint32_t m_flags;
+#ifdef HAVE_CXX11
+ value_type& _value() { return t_value; }
+ const value_type& _value() const { return t_value; }
+ string_type& _string() { return t_string; }
+ const string_type& _string() const { return t_string; }
+ list_type& _list() { return t_list; }
+ const list_type& _list() const { return t_list; }
+ map_type& _map() { return *t_map; }
+ const map_type& _map() const { return *t_map; }
+ map_ptr_type& _map_ptr() { return t_map; }
+ const map_ptr_type& _map_ptr() const { return t_map; }
+ dict_key_type& _dict_key() { return t_dict_key; }
+ const dict_key_type& _dict_key() const { return t_dict_key; }
+ raw_object& _raw_object() { return t_raw_object; }
+ const raw_object& _raw_object() const { return t_raw_object; }
+ raw_bencode& _raw_bencode() { return t_raw_bencode; }
+ const raw_bencode& _raw_bencode() const { return t_raw_bencode; }
+ raw_string& _raw_string() { return t_raw_string; }
+ const raw_string& _raw_string() const { return t_raw_string; }
+ raw_list& _raw_list() { return t_raw_list; }
+ const raw_list& _raw_list() const { return t_raw_list; }
+ raw_map& _raw_map() { return t_raw_map; }
+ const raw_map& _raw_map() const { return t_raw_map; }
+
+ union pod_types {
+ value_type t_value;
+ raw_object t_raw_object;
+ raw_bencode t_raw_bencode;
+ raw_string t_raw_string;
+ raw_list t_raw_list;
+ raw_map t_raw_map;
+ };
+
+ union {
+ pod_types t_pod;
+
+ value_type t_value;
+ string_type t_string;
+ list_type t_list;
+ map_type* t_map;
+ dict_key_type t_dict_key;
+ raw_object t_raw_object;
+ raw_bencode t_raw_bencode;
+ raw_string t_raw_string;
+ raw_list t_raw_list;
+ raw_map t_raw_map;
+ };
+
+#else
+#error "WTF we're testing C++11 now."
+
value_type& _value() { return reinterpret_cast<value_type&>(t_pod); }
const value_type& _value() const { return reinterpret_cast<const value_type&>(t_pod); }
string_type& _string() { return reinterpret_cast<string_type&>(t_string); }
@@ -283,6 +334,7 @@ class LIBTORRENT_EXPORT Object {
char t_list[sizeof(list_type)];
char t_dict_key[sizeof(dict_key_type)];
};
+#endif
};
inline
View
12 src/torrent/utils/log.cc
@@ -53,7 +53,7 @@
#include <tr1/functional>
#include <tr1/memory>
-namespace std { using namespace tr1; }
+namespace tr1 { using namespace std::tr1; }
namespace torrent {
@@ -160,8 +160,8 @@ log_group::internal_print(const char* fmt, ...) {
va_end(ap);
if (count >= 0)
- std::for_each(m_first, m_last, std::bind(&log_slot::operator(),
- std::placeholders::_1, buffer,
+ std::for_each(m_first, m_last, tr1::bind(&log_slot::operator(),
+ tr1::placeholders::_1, buffer,
std::min<unsigned int>(count, buffer_size - 1),
std::distance(log_groups.begin(), this)));
}
@@ -178,8 +178,8 @@ log_group::internal_print_info(const DownloadInfo* info, const char* fmt, ...) {
va_end(ap);
if (count >= 0)
- std::for_each(m_first, m_last, std::bind(&log_slot::operator(),
- std::placeholders::_1, buffer,
+ std::for_each(m_first, m_last, tr1::bind(&log_slot::operator(),
+ tr1::placeholders::_1, buffer,
std::min<unsigned int>(count, buffer_size - 1),
std::distance(log_groups.begin(), this)));
}
@@ -255,7 +255,7 @@ void
log_open_output(const char* name, log_slot slot) {
if (log_outputs.size() >= (size_t)std::numeric_limits<uint64_t>::digits)
throw input_error("Cannot open more than 64 log output handlers.");
-
+
if (log_find_output_name(name) != log_outputs.end())
throw input_error("Log name already used.");
View
2 src/torrent/utils/resume.cc
@@ -94,8 +94,6 @@ resume_load_progress(Download download, const Object& object) {
}
Object::list_const_iterator filesItr = files.begin();
- Object::list_const_iterator filesLast = files.end();
-
FileList* fileList = download.file_list();
for (FileList::iterator listItr = fileList->begin(), listLast = fileList->end(); listItr != listLast; ++listItr, ++filesItr) {
View
2 test/rak/ranges_test.cc
@@ -2,7 +2,7 @@
#include <iostream>
-#import "ranges_test.h"
+#include "ranges_test.h"
CPPUNIT_TEST_SUITE_REGISTRATION(RangesTest);
View
4 test/torrent/object_static_map_test.cc
@@ -5,8 +5,8 @@
#include "torrent/object_static_map.h"
#include "protocol/extensions.h"
-#import "object_test_utils.h"
-#import "object_static_map_test.h"
+#include "object_test_utils.h"
+#include "object_static_map_test.h"
CPPUNIT_TEST_SUITE_REGISTRATION(ObjectStaticMapTest);
View
6 test/torrent/object_stream_test.cc
@@ -3,8 +3,8 @@
#include <sstream>
#include <torrent/object.h>
-#import "object_stream_test.h"
-#import "object_test_utils.h"
+#include "object_stream_test.h"
+#include "object_test_utils.h"
CPPUNIT_TEST_SUITE_REGISTRATION(ObjectStreamTest);
@@ -81,7 +81,7 @@ ObjectStreamTest::testReadBencodeC() {
bool object_write_bencode(const torrent::Object& obj, const char* original) {
try {
- char buffer[1023];
+ char buffer[1025];
char* last = torrent::object_write_bencode(buffer, buffer + 1024, &obj).first;
return std::strncmp(buffer, original, std::distance(buffer, last)) == 0;
View
4 test/torrent/object_test.cc
@@ -3,8 +3,8 @@
#include <iostream>
#include <torrent/object.h>
-#import "object_test.h"
-#import "object_test_utils.h"
+#include "object_test.h"
+#include "object_test_utils.h"
CPPUNIT_TEST_SUITE_REGISTRATION(ObjectTest);
View
2 test/torrent/object_test_utils.cc
@@ -4,7 +4,7 @@
#include <cppunit/extensions/HelperMacros.h>
#include "torrent/object_stream.h"
-#import "object_test_utils.h"
+#include "object_test_utils.h"
torrent::Object
create_bencode(const char* str) {
View
3 test/torrent/tracker_controller_test.cc
@@ -1,6 +1,7 @@
#include "config.h"
#include <iostream>
+#include <tr1/functional>
#include <torrent/tracker_controller.h>
#include "rak/priority_queue_default.h"
@@ -9,7 +10,7 @@
#include "tracker_list_test.h"
#include "tracker_controller_test.h"
-namespace std { using namespace tr1; }
+namespace tr1 { using namespace std::tr1; }
CPPUNIT_TEST_SUITE_REGISTRATION(tracker_controller_test);
View
6 test/torrent/tracker_list_test.cc
@@ -5,8 +5,6 @@
#include "tracker_list_test.h"
-namespace std { using namespace tr1; }
-
CPPUNIT_TEST_SUITE_REGISTRATION(tracker_list_test);
#define TRACKER_SETUP() \
@@ -177,7 +175,7 @@ tracker_list_test::test_tracker_flags() {
CPPUNIT_ASSERT((tracker_list[1]->flags() & torrent::Tracker::mask_base_flags) == 0);
CPPUNIT_ASSERT((tracker_list[2]->flags() & torrent::Tracker::mask_base_flags) == torrent::Tracker::flag_enabled);
CPPUNIT_ASSERT((tracker_list[3]->flags() & torrent::Tracker::mask_base_flags) == torrent::Tracker::flag_extra_tracker);
- CPPUNIT_ASSERT((tracker_list[4]->flags() & torrent::Tracker::mask_base_flags) == torrent::Tracker::flag_enabled | torrent::Tracker::flag_extra_tracker);
+ CPPUNIT_ASSERT((tracker_list[4]->flags() & torrent::Tracker::mask_base_flags) == (torrent::Tracker::flag_enabled | torrent::Tracker::flag_extra_tracker));
}
void
@@ -203,7 +201,7 @@ tracker_list_test::test_find_url() {
void
tracker_list_test::test_can_scrape() {
TRACKER_SETUP();
- torrent::Http::set_factory(sigc::ptr_fun(&http_factory));
+ torrent::Http::set_factory(std::tr1::bind(&http_factory));
tracker_list.insert_url(0, "http://example.com/announce");
CPPUNIT_ASSERT((tracker_list.back()->flags() & torrent::Tracker::flag_can_scrape));
View
18 test/torrent/utils/log_test.cc
@@ -1,5 +1,7 @@
#include "config.h"
+#include <algorithm>
+#include <cstring>
#include <fstream>
#include <iostream>
#include <tr1/functional>
@@ -10,16 +12,16 @@
CPPUNIT_TEST_SUITE_REGISTRATION(utils_log_test);
-namespace std { using namespace tr1; }
+namespace tr1 { using namespace std::tr1; }
const char* expected_output = NULL;
unsigned int output_mask;
static void
test_output(const char* output, unsigned int length, unsigned int mask) {
CPPUNIT_ASSERT_MESSAGE("'" + std::string(output) + "' != '" + std::string(expected_output) + "'",
- strcmp(output, expected_output) == 0);
- CPPUNIT_ASSERT_MESSAGE("'" + std::string(output) + "'", strlen(output) == length);
+ std::strcmp(output, expected_output) == 0);
+ CPPUNIT_ASSERT_MESSAGE("'" + std::string(output) + "'", std::strlen(output) == length);
output_mask |= mask;
}
@@ -68,7 +70,15 @@ utils_log_test::test_output_open() {
CPPUNIT_ASSERT(torrent::log_groups[0].size_outputs() == 1);
// Test inserting duplicate names, should catch.
- CPPUNIT_ASSERT_THROW(torrent::log_open_output("test_output_1", torrent::log_slot());, torrent::input_error);
+ // CPPUNIT_ASSERT_THROW(torrent::log_open_output("test_output_1", torrent::log_slot());, torrent::input_error);
+
+ try {
+ torrent::log_open_output("test_output_1", torrent::log_slot());
+ } catch (torrent::input_error& e) {
+ return;
+ }
+
+ CPPUNIT_ASSERT(false);
// Test more than 64 entries.
}

0 comments on commit c6ef90e

Please sign in to comment.