Commit b57b8b20 authored by elelel's avatar elelel Committed by Davis E. King
Browse files

Migrate from dlib::scoped_ptr to std::unique_ptr (#593)

* Convert unique_ptr

* Fix passing unique_ptr as copy by value

* Remove scoped_ptr implementation

* Fix missed files

* Move bool cast into tester macro

* Reexport scoped_ptr alias from sockets
parent 31f02b00
...@@ -294,14 +294,14 @@ namespace dlib ...@@ -294,14 +294,14 @@ namespace dlib
unsigned long timeout unsigned long timeout
) )
{ {
scoped_ptr<connection> ptr(con); std::unique_ptr<connection> ptr(con);
close_gracefully(ptr,timeout); close_gracefully(ptr,timeout);
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
void close_gracefully ( void close_gracefully (
scoped_ptr<connection>& con, std::unique_ptr<connection>& con,
unsigned long timeout unsigned long timeout
) )
{ {
......
...@@ -3,11 +3,14 @@ ...@@ -3,11 +3,14 @@
#ifndef DLIB_SOCKETS_EXTENSIONs_ #ifndef DLIB_SOCKETS_EXTENSIONs_
#define DLIB_SOCKETS_EXTENSIONs_ #define DLIB_SOCKETS_EXTENSIONs_
#include <iosfwd>
#include <memory>
#include <string> #include <string>
#include "../sockets.h" #include "../sockets.h"
#include "sockets_extensions_abstract.h"
#include "../smart_pointers/scoped_ptr.h" #include "../smart_pointers/scoped_ptr.h"
#include <iosfwd> #include "sockets_extensions_abstract.h"
namespace dlib namespace dlib
{ {
...@@ -132,7 +135,7 @@ namespace dlib ...@@ -132,7 +135,7 @@ namespace dlib
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
void close_gracefully ( void close_gracefully (
scoped_ptr<connection>& con, std::unique_ptr<connection>& con,
unsigned long timeout = 500 unsigned long timeout = 500
); );
......
...@@ -3,9 +3,10 @@ ...@@ -3,9 +3,10 @@
#undef DLIB_SOCKETS_EXTENSIONs_ABSTRACT_ #undef DLIB_SOCKETS_EXTENSIONs_ABSTRACT_
#ifdef DLIB_SOCKETS_EXTENSIONs_ABSTRACT_ #ifdef DLIB_SOCKETS_EXTENSIONs_ABSTRACT_
#include <memory>
#include <string> #include <string>
#include "sockets_kernel_abstract.h" #include "sockets_kernel_abstract.h"
#include "../smart_pointers.h"
#include "../error.h" #include "../error.h"
namespace dlib namespace dlib
...@@ -265,7 +266,7 @@ namespace dlib ...@@ -265,7 +266,7 @@ namespace dlib
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
void close_gracefully ( void close_gracefully (
scoped_ptr<connection>& con, std::unique_ptr<connection>& con,
unsigned long timeout = 500 unsigned long timeout = 500
); );
/*! /*!
......
...@@ -498,7 +498,7 @@ namespace dlib ...@@ -498,7 +498,7 @@ namespace dlib
int listener:: int listener::
accept ( accept (
scoped_ptr<connection>& new_connection, std::unique_ptr<connection>& new_connection,
unsigned long timeout unsigned long timeout
) )
{ {
...@@ -646,7 +646,7 @@ namespace dlib ...@@ -646,7 +646,7 @@ namespace dlib
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
int create_listener ( int create_listener (
scoped_ptr<listener>& new_listener, std::unique_ptr<listener>& new_listener,
unsigned short port, unsigned short port,
const std::string& ip const std::string& ip
) )
...@@ -764,7 +764,7 @@ namespace dlib ...@@ -764,7 +764,7 @@ namespace dlib
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
int create_connection ( int create_connection (
scoped_ptr<connection>& new_connection, std::unique_ptr<connection>& new_connection,
unsigned short foreign_port, unsigned short foreign_port,
const std::string& foreign_ip, const std::string& foreign_ip,
unsigned short local_port, unsigned short local_port,
......
...@@ -9,8 +9,10 @@ ...@@ -9,8 +9,10 @@
#include "sockets_kernel_abstract.h" #include "sockets_kernel_abstract.h"
#include "../algs.h" #include <memory>
#include <string> #include <string>
#include "../algs.h"
#include "../threads.h" #include "../threads.h"
#include "../uintn.h" #include "../uintn.h"
...@@ -268,7 +270,7 @@ namespace dlib ...@@ -268,7 +270,7 @@ namespace dlib
); );
int accept ( int accept (
scoped_ptr<connection>& new_connection, std::unique_ptr<connection>& new_connection,
unsigned long timeout = 0 unsigned long timeout = 0
); );
...@@ -323,13 +325,13 @@ namespace dlib ...@@ -323,13 +325,13 @@ namespace dlib
); );
int create_listener ( int create_listener (
scoped_ptr<listener>& new_listener, std::unique_ptr<listener>& new_listener,
unsigned short port, unsigned short port,
const std::string& ip = "" const std::string& ip = ""
); );
int create_connection ( int create_connection (
scoped_ptr<connection>& new_connection, std::unique_ptr<connection>& new_connection,
unsigned short foreign_port, unsigned short foreign_port,
const std::string& foreign_ip, const std::string& foreign_ip,
unsigned short local_port = 0, unsigned short local_port = 0,
......
...@@ -527,7 +527,7 @@ namespace dlib ...@@ -527,7 +527,7 @@ namespace dlib
int listener:: int listener::
accept ( accept (
scoped_ptr<connection>& new_connection, std::unique_ptr<connection>& new_connection,
unsigned long timeout unsigned long timeout
) )
{ {
...@@ -788,7 +788,7 @@ namespace dlib ...@@ -788,7 +788,7 @@ namespace dlib
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
int create_listener ( int create_listener (
scoped_ptr<listener>& new_listener, std::unique_ptr<listener>& new_listener,
unsigned short port, unsigned short port,
const std::string& ip const std::string& ip
) )
...@@ -907,7 +907,7 @@ namespace dlib ...@@ -907,7 +907,7 @@ namespace dlib
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
int create_connection ( int create_connection (
scoped_ptr<connection>& new_connection, std::unique_ptr<connection>& new_connection,
unsigned short foreign_port, unsigned short foreign_port,
const std::string& foreign_ip, const std::string& foreign_ip,
unsigned short local_port, unsigned short local_port,
......
...@@ -13,10 +13,14 @@ ...@@ -13,10 +13,14 @@
#define _BSD_SOCKLEN_T_ #define _BSD_SOCKLEN_T_
#include <ctime>
#include <memory>
#include <string>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <errno.h> #include <errno.h>
#include <ctime>
#ifndef HPUX #ifndef HPUX
#include <sys/select.h> #include <sys/select.h>
#endif #endif
...@@ -26,13 +30,12 @@ ...@@ -26,13 +30,12 @@
#include <netdb.h> #include <netdb.h>
#include <unistd.h> #include <unistd.h>
#include <sys/param.h> #include <sys/param.h>
#include <string>
#include <netinet/in.h> #include <netinet/in.h>
#include "../threads.h" #include "../threads.h"
#include "../algs.h" #include "../algs.h"
#include "../smart_pointers/scoped_ptr.h"
...@@ -312,7 +315,7 @@ namespace dlib ...@@ -312,7 +315,7 @@ namespace dlib
); );
int accept ( int accept (
scoped_ptr<connection>& new_connection, std::unique_ptr<connection>& new_connection,
unsigned long timeout = 0 unsigned long timeout = 0
); );
...@@ -368,13 +371,13 @@ namespace dlib ...@@ -368,13 +371,13 @@ namespace dlib
); );
int create_listener ( int create_listener (
scoped_ptr<listener>& new_listener, std::unique_ptr<listener>& new_listener,
unsigned short port, unsigned short port,
const std::string& ip = "" const std::string& ip = ""
); );
int create_connection ( int create_connection (
scoped_ptr<connection>& new_connection, std::unique_ptr<connection>& new_connection,
unsigned short foreign_port, unsigned short foreign_port,
const std::string& foreign_ip, const std::string& foreign_ip,
unsigned short local_port = 0, unsigned short local_port = 0,
......
...@@ -117,13 +117,13 @@ namespace dlib ...@@ -117,13 +117,13 @@ namespace dlib
!*/ !*/
int create_listener ( int create_listener (
scoped_ptr<listener>& new_listener, std::unique_ptr<listener>& new_listener,
unsigned short port, unsigned short port,
const std::string& ip = "" const std::string& ip = ""
); );
/*! /*!
This function is just an overload of the above function but it gives you a This function is just an overload of the above function but it gives you a
scoped_ptr smart pointer instead of a C pointer. std::unique_ptr smart pointer instead of a C pointer.
!*/ !*/
int create_connection ( int create_connection (
...@@ -154,7 +154,7 @@ namespace dlib ...@@ -154,7 +154,7 @@ namespace dlib
!*/ !*/
int create_connection ( int create_connection (
scoped_ptr<connection>& new_connection, std::unique_ptr<connection>& new_connection,
unsigned short foreign_port, unsigned short foreign_port,
const std::string& foreign_ip, const std::string& foreign_ip,
unsigned short local_port = 0, unsigned short local_port = 0,
...@@ -162,7 +162,7 @@ namespace dlib ...@@ -162,7 +162,7 @@ namespace dlib
); );
/*! /*!
This function is just an overload of the above function but it gives you a This function is just an overload of the above function but it gives you a
scoped_ptr smart pointer instead of a C pointer. std::unique_ptr smart pointer instead of a C pointer.
!*/ !*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
...@@ -459,12 +459,12 @@ namespace dlib ...@@ -459,12 +459,12 @@ namespace dlib
!*/ !*/
int accept ( int accept (
scoped_ptr<connection>& new_connection, std::unique_ptr<connection>& new_connection,
unsigned long timeout = 0 unsigned long timeout = 0
); );
/*! /*!
This function is just an overload of the above function but it gives you a This function is just an overload of the above function but it gives you a
scoped_ptr smart pointer instead of a C pointer. std::unique_ptr smart pointer instead of a C pointer.
!*/ !*/
unsigned short get_listening_port ( unsigned short get_listening_port (
......
...@@ -47,7 +47,7 @@ namespace dlib ...@@ -47,7 +47,7 @@ namespace dlib
} }
sockstreambuf ( sockstreambuf (
const scoped_ptr<connection>& con_ const std::unique_ptr<connection>& con_
) : ) :
con(*con_), con(*con_),
out_buffer(0), out_buffer(0),
......
...@@ -4,7 +4,9 @@ ...@@ -4,7 +4,9 @@
#ifdef DLIB_SOCKSTREAMBUF_ABSTRACT_ #ifdef DLIB_SOCKSTREAMBUF_ABSTRACT_
#include <iosfwd> #include <iosfwd>
#include <memory>
#include <streambuf> #include <streambuf>
#include "../sockets/sockets_kernel_abstract.h" #include "../sockets/sockets_kernel_abstract.h"
namespace dlib namespace dlib
...@@ -60,7 +62,7 @@ namespace dlib ...@@ -60,7 +62,7 @@ namespace dlib
!*/ !*/
sockstreambuf ( sockstreambuf (
const scoped_ptr<connection>& con const std::unique_ptr<connection>& con
); );
/*! /*!
requires requires
......
...@@ -57,7 +57,7 @@ namespace dlib ...@@ -57,7 +57,7 @@ namespace dlib
{} {}
sockstreambuf_unbuffered ( sockstreambuf_unbuffered (
const scoped_ptr<connection>& con_ const std::unique_ptr<connection>& con_
) : ) :
con(*con_), con(*con_),
peek(EOF), peek(EOF),
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#define DLIB_KKMEANs_ #define DLIB_KKMEANs_
#include <cmath> #include <cmath>
#include <vector>
#include "../matrix/matrix_abstract.h" #include "../matrix/matrix_abstract.h"
#include "../algs.h" #include "../algs.h"
#include "../serialize.h" #include "../serialize.h"
...@@ -12,8 +14,6 @@ ...@@ -12,8 +14,6 @@
#include "kcentroid.h" #include "kcentroid.h"
#include "kkmeans_abstract.h" #include "kkmeans_abstract.h"
#include "../noncopyable.h" #include "../noncopyable.h"
#include "../smart_pointers/scoped_ptr.h"
#include <vector>
namespace dlib namespace dlib
{ {
...@@ -176,7 +176,7 @@ namespace dlib ...@@ -176,7 +176,7 @@ namespace dlib
item.centers.resize(num); item.centers.resize(num);
for (unsigned long i = 0; i < item.centers.size(); ++i) for (unsigned long i = 0; i < item.centers.size(); ++i)
{ {
scoped_ptr<kcentroid<kernel_type> > temp(new kcentroid<kernel_type>(kernel_type())); std::unique_ptr<kcentroid<kernel_type> > temp(new kcentroid<kernel_type>(kernel_type()));
deserialize(*temp, in); deserialize(*temp, in);
item.centers[i].swap(temp); item.centers[i].swap(temp);
} }
...@@ -270,7 +270,7 @@ namespace dlib ...@@ -270,7 +270,7 @@ namespace dlib
} }
array<scoped_ptr<kcentroid<kernel_type> > > centers; array<std::unique_ptr<kcentroid<kernel_type> > > centers;
kcentroid<kernel_type> kc; kcentroid<kernel_type> kc;
scalar_type min_change; scalar_type min_change;
......
...@@ -332,7 +332,7 @@ namespace dlib ...@@ -332,7 +332,7 @@ namespace dlib
}; };
scoped_ptr<base> the_problem; std::unique_ptr<base> the_problem;
}; };
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
...@@ -2,10 +2,12 @@ ...@@ -2,10 +2,12 @@
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include <sstream>
#include <string>
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
#include <memory>
#include <sstream>
#include <string>
#include <dlib/sockets.h> #include <dlib/sockets.h>
#include <dlib/server.h> #include <dlib/server.h>
#include <dlib/misc_api.h> #include <dlib/misc_api.h>
...@@ -118,7 +120,7 @@ namespace { ...@@ -118,7 +120,7 @@ namespace {
} }
int status; int status;
scoped_ptr<connection> con; std::unique_ptr<connection> con;
string hostname; string hostname;
string ip; string ip;
status = get_local_hostname(hostname); status = get_local_hostname(hostname);
......
// Copyright (C) 2008 Davis E. King (davis@dlib.net) // Copyright (C) 2008 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include <algorithm>
#include <memory>
#include "tester.h" #include "tester.h"
#include <dlib/sockets.h> #include <dlib/sockets.h>
#include <dlib/threads.h> #include <dlib/threads.h>
#include <dlib/array.h> #include <dlib/array.h>
#include <algorithm>
// This is called an unnamed-namespace and it has the effect of making everything // This is called an unnamed-namespace and it has the effect of making everything
// inside this file "private" so that everything you declare will have static linkage. // inside this file "private" so that everything you declare will have static linkage.
...@@ -39,7 +41,7 @@ namespace ...@@ -39,7 +41,7 @@ namespace
{ {
try try
{ {
scoped_ptr<connection> con(connect("127.0.0.1", port_num)); std::unique_ptr<connection> con(connect("127.0.0.1", port_num));
// Send a copy of the data down the connection so we can test our the read() function // Send a copy of the data down the connection so we can test our the read() function
// that uses timeouts in the main thread. // that uses timeouts in the main thread.
...@@ -63,7 +65,7 @@ namespace ...@@ -63,7 +65,7 @@ namespace
{ {
try try
{ {
scoped_ptr<connection> con(connect("127.0.0.1", port_num)); std::unique_ptr<connection> con(connect("127.0.0.1", port_num));
// just do nothing until the connection closes // just do nothing until the connection closes
char ch; char ch;
...@@ -120,17 +122,17 @@ namespace ...@@ -120,17 +122,17 @@ namespace
dlog << LINFO << "data block size: " << data_to_send.size(); dlog << LINFO << "data block size: " << data_to_send.size();
scoped_ptr<listener> list; std::unique_ptr<listener> list;
DLIB_TEST(create_listener(list, port_num, "127.0.0.1") == 0); DLIB_TEST(create_listener(list, port_num, "127.0.0.1") == 0);
DLIB_TEST(list); DLIB_TEST(bool(list));
// kick off the sending threads // kick off the sending threads
start(); start();
dlib::array<scoped_ptr<connection> > cons; dlib::array<std::unique_ptr<connection> > cons;
std::vector<long> bytes_received(6,0); std::vector<long> bytes_received(6,0);
scoped_ptr<connection> con_temp; std::unique_ptr<connection> con_temp;
// accept the 6 connections we should get // accept the 6 connections we should get
for (int i = 0; i < 6; ++i) for (int i = 0; i < 6; ++i)
......
...@@ -2,15 +2,15 @@ ...@@ -2,15 +2,15 @@
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include <cstdlib>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <cstdlib> #include <vector>
#include <ctime> #include <ctime>
#include <dlib/sockets.h> #include <dlib/sockets.h>
#include <dlib/misc_api.h> #include <dlib/misc_api.h>
#include <dlib/sockstreambuf.h> #include <dlib/sockstreambuf.h>
#include <vector>
#include <dlib/smart_pointers/scoped_ptr.h>
#include "tester.h" #include "tester.h"
...@@ -143,7 +143,7 @@ namespace ...@@ -143,7 +143,7 @@ namespace
} }
// make sure con gets deleted // make sure con gets deleted
scoped_ptr<connection> del_con(con); std::unique_ptr<connection> del_con(con);
ssb buf(con); ssb buf(con);
istream in(&buf); istream in(&buf);
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
#endif #endif
#define DLIB_TEST(_exp) check_test(_exp, __LINE__, __FILE__, #_exp) #define DLIB_TEST(_exp) check_test(bool(_exp), __LINE__, __FILE__, #_exp)
#define DLIB_TEST_MSG(_exp,_message) \ #define DLIB_TEST_MSG(_exp,_message) \
do{increment_test_count(); if ( !(_exp) ) \ do{increment_test_count(); if ( !(_exp) ) \
......
// Copyright (C) 2007 Davis E. King (davis@dlib.net) // Copyright (C) 2007 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_THREAD_FUNCTIOn_ #ifndef DLIB_THREAD_FUNCTIOn_
#define DLIB_THREAD_FUNCTIOn_ #define DLIB_THREAD_FUNCTIOn_
#include <memory>
#include "thread_function_extension_abstract.h" #include "thread_function_extension_abstract.h"
#include "threads_kernel.h" #include "threads_kernel.h"
#include "auto_mutex_extension.h" #include "auto_mutex_extension.h"
#include "threaded_object_extension.h" #include "threaded_object_extension.h"
#include "../smart_pointers/scoped_ptr.h"
namespace dlib namespace dlib
{ {
...@@ -197,7 +198,7 @@ namespace dlib ...@@ -197,7 +198,7 @@ namespace dlib
f->go(); f->go();
} }
scoped_ptr<base_funct> f; std::unique_ptr<base_funct> f;
// restricted functions // restricted functions
thread_function(thread_function&); // copy constructor thread_function(thread_function&); // copy constructor
......
...@@ -21,13 +21,14 @@ ...@@ -21,13 +21,14 @@
examples and want to see a more in-depth example then by all means, continue reading. :) examples and want to see a more in-depth example then by all means, continue reading. :)
*/ */
#include <dlib/gui_widgets.h> #include <memory>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <dlib/gui_widgets.h>
#include <dlib/directed_graph.h> #include <dlib/directed_graph.h>
#include <dlib/string.h> #include <dlib/string.h>
#include <dlib/bayes_utils.h> #include <dlib/bayes_utils.h>
#include <dlib/smart_pointers/scoped_ptr.h>
#include <dlib/set.h> #include <dlib/set.h>
#include <dlib/graph_utils.h> #include <dlib/graph_utils.h>
#include <dlib/stl_checked.h> #include <dlib/stl_checked.h>
...@@ -123,7 +124,7 @@ private: ...@@ -123,7 +124,7 @@ private:
widget_group cpt_group; widget_group cpt_group;
widget_group ppt_group; widget_group ppt_group;
scoped_ptr<bayesian_network_join_tree> solution; std::unique_ptr<bayesian_network_join_tree> solution;
join_tree_type join_tree; join_tree_type join_tree;
// The std_vector_c is an object identical to the std::vector except that it checks // The std_vector_c is an object identical to the std::vector except that it checks
// all its preconditions and throws a dlib::fatal_error if they are violated. // all its preconditions and throws a dlib::fatal_error if they are violated.
......
...@@ -15,9 +15,11 @@ ...@@ -15,9 +15,11 @@
http://www.jmarshall.com/easy/http/ http://www.jmarshall.com/easy/http/
*/ */
#include <iostream>
#include <memory>
#include <dlib/sockets.h> #include <dlib/sockets.h>
#include <dlib/sockstreambuf.h> #include <dlib/sockstreambuf.h>
#include <iostream>
using namespace std; using namespace std;
using namespace dlib; using namespace dlib;
...@@ -31,7 +33,7 @@ int main() ...@@ -31,7 +33,7 @@ int main()
// are using a smart pointer here to contain the connection pointer // are using a smart pointer here to contain the connection pointer
// returned from connect. Doing this ensures that the connection // returned from connect. Doing this ensures that the connection
// is deleted even if someone throws an exception somewhere in your code. // is deleted even if someone throws an exception somewhere in your code.
scoped_ptr<connection> con(connect("www.google.com",80)); std::unique_ptr<connection> con(connect("www.google.com",80));
{ {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment