<?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>Karig &#187; REALbasic</title>
	<atom:link href="http://karig.net/topics/realbasic/feed/" rel="self" type="application/rss+xml" />
	<link>http://karig.net</link>
	<description>My humble home on the Web</description>
	<lastBuildDate>Thu, 17 Dec 2009 20:24:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>String vs. MemoryBlock</title>
		<link>http://karig.net/2009/11/string-vs-memoryblock/</link>
		<comments>http://karig.net/2009/11/string-vs-memoryblock/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 06:23:41 +0000</pubDate>
		<dc:creator>Karig</dc:creator>
				<category><![CDATA[REALbasic]]></category>

		<guid isPermaLink="false">http://karig.net/?p=538</guid>
		<description><![CDATA[My text editor has to have a way to store text in memory. REALbasic provides two classes for this: String and MemoryBlock. For my purposes, MemoryBlock is better. ]]></description>
			<content:encoded><![CDATA[<p>My text editor has to have a way to store text in memory. REALbasic provides two classes for this: String and MemoryBlock. You might think at first that Strings are for text and MemoryBlocks are for binary data, but for my purposes, MemoryBlock is better.</p>
<h3>String objects</h3>
<p>Strings in REALbasic are immutable — that is, you can&#8217;t write code to just reach inside a String and rearrange its contents. If you write code to change the text in a String, your program will always create a new String object and copy all of the text from the old String into the new String. That&#8217;s just how REALbasic Strings work. The whole point of a text editor is to allow you to change the text in a file at any time, so this operation should be efficient, and REALbasic&#8217;s String class is not efficient for this.</p>
<p>This is how you insert a new piece of text into a String:</p>
<pre>Dim offset as Integer
//...
buffer = Left( buffer, offset ) _
    + newText _
    + Right( buffer, Len( buffer ) - offset )</pre>
<p>Here is what happens under the hood whenever the above code is run: REALbasic creates a new &#8220;buffer&#8221; object, copies all of the data from the old &#8220;buffer&#8221; and &#8220;newText&#8221; objects into the new &#8220;buffer&#8221; object, returns the new &#8220;buffer&#8221; object, and marks the old &#8220;buffer&#8221; object as empty space to be reclaimed by the operating system. This isn&#8217;t so bad if &#8220;buffer&#8221; is always a small String, but what if the String is the text from a 200KB file? Then every time the user types something, the editor has to borrow a 200KB block from the operating system to build the new object, then copy 200KB of data from the existing buffer into the new object, then let the REALbasic framework &#8220;collect&#8221; the old buffer so the OS can have its 200KB block back. This takes up both CPU time and memory. Surely there is a way to insert new text that takes up less?</p>
<p>There is, of course. Instead of keeping a file&#8217;s contents in memory as a single big long String, the editor could split the file&#8217;s contents up into many short Strings and store references to those Strings in an array. Inserting a String into an array actually inserts only the location of the String, not its contents, so insertion is quick. Now the code to insert new text into the buffer looks like this:</p>
<pre>Dim index, offset as Integer
Dim leftPart, rightPart as String
//...
leftPart = Left( buffer( index ), offset )
rightPart = Right( buffer( index ), _
    Len(buffer( index )) - offset)
buffer.Insert index, leftPart
buffer.Insert index+1, newText
buffer.Insert index+2, rightPart</pre>
<p>The code is longer, but it runs faster because there is less going on under the hood: New text is inserted by splitting only a single segment of the file and inserting the new text in between the parts. It isn&#8217;t necessary to append the parts together into a long String again, because the array keeps the parts in order.</p>
<p>This clearly alleviates the problem, but it does not solve it: To insert new data into the buffer, you still have to create a copy of data you already have.</p>
<p>In addition, there is another problem: Searching the buffer becomes very awkward when the buffer consists of lots of fragments in an array instead of one great big long string. So in this case the editor would probably append all of these items into a single string when a search is about to be performed. Again, the data in the many segments of the buffer would need to be copied into a separate String object just to perform a search. As before, this takes time that the editor could spend more productively doing something else, and it doubles the editor&#8217;s memory requirements because it has to make a second copy of all of the text data. There has to be a better way.</p>
<p><i>(Side note: Theodore H. Smith offers a REALbasic plugin called <a href="http://www.elfdata.com/plugin/">ElfData</a>. The plugin offers special string classes (ElfData and FastString) that let you split and recombine strings much more efficiently than REALbasic&#8217;s String class, mostly by cutting down on the amount of data copying and memory allocation that has to be done at runtime. However, the problem (from my perspective) is the same: The ElfData and FastString classes still work like String objects. To insert text into a long string, you still have to break the long string apart and copy all of the text from the various pieces into a new object, and you need space in memory for two copies of your data to do that. A text editor would use memory more efficiently if it could avoid creating a second copy of the text altogether.)</i></p>
<h3>MemoryBlock objects</h3>
<p>Unlike String objects, MemoryBlocks in REALbasic can be updated in place: You can insert new text into a MemoryBlock without the REALbasic framework recreating the whole MemoryBlock each time. Further, as <a href="http://www.declaresub.com/article/149/realbasics-secret-string-buffer">Charles Yeomans points out</a>, if you use a BinaryStream object as the front end for a MemoryBlock, you end up with a buffer that acts like a file: you can &#8220;seek&#8221; to a given position anywhere within the buffer and read or write data at that point, and if you write data beyond the end of the buffer, then the buffer will be made large enough to hold the new data.</p>
<p>When you write ten bytes of data into the MemoryBlock, then ten bytes of space within the MemoryBlock, beginning with the current position, will be overwritten. If the current position is ten or more bytes from the end of the buffer, then no data in the MemoryBlock is copied or moved, so the write operation is extremely fast. However, if the current position is less than ten bytes from the end of the buffer, then a new MemoryBlock has to be allocated, and the data in the old MemoryBlock has to be copied into the new one. If you use a BinaryStream object as the front end for a MemoryBlock, as explained in Charles Yeomans&#8217;s article, the new MemoryBlock will be twice the size of the old one, so the need to move data to a new MemoryBlock should not arise often.</p>
<p>But if you want to <em>insert</em> text into a MemoryBlock, you don&#8217;t want to overwrite the text already in there. So it would seem that you&#8217;d still need to copy existing text to a new location to make room for the new text. However, if the text to be overwritten is actually considered garbage and not part of the text you want to keep, then you can &#8220;insert&#8221; your text extremely quickly. This is the basis for the <a href="http://en.wikipedia.org/wiki/Gap_buffer">gap buffer</a> that is used in many text editors. A gap buffer is a text buffer with three regions: the text before the current position, the text after the current position, and the &#8220;gap&#8221; in between. The gap is of course just a stretch of nonsense text that isn&#8217;t considered part of the main text and that is present to be overwritten with whatever new text the user types or pastes in.</p>
<p>If you need to search the text being edited, the search might be easier if the gap is removed. This of course requires copying text within the buffer to remove the gap. However, no new objects are created, so the editor would not need to borrow more memory from the operating system to perform this operation.</p>
<h3>MemoryBlocks are best</h3>
<p>Clearly, if you want to write a text editor in REALbasic, it is better to use a MemoryBlock to store the text being edited. Inserting and deleting text will be much faster and will require less memory than if you were to try to store the text in String objects.</p>
]]></content:encoded>
			<wfw:commentRss>http://karig.net/2009/11/string-vs-memoryblock/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why REALbasic?</title>
		<link>http://karig.net/2009/10/why-realbasic/</link>
		<comments>http://karig.net/2009/10/why-realbasic/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 00:30:25 +0000</pubDate>
		<dc:creator>Karig</dc:creator>
				<category><![CDATA[REALbasic]]></category>

		<guid isPermaLink="false">http://karig.net/?p=283</guid>
		<description><![CDATA[My current plan is to write my text editor in REALbasic. REALbasic as a language is easy to learn, easy to write, and easy to read. You can create your own GUI controls, you can compile your application for Windows and Linux and the Mac all at once, and you can even write plugins for REALbasic in C++ if REALbasic isn't fast enough.]]></description>
			<content:encoded><![CDATA[<p>My current plan is to write my text editor in <a href="http://www.realsoftware.com/realbasic/">REALbasic</a>.</p>
<p>I like REALbasic. The language is easy to learn, easy to write, and easy to read. The REALbasic IDE provides lots of pre-built controls for building your program&#8217;s user interface, but you can also create your own controls from the ground up. If you have the Professional version of REALbasic, you can write your program once and use it to build executables for Windows, Mac OS X, and Linux all at once. And if some part of your program should prove to be unacceptably slow, you have the option of writing a REALbasic plugin in C++ to speed things up.</p>
<h3>What&#8217;s good about REALbasic?</h3>
<p>There is a lot to like about this language.</p>
<ul>
<li>The language is easy to pick up. It&#8217;s BASIC, after all.</li>
<li>The language is object-oriented. You can create your own classes, or subclasses of existing classes. You can define interfaces as in Java and then create classes that implement those interfaces.</li>
<li>Objects are reference-counted and are deallocated automatically when no other object points to them.</li>
<li>Some of the features the language has include:
<ul>
<li><strong>Basic data types:</strong> Integer, Single, Double, Boolean, Color, Date, Currency, String, Array, Variant, Dictionary (actually a <a href="http://en.wikipedia.org/wiki/Hash_table">hash table</a>), MemoryBlock. REALbasic has many functions for dealing with variables of each type.</li>
<li><strong>Low-level data types</strong> for working with binary files or C functions in a library or the operating system: Int8, Int16, Int32, Int64; UInt8 (Byte), UInt16, UInt32, UInt64; Ptr; CString, PString, WString.</li>
<li><strong>Literal syntax</strong> not only for hexadecimal (<tt>&amp;hFF</tt>), binary (<tt>&amp;b11101101</tt>), and octal (<tt>&amp;o137</tt>) numbers, but also for colors (<tt>&amp;cFF6F00</tt> for orange) and Unicode codepoints (<tt>&amp;u2014</tt> for an em dash).</li>
<li><strong>Delegates</strong> are essentially typesafe function pointers. A Delegate can point at any function (or object method) with a specific list of argument types and a specific return type — for example, any function with a first argument &#8220;As Integer&#8221;, a second argument &#8220;As String&#8221;, and no other arguments, and that returns a Boolean. You can use these at runtime and change what your program does in response to a keystroke or a button press.</li>
<li>Support for <strong>exceptions</strong>. You can define your own exception classes, use the Raise statement to throw an exception, and Try&#8230;Catch&#8230;Finally to catch and handle exceptions.</li>
<li><strong>Declare</strong> lets you call subroutines or functions in DLLs (on Windows) or shared libraries (on Mac OS X or Linux).</li>
<li><strong>Assigns</strong> lets you set up a Sub to be called with &#8220;theSub(a, b) = c&#8221; instead of &#8220;theSub(a, b, c)&#8221;.</li>
<li><strong>Extends</strong> lets you set up a Sub or Function to be called with &#8220;a.theSub(b, c)&#8221; instead of theSub(a, b, c)&#8221;.</li>
<li><strong>#If&#8230;#Endif</strong> allows conditional compilation. This is most useful for allowing or &#8220;commenting out&#8221; code depending on whether (1) a constant is defined, or (2) the running within the IDE, or (3) the application is running on Windows or the Mac or Linux.</li>
</ul>
</li>
<li>The IDE lets you build your GUI by dragging controls onto a window and setting their properties, just as <a href="http://en.wikipedia.org/wiki/Visual_Basic">Visual Basic</a> does.</li>
<li>If the controls supplied with REALbasic don&#8217;t do what you want, you can subclass an existing control and write event handlers to change how it works, or you can even subclass the Canvas control and create your own custom control from the ground up.</li>
<li>REALbasic comes with classes for all sorts of things &#8212; standard dialog boxes (for picking folders, files, or colors), regular expressions, XML, databases, shell commands, graphics (lines, shapes, text), networking (including sending data to and receiving data from websites), conversion between text encodings, cooperative (not pre-emptive) threads, sound, editing video (via QuickTime), etc.</li>
<li>There is even a command to make the computer speak the text you pass to it. (This works on Windows and on the Mac, but not on Linux.)</li>
<li>RBscript is a scaled-down version of REALbasic that you can compile into your application so that users can run scripts. Even better, these scripts are compiled, not interpreted, before being run.</li>
<li>REALbasic (the Professional edition of it at any rate) is cross-platform &#8212; you can build applications for Windows, for Mac OS X, and for Linux from a single project file.</li>
<li>REALbasic code is compiled into native machine code, not interpreted.</li>
<li>You don&#8217;t have to pay any royalties for distributing your applications.</li>
<li>REALbasic Personal Edition for Linux is free. If you have a Linux machine, you can write and test REALbasic code on it and not pay a cent to REAL Software until you&#8217;re ready to build executables for Windows or the Mac.</li>
<li>If the code that REALbasic produces isn&#8217;t fast enough, you can write a plugin for REALbasic in C++ containing code to alter the contents of your REALbasic objects.</li>
<li>Many developers have developed their own plugins and controls and offer them either for sale or free of charge.</li>
</ul>
<h3>What&#8217;s bad about REALbasic?</h3>
<p>There are some drawbacks to REALbasic, though none of these are showstoppers for me personally.</p>
<ul>
<li>Every version of REALbasic other than the Personal Edition for Linux is commercial software. If you want to compile for any platform other than Linux, you have to <a href="http://www.realsoftware.com/store/">pay up</a>.</li>
<li>REALbasic&#8217;s IDE can be constraining at times. You can only see one function or one class method in the IDE at a time. People accustomed to seeing two or more function definitions at once in a text editor may chafe at this.</li>
<li>REALbasic&#8217;s controls reflect the lowest common denominator of the equivalent controls on the three platforms. For example, REALbasic offers a TextArea for letting the user edit styled text, but the TextArea does not have built-in undo; you have to supply that.</li>
<li>REALbasic&#8217;s executables are large. A do-nothing GUI application is around 1.5MB because REALbasic compiles a large part of the runtime framework into the application. Even a do-nothing console application is about a megabyte in size.</li>
<li>REALbasic does not support <a href="http://developer.apple.com/cocoa/">Cocoa</a> (although a new release that <em>does</em> support Cocoa is supposed to be released by the end of the year), so any Mac OS X application built with REALbasic right now relies on <a href="http://developer.apple.com/carbon/">Carbon</a>. (This is one reason why developers who develop only for the Mac often prefer <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html">Objective-C</a> and <a href="http://developer.apple.com/tools/xcode/">Xcode</a> over REALbasic.)</li>
<li>REALbasic currently produces only 32-bit code, not 64-bit code.</li>
<li>Documentation for the plugin API is spotty, and actually writing the plugin appears to be something of a dark art. (I might try gathering this information and writing up a how-to on building a plugin with gcc in a later post.)</li>
<li>REAL Software sometimes adds new features to REALbasic and releases them before they are ready. (The latest release, 2009R4, has new report-building functionality that is apparently <a href="http://forums.realsoftware.com/viewtopic.php?f=1&amp;t=30272">not yet ready</a> <a href="http://forums.realsoftware.com/viewtopic.php?f=1&amp;t=30282">for prime time</a>.)</li>
</ul>
<h3>Good enough</h3>
<p>Every programming language and package involves tradeoffs. REALbasic, like every other programming language, has its good points and its bad points. I like its good points and I can live with its bad points, so I&#8217;m going with REALbasic.</p>
<h3>Links</h3>
<ul>
<li>Markus Winter <a href="http://www.realsoftware.com/support/whyrealbasic.php">gives his take on REALbasic</a>.</li>
<li>Thomas Tempelmann warns those new to REALbasic about some <a href="http://www.tempel.org/REALbasicAnnoyances">annoyances</a> to watch for.</li>
<li>Many developers who develop only for the Mac <a href="http://www.cocoadev.com/index.pl?RealBasic">prefer Cocoa and XCode over REALbasic</a>.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://karig.net/2009/10/why-realbasic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
