root/doc/html/classitpp_1_1Factory.html @ 353

Revision 353, 9.6 kB (checked in by smidl, 16 years ago)

doc

Line 
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>mixpp: itpp::Factory Class Reference</title>
4<link href="tabs.css" rel="stylesheet" type="text/css">
5<link href="doxygen.css" rel="stylesheet" type="text/css">
6</head><body>
7<!-- Generated by Doxygen 1.5.8 -->
8<script type="text/javascript">
9<!--
10function changeDisplayState (e){
11  var num=this.id.replace(/[^[0-9]/g,'');
12  var button=this.firstChild;
13  var sectionDiv=document.getElementById('dynsection'+num);
14  if (sectionDiv.style.display=='none'||sectionDiv.style.display==''){
15    sectionDiv.style.display='block';
16    button.src='open.gif';
17  }else{
18    sectionDiv.style.display='none';
19    button.src='closed.gif';
20  }
21}
22function initDynSections(){
23  var divs=document.getElementsByTagName('div');
24  var sectionCounter=1;
25  for(var i=0;i<divs.length-1;i++){
26    if(divs[i].className=='dynheader'&&divs[i+1].className=='dynsection'){
27      var header=divs[i];
28      var section=divs[i+1];
29      var button=header.firstChild;
30      if (button!='IMG'){
31        divs[i].insertBefore(document.createTextNode(' '),divs[i].firstChild);
32        button=document.createElement('img');
33        divs[i].insertBefore(button,divs[i].firstChild);
34      }
35      header.style.cursor='pointer';
36      header.onclick=changeDisplayState;
37      header.id='dynheader'+sectionCounter;
38      button.src='closed.gif';
39      section.id='dynsection'+sectionCounter;
40      section.style.display='none';
41      section.style.marginLeft='14px';
42      sectionCounter++;
43    }
44  }
45}
46window.onload = initDynSections;
47-->
48</script>
49<div class="navigation" id="top">
50  <div class="tabs">
51    <ul>
52      <li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
53      <li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
54      <li><a href="modules.html"><span>Modules</span></a></li>
55      <li class="current"><a href="annotated.html"><span>Classes</span></a></li>
56      <li><a href="files.html"><span>Files</span></a></li>
57    </ul>
58  </div>
59  <div class="tabs">
60    <ul>
61      <li><a href="annotated.html"><span>Class&nbsp;List</span></a></li>
62      <li><a href="classes.html"><span>Class&nbsp;Index</span></a></li>
63      <li><a href="hierarchy.html"><span>Class&nbsp;Hierarchy</span></a></li>
64      <li><a href="functions.html"><span>Class&nbsp;Members</span></a></li>
65    </ul>
66  </div>
67  <div class="navpath"><b>itpp</b>::<a class="el" href="classitpp_1_1Factory.html">Factory</a>
68  </div>
69</div>
70<div class="contents">
71<h1>itpp::Factory Class Reference</h1><!-- doxytag: class="itpp::Factory" -->Base class for class factories. 
72<a href="#_details">More...</a>
73<p>
74<code>#include &lt;<a class="el" href="factory_8h-source.html">factory.h</a>&gt;</code>
75<p>
76
77<p>
78<a href="classitpp_1_1Factory-members.html">List of all members.</a><table border="0" cellpadding="0" cellspacing="0">
79<tr><td></td></tr>
80<tr><td colspan="2"><br><h2>Public Member Functions</h2></td></tr>
81<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="34778681d273c6851ed2aca466aefbeb"></a><!-- doxytag: member="itpp::Factory::Factory" ref="34778681d273c6851ed2aca466aefbeb" args="()" -->
82&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classitpp_1_1Factory.html#34778681d273c6851ed2aca466aefbeb">Factory</a> ()</td></tr>
83
84<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Default constructor. <br></td></tr>
85<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="4dbd8f1c6bb8845b9e62cea6387c345c"></a><!-- doxytag: member="itpp::Factory::~Factory" ref="4dbd8f1c6bb8845b9e62cea6387c345c" args="()" -->
86virtual&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classitpp_1_1Factory.html#4dbd8f1c6bb8845b9e62cea6387c345c">~Factory</a> ()</td></tr>
87
88<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Destructor. <br></td></tr>
89</table>
90<hr><a name="_details"></a><h2>Detailed Description</h2>
91Base class for class factories.
92<p>
93A class factory (or virtual constructor) is a class that can create instances of another class. <a class="el" href="classitpp_1_1Factory.html" title="Base class for class factories.">Factory</a> is a base class for such factories. When declaring an <a class="el" href="classitpp_1_1Array.html" title="General array class.">Array</a>, <a class="el" href="classitpp_1_1Vec.html" title="Vector Class (Templated).">Vec</a> or <a class="el" href="classitpp_1_1Mat.html" title="Matrix Class (Templated).">Mat</a>, a factory can be passed as an (optional) constructor argument: <div class="fragment"><pre class="fragment">  <span class="comment">// Declare a Vec&lt;type&gt; with size=10 and factory=DEFAULT_FACTORY</span>
94  Vec&lt;type&gt; a(10);
95
96  <span class="comment">// Declare a Vec&lt;type&gt; with size=10 and factory=f</span>
97  <a class="code" href="classitpp_1_1Factory.html#34778681d273c6851ed2aca466aefbeb" title="Default constructor.">Factory</a> f;
98  Vec&lt;type&gt; b(10, f);
99</pre></div><p>
100By default, the factory (<code>DEFAULT_FACTORY</code> and <code>f</code> in the above examples) is not used at all! However, by overloading a help function called <em>create_elements</em> we can force Array/Vec/Mat to use the factory for element creation (instead of using the default constructor for the element type).<p>
101<dl class="note" compact><dt><b>Note:</b></dt><dd>It is the <em>numeric</em> elements that will be created by the factory, i.e. for an <a class="el" href="classitpp_1_1Array.html" title="General array class.">Array</a>&lt;<a class="el" href="classitpp_1_1Mat.html">Mat&lt;T&gt;</a> &gt;, the factory will be used for creating the <a class="el" href="classitpp_1_1Mat.html" title="Matrix Class (Templated).">Mat</a> elements rather than the <a class="el" href="classitpp_1_1Array.html" title="General array class.">Array</a> elements.</dd></dl>
102Here is an example that (partly) defines a user-defined numeric type My_Type, a corresponding factory My_Factory and a corresponding help function create_elements&lt;My_Type&gt; that will be used by <a class="el" href="classitpp_1_1Array.html" title="General array class.">Array</a>, <a class="el" href="classitpp_1_1Vec.html" title="Vector Class (Templated).">Vec</a> and <a class="el" href="classitpp_1_1Mat.html" title="Matrix Class (Templated).">Mat</a> for element creation. <div class="fragment"><pre class="fragment">  <span class="keyword">class </span>My_Type {
103  <span class="keyword">public</span>:
104  <span class="comment">// Default constructor</span>
105  My_Type() : data(0) {}
106  <span class="comment">// Constructor</span>
107  My_Type(<span class="keywordtype">int</span> d) : data(d) {}
108  .
109  .
110  .
111  <span class="keyword">protected</span>:
112  <span class="keywordtype">int</span> data;
113  };
114
115  <span class="keyword">class </span>My_Factory : <span class="keyword">public</span> <a class="code" href="classitpp_1_1Factory.html#34778681d273c6851ed2aca466aefbeb" title="Default constructor.">Factory</a> {
116  <span class="keyword">public</span>:
117  <span class="comment">// Constructor</span>
118  <span class="keyword">explicit</span> My_Factory(<span class="keywordtype">int</span> d) : init_data(d) {}
119  <span class="comment">// Destructor</span>
120  <span class="keyword">virtual</span> ~My_Factory() {}
121  <span class="comment">// Create an n-length array of My_Type</span>
122  <span class="keyword">virtual</span> <span class="keywordtype">void</span> create(My_Type* &amp;ptr, <span class="keywordtype">int</span> n)<span class="keyword"> const </span>{ptr = <span class="keyword">new</span> My_Type[n](init_data);}
123  <span class="keyword">protected</span>:
124  <span class="keywordtype">int</span> init_data;
125  };
126
127  <span class="comment">// Create an n-length array of My_Type using My_Factory f</span>
128  <span class="keyword">template</span>&lt;&gt;
129  <span class="keywordtype">void</span> create_elements&lt;My_Type&gt;(My_Type* &amp;ptr, <span class="keywordtype">int</span> n, <span class="keyword">const</span> <a class="code" href="classitpp_1_1Factory.html#34778681d273c6851ed2aca466aefbeb" title="Default constructor.">Factory</a> &amp;f)
130  {
131  <span class="keywordflow">if</span> (<span class="keyword">const</span> My_Factory *my_factory_ptr = dynamic_cast&lt;const My_Factory*&gt;(&amp;f)) {
132  <span class="comment">// Yes, f seems to be a My_Factory. Now call the My_Factory::create method</span>
133  my_factory_ptr-&gt;create(ptr, n);
134  }
135  <span class="keywordflow">else</span> {
136  <span class="comment">// No, f does not seem to be a My_Factory. As a fallback solution,</span>
137  <span class="comment">// assume that f is DEFAULT_FACTORY and use the default constructor</span>
138  ptr = <span class="keyword">new</span> My_Type[n];
139  }
140  }
141</pre></div><p>
142Now, <div class="fragment"><pre class="fragment">  <span class="comment">// Declare a My_Factory for init_data = 123</span>
143  My_Factory my123_factory(123);
144
145  <span class="comment">// Declare a Vec&lt;My_Type&gt; with size 10 that uses My_Type() for element creation</span>
146  Vec&lt;My_Type&gt; v1(10);
147
148  <span class="comment">// Declare a Vec&lt;My_Type&gt; with size 10 that uses My_Type(123) for element creation</span>
149  Vec&lt;My_Type&gt; v1(10, my123_factory);
150</pre></div><p>
151For a more interesting example, see <a class="el" href="classitpp_1_1Fix__Factory.html" title="Class factory for fixed-point data types Fix and CFix.">Fix_Factory</a>. <hr>The documentation for this class was generated from the following file:<ul>
152<li><a class="el" href="factory_8h-source.html">factory.h</a></ul>
153</div>
154<hr size="1"><address style="text-align: right;"><small>Generated on Tue Jun 2 10:02:18 2009 for mixpp by&nbsp;
155<a href="http://www.doxygen.org/index.html">
156<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.8 </small></address>
157</body>
158</html>
Note: See TracBrowser for help on using the browser.