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
This commit is contained in:
elelel 2017-05-14 23:52:34 +00:00 committed by Davis E. King
parent 31f02b00eb
commit b57b8b20aa
40 changed files with 186 additions and 531 deletions

View File

@ -4,7 +4,9 @@
#define DLIB_AnY_H_
#include "any_abstract.h"
#include "../smart_pointers/scoped_ptr.h"
#include "../algs.h"
#include <memory>
#include <typeinfo>
namespace dlib
@ -136,7 +138,7 @@ namespace dlib
virtual ~base() {}
virtual void copy_to (
scoped_ptr<base>& dest
std::unique_ptr<base>& dest
) const = 0;
};
@ -148,14 +150,14 @@ namespace dlib
derived(const T& val) : item(val) {}
virtual void copy_to (
scoped_ptr<base>& dest
std::unique_ptr<base>& dest
) const
{
dest.reset(new derived<T>(item));
}
};
scoped_ptr<base> data;
std::unique_ptr<base> data;
};
// ----------------------------------------------------------------------------------------

View File

@ -147,7 +147,7 @@ namespace dlib
virtual ~base() {}
virtual void copy_to (
scoped_ptr<base>& dest
std::unique_ptr<base>& dest
) const = 0;
virtual result_type evaluate (
@ -163,7 +163,7 @@ namespace dlib
derived(const T& val) : item(val) {}
virtual void copy_to (
scoped_ptr<base>& dest
std::unique_ptr<base>& dest
) const
{
dest.reset(new derived<T>(item));
@ -177,7 +177,7 @@ namespace dlib
}
};
scoped_ptr<base> data;
std::unique_ptr<base> data;
};
// ----------------------------------------------------------------------------------------

View File

@ -166,7 +166,7 @@ struct Tbase
{
virtual ~Tbase() {}
virtual result_type evaluate () const = 0;
virtual void copy_to ( scoped_ptr<Tbase>& dest) const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
template <
@ -177,7 +177,7 @@ struct Tbase<T (A1)>
{
virtual ~Tbase() {}
virtual T evaluate ( A1) const = 0;
virtual void copy_to ( scoped_ptr<Tbase>& dest) const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
template <
@ -188,7 +188,7 @@ struct Tbase<T (A1,A2)>
{
virtual ~Tbase() {}
virtual T evaluate (A1,A2) const = 0;
virtual void copy_to ( scoped_ptr<Tbase>& dest) const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
template <
@ -199,7 +199,7 @@ struct Tbase<T (A1,A2,A3)>
{
virtual ~Tbase() {}
virtual T evaluate (A1,A2,A3) const = 0;
virtual void copy_to ( scoped_ptr<Tbase>& dest) const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
template <
@ -211,7 +211,7 @@ struct Tbase<T (A1,A2,A3,A4)>
{
virtual ~Tbase() {}
virtual T evaluate (A1,A2,A3,A4) const = 0;
virtual void copy_to ( scoped_ptr<Tbase>& dest) const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
template <
@ -223,7 +223,7 @@ struct Tbase<T (A1,A2,A3,A4,A5)>
{
virtual ~Tbase() {}
virtual T evaluate (A1,A2,A3,A4,A5) const = 0;
virtual void copy_to ( scoped_ptr<Tbase>& dest) const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
template <
@ -235,7 +235,7 @@ struct Tbase<T (A1,A2,A3,A4,A5,A6)>
{
virtual ~Tbase() {}
virtual T evaluate (A1,A2,A3,A4,A5,A6) const = 0;
virtual void copy_to ( scoped_ptr<Tbase>& dest) const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
template <
@ -248,7 +248,7 @@ struct Tbase<T (A1,A2,A3,A4,A5,A6,A7)>
{
virtual ~Tbase() {}
virtual T evaluate (A1,A2,A3,A4,A5,A6,A7) const = 0;
virtual void copy_to ( scoped_ptr<Tbase>& dest) const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
template <
@ -261,7 +261,7 @@ struct Tbase<T (A1,A2,A3,A4,A5,A6,A7,A8)>
{
virtual ~Tbase() {}
virtual T evaluate (A1,A2,A3,A4,A5,A6,A7,A8) const = 0;
virtual void copy_to ( scoped_ptr<Tbase>& dest) const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
template <
@ -274,7 +274,7 @@ struct Tbase<T (A1,A2,A3,A4,A5,A6,A7,A8,A9)>
{
virtual ~Tbase() {}
virtual T evaluate (A1,A2,A3,A4,A5,A6,A7,A8,A9) const = 0;
virtual void copy_to ( scoped_ptr<Tbase>& dest) const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
template <
@ -288,7 +288,7 @@ struct Tbase<T (A1,A2,A3,A4,A5,A6,A7,A8,A9,A10)>
{
virtual ~Tbase() {}
virtual T evaluate (A1,A2,A3,A4,A5,A6,A7,A8,A9,A10) const = 0;
virtual void copy_to ( scoped_ptr<Tbase>& dest) const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
typedef Tbase<function_type> base;
@ -318,7 +318,7 @@ static typename disable_if<is_function<T>,const T&>::type deref (const U& item)
typename funct_type<T>::type item; \
derived() {} \
derived(const T& val) : item(copy(val)) {} \
virtual void copy_to ( scoped_ptr<base>& dest) const \
virtual void copy_to ( std::unique_ptr<base>& dest) const \
{ dest.reset(new derived(deref<T>(item))); }
template <typename T, typename FT>
@ -508,7 +508,7 @@ struct derived<T,result_type (A1,A2,A3,A4,A5,A6,A7,A8,A9,A10)> : public base
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
};
scoped_ptr<base> data;
std::unique_ptr<base> data;
#undef DLIB_ANY_FUNCTION_DERIVED_BOILERPLATE

View File

@ -156,7 +156,7 @@ namespace dlib
) const = 0;
virtual void copy_to (
scoped_ptr<base>& dest
std::unique_ptr<base>& dest
) const = 0;
};
@ -168,7 +168,7 @@ namespace dlib
derived(const T& val) : item(val) {}
virtual void copy_to (
scoped_ptr<base>& dest
std::unique_ptr<base>& dest
) const
{
dest.reset(new derived<T>(item));
@ -183,7 +183,7 @@ namespace dlib
}
};
scoped_ptr<base> data;
std::unique_ptr<base> data;
};
// ----------------------------------------------------------------------------------------

View File

@ -7,6 +7,7 @@
#include <algorithm>
#include <ctime>
#include <memory>
#include <vector>
#include "../string.h"
@ -17,7 +18,6 @@
#include "../set.h"
#include "../algs.h"
#include "../noncopyable.h"
#include "../smart_pointers/scoped_ptr.h"
#include "../graph.h"
namespace dlib
@ -1660,7 +1660,7 @@ namespace dlib
private:
scoped_ptr<bayesian_network_join_tree_helpers::bnjt> impl;
std::unique_ptr<bayesian_network_join_tree_helpers::bnjt> impl;
unsigned long num_nodes;
};

View File

@ -3,17 +3,19 @@
#ifndef DLIB_BRIDGe_Hh_
#define DLIB_BRIDGe_Hh_
#include "bridge_abstract.h"
#include <iostream>
#include <memory>
#include <string>
#include "bridge_abstract.h"
#include "../pipe.h"
#include "../threads.h"
#include "../smart_pointers/scoped_ptr.h"
#include "../serialize.h"
#include "../sockets.h"
#include "../sockstreambuf.h"
#include "../logger.h"
#include "../algs.h"
#include <iostream>
namespace dlib
{
@ -545,8 +547,8 @@ namespace dlib
signaler s;
bool receive_thread_active;
bool transmit_thread_active;
scoped_ptr<connection> con;
scoped_ptr<listener> list;
std::unique_ptr<connection> con;
std::unique_ptr<listener> list;
const unsigned short port;
const std::string ip;
transmit_pipe_type* const transmit_pipe;
@ -656,7 +658,7 @@ namespace dlib
private:
scoped_ptr<impl_brns::impl_bridge_base> pimpl;
std::unique_ptr<impl_brns::impl_bridge_base> pimpl;
};
// ----------------------------------------------------------------------------------------

View File

@ -25,7 +25,7 @@ namespace dlib
cons.clear();
for (unsigned long i = 0; i < hosts.size(); ++i)
{
scoped_ptr<bsp_con> con(new bsp_con(hosts[i]));
std::unique_ptr<bsp_con> con(new bsp_con(hosts[i]));
dlib::serialize(node_id, con->stream); // tell the other end our node_id
unsigned long id = i+1;
cons.add(id, con);
@ -44,7 +44,7 @@ namespace dlib
{
try
{
scoped_ptr<bsp_con> con(new bsp_con(hosts[i].addr));
std::unique_ptr<bsp_con> con(new bsp_con(hosts[i].addr));
dlib::serialize(node_id, con->stream); // tell the other end our node_id
con->stream.flush();
unsigned long id = hosts[i].node_id;
@ -317,7 +317,7 @@ namespace dlib
_cons.reset();
while (_cons.move_next())
{
scoped_ptr<thread_function> ptr(new thread_function(&impl2::read_thread,
std::unique_ptr<thread_function> ptr(new thread_function(&impl2::read_thread,
_cons.element().value().get(),
_node_id,
_cons.element().key(),

View File

@ -4,18 +4,19 @@
#define DLIB_BsP_Hh_
#include "bsp_abstract.h"
#include <memory>
#include <queue>
#include <vector>
#include "../sockets.h"
#include "../array.h"
#include "../smart_pointers/scoped_ptr.h"
#include "../sockstreambuf.h"
#include "../string.h"
#include "../serialize.h"
#include "../map.h"
#include "../ref.h"
#include "../vectorstream.h"
#include <memory>
#include <queue>
#include <vector>
namespace dlib
{
@ -42,7 +43,7 @@ namespace dlib
}
bsp_con(
scoped_ptr<connection>& conptr
std::unique_ptr<connection>& conptr
) :
buf(conptr),
stream(&buf),
@ -54,13 +55,13 @@ namespace dlib
con->disable_nagle();
}
scoped_ptr<connection> con;
std::unique_ptr<connection> con;
sockstreambuf buf;
std::iostream stream;
bool terminated;
};
typedef dlib::map<unsigned long, scoped_ptr<bsp_con> >::kernel_1a_c map_id_to_con;
typedef dlib::map<unsigned long, std::unique_ptr<bsp_con> >::kernel_1a_c map_id_to_con;
void connect_all (
map_id_to_con& cons,
@ -135,7 +136,7 @@ namespace dlib
)
{
cons.clear();
scoped_ptr<listener> list;
std::unique_ptr<listener> list;
const int status = create_listener(list, port);
if (status == PORTINUSE)
{
@ -149,13 +150,13 @@ namespace dlib
port_notify_function(list->get_listening_port());
scoped_ptr<connection> con;
std::unique_ptr<connection> con;
if (list->accept(con))
{
throw socket_error("Error occurred while accepting new connection");
}
scoped_ptr<bsp_con> temp(new bsp_con(con));
std::unique_ptr<bsp_con> temp(new bsp_con(con));
unsigned long remote_node_id;
dlib::deserialize(remote_node_id, temp->stream);
@ -198,7 +199,7 @@ namespace dlib
while (cons2.size() > 0)
{
unsigned long id;
scoped_ptr<bsp_con> temp;
std::unique_ptr<bsp_con> temp;
cons2.remove_any(id,temp);
cons.add(id,temp);
}
@ -534,7 +535,7 @@ namespace dlib
impl1::map_id_to_con& _cons;
const unsigned long _node_id;
array<scoped_ptr<thread_function> > threads;
array<std::unique_ptr<thread_function> > threads;
// -----------------------------------

View File

@ -4,6 +4,7 @@
#ifndef DLIB_BASE_WIDGETs_
#define DLIB_BASE_WIDGETs_
#include <cctype>
#include <memory>
#include "base_widgets_abstract.h"
@ -19,9 +20,7 @@
#include "../image_transforms/assign_image.h"
#include "../array.h"
#include "style.h"
#include "../smart_pointers/scoped_ptr.h"
#include "../unicode.h"
#include <cctype>
#include "../any.h"
@ -696,7 +695,7 @@ namespace dlib
};
friend struct data;
scoped_ptr<data> stuff;
std::unique_ptr<data> stuff;
@ -954,7 +953,7 @@ namespace dlib
any_function<void(button&)> button_down_handler_self;
any_function<void(bool,button&)> button_up_handler_self;
scoped_ptr<button_style> style;
std::unique_ptr<button_style> style;
protected:
@ -1417,7 +1416,7 @@ namespace dlib
timer<scroll_bar> top_filler_timer;
timer<scroll_bar> bottom_filler_timer;
long delayed_pos;
scoped_ptr<scroll_bar_style> style;
std::unique_ptr<scroll_bar_style> style;
// restricted functions
scroll_bar(scroll_bar&); // copy constructor
@ -1927,7 +1926,7 @@ namespace dlib
{
auto_mutex M(wm);
bool t = true;
scoped_ptr<menu_item> item(new menu_item_type(new_item));
std::unique_ptr<menu_item> item(new menu_item_type(new_item));
items.push_back(item);
item_enabled.push_back(t);
@ -2137,7 +2136,7 @@ namespace dlib
rectangle win_rect;
unsigned long left_width;
unsigned long middle_width;
array<scoped_ptr<menu_item> > items;
array<std::unique_ptr<menu_item> > items;
array<bool> item_enabled;
array<rectangle> left_rects;
array<rectangle> middle_rects;
@ -2358,7 +2357,7 @@ namespace dlib
scroll_bar vsb;
scroll_bar hsb;
scoped_ptr<scrollable_region_style> style;
std::unique_ptr<scrollable_region_style> style;
// restricted functions
zoomable_region(zoomable_region&); // copy constructor
@ -2578,7 +2577,7 @@ namespace dlib
bool mouse_drag_enabled_;
bool user_is_dragging_mouse;
point drag_origin;
scoped_ptr<scrollable_region_style> style;
std::unique_ptr<scrollable_region_style> style;
};

View File

@ -3002,7 +3002,7 @@ namespace dlib
const std::string old_path = path;
const long old_cur_dir = cur_dir;
scoped_ptr<toggle_button> new_btn(new toggle_button(*this));
std::unique_ptr<toggle_button> new_btn(new toggle_button(*this));
new_btn->set_name(folder_name);
new_btn->set_click_handler(*this,&box_win::on_path_button_click);
@ -3011,7 +3011,7 @@ namespace dlib
{
while (sob.size() > (unsigned long)(cur_dir+1))
{
scoped_ptr<toggle_button> junk;
std::unique_ptr<toggle_button> junk;
sob.remove(cur_dir+1,junk);
}
}

View File

@ -24,7 +24,6 @@
#include "../sequence.h"
#include "../dir_nav.h"
#include "../queue.h"
#include "../smart_pointers/scoped_ptr.h"
#include "style.h"
#include "../string.h"
#include "../misc_api.h"
@ -293,7 +292,7 @@ namespace dlib
any_function<void()> event_handler;
any_function<void(toggle_button&)> event_handler_self;
scoped_ptr<toggle_button_style> style;
std::unique_ptr<toggle_button_style> style;
protected:
@ -656,7 +655,7 @@ namespace dlib
any_function<void()> enter_key_handler;
any_function<void()> focus_lost_handler;
scoped_ptr<text_field_style> style;
std::unique_ptr<text_field_style> style;
timer<text_field> t;
@ -1052,7 +1051,7 @@ namespace dlib
any_function<void()> enter_key_handler;
any_function<void()> focus_lost_handler;
scoped_ptr<text_box_style> style;
std::unique_ptr<text_box_style> style;
timer<text_box> t;
@ -1829,7 +1828,7 @@ namespace dlib
any_function<void(unsigned long)> single_click_event_handler;
unsigned long last_selected;
scoped_ptr<list_box_style> style;
std::unique_ptr<list_box_style> style;
// restricted functions
list_box(list_box&); // copy constructor
@ -1928,7 +1927,7 @@ namespace dlib
int cur_dir;
any_function<void(const std::string&)> event_handler;
sequence<scoped_ptr<toggle_button> >::kernel_2a_c sob;
sequence<std::unique_ptr<toggle_button> >::kernel_2a_c sob;
};
}

View File

@ -3,8 +3,9 @@
#ifndef DLIB_PNG_IMPORT
#define DLIB_PNG_IMPORT
#include <memory>
#include "png_loader_abstract.h"
#include "../smart_pointers/scoped_ptr.h"
#include "image_loader.h"
#include "../pixel.h"
#include "../dir_nav.h"
@ -193,7 +194,7 @@ namespace dlib
unsigned height_, width_;
unsigned bit_depth_;
int color_type_;
scoped_ptr<LibpngData> ld_;
std::unique_ptr<LibpngData> ld_;
};
// ----------------------------------------------------------------------------------------

View File

@ -154,10 +154,10 @@ namespace dlib
thecon->shutdown();
}
scoped_ptr<timeout> con_timeout;
std::unique_ptr<timeout> con_timeout;
rmutex class_mutex;
std::shared_ptr<connection> con;
scoped_ptr<sockstreambuf> buf;
std::unique_ptr<sockstreambuf> buf;
};

View File

@ -202,7 +202,7 @@ namespace dlib
}
else
{
scoped_ptr<T> temp (new T);
std::unique_ptr<T> temp (new T);
temp->val = c.val;
assign_tables(*temp, last, val);
c.table.add(first,temp);

View File

@ -3,20 +3,21 @@
#ifndef DLIB_LOGGER_KERNEl_1_
#define DLIB_LOGGER_KERNEl_1_
#include <limits>
#include <memory>
#include <cstring>
#include <streambuf>
#include <vector>
#include "../threads.h"
#include "../misc_api.h"
#include "../set.h"
#include "logger_kernel_abstract.h"
#include <limits>
#include <cstring>
#include "../algs.h"
#include "../assert.h"
#include "../uintn.h"
#include "../map.h"
#include "../smart_pointers/scoped_ptr.h"
#include "../member_function_pointer.h"
#include <streambuf>
#include <vector>
namespace dlib
{
@ -445,7 +446,7 @@ namespace dlib
level_container ();
log_level val;
map<std::string,scoped_ptr<level_container> >::kernel_1b_c table;
map<std::string,std::unique_ptr<level_container> >::kernel_1b_c table;
} level_table;
const log_level level (
@ -473,7 +474,7 @@ namespace dlib
struct auto_flush_container
{
bool val;
map<std::string,scoped_ptr<auto_flush_container> >::kernel_1b_c table;
map<std::string,std::unique_ptr<auto_flush_container> >::kernel_1b_c table;
} auto_flush_table;
bool auto_flush (
@ -501,7 +502,7 @@ namespace dlib
struct output_streambuf_container
{
std::streambuf* val;
map<std::string,scoped_ptr<output_streambuf_container> >::kernel_1b_c table;
map<std::string,std::unique_ptr<output_streambuf_container> >::kernel_1b_c table;
} streambuf_table;
std::streambuf* output_streambuf (
@ -542,7 +543,7 @@ namespace dlib
struct output_hook_container
{
hook_mfp val;
map<std::string,scoped_ptr<output_hook_container> >::kernel_1b_c table;
map<std::string,std::unique_ptr<output_hook_container> >::kernel_1b_c table;
} hook_table;
hook_mfp output_hook (
@ -570,7 +571,7 @@ namespace dlib
struct logger_header_container
{
print_header_type val;
map<std::string,scoped_ptr<logger_header_container> >::kernel_1b_c table;
map<std::string,std::unique_ptr<logger_header_container> >::kernel_1b_c table;
} header_table;
print_header_type logger_header (

View File

@ -18,8 +18,9 @@
#include <algorithm>
#include <cmath>
#include <memory>
#include "../matrix.h"
#include "../smart_pointers/scoped_ptr.h"
#include "optimization_bobyqa_abstract.h"
#include "optimization.h"
@ -59,7 +60,7 @@ namespace dlib
{
const unsigned long n = x.size();
const unsigned long w_size = (npt+5)*(npt+n)+3*n*(n+5)/2;
scoped_ptr<doublereal[]> w(new doublereal[w_size]);
std::unique_ptr<doublereal[]> w(new doublereal[w_size]);
// make these temporary matrices becuse U might be some
// kind of matrix_exp that doesn't support taking the address

View File

@ -5,13 +5,14 @@
#include "server_kernel_abstract.h"
#include <memory>
#include <string>
#include "../threads.h"
#include "../sockets.h"
#include <string>
#include "../algs.h"
#include "../set.h"
#include "../logger.h"
#include "../smart_pointers/scoped_ptr.h"
namespace dlib
@ -209,8 +210,8 @@ namespace dlib
int max_connections;
mutex max_connections_mutex;
signaler thread_count_zero;
scoped_ptr<thread_function> async_start_thread;
scoped_ptr<listener> sock;
std::unique_ptr<thread_function> async_start_thread;
std::unique_ptr<listener> sock;
unsigned long graceful_close_timeout;

View File

@ -13,9 +13,11 @@
#pragma GCC warning "smart_pointers.h is included which will fail to compile under C++17"
#endif
#include "smart_pointers/scoped_ptr.h"
#include <memory>
#include "smart_pointers/shared_ptr.h"
#include "smart_pointers/weak_ptr.h"
#include "smart_pointers/scoped_ptr.h"
#endif // DLIB_SMART_POINTERs_H_

View File

@ -1,217 +1,16 @@
// Copyright (C) 2007 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_SCOPED_PTr_
#define DLIB_SCOPED_PTr_
#ifndef DLIB_SCOPED_PTr_H_
#define DLIB_SCOPED_PTr_H_
#include <algorithm>
#include "../noncopyable.h"
#include "../algs.h"
#include "scoped_ptr_abstract.h"
#include <memory>
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <typename T>
struct default_deleter
{
void operator() (T* item) const
{
delete item;
}
};
template <typename T>
struct default_deleter<T[]>
{
void operator() (T* item) const
{
delete [] item;
}
};
// ----------------------------------------------------------------------------------------
template <
typename T,
typename deleter = default_deleter<T>
>
class scoped_ptr : noncopyable
{
/*!
CONVENTION
- get() == ptr
!*/
public:
typedef T element_type;
typedef deleter deleter_type;
explicit scoped_ptr (
T* p = 0
) : ptr(p) { }
~scoped_ptr()
{
if (ptr)
{
deleter del;
del(ptr);
}
}
void reset (
T* p = 0
)
{
if (ptr)
{
deleter del;
del(ptr);
}
ptr = p;
}
T& operator*() const
{
DLIB_ASSERT(get() != 0,
"\tscoped_ptr::operator*()"
<< "\n\tget() can't be null if you are going to dereference it"
<< "\n\tthis: " << this
);
return *ptr;
}
T* operator->() const
{
DLIB_ASSERT(get() != 0,
"\tscoped_ptr::operator*()"
<< "\n\tget() can't be null"
<< "\n\tthis: " << this
);
return ptr;
}
T* get() const
{
return ptr;
}
operator bool() const
{
return (ptr != 0);
}
void swap(
scoped_ptr& b
)
{
std::swap(ptr,b.ptr);
}
private:
T* ptr;
};
// ----------------------------------------------------------------------------------------
template <
typename T,
typename deleter
>
class scoped_ptr<T[],deleter> : noncopyable
{
/*!
CONVENTION
- get() == ptr
!*/
public:
typedef T element_type;
explicit scoped_ptr (
T* p = 0
) : ptr(p) { }
~scoped_ptr()
{
if (ptr)
{
deleter del;
del(ptr);
}
}
void reset (
T* p = 0
)
{
if (ptr)
{
deleter del;
del(ptr);
}
ptr = p;
}
T& operator[] (
unsigned long idx
) const
{
DLIB_ASSERT(get() != 0,
"\tscoped_ptr::operator[]()"
<< "\n\tget() can't be null if you are going to dereference it"
<< "\n\tthis: " << this
);
return ptr[idx];
}
T* get() const
{
return ptr;
}
operator bool() const
{
return (ptr != 0);
}
void swap(
scoped_ptr& b
)
{
std::swap(ptr,b.ptr);
}
private:
T* ptr;
};
// ----------------------------------------------------------------------------------------
template <
typename T,
typename deleter
>
void swap(
scoped_ptr<T,deleter>& a,
scoped_ptr<T,deleter>& b
)
{
a.swap(b);
}
// ----------------------------------------------------------------------------------------
namespace dlib {
// Template alias for compatibility with clients using old dlib::scoped_ptr
// Old scoped_ptr implementation is removed completely
// This alias may fail in some reference deduction cases
template <class T, class Deleter = std::default_delete<T> >
using scoped_ptr = std::unique_ptr<T, Deleter>;
}
#endif // DLIB_SCOPED_PTr_
#endif

View File

@ -1,172 +0,0 @@
// Copyright (C) 2007 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_SCOPED_PTr_ABSTRACT_
#ifdef DLIB_SCOPED_PTr_ABSTRACT_
#include "../noncopyable.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <typename T>
struct default_deleter
{
void operator() (
T* item
) const;
/*!
ensures
- if (T is an array type (e.g. int[])) then
- performs "delete [] item;"
- else
- performs "delete item;"
!*/
};
// ----------------------------------------------------------------------------------------
template <
typename T,
typename deleter = default_deleter<T>
>
class scoped_ptr : noncopyable
{
/*!
REQUIREMENTS ON deleter
Must be a function object that performs deallocation of a pointer
of type T. For example, see the default_deleter type defined above.
It must also not throw when constructed or when performing a delete.
INITIAL VALUE
defined by constructor
WHAT THIS OBJECT REPRESENTS
This is a smart pointer class inspired by the implementation of the scoped_ptr
class found in the Boost C++ library. So this is a simple smart pointer
class which guarantees that the pointer contained within it will always be
deleted.
The class does not permit copying and so does not do any kind of
reference counting. Thus it is very simply and quite fast.
Note that this class allows you to use pointers to arrays as well as
pointers to single items. To let it know that it is supposed to point
to an array you have to declare it using the bracket syntax. Consider
the following examples:
// This is how you make a scoped pointer to a single thing
scoped_ptr<int> single_item(new int);
// This is how you can use a scoped pointer to contain array pointers.
// Note the use of []. This ensures that the proper version of delete
// is called.
scoped_ptr<int[]> array_of_ints(new int[50]);
!*/
public:
typedef T element_type;
typedef deleter deleter_type;
explicit scoped_ptr (
T* p = 0
);
/*!
ensures
- #get() == p
!*/
~scoped_ptr(
);
/*!
ensures
- if (get() != 0) then
- calls deleter()(get())
(i.e. uses the deleter type to delete the pointer that is
contained in this scoped pointer)
!*/
void reset (
T* p = 0
);
/*!
ensures
- if (get() != 0) then
- calls deleter()(get())
(i.e. uses the deleter type to delete the pointer that is
contained in this scoped pointer)
- #get() == p
(i.e. makes this object contain a pointer to p instead of whatever it
used to contain)
!*/
T& operator*(
) const;
/*!
requires
- get() != 0
- T is NOT an array type (e.g. not int[])
ensures
- returns a reference to *get()
!*/
T* operator->(
) const;
/*!
requires
- get() != 0
- T is NOT an array type (e.g. not int[])
ensures
- returns the pointer contained in this object
!*/
T& operator[](
unsigned long idx
) const;
/*!
requires
- get() != 0
- T is an array type (e.g. int[])
ensures
- returns get()[idx]
!*/
T* get(
) const;
/*!
ensures
- returns the pointer contained in this object
!*/
operator bool(
) const;
/*!
ensures
- returns get() != 0
!*/
void swap(
scoped_ptr& b
);
/*!
ensures
- swaps *this and item
!*/
};
template <
typename T
>
void swap(
scoped_ptr<T>& a,
scoped_ptr<T>& b
) { a.swap(b); }
/*!
provides a global swap function
!*/
}
#endif // DLIB_SCOPED_PTr_ABSTRACT_

View File

@ -294,14 +294,14 @@ namespace dlib
unsigned long timeout
)
{
scoped_ptr<connection> ptr(con);
std::unique_ptr<connection> ptr(con);
close_gracefully(ptr,timeout);
}
// ----------------------------------------------------------------------------------------
void close_gracefully (
scoped_ptr<connection>& con,
std::unique_ptr<connection>& con,
unsigned long timeout
)
{

View File

@ -3,11 +3,14 @@
#ifndef DLIB_SOCKETS_EXTENSIONs_
#define DLIB_SOCKETS_EXTENSIONs_
#include <string>
#include "../sockets.h"
#include "sockets_extensions_abstract.h"
#include "../smart_pointers/scoped_ptr.h"
#include <iosfwd>
#include <memory>
#include <string>
#include "../sockets.h"
#include "../smart_pointers/scoped_ptr.h"
#include "sockets_extensions_abstract.h"
namespace dlib
{
@ -132,7 +135,7 @@ namespace dlib
// ----------------------------------------------------------------------------------------
void close_gracefully (
scoped_ptr<connection>& con,
std::unique_ptr<connection>& con,
unsigned long timeout = 500
);

View File

@ -3,9 +3,10 @@
#undef DLIB_SOCKETS_EXTENSIONs_ABSTRACT_
#ifdef DLIB_SOCKETS_EXTENSIONs_ABSTRACT_
#include <memory>
#include <string>
#include "sockets_kernel_abstract.h"
#include "../smart_pointers.h"
#include "../error.h"
namespace dlib
@ -265,7 +266,7 @@ namespace dlib
// ----------------------------------------------------------------------------------------
void close_gracefully (
scoped_ptr<connection>& con,
std::unique_ptr<connection>& con,
unsigned long timeout = 500
);
/*!

View File

@ -498,7 +498,7 @@ namespace dlib
int listener::
accept (
scoped_ptr<connection>& new_connection,
std::unique_ptr<connection>& new_connection,
unsigned long timeout
)
{
@ -646,7 +646,7 @@ namespace dlib
// ----------------------------------------------------------------------------------------
int create_listener (
scoped_ptr<listener>& new_listener,
std::unique_ptr<listener>& new_listener,
unsigned short port,
const std::string& ip
)
@ -764,7 +764,7 @@ namespace dlib
// ----------------------------------------------------------------------------------------
int create_connection (
scoped_ptr<connection>& new_connection,
std::unique_ptr<connection>& new_connection,
unsigned short foreign_port,
const std::string& foreign_ip,
unsigned short local_port,

View File

@ -9,8 +9,10 @@
#include "sockets_kernel_abstract.h"
#include "../algs.h"
#include <memory>
#include <string>
#include "../algs.h"
#include "../threads.h"
#include "../uintn.h"
@ -268,7 +270,7 @@ namespace dlib
);
int accept (
scoped_ptr<connection>& new_connection,
std::unique_ptr<connection>& new_connection,
unsigned long timeout = 0
);
@ -323,13 +325,13 @@ namespace dlib
);
int create_listener (
scoped_ptr<listener>& new_listener,
std::unique_ptr<listener>& new_listener,
unsigned short port,
const std::string& ip = ""
);
int create_connection (
scoped_ptr<connection>& new_connection,
std::unique_ptr<connection>& new_connection,
unsigned short foreign_port,
const std::string& foreign_ip,
unsigned short local_port = 0,

View File

@ -527,7 +527,7 @@ namespace dlib
int listener::
accept (
scoped_ptr<connection>& new_connection,
std::unique_ptr<connection>& new_connection,
unsigned long timeout
)
{
@ -788,7 +788,7 @@ namespace dlib
// ----------------------------------------------------------------------------------------
int create_listener (
scoped_ptr<listener>& new_listener,
std::unique_ptr<listener>& new_listener,
unsigned short port,
const std::string& ip
)
@ -907,7 +907,7 @@ namespace dlib
// ----------------------------------------------------------------------------------------
int create_connection (
scoped_ptr<connection>& new_connection,
std::unique_ptr<connection>& new_connection,
unsigned short foreign_port,
const std::string& foreign_ip,
unsigned short local_port,

View File

@ -13,10 +13,14 @@
#define _BSD_SOCKLEN_T_
#include <ctime>
#include <memory>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <ctime>
#ifndef HPUX
#include <sys/select.h>
#endif
@ -26,13 +30,12 @@
#include <netdb.h>
#include <unistd.h>
#include <sys/param.h>
#include <string>
#include <netinet/in.h>
#include "../threads.h"
#include "../algs.h"
#include "../smart_pointers/scoped_ptr.h"
@ -312,7 +315,7 @@ namespace dlib
);
int accept (
scoped_ptr<connection>& new_connection,
std::unique_ptr<connection>& new_connection,
unsigned long timeout = 0
);
@ -368,13 +371,13 @@ namespace dlib
);
int create_listener (
scoped_ptr<listener>& new_listener,
std::unique_ptr<listener>& new_listener,
unsigned short port,
const std::string& ip = ""
);
int create_connection (
scoped_ptr<connection>& new_connection,
std::unique_ptr<connection>& new_connection,
unsigned short foreign_port,
const std::string& foreign_ip,
unsigned short local_port = 0,

View File

@ -117,13 +117,13 @@ namespace dlib
!*/
int create_listener (
scoped_ptr<listener>& new_listener,
std::unique_ptr<listener>& new_listener,
unsigned short port,
const std::string& ip = ""
);
/*!
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 (
@ -154,7 +154,7 @@ namespace dlib
!*/
int create_connection (
scoped_ptr<connection>& new_connection,
std::unique_ptr<connection>& new_connection,
unsigned short foreign_port,
const std::string& foreign_ip,
unsigned short local_port = 0,
@ -162,7 +162,7 @@ namespace dlib
);
/*!
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
!*/
int accept (
scoped_ptr<connection>& new_connection,
std::unique_ptr<connection>& new_connection,
unsigned long timeout = 0
);
/*!
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 (

View File

@ -47,7 +47,7 @@ namespace dlib
}
sockstreambuf (
const scoped_ptr<connection>& con_
const std::unique_ptr<connection>& con_
) :
con(*con_),
out_buffer(0),

View File

@ -4,7 +4,9 @@
#ifdef DLIB_SOCKSTREAMBUF_ABSTRACT_
#include <iosfwd>
#include <memory>
#include <streambuf>
#include "../sockets/sockets_kernel_abstract.h"
namespace dlib
@ -60,7 +62,7 @@ namespace dlib
!*/
sockstreambuf (
const scoped_ptr<connection>& con
const std::unique_ptr<connection>& con
);
/*!
requires

View File

@ -57,7 +57,7 @@ namespace dlib
{}
sockstreambuf_unbuffered (
const scoped_ptr<connection>& con_
const std::unique_ptr<connection>& con_
) :
con(*con_),
peek(EOF),

View File

@ -4,6 +4,8 @@
#define DLIB_KKMEANs_
#include <cmath>
#include <vector>
#include "../matrix/matrix_abstract.h"
#include "../algs.h"
#include "../serialize.h"
@ -12,8 +14,6 @@
#include "kcentroid.h"
#include "kkmeans_abstract.h"
#include "../noncopyable.h"
#include "../smart_pointers/scoped_ptr.h"
#include <vector>
namespace dlib
{
@ -176,7 +176,7 @@ namespace dlib
item.centers.resize(num);
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);
item.centers[i].swap(temp);
}
@ -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;
scalar_type min_change;

View File

@ -332,7 +332,7 @@ namespace dlib
};
scoped_ptr<base> the_problem;
std::unique_ptr<base> the_problem;
};
// ----------------------------------------------------------------------------------------

View File

@ -2,10 +2,12 @@
// License: Boost Software License See LICENSE.txt for the full license.
#include <sstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <memory>
#include <sstream>
#include <string>
#include <dlib/sockets.h>
#include <dlib/server.h>
#include <dlib/misc_api.h>
@ -118,7 +120,7 @@ namespace {
}
int status;
scoped_ptr<connection> con;
std::unique_ptr<connection> con;
string hostname;
string ip;
status = get_local_hostname(hostname);

View File

@ -1,11 +1,13 @@
// Copyright (C) 2008 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#include <algorithm>
#include <memory>
#include "tester.h"
#include <dlib/sockets.h>
#include <dlib/threads.h>
#include <dlib/array.h>
#include <algorithm>
// 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.
@ -39,7 +41,7 @@ namespace
{
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
// that uses timeouts in the main thread.
@ -63,7 +65,7 @@ namespace
{
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
char ch;
@ -120,17 +122,17 @@ namespace
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(list);
DLIB_TEST(bool(list));
// kick off the sending threads
start();
dlib::array<scoped_ptr<connection> > cons;
dlib::array<std::unique_ptr<connection> > cons;
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
for (int i = 0; i < 6; ++i)

View File

@ -2,15 +2,15 @@
// License: Boost Software License See LICENSE.txt for the full license.
#include <cstdlib>
#include <sstream>
#include <string>
#include <cstdlib>
#include <vector>
#include <ctime>
#include <dlib/sockets.h>
#include <dlib/misc_api.h>
#include <dlib/sockstreambuf.h>
#include <vector>
#include <dlib/smart_pointers/scoped_ptr.h>
#include "tester.h"
@ -143,7 +143,7 @@ namespace
}
// make sure con gets deleted
scoped_ptr<connection> del_con(con);
std::unique_ptr<connection> del_con(con);
ssb buf(con);
istream in(&buf);

View File

@ -17,7 +17,7 @@
#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) \
do{increment_test_count(); if ( !(_exp) ) \

View File

@ -1,13 +1,14 @@
// Copyright (C) 2007 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_THREAD_FUNCTIOn_
#define DLIB_THREAD_FUNCTIOn_
#define DLIB_THREAD_FUNCTIOn_
#include <memory>
#include "thread_function_extension_abstract.h"
#include "threads_kernel.h"
#include "auto_mutex_extension.h"
#include "threaded_object_extension.h"
#include "../smart_pointers/scoped_ptr.h"
namespace dlib
{
@ -197,7 +198,7 @@ namespace dlib
f->go();
}
scoped_ptr<base_funct> f;
std::unique_ptr<base_funct> f;
// restricted functions
thread_function(thread_function&); // copy constructor

View File

@ -21,13 +21,14 @@
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 <string>
#include <dlib/gui_widgets.h>
#include <dlib/directed_graph.h>
#include <dlib/string.h>
#include <dlib/bayes_utils.h>
#include <dlib/smart_pointers/scoped_ptr.h>
#include <dlib/set.h>
#include <dlib/graph_utils.h>
#include <dlib/stl_checked.h>
@ -123,7 +124,7 @@ private:
widget_group cpt_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;
// 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.

View File

@ -15,9 +15,11 @@
http://www.jmarshall.com/easy/http/
*/
#include <iostream>
#include <memory>
#include <dlib/sockets.h>
#include <dlib/sockstreambuf.h>
#include <iostream>
using namespace std;
using namespace dlib;
@ -31,7 +33,7 @@ int main()
// are using a smart pointer here to contain the connection pointer
// returned from connect. Doing this ensures that the connection
// 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));
{