<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>GuShH&#039;s Development Blog &#187; template</title>
	<atom:link href="http://gushh.net/blog/tag/template/feed/" rel="self" type="application/rss+xml" />
	<link>http://gushh.net/blog</link>
	<description>This blog is about software, electronics and game development.</description>
	<lastBuildDate>Wed, 08 Sep 2010 03:02:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Average template</title>
		<link>http://gushh.net/blog/2009/08/05/average-template/</link>
		<comments>http://gushh.net/blog/2009/08/05/average-template/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 12:16:52 +0000</pubDate>
		<dc:creator>GuShH</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[PureBasic]]></category>
		<category><![CDATA[average]]></category>
		<category><![CDATA[numbers]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[purebasic]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://gushh.net/blog/?p=161</guid>
		<description><![CDATA[A tiny template for averaging numbers. I needed this for a prototype and I didn&#8217;t want to &#8220;wing it&#8221; anymore. I think there should be a package with similar templates that people could download to enhance the language, I&#8217;m just saying&#8230; It takes little to come up with this kind of code and in the [...]]]></description>
			<content:encoded><![CDATA[<!-- AdSense Now! V1.95 -->
<!-- Post[count: 3] -->
<div class="adsense adsense-leadin" style="text-align:center;margin: 12px;"><script type="text/javascript"><!--
google_ad_client = "pub-2462949361920197";
/* 468x15, created 4/6/10 */
google_ad_slot = "8315313616";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p>A tiny template for averaging numbers.<br />
I needed this for a prototype and I didn&#8217;t want to &#8220;wing it&#8221; anymore.</p>
<p>I think there should be a package with similar templates that people could download to enhance the language, I&#8217;m just saying&#8230; It takes little to come up with this kind of code and in the end it&#8217;s always useful to have it around.</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">Macro</span> Average_Register<span style="color: #000066;">&#40;</span> _type <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Structure</span> AVERAGE#_type
		sum._type
		average._type
		count.i
	<span style="color: #000066; font-weight: bold;">EndStructure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>.i Average#_type#_Create<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">Define</span>.AVERAGE#_type <span style="color: #000066;">*</span>this <span style="color: #000066;">=</span> <span style="color: #0000ff;">AllocateMemory</span><span style="color: #000066;">&#40;</span> <span style="color: #0000ff;">SizeOf</span><span style="color: #000066;">&#40;</span> AVERAGE#_type <span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>.i Average#_type#_Destroy<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AVERAGE#_type <span style="color: #000066;">&#41;</span>
		<span style="color: #0000ff;">FreeMemory</span><span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this <span style="color: #000066;">&#41;</span>
		<span style="color: #000066;">*</span>this <span style="color: #000066;">=</span> #Null
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>._type Average#_type#_Calculate<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AVERAGE#_type <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>count <span style="color: #000066; font-weight: bold;">And</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>sum
			<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>average <span style="color: #000066;">=</span> <span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>sum <span style="color: #000066;">/</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>count <span style="color: #000066;">&#41;</span>
			<span style="color: #000066; font-weight: bold;">ProcedureReturn</span>	<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>average
		<span style="color: #000066; font-weight: bold;">EndIf</span>
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>.i Average#_type#_Reset<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AVERAGE#_type <span style="color: #000066;">&#41;</span>
		<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>sum 	<span style="color: #000066;">=</span> <span style="color: #CC0000;">0.0</span>
		<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>average 	<span style="color: #000066;">=</span> <span style="color: #CC0000;">0.0</span>
		<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>count 	<span style="color: #000066;">=</span> <span style="color: #CC0000;">0</span>
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>.i Average#_type#_Add<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AVERAGE#_type, sample.f <span style="color: #000066;">&#41;</span>
		<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>sum 	<span style="color: #000066;">+</span> sample
		<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>count 	<span style="color: #000066;">+</span> <span style="color: #CC0000;">1</span>
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>.i Average#_type#_Count<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AVERAGE#_type <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>count
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>._type Average#_type#_Sum<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AVERAGE#_type <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>sum
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>._type Average#_type#_Get<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AVERAGE#_type <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>average
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndMacro</span></pre></div></div>

<p>You may register it as any numerical type.<br />
Usage example:</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;">Average_Register<span style="color: #000066;">&#40;</span> f <span style="color: #000066;">&#41;</span> <span style="color: #ff0000; font-style: italic;">; register with floats.</span>
<span style="color: #000066; font-weight: bold;">Define</span>.AVERAGEf <span style="color: #000fff;">test</span>
&nbsp;
Averagef_Add<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span>, <span style="color: #CC0000;">1.0</span> <span style="color: #000066;">&#41;</span>
Averagef_Add<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span>, <span style="color: #CC0000;">2.0</span> <span style="color: #000066;">&#41;</span>
Averagef_Add<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span>, <span style="color: #CC0000;">3.0</span> <span style="color: #000066;">&#41;</span>
Averagef_Add<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span>, <span style="color: #CC0000;">4.0</span> <span style="color: #000066;">&#41;</span>
Averagef_Add<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span>, <span style="color: #CC0000;">5.0</span> <span style="color: #000066;">&#41;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Debug</span> Averagef_Calculate<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span> <span style="color: #000066;">&#41;</span>
<span style="color: #000066; font-weight: bold;">Debug</span> Averagef_Count<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span> <span style="color: #000066;">&#41;</span>
<span style="color: #000066; font-weight: bold;">Debug</span> Averagef_Sum<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span> <span style="color: #000066;">&#41;</span>
&nbsp;
Averagef_Reset<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span> <span style="color: #000066;">&#41;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Debug</span> Averagef_Calculate<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span> <span style="color: #000066;">&#41;</span>
<span style="color: #000066; font-weight: bold;">Debug</span> Averagef_Count<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span> <span style="color: #000066;">&#41;</span>
<span style="color: #000066; font-weight: bold;">Debug</span> Averagef_Sum<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span> <span style="color: #000066;">&#41;</span></pre></div></div>

<p>You can allocate an instance of the structure with the Create function.<br />
For example, if you registered the template as float, then you may create a dynamic &#8220;object&#8221; in this way:</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">Define</span>.AVERAGEf <span style="color: #000066;">*</span><span style="color: #000fff;">test</span> <span style="color: #000066;">=</span> Averagef_Create<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span> <span style="color: #ff0000; font-style: italic;">; the *test variable holds an instance of the AVERAGEf structure</span></pre></div></div>

<p>Likewise, you should call the Destroy() function to get rid of it.</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;">Averagef_Destroy<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span><span style="color: #000fff;">test</span> <span style="color: #000066;">&#41;</span></pre></div></div>

<p>Trivial but useful. I&#8217;m not particularly happy with the naming convention though. But, let&#8217;s not forget that this is not an attempt at emulating OOP at all; it&#8217;s just a way of giving flexibility to an encapsulated piece of code. For OOP there are proper languages and there is no point whatsoever in trying to emulate it.</p>
<p>Cheers</p>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://gushh.net/blog/2009/07/13/macro-templates-by-example/" rel="bookmark" class="crp_title">Macro Templates, by example.</a></li><li><a href="http://gushh.net/blog/2008/03/30/url-encoding-in-purebasic-the-proper-way/" rel="bookmark" class="crp_title">URL Encoding in PureBasic, the proper way.</a></li><li><a href="http://gushh.net/blog/2009/08/28/structuring-libraries-basic-design-tips/" rel="bookmark" class="crp_title">Structuring libraries, basic design tips.</a></li><li><a href="http://gushh.net/blog/2010/07/17/get-mx-records/" rel="bookmark" class="crp_title">Get MX Records</a></li><li><a href="http://gushh.net/blog/2009/08/27/float-modulo-in-purebasic/" rel="bookmark" class="crp_title">Float Modulo in PureBasic</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://gushh.net/blog/2009/08/05/average-template/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Macro Templates, by example.</title>
		<link>http://gushh.net/blog/2009/07/13/macro-templates-by-example/</link>
		<comments>http://gushh.net/blog/2009/07/13/macro-templates-by-example/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 16:46:50 +0000</pubDate>
		<dc:creator>GuShH</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[PureBasic]]></category>
		<category><![CDATA[macro]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[object factory]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[pb]]></category>
		<category><![CDATA[purebasic]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[vec3]]></category>
		<category><![CDATA[vector]]></category>

		<guid isPermaLink="false">http://gushh.net/blog/?p=114</guid>
		<description><![CDATA[Heres an example of what I call &#8220;macro templates&#8221; in PureBasic. A template encapsulates certain functionality, allowing you to dynamically generate the code in a flexible manner. This example implements a bare-bones n-vector library using a structure and a static array: Macro Vector_Register&#40; _n, _type &#41; &#160; Structure VECTOR#_n#_type vector._type&#91;_n&#93; EndStructure &#160; Procedure._type Vector#_n#_type#_Add&#40; *r.VECTOR#_n#_type, [...]]]></description>
			<content:encoded><![CDATA[<p>Heres an example of what I call &#8220;macro templates&#8221; in PureBasic. A template encapsulates certain functionality, allowing you to dynamically generate the code in a flexible manner.</p>
<p>This example implements a bare-bones n-vector library using a structure and a static array:</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">Macro</span> Vector_Register<span style="color: #000066;">&#40;</span> _n, _type <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Structure</span> VECTOR#_n#_type
		vector._type<span style="color: #000066;">&#91;</span>_n<span style="color: #000066;">&#93;</span>
	<span style="color: #000066; font-weight: bold;">EndStructure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>._type Vector#_n#_type#_Add<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>r.VECTOR#_n#_type, <span style="color: #000066;">*</span>a.VECTOR#_n#_type, <span style="color: #000066;">*</span>b.VECTOR#_n#_type <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">Define</span>.i i
		<span style="color: #000066; font-weight: bold;">For</span> i<span style="color: #000066;">=</span><span style="color: #CC0000;">0</span> <span style="color: #000066; font-weight: bold;">To</span> _n <span style="color: #000066;">-</span> <span style="color: #CC0000;">1</span>
			<span style="color: #000066;">*</span>r<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span>i<span style="color: #000066;">&#93;</span> <span style="color: #000066;">=</span> <span style="color: #000066;">*</span>a<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span>i<span style="color: #000066;">&#93;</span> <span style="color: #000066;">+</span> <span style="color: #000066;">*</span>b<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span>i<span style="color: #000066;">&#93;</span>
		<span style="color: #000066; font-weight: bold;">Next</span>
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>._type Vector#_n#_type#_Subtract<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>r.VECTOR#_n#_type, <span style="color: #000066;">*</span>a.VECTOR#_n#_type, <span style="color: #000066;">*</span>b.VECTOR#_n#_type <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">Define</span>.i i
		<span style="color: #000066; font-weight: bold;">For</span> i<span style="color: #000066;">=</span><span style="color: #CC0000;">0</span> <span style="color: #000066; font-weight: bold;">To</span> _n <span style="color: #000066;">-</span> <span style="color: #CC0000;">1</span>
			<span style="color: #000066;">*</span>r<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span>i<span style="color: #000066;">&#93;</span> <span style="color: #000066;">=</span> <span style="color: #000066;">*</span>a<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span>i<span style="color: #000066;">&#93;</span> <span style="color: #000066;">-</span> <span style="color: #000066;">*</span>b<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span>i<span style="color: #000066;">&#93;</span>
		<span style="color: #000066; font-weight: bold;">Next</span>
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>._type Vector#_n#_type#_Divide<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>r.VECTOR#_n#_type, <span style="color: #000066;">*</span>a.VECTOR#_n#_type, <span style="color: #000066;">*</span>b.VECTOR#_n#_type <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">Define</span>.i i
		<span style="color: #000066; font-weight: bold;">For</span> i<span style="color: #000066;">=</span><span style="color: #CC0000;">0</span> <span style="color: #000066; font-weight: bold;">To</span> _n <span style="color: #000066;">-</span> <span style="color: #CC0000;">1</span>
			<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>b<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span>i<span style="color: #000066;">&#93;</span> <span style="color: #000066;">&lt;&gt;</span> <span style="color: #CC0000;">0.0</span> <span style="color: #000066; font-weight: bold;">And</span> <span style="color: #000066;">*</span>a<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span>i<span style="color: #000066;">&#93;</span> <span style="color: #000066;">&lt;&gt;</span> <span style="color: #CC0000;">0.0</span>
				<span style="color: #000066;">*</span>r<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span>i<span style="color: #000066;">&#93;</span> <span style="color: #000066;">=</span> <span style="color: #000066;">*</span>a<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span>i<span style="color: #000066;">&#93;</span> <span style="color: #000066;">/</span> <span style="color: #000066;">*</span>b<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span>i<span style="color: #000066;">&#93;</span>
			<span style="color: #000066; font-weight: bold;">EndIf</span>
		<span style="color: #000066; font-weight: bold;">Next</span>
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>._type Vector#_n#_type#_Multiply<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>r.VECTOR#_n#_type, <span style="color: #000066;">*</span>a.VECTOR#_n#_type, <span style="color: #000066;">*</span>b.VECTOR#_n#_type <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">Define</span>.i i
		<span style="color: #000066; font-weight: bold;">For</span> i<span style="color: #000066;">=</span><span style="color: #CC0000;">0</span> <span style="color: #000066; font-weight: bold;">To</span> _n <span style="color: #000066;">-</span> <span style="color: #CC0000;">1</span>
			<span style="color: #000066;">*</span>r<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span>i<span style="color: #000066;">&#93;</span> <span style="color: #000066;">=</span> <span style="color: #000066;">*</span>a<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span>i<span style="color: #000066;">&#93;</span> <span style="color: #000066;">*</span> <span style="color: #000066;">*</span>b<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span>i<span style="color: #000066;">&#93;</span>
		<span style="color: #000066; font-weight: bold;">Next</span>
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>._type Vector#_n#_type#_DotProduct<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>a.VECTOR#_n#_type, <span style="color: #000066;">*</span>b.VECTOR#_n#_type <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">Define</span>.i i
		<span style="color: #000066; font-weight: bold;">Define</span>._type result
		<span style="color: #000066; font-weight: bold;">For</span> i<span style="color: #000066;">=</span><span style="color: #CC0000;">0</span> <span style="color: #000066; font-weight: bold;">To</span> _n <span style="color: #000066;">-</span> <span style="color: #CC0000;">1</span>
			result <span style="color: #000066;">+</span> <span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>a<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span>i<span style="color: #000066;">&#93;</span> <span style="color: #000066;">*</span> <span style="color: #000066;">*</span>b<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span>i<span style="color: #000066;">&#93;</span> <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">Next</span>
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> result
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>._type Vector#_n#_type#_Length<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>v.VECTOR#_n#_type <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #0000ff;">Sqr</span><span style="color: #000066;">&#40;</span> Vector#_n#_type#_DotProduct<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>v, <span style="color: #000066;">*</span>v <span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>._type Vector#_n#_type#_Distance<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>a.VECTOR#_n#_type, <span style="color: #000066;">*</span>b.VECTOR#_n#_type <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">Define</span>.VECTOR#_n#_type temp
		Vector#_n#_type#_Subtract<span style="color: #000066;">&#40;</span> temp, <span style="color: #000066;">*</span>a, <span style="color: #000066;">*</span>b <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> Vector#_n#_type#_Length<span style="color: #000066;">&#40;</span> temp <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>.s Vector#_n#_type#_Debug<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>v.VECTOR#_n#_type, Decimals.i<span style="color: #000066;">=</span>#PB_Default <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">Define</span>.i i
		<span style="color: #000066; font-weight: bold;">Define</span>.s tmp <span style="color: #000066;">=</span> <span style="color: #009900;">&quot;[&quot;</span>
		<span style="color: #000066; font-weight: bold;">For</span> i<span style="color: #000066;">=</span><span style="color: #CC0000;">0</span> <span style="color: #000066; font-weight: bold;">To</span> _n<span style="color: #000066;">-</span><span style="color: #CC0000;">1</span>
			tmp <span style="color: #000066;">+</span> <span style="color: #0000ff;">StrF</span><span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>v<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span>i<span style="color: #000066;">&#93;</span>, Decimals.i <span style="color: #000066;">&#41;</span>
			<span style="color: #000066; font-weight: bold;">If</span> i <span style="color: #000066;">&lt;&gt;</span> _n<span style="color: #000066;">-</span><span style="color: #CC0000;">1</span>
				tmp <span style="color: #000066;">+</span> <span style="color: #009900;">&quot;, &quot;</span>
			<span style="color: #000066; font-weight: bold;">EndIf</span>
		<span style="color: #000066; font-weight: bold;">Next</span>
		tmp <span style="color: #000066;">+</span> <span style="color: #009900;">&quot;]&quot;</span>
		<span style="color: #000066; font-weight: bold;">Debug</span> tmp
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> tmp
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndMacro</span></pre></div></div>

<p>It might look strange/complicated at first, but once you read it you&#8217;ll realize it&#8217;s fairly simple.</p>
<p>Let&#8217;s see the usage of this particular template:</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;">Vector_Register<span style="color: #000066;">&#40;</span><span style="color: #CC0000;">3</span>, f <span style="color: #000066;">&#41;</span>	<span style="color: #ff0000; font-style: italic;">; Register a float &quot;vec3&quot;.</span>
<span style="color: #000066; font-weight: bold;">Define</span>.VECTOR3f a,b,c	<span style="color: #ff0000; font-style: italic;">; Define a few vectors with the new structure.</span>
&nbsp;
a<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #000066;">&#93;</span> <span style="color: #000066;">=</span> <span style="color: #CC0000;">10.0</span>
a<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #000066;">&#93;</span> <span style="color: #000066;">=</span> <span style="color: #CC0000;">20.0</span>
a<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #000066;">&#93;</span> <span style="color: #000066;">=</span> <span style="color: #CC0000;">30.0</span>
&nbsp;
b<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #000066;">&#93;</span> <span style="color: #000066;">=</span> <span style="color: #CC0000;">100.0</span>
b<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #000066;">&#93;</span> <span style="color: #000066;">=</span> <span style="color: #CC0000;">200.0</span>
b<span style="color: #000066;">\</span>vector<span style="color: #000066;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #000066;">&#93;</span> <span style="color: #000066;">=</span> <span style="color: #CC0000;">300.0</span>
&nbsp;
Vector3f_Add<span style="color: #000066;">&#40;</span>c, a, b<span style="color: #000066;">&#41;</span>	<span style="color: #ff0000; font-style: italic;">; c = a + b</span>
Vector3f_Debug<span style="color: #000066;">&#40;</span>c<span style="color: #000066;">&#41;</span>	<span style="color: #ff0000; font-style: italic;">; show each element using the debug output.</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Debug</span> Vector3f_DotProduct<span style="color: #000066;">&#40;</span> a, b <span style="color: #000066;">&#41;</span>
<span style="color: #000066; font-weight: bold;">Debug</span> Vector3f_Length<span style="color: #000066;">&#40;</span> a <span style="color: #000066;">&#41;</span>
<span style="color: #000066; font-weight: bold;">Debug</span> Vector3f_Distance<span style="color: #000066;">&#40;</span> a, b <span style="color: #000066;">&#41;</span></pre></div></div>

<p>Cool, huh?. And you can define any amount of elements with any basic type.</p>
<p>Of course we sacrificed speed for flexibility. In those cases where we have to define n-vectors, this would be an ideal solution. For everything else, I suggest a specific library, such as my <a href="http://gushh.net/blog/2009/01/09/a-fast-vector-library-for-purebasic/" target="_blank">vec3</a> macro lib.</p>
<p>Using this principle you can abstract almost anything, within reason. One good example is my <a href="http://gushh.net/blog/2009/05/03/object-factory-for-purebasic/" target="_blank">object factory</a> template.  Ideally one would have arrays, lists, etc. Implemented in this very same way, in such case the possibilities would be endless and you&#8217;d be able to define dynamic lists/arrays inside structures, etc.</p>
<p>I strongly advice you to implement at least one of those templates, even if it&#8217;s just for an exercise.</p>
<p>Having the extra tools can&#8217;t hurt!</p>
<p>Cheers.</p>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://gushh.net/blog/2009/08/05/average-template/" rel="bookmark" class="crp_title">Average template</a></li><li><a href="http://gushh.net/blog/2009/01/09/a-fast-vector-library-for-purebasic/" rel="bookmark" class="crp_title">A fast vector library for purebasic.</a></li><li><a href="http://gushh.net/blog/2009/05/10/vec3-and-easyobject-libraries-updated/" rel="bookmark" class="crp_title">VEC3 and EasyObject Libraries updated.</a></li><li><a href="http://gushh.net/blog/2009/01/25/updated-the-vec3-lib-major-overhaul/" rel="bookmark" class="crp_title">Updated the vec3 lib, major overhaul.</a></li><li><a href="http://gushh.net/blog/2009/08/28/structuring-libraries-basic-design-tips/" rel="bookmark" class="crp_title">Structuring libraries, basic design tips.</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://gushh.net/blog/2009/07/13/macro-templates-by-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
