<?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 DevBlog &#187; pattern</title>
	<atom:link href="http://gushh.net/blog/?tag=pattern&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://gushh.net/blog</link>
	<description>This blog is about software, electronics engineering and game development.</description>
	<lastBuildDate>Tue, 24 Jan 2012 00:31:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Structuring libraries, basic design tips.</title>
		<link>http://gushh.net/blog/structuring-libraries-basic-design-tips/</link>
		<comments>http://gushh.net/blog/structuring-libraries-basic-design-tips/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 23:20:56 +0000</pubDate>
		<dc:creator>GuShH</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[PureBasic]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[instancing]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[prng]]></category>
		<category><![CDATA[programming tutorial]]></category>
		<category><![CDATA[purebasic]]></category>
		<category><![CDATA[random]]></category>

		<guid isPermaLink="false">http://gushh.net/blog/?p=227</guid>
		<description><![CDATA[One of the most important things a programmer has to know and learn is proper code design, structuring of the code, etc. This may not be imperative for you if you&#8217;re just starting out, but sooner or later you&#8217;ll be able to spot the patterns and realize that your current way of doing things is [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most important things a programmer has to know and learn is proper code design, structuring of the code, etc. This may not be imperative for you if you&#8217;re just starting out, but sooner or later you&#8217;ll be able to spot the patterns and realize that your current way of doing things is not optimal but rather cumbersome. This is when you start to separate things into modules and you begin to design your code with more meaning and purpose.</p>
<p>By breaking your code into modules or &#8220;classes&#8221; you can have a finer granularity of the code itself, you can encapsulate the base code and work on top of it, you can also reuse the code in other projects if you have a good design. An example would be a log library (for logging useful debugging information onto text files) with proper design and care you&#8217;ll be able to use the same library in almost all of your projects with little to no modification at all.<br />
<span id="more-227"></span><br />
The main points you should never forget about are:</p>
<ul>
<li>Instancing
<ul>
<li>This is when you create an &#8220;object&#8221; &#8211; And as you create it, you should also be able to destroy it. (Constructors / Destructors in OOP). An object factory may be implemented (this would be another class) to handle the instances. <em>In CPP you may use smart pointers, but we&#8217;re talking about PB in this case.</em></li>
</ul>
</li>
<li>Members
<ul>
<li>In our case a main structure is used as the &#8220;members&#8221; (Again, in reference to OOP) &#8211; This structure is instantiated by the constructor or &#8220;create&#8221; function. Don&#8217;t forget that anything you allocate you should also be able to deallocate. It doesn&#8217;t matter if the language or OS handles it underneath; it&#8217;s bad coding practice to avoid such trivial &#8211; but important &#8211; code.</li>
</ul>
</li>
<li>Methods
<ul>
<li>They give you functionality, in PB there are no methods but a similar concept applies: you have functions or routines that you create specifically for the library itself. Some may be of the Get / Set type (which either get or set a variable) Or other methods such as converters, helpers, accesibility calls, etc.</li>
</ul>
</li>
</ul>
<p>All together they make a class; in our case a library.</p>
<p>Let&#8217;s write one and follow by example!</p>
<p>Imagine you needed to encapsulate the random functions to provide extra functionality, then all you need to do is first define a structure (if you&#8217;re going to have members; for instance do you want the library to &#8220;remember&#8221; the previous and current numbers?, if so you would need a structure!)</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">;- Library Structure</span>
<span style="color: #000066; font-weight: bold;">Structure</span> <span style="color: #0000ff;">RANDOM</span>
	LastNumber.i
	PreviousNumber.i
	Seed.i
<span style="color: #000066; font-weight: bold;">EndStructure</span></pre></div></div>

<p>Alright, now we need to write our constructor and destructor functions! This will create and destroy the instances.</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">;- Library Instancing</span>
<span style="color: #000066; font-weight: bold;">Procedure</span>.i RandomCreate<span style="color: #000066;">&#40;</span> Seed.i <span style="color: #000066;">=</span> <span style="color: #CC0000;">0</span> <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Define</span>.<span style="color: #0000ff;">RANDOM</span> <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> <span style="color: #0000ff;">RANDOM</span> <span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
&nbsp;
		<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066; font-weight: bold;">Not</span> Seed
			Seed <span style="color: #000066;">=</span> <span style="color: #0000ff;">ElapsedMilliseconds</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">With</span> <span style="color: #000066;">*</span>this
			 <span style="color: #000066;">\</span>LastNumber 		<span style="color: #000066;">=</span> <span style="color: #CC0000;">0</span>
			 <span style="color: #000066;">\</span>PreviousNumber 	<span style="color: #000066;">=</span> <span style="color: #CC0000;">0</span>
			 <span style="color: #000066;">\</span>Seed 			<span style="color: #000066;">=</span> Seed
		<span style="color: #000066; font-weight: bold;">EndWith</span>
&nbsp;
		<span style="color: #0000ff;">RandomSeed</span><span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>Seed <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this
&nbsp;
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i RandomDestroy<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.<span style="color: #0000ff;">RANDOM</span> <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
&nbsp;
		<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;">ProcedureReturn</span> <span style="color: #000066;">*</span>this
&nbsp;
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span></pre></div></div>

<p>That&#8217;s not too bad, first we allocate an instance and then we populate the member variables just to return the pointer of our new &#8220;object&#8221;. The destructor simply frees any allocated memory and clears out the pointer itself.</p>
<p>Now we need to define our library functions, in this case we only need one so we may generate a random number and keep track of the previous and current numbers.</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">;- Library functions</span>
<span style="color: #000066; font-weight: bold;">Procedure</span>.i RandomGetInteger<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.<span style="color: #0000ff;">RANDOM</span>, min.i, max.i <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
&nbsp;
		<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>PreviousNumber 	<span style="color: #000066;">=</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>LastNumber
		<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>LastNumber 	<span style="color: #000066;">=</span> <span style="color: #000066;">&#40;</span> min <span style="color: #000066;">+</span> <span style="color: #0000ff;">Random</span><span style="color: #000066;">&#40;</span> max<span style="color: #000066;">-</span>min <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;">\</span>LastNumber
&nbsp;
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span></pre></div></div>

<p>That&#8217;s ok, but we can still add functionality. For instance, we need a way to access the structure fields (or &#8220;members&#8221;) without having to cast pointers, etc. We can do this by implementing a few Get/Set routines. In this case we only need to get the previous and current numbers, so&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">;- Helper functions</span>
<span style="color: #000066; font-weight: bold;">Procedure</span>.i RandomGetLast<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.<span style="color: #0000ff;">RANDOM</span> <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>LastNumber
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Procedure</span>.i RandomGetPrevious<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.<span style="color: #0000ff;">RANDOM</span> <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>PreviousNumber
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span></pre></div></div>

<p>But wait, we can still add functionality! such as utility functions, in this case we would like to fill a given array with random numbers, so&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">;- Utility functions</span>
<span style="color: #000066; font-weight: bold;">Procedure</span>.i RandomFillArray<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.<span style="color: #0000ff;">RANDOM</span>, min.i, max.i, Array DestArray.i<span style="color: #000066;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
&nbsp;
		<span style="color: #000066; font-weight: bold;">Define</span>.i Size, i
		Size <span style="color: #000066;">=</span> ArraySize<span style="color: #000066;">&#40;</span> DestArray<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>, <span style="color: #CC0000;">1</span> <span style="color: #000066;">&#41;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">If</span> Size
			<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> Size
				DestArray<span style="color: #000066;">&#40;</span>i<span style="color: #000066;">&#41;</span> <span style="color: #000066;">=</span> RandomGetInteger<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this, min, max <span style="color: #000066;">&#41;</span>
			<span style="color: #000066; font-weight: bold;">Next</span>
		<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span></pre></div></div>

<p>And while we&#8217;re at it, let&#8217;s fill lists as well!</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">Procedure</span>.i RandomFillList<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.<span style="color: #0000ff;">RANDOM</span>, min.i, max.i, elements.i, List DestList.i<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>, Clear.i<span style="color: #000066;">=</span>#False <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this
&nbsp;
		<span style="color: #000066; font-weight: bold;">If</span> Clear
			<span style="color: #0000ff;">ClearList</span><span style="color: #000066;">&#40;</span>DestList<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">Define</span>.i i
&nbsp;
		<span style="color: #000066; font-weight: bold;">If</span> elements
			<span style="color: #000066; font-weight: bold;">For</span> i<span style="color: #000066;">=</span><span style="color: #CC0000;">1</span> <span style="color: #000066; font-weight: bold;">To</span> elements
				<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #0000ff;">AddElement</span><span style="color: #000066;">&#40;</span> DestList<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
					DestList<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">=</span> RandomGetInteger<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this, min, max <span style="color: #000066;">&#41;</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;">EndIf</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">EndIf</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndProcedure</span></pre></div></div>

<p>If you don&#8217;t need to keep track of the current and previous numbers, you can just opt out the instancing system and modify the utility calls to work in an &#8220;instance-less&#8221; mode, however that&#8217;s up to you.</p>
<p>But, let&#8217;s take it for a ride first!</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">;- Example</span>
<span style="color: #000066; font-weight: bold;">Define</span>.<span style="color: #0000ff;">RANDOM</span> <span style="color: #000066;">*</span>rnd <span style="color: #000066;">=</span> RandomCreate<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
<span style="color: #000066; font-weight: bold;">Define</span>.i i
&nbsp;
<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>rnd
&nbsp;
	<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> <span style="color: #CC0000;">99</span>
		<span style="color: #000066; font-weight: bold;">Debug</span> RandomGetInteger<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>rnd, <span style="color: #CC0000;">5</span>, <span style="color: #CC0000;">10</span> <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">Next</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Debug</span> <span style="color: #009900;">&quot;--&quot;</span>
	<span style="color: #000066; font-weight: bold;">Debug</span> RandomGetLast<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>rnd <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">Debug</span> RandomGetPrevious<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>rnd <span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">Debug</span> <span style="color: #009900;">&quot;--&quot;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Dim</span> TestArray.i<span style="color: #000066;">&#40;</span><span style="color: #CC0000;">9</span><span style="color: #000066;">&#41;</span>
	RandomFillArray<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>rnd, <span style="color: #CC0000;">100</span>, <span style="color: #CC0000;">200</span>, TestArray<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
&nbsp;
	<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> <span style="color: #CC0000;">9</span>
		<span style="color: #000066; font-weight: bold;">Debug</span> TestArray<span style="color: #000066;">&#40;</span>i<span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">Next</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Debug</span> <span style="color: #009900;">&quot;--&quot;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">NewList</span> TestList.i<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
	RandomFillList<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>rnd, <span style="color: #CC0000;">10</span>, <span style="color: #CC0000;">20</span>, <span style="color: #CC0000;">5</span>, TestList<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">ForEach</span> TestList<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">Debug</span> TestList<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">Next</span>
&nbsp;
	RandomDestroy<span style="color: #000066;">&#40;</span><span style="color: #000066;">*</span>rnd<span style="color: #000066;">&#41;</span>
<span style="color: #000066; font-weight: bold;">EndIf</span></pre></div></div>

<p>Ok, so we tested the instancing and most if not all of the &#8220;methods&#8221; we just created. By now you should realize how useful such systems can be if you happen to design them correctly.</p>
<p>This particular lib could be extended to support averaging of the numbers, median / mean, etc. You could even implement a custom PRNG and give the option to choose between yours and the built-in one!.</p>
<p>You may download the entire source from <a href="http://gushh.net/dev/?file=pb/random.pb">here</a>. As an exercise you could implement the Get/Set methods for the Seed.</p>
<p>Cheers!</p>
<p>PS: Someone remind me to update the language file for GeShi and to fix the font <img src='http://gushh.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  (The indentation is all wrong!)</p>
<g:plusone href='http://gushh.net/blog/structuring-libraries-basic-design-tips/'></g:plusone>]]></content:encoded>
			<wfw:commentRss>http://gushh.net/blog/structuring-libraries-basic-design-tips/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Macro Templates, by example.</title>
		<link>http://gushh.net/blog/macro-templates-by-example/</link>
		<comments>http://gushh.net/blog/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>
<g:plusone href='http://gushh.net/blog/macro-templates-by-example/'></g:plusone>]]></content:encoded>
			<wfw:commentRss>http://gushh.net/blog/macro-templates-by-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An efficient and simple object factory for PureBasic</title>
		<link>http://gushh.net/blog/object-factory-for-purebasic/</link>
		<comments>http://gushh.net/blog/object-factory-for-purebasic/#comments</comments>
		<pubDate>Sun, 03 May 2009 16:43:05 +0000</pubDate>
		<dc:creator>GuShH</dc:creator>
				<category><![CDATA[Libraries]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[PureBasic]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[EasyObject]]></category>
		<category><![CDATA[factory]]></category>
		<category><![CDATA[GuShH]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[purebasic]]></category>

		<guid isPermaLink="false">http://gushh.net/blog/?p=65</guid>
		<description><![CDATA[Whenever you deal with structures and what have you it&#8217;s obvious that there has to be a better way of doing it. Think about it, you encapsulate your structure-related code in such a way that it becomes clean, nice and tidy &#8212; in other words it makes it easy to understand what it does. However [...]]]></description>
			<content:encoded><![CDATA[<div>
<p>Whenever you deal with structures and what have you it&#8217;s obvious that there has to be a better way of doing it. Think about it, you encapsulate your structure-related code in such a way that it becomes clean, nice and tidy &#8212; in other words it makes it easy to understand what it does.</p>
<p>However in essence, you end up writing pretty much the same allocate / deallocate, list, etc. code every time you incorporate a new &#8220;class&#8221; into your project. Why should we?, let&#8217;s avoid it all together and focus our efforts in the task at hand rather than in the mundane stuff.</p>
<p>If you open your eyes you&#8217;ll see that this is an obvious pattern but what isn&#8217;t so obvious is how do you implement a template that deals with it in an efficient and clean way within the current constraints of the language.</p>
<p>Well&#8230; Have no fear, EasyObject is here!<br />
First, <a href="http://gushh.net/dls/easyobject.zip" target="_blank">download it</a>. Secondly, embrace it.</p>
<p>Third, you&#8217;re free to post any comments regarding on how to enhance it if you dear to!.</p>
<p><span id="more-65"></span>Ok, now let&#8217;s describe this thing a little more and leave the marketing speech in the past.</p>
<p>From the &#8220;readme&#8221;:</p>
<blockquote><p>EasyObject is an object factory template for PureBasic. It allows you to register any structure as a &#8220;class&#8221;. With EasyObject you can define constructors and destructors as well as iterators with ease.</p>
<p>The purpose of EasyObject is mainly to encapsulate trivial and redundant code usually regarded to allocation, deallocation and keeping track of the objects. The usage is fairly simple: classes can be registered and unregistered. Once a class has been registered, we can create new objects and delete them at will. If we decide to unregister a class, all of the objects will be freed and their respective destructors will be called.</p></blockquote>
<p>More from where that came from&#8230;</p>
<blockquote><p>Macros in this case are used as an aid to define templates (like you would in C++ for instance). There are certain limitations that forced me to design the template as it is right now but the usage is very simple. We&#8217;re not trying to mimic any language here but rather provide a way of dealing with objects without having to write redundant code all the time. Because overloading doesn&#8217;t exist in PB, the constructor will be called twice (once by the template and once by yourself) if you want to define the members using said ctor, just make certain that any dynamic allocations are performed only once or you&#8217;ll have a nasty memory leak in your hands.</p></blockquote>
<p>It&#8217;s clear that some limitations are present in the template but for most of the tasks this is a great addition to anyone&#8217;s code.</p>
<p>Let me know if you find any bugs!</p>
<p>Cheers,<br />
GuShH</p></div>
<g:plusone href='http://gushh.net/blog/object-factory-for-purebasic/'></g:plusone>]]></content:encoded>
			<wfw:commentRss>http://gushh.net/blog/object-factory-for-purebasic/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

<!-- Served from: gushh.net @ 2012-02-09 07:19:03 by W3 Total Cache -->
