Updated documentation
[libs.git] / docs / html / _any_8h-source.html
blob8d14b50822a585840382f17607c46465a85522fd
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
3 <title>LibSylph: Sylph/Core/Any.h Source File</title>
4 <link href="doxygen.css" rel="stylesheet" type="text/css">
5 </head><body>
6 <!-- Generated by Doxygen 1.5.8 -->
7 <div class="navigation" id="top">
8 <div class="tabs">
9 <ul>
10 <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
11 <li><a href="namespaces.html"><span>Namespaces</span></a></li>
12 <li><a href="classes.html"><span>Classes</span></a></li>
13 <li class="current"><a href="files.html"><span>Files</span></a></li>
14 <li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
15 </ul>
16 </div>
17 <div class="tabs">
18 <ul>
19 <li><a href="files.html"><span>File&nbsp;List</span></a></li>
20 </ul>
21 </div>
22 <h1>Sylph/Core/Any.h</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/* </span>
23 <a name="l00002"></a>00002 <span class="comment"> * LibSylph Class Library</span>
24 <a name="l00003"></a>00003 <span class="comment"> * Copyright (C) 2009 Frank "SeySayux" Erens &lt;seysayux@gmail.com&gt;</span>
25 <a name="l00004"></a>00004 <span class="comment"> *</span>
26 <a name="l00005"></a>00005 <span class="comment"> * This library is free software; you can redistribute it and/or</span>
27 <a name="l00006"></a>00006 <span class="comment"> * modify it under the terms of the LibSylph Pulbic License as published</span>
28 <a name="l00007"></a>00007 <span class="comment"> * by the LibSylph Developers; either version 1.0 of the License, or</span>
29 <a name="l00008"></a>00008 <span class="comment"> * (at your option) any later version.</span>
30 <a name="l00009"></a>00009 <span class="comment"> *</span>
31 <a name="l00010"></a>00010 <span class="comment"> * This library is distributed in the hope that it will be useful,</span>
32 <a name="l00011"></a>00011 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
33 <a name="l00012"></a>00012 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LibSylph</span>
34 <a name="l00013"></a>00013 <span class="comment"> * Public License for more details.</span>
35 <a name="l00014"></a>00014 <span class="comment"> *</span>
36 <a name="l00015"></a>00015 <span class="comment"> * You should have received a copy of the LibSylph Public License</span>
37 <a name="l00016"></a>00016 <span class="comment"> * along with this Library, if not, contact the LibSylph Developers.</span>
38 <a name="l00017"></a>00017 <span class="comment"> *</span>
39 <a name="l00018"></a>00018 <span class="comment"> * Created on 22 maart 2009, 16:40</span>
40 <a name="l00019"></a>00019 <span class="comment"> */</span>
41 <a name="l00020"></a>00020
42 <a name="l00021"></a>00021 <span class="preprocessor">#ifndef ANY_H_</span>
43 <a name="l00022"></a>00022 <span class="preprocessor"></span><span class="preprocessor">#define ANY_H_</span>
44 <a name="l00023"></a>00023 <span class="preprocessor"></span>
45 <a name="l00024"></a>00024 <span class="preprocessor">#include "Object.h"</span>
46 <a name="l00025"></a>00025 <span class="preprocessor">#include "Exception.h"</span>
47 <a name="l00026"></a>00026 <span class="preprocessor">#include "Primitives.h"</span>
48 <a name="l00027"></a>00027
49 <a name="l00028"></a>00028 <span class="preprocessor">#include &lt;typeinfo&gt;</span>
50 <a name="l00029"></a>00029
51 <a name="l00030"></a><a class="code" href="namespace_sylph.html">00030</a> SYLPH_BEGIN_NAMESPACE
52 <a name="l00031"></a>00031 SYLPH_PUBLIC
53 <a name="l00032"></a>00032
54 <a name="l00046"></a><a class="code" href="class_sylph_1_1_any.html">00046</a> <span class="keyword">class </span><a class="code" href="class_sylph_1_1_any.html" title="Wrapper class for any kind of class.">Any</a> : <span class="keyword">public</span> <span class="keyword">virtual</span> <a class="code" href="class_sylph_1_1_object.html" title="The root of all classes.">Object</a> {
55 <a name="l00047"></a>00047 <span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt; <span class="keyword">friend</span> T* <a class="code" href="namespace_sylph.html#1d4330bb87843d404b430a8b8a4b4af5" title="Cast an Any to the type it is holding.">any_cast</a>(<a class="code" href="class_sylph_1_1_any.html" title="Wrapper class for any kind of class.">Any</a> &amp; any);
56 <a name="l00048"></a>00048 <span class="keyword">public</span>:
57 <a name="l00049"></a>00049
58 <a name="l00054"></a><a class="code" href="class_sylph_1_1_any.html#63cde8641e21cda8efe82b3752bed66f">00054</a> <a class="code" href="class_sylph_1_1_any.html#63cde8641e21cda8efe82b3752bed66f" title="Creates a new, empty Any object.">Any</a>() : content(null) {}
59 <a name="l00055"></a>00055 <span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;
60 <a name="l00063"></a><a class="code" href="class_sylph_1_1_any.html#b03f14f5215fe956a8e76d6350024f02">00063</a> <a class="code" href="class_sylph_1_1_any.html#b03f14f5215fe956a8e76d6350024f02" title="Creates a new Any object from an existing object.">Any</a>(<span class="keyword">const</span> T &amp; value) : content(new AnyContainer&lt;T&gt;(value)) {}
61 <a name="l00064"></a>00064
62 <a name="l00069"></a><a class="code" href="class_sylph_1_1_any.html#ee814145fbd7b187170205dfb8b866d8">00069</a> <a class="code" href="class_sylph_1_1_any.html" title="Wrapper class for any kind of class.">Any</a>(<span class="keyword">const</span> <a class="code" href="class_sylph_1_1_any.html" title="Wrapper class for any kind of class.">Any</a> &amp; other) : content(other.content ?
63 <a name="l00070"></a>00070 other.content-&gt;clone() : 0) {}
64 <a name="l00074"></a><a class="code" href="class_sylph_1_1_any.html#f7ddbe1a5c8bd1633b055ca2094cd92e">00074</a> <a class="code" href="class_sylph_1_1_any.html#f7ddbe1a5c8bd1633b055ca2094cd92e" title="Default destructor.">~Any</a>() { <span class="keyword">delete</span> content; }
65 <a name="l00075"></a>00075
66 <a name="l00081"></a><a class="code" href="class_sylph_1_1_any.html#eb843e6be33616bd4559f68957799c25">00081</a> <span class="keywordtype">bool</span> <a class="code" href="class_sylph_1_1_any.html#eb843e6be33616bd4559f68957799c25" title="Check if this Any is pointing to something.">empty</a>() { <span class="keywordflow">return</span> !content; }
67 <a name="l00082"></a>00082
68 <a name="l00098"></a><a class="code" href="class_sylph_1_1_any.html#77fc0fdfa5c15a6366378f5f770e2e4d">00098</a> <span class="keyword">const</span> std::type_info &amp; <a class="code" href="class_sylph_1_1_any.html#77fc0fdfa5c15a6366378f5f770e2e4d" title="Gets the typeinfo of the data pointed to.">type</a>()<span class="keyword"> const </span>{
69 <a name="l00099"></a>00099 <span class="keywordflow">return</span> content ? content-&gt;type() : <span class="keyword">typeid</span>(void);
70 <a name="l00100"></a>00100 }
71 <a name="l00101"></a>00101
72 <a name="l00108"></a><a class="code" href="class_sylph_1_1_any.html#85164cb8a7df065867204ca565f18675">00108</a> <a class="code" href="class_sylph_1_1_any.html" title="Wrapper class for any kind of class.">Any</a> &amp; <a class="code" href="class_sylph_1_1_any.html#85164cb8a7df065867204ca565f18675" title="Assigns an other Any instance to this one.">operator=</a>(<span class="keyword">const</span> <a class="code" href="class_sylph_1_1_any.html" title="Wrapper class for any kind of class.">Any</a> &amp; rhs) {
73 <a name="l00109"></a>00109 this-&gt;content = rhs.<a class="code" href="class_sylph_1_1_any.html#fb53f24e61d99c6e0da59ffc396aea2f">content</a>;
74 <a name="l00110"></a>00110 <span class="keywordflow">return</span> *<span class="keyword">this</span>;
75 <a name="l00111"></a>00111 }
76 <a name="l00112"></a>00112
77 <a name="l00121"></a>00121 <span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;
78 <a name="l00122"></a><a class="code" href="class_sylph_1_1_any.html#a7cc9001e0b52ac76a89451828a9ab4d">00122</a> <a class="code" href="class_sylph_1_1_any.html" title="Wrapper class for any kind of class.">Any</a> &amp; <a class="code" href="class_sylph_1_1_any.html#a7cc9001e0b52ac76a89451828a9ab4d" title="Assigns a new value to this Any.">operator=</a>(T &amp; value) {
79 <a name="l00123"></a>00123 <span class="keyword">delete</span> this-&gt;content;
80 <a name="l00124"></a>00124 this-&gt;content = <span class="keyword">new</span> AnyContainer&lt;T&gt;(value);
81 <a name="l00125"></a>00125 <span class="keywordflow">return</span> *<span class="keyword">this</span>;
82 <a name="l00126"></a>00126 }
83 <a name="l00127"></a>00127 <span class="keyword">private</span>:
84 <a name="l00128"></a>00128
85 <a name="l00129"></a>00129 <span class="keyword">class </span>AnyContainerBase {
86 <a name="l00130"></a>00130 <span class="keyword">public</span>:
87 <a name="l00131"></a>00131
88 <a name="l00132"></a>00132 <span class="keyword">virtual</span> ~AnyContainerBase() {
89 <a name="l00133"></a>00133 }
90 <a name="l00134"></a>00134 <span class="keyword">virtual</span> <span class="keyword">const</span> std::type_info &amp; type() <span class="keyword">const</span> = 0;
91 <a name="l00135"></a>00135 <span class="keyword">virtual</span> AnyContainerBase * clone() <span class="keyword">const</span> = 0;
92 <a name="l00136"></a>00136 };
93 <a name="l00137"></a>00137
94 <a name="l00138"></a>00138 <span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;
95 <a name="l00139"></a>00139 <span class="keyword">class </span>AnyContainer : <span class="keyword">public</span> AnyContainerBase {
96 <a name="l00140"></a>00140 <span class="keyword">public</span>:
97 <a name="l00141"></a>00141
98 <a name="l00142"></a>00142 AnyContainer(<span class="keyword">const</span> T &amp; value) : held(value) {
99 <a name="l00143"></a>00143 }
100 <a name="l00144"></a>00144
101 <a name="l00145"></a>00145 <span class="keyword">virtual</span> <span class="keyword">const</span> std::type_info &amp; type()<span class="keyword"> const </span>{
102 <a name="l00146"></a>00146 <span class="keywordflow">return</span> <span class="keyword">typeid</span> (T);
103 <a name="l00147"></a>00147 }
104 <a name="l00148"></a>00148
105 <a name="l00149"></a>00149 <span class="keyword">virtual</span> AnyContainerBase * clone()<span class="keyword"> const </span>{
106 <a name="l00150"></a>00150 <span class="keywordflow">return</span> <span class="keyword">new</span> AnyContainer&lt;T&gt;(held);
107 <a name="l00151"></a>00151 }
108 <a name="l00152"></a>00152
109 <a name="l00153"></a>00153 T held;
110 <a name="l00154"></a>00154
111 <a name="l00155"></a>00155 <span class="keyword">private</span>:
112 <a name="l00156"></a>00156 AnyContainer&lt;T&gt; &amp; operator=(<span class="keyword">const</span> AnyContainer&lt;T&gt; &amp;);
113 <a name="l00157"></a>00157
114 <a name="l00158"></a>00158 };
115 <a name="l00159"></a>00159
116 <a name="l00160"></a>00160 AnyContainerBase * content;
117 <a name="l00161"></a>00161 };
118 <a name="l00162"></a>00162
119 <a name="l00170"></a><a class="code" href="namespace_sylph.html#1d4330bb87843d404b430a8b8a4b4af5">00170</a> <span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt; T * <a class="code" href="namespace_sylph.html#1d4330bb87843d404b430a8b8a4b4af5" title="Cast an Any to the type it is holding.">any_cast</a>(<a class="code" href="class_sylph_1_1_any.html" title="Wrapper class for any kind of class.">Any</a> &amp; any) {
120 <a name="l00171"></a>00171 check_nullptr(any.<a class="code" href="class_sylph_1_1_any.html#fb53f24e61d99c6e0da59ffc396aea2f">content</a>);
121 <a name="l00172"></a>00172 <span class="keywordflow">if</span>(any.<a class="code" href="class_sylph_1_1_any.html#77fc0fdfa5c15a6366378f5f770e2e4d" title="Gets the typeinfo of the data pointed to.">type</a>() != <span class="keyword">typeid</span>(T)) <span class="keywordflow">return</span> null;
122 <a name="l00173"></a>00173 <span class="keywordflow">else</span> <span class="keywordflow">return</span> &amp;(*(<span class="keyword">static_cast&lt;</span>Any::AnyContainer&lt;T&gt; *<span class="keyword">&gt;</span>(any.<a class="code" href="class_sylph_1_1_any.html#fb53f24e61d99c6e0da59ffc396aea2f">content</a>))-&gt;held);
123 <a name="l00174"></a>00174 }
124 <a name="l00175"></a>00175 SYLPH_END_NAMESPACE
125 <a name="l00176"></a>00176
126 <a name="l00177"></a>00177 <span class="preprocessor">#endif </span><span class="comment">/* ANY_H_ */</span>
127 <a name="l00178"></a>00178
128 </pre></div></div>
129 <hr size="1" class="unhidden"><address style="text-align: right;"><small>
130 Documentation for LibSylph by Doxygen
131 </body>
132 </html>