|
<html><head><title>dlib C++ Library - any.h</title></head><body bgcolor='white'><pre> |
|
<font color='#009900'>// Copyright (C) 2010 Davis E. King (davis@dlib.net) |
|
</font><font color='#009900'>// License: Boost Software License See LICENSE.txt for the full license. |
|
</font><font color='#0000FF'>#ifndef</font> DLIB_AnY_H_ |
|
<font color='#0000FF'>#define</font> DLIB_AnY_H_ |
|
|
|
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='any_abstract.h.html'>any_abstract.h</a>" |
|
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../algs.h.html'>../algs.h</a>" |
|
|
|
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>memory<font color='#5555FF'>></font> |
|
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>typeinfo<font color='#5555FF'>></font> |
|
|
|
<font color='#0000FF'>namespace</font> dlib |
|
<b>{</b> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'>class</font> <b><a name='bad_any_cast'></a>bad_any_cast</b> : <font color='#0000FF'>public</font> std::bad_cast |
|
<b>{</b> |
|
<font color='#0000FF'>public</font>: |
|
<font color='#0000FF'>virtual</font> <font color='#0000FF'>const</font> <font color='#0000FF'><u>char</u></font> <font color='#5555FF'>*</font> <b><a name='what'></a>what</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> <font color='#0000FF'>throw</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>return</font> "<font color='#CC0000'>bad_any_cast</font>"; |
|
<b>}</b> |
|
<b>}</b>; |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'>class</font> <b><a name='any'></a>any</b> |
|
<b>{</b> |
|
|
|
<font color='#0000FF'>public</font>: |
|
|
|
<b><a name='any'></a>any</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<b>}</b> |
|
|
|
<b><a name='any'></a>any</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> any<font color='#5555FF'>&</font> item |
|
<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>item.data<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
item.data<font color='#5555FF'>-</font><font color='#5555FF'>></font><font color='#BB00BB'>copy_to</font><font face='Lucida Console'>(</font>data<font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>></font> |
|
<b><a name='any'></a>any</b> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> T<font color='#5555FF'>&</font> item |
|
<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> basic_type<font color='#5555FF'><</font>T<font color='#5555FF'>></font>::type U; |
|
data.<font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font color='#0000FF'>new</font> derived<font color='#5555FF'><</font>U<font color='#5555FF'>></font><font face='Lucida Console'>(</font>item<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='clear'></a>clear</b> <font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
data.<font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>></font> |
|
<font color='#0000FF'><u>bool</u></font> <b><a name='contains'></a>contains</b> <font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> |
|
<b>{</b> |
|
<font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> basic_type<font color='#5555FF'><</font>T<font color='#5555FF'>></font>::type U; |
|
<font color='#0000FF'>return</font> <font color='#0000FF'>dynamic_cast</font><font color='#5555FF'><</font>derived<font color='#5555FF'><</font>U<font color='#5555FF'>></font><font color='#5555FF'>*</font><font color='#5555FF'>></font><font face='Lucida Console'>(</font>data.<font color='#BB00BB'>get</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>0</font>; |
|
<b>}</b> |
|
|
|
<font color='#0000FF'><u>bool</u></font> <b><a name='is_empty'></a>is_empty</b><font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> |
|
<b>{</b> |
|
<font color='#0000FF'>return</font> data.<font color='#BB00BB'>get</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font>; |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>></font> |
|
T<font color='#5555FF'>&</font> <b><a name='cast_to'></a>cast_to</b><font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> basic_type<font color='#5555FF'><</font>T<font color='#5555FF'>></font>::type U; |
|
derived<font color='#5555FF'><</font>U<font color='#5555FF'>></font><font color='#5555FF'>*</font> d <font color='#5555FF'>=</font> <font color='#0000FF'>dynamic_cast</font><font color='#5555FF'><</font>derived<font color='#5555FF'><</font>U<font color='#5555FF'>></font><font color='#5555FF'>*</font><font color='#5555FF'>></font><font face='Lucida Console'>(</font>data.<font color='#BB00BB'>get</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>d <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>throw</font> <font color='#BB00BB'>bad_any_cast</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>return</font> d<font color='#5555FF'>-</font><font color='#5555FF'>></font>item; |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>></font> |
|
<font color='#0000FF'>const</font> T<font color='#5555FF'>&</font> <b><a name='cast_to'></a>cast_to</b><font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> |
|
<b>{</b> |
|
<font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> basic_type<font color='#5555FF'><</font>T<font color='#5555FF'>></font>::type U; |
|
derived<font color='#5555FF'><</font>U<font color='#5555FF'>></font><font color='#5555FF'>*</font> d <font color='#5555FF'>=</font> <font color='#0000FF'>dynamic_cast</font><font color='#5555FF'><</font>derived<font color='#5555FF'><</font>U<font color='#5555FF'>></font><font color='#5555FF'>*</font><font color='#5555FF'>></font><font face='Lucida Console'>(</font>data.<font color='#BB00BB'>get</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>d <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>throw</font> <font color='#BB00BB'>bad_any_cast</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>return</font> d<font color='#5555FF'>-</font><font color='#5555FF'>></font>item; |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>></font> |
|
T<font color='#5555FF'>&</font> <b><a name='get'></a>get</b><font face='Lucida Console'>(</font> |
|
<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> basic_type<font color='#5555FF'><</font>T<font color='#5555FF'>></font>::type U; |
|
derived<font color='#5555FF'><</font>U<font color='#5555FF'>></font><font color='#5555FF'>*</font> d <font color='#5555FF'>=</font> <font color='#0000FF'>dynamic_cast</font><font color='#5555FF'><</font>derived<font color='#5555FF'><</font>U<font color='#5555FF'>></font><font color='#5555FF'>*</font><font color='#5555FF'>></font><font face='Lucida Console'>(</font>data.<font color='#BB00BB'>get</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>d <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
d <font color='#5555FF'>=</font> <font color='#0000FF'>new</font> derived<font color='#5555FF'><</font>U<font color='#5555FF'>></font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; |
|
data.<font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font>d<font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>return</font> d<font color='#5555FF'>-</font><font color='#5555FF'>></font>item; |
|
<b>}</b> |
|
|
|
any<font color='#5555FF'>&</font> <b><a name='operator'></a>operator</b><font color='#5555FF'>=</font> <font face='Lucida Console'>(</font> |
|
<font color='#0000FF'>const</font> any<font color='#5555FF'>&</font> item |
|
<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
<font color='#BB00BB'>any</font><font face='Lucida Console'>(</font>item<font face='Lucida Console'>)</font>.<font color='#BB00BB'>swap</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font><font face='Lucida Console'>)</font>; |
|
<font color='#0000FF'>return</font> <font color='#5555FF'>*</font><font color='#0000FF'>this</font>; |
|
<b>}</b> |
|
|
|
<font color='#0000FF'><u>void</u></font> <b><a name='swap'></a>swap</b> <font face='Lucida Console'>(</font> |
|
any<font color='#5555FF'>&</font> item |
|
<font face='Lucida Console'>)</font> |
|
<b>{</b> |
|
data.<font color='#BB00BB'>swap</font><font face='Lucida Console'>(</font>item.data<font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
|
|
<font color='#0000FF'>private</font>: |
|
|
|
<font color='#0000FF'>struct</font> <b><a name='base'></a>base</b> |
|
<b>{</b> |
|
<font color='#0000FF'>virtual</font> ~<b><a name='base'></a>base</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <b>{</b><b>}</b> |
|
|
|
<font color='#0000FF'>virtual</font> <font color='#0000FF'><u>void</u></font> <b><a name='copy_to'></a>copy_to</b> <font face='Lucida Console'>(</font> |
|
std::unique_ptr<font color='#5555FF'><</font>base<font color='#5555FF'>></font><font color='#5555FF'>&</font> dest |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> <font color='#5555FF'>=</font> <font color='#979000'>0</font>; |
|
<b>}</b>; |
|
|
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>></font> |
|
<font color='#0000FF'>struct</font> <b><a name='derived'></a>derived</b> : <font color='#0000FF'>public</font> base |
|
<b>{</b> |
|
T item; |
|
<b><a name='derived'></a>derived</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <b>{</b><b>}</b> |
|
<b><a name='derived'></a>derived</b><font face='Lucida Console'>(</font><font color='#0000FF'>const</font> T<font color='#5555FF'>&</font> val<font face='Lucida Console'>)</font> : item<font face='Lucida Console'>(</font>val<font face='Lucida Console'>)</font> <b>{</b><b>}</b> |
|
|
|
<font color='#0000FF'>virtual</font> <font color='#0000FF'><u>void</u></font> <b><a name='copy_to'></a>copy_to</b> <font face='Lucida Console'>(</font> |
|
std::unique_ptr<font color='#5555FF'><</font>base<font color='#5555FF'>></font><font color='#5555FF'>&</font> dest |
|
<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> |
|
<b>{</b> |
|
dest.<font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font color='#0000FF'>new</font> derived<font color='#5555FF'><</font>T<font color='#5555FF'>></font><font face='Lucida Console'>(</font>item<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; |
|
<b>}</b> |
|
<b>}</b>; |
|
|
|
std::unique_ptr<font color='#5555FF'><</font>base<font color='#5555FF'>></font> data; |
|
<b>}</b>; |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'>inline</font> <font color='#0000FF'><u>void</u></font> <b><a name='swap'></a>swap</b> <font face='Lucida Console'>(</font> |
|
any<font color='#5555FF'>&</font> a, |
|
any<font color='#5555FF'>&</font> b |
|
<font face='Lucida Console'>)</font> <b>{</b> a.<font color='#BB00BB'>swap</font><font face='Lucida Console'>(</font>b<font face='Lucida Console'>)</font>; <b>}</b> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>></font> T<font color='#5555FF'>&</font> <b><a name='any_cast'></a>any_cast</b><font face='Lucida Console'>(</font>any<font color='#5555FF'>&</font> a<font face='Lucida Console'>)</font> <b>{</b> <font color='#0000FF'>return</font> a.cast_to<font color='#5555FF'><</font>T<font color='#5555FF'>></font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <b>}</b> |
|
<font color='#0000FF'>template</font> <font color='#5555FF'><</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>></font> <font color='#0000FF'>const</font> T<font color='#5555FF'>&</font> <b><a name='any_cast'></a>any_cast</b><font face='Lucida Console'>(</font><font color='#0000FF'>const</font> any<font color='#5555FF'>&</font> a<font face='Lucida Console'>)</font> <b>{</b> <font color='#0000FF'>return</font> a.cast_to<font color='#5555FF'><</font>T<font color='#5555FF'>></font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <b>}</b> |
|
|
|
<font color='#009900'>// ---------------------------------------------------------------------------------------- |
|
</font> |
|
<b>}</b> |
|
|
|
|
|
<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_AnY_H_ |
|
</font> |
|
|
|
|
|
|
|
</pre></body></html> |