{{fbdoc item="title" value="OOP In Non-OOP Languages"}}----

Contrary to popular belief object oriented programming does not require an OO language.

What you get with an OO language is a set of built in constructs that assist you in writing OO programs but in many cases they are unnecessary and sometimes they are counterproductive.

Anyway, this isn't a rant against OO languages but rather a rant against the unquestioning acceptance of the idea that a specifically OO language is necessary to write object oriented programs.

In order to demonstrate that it is not necessary to have an OO language this example presents a technique that is usually presented as an example of class based programming; and so it is but you won't find the word class in this example.

If you have to concatenate a lot of strings in most Basics you usually find that it is a time consuming process.  Actually ""FreeBASIC"" string operations are remarkably quick but you can still do better using a string builder.

A string builder is simply a class that maintains a string buffer in such a way as to avoid repeated calls to the memory allocation function because this is a relatively expensive operation.  The methods of the class provide ways of manipulating the buffer and converting between it and the native string type.

The trick that makes it faster than the built type for large strings and large numbers of appends is that the string is held in a heap allocated buffer that is always larger than the actual length of the string.  This means that appending to the end of the string usually simply means copying the contents of the new string to the memory location following the last character of the current string.  In this implementation the buffer is a ZString so it is easy to convert it to an ordinary dynamic string.

The ""FreeBASIC"" module encapsulates a type definition for a struct.  Instances of this struct hold the attributes of the object.  The methods are simply normal ""FreeBASIC"" public functions and subs defined in the same module.  When you want to call a method you use the normal ""FreeBASIC"" syntax:
%%(freebasic) s = StringB_ToString(AStringBInstance)%%

By convention all methods names begin with the name of the class and an underscore and the first argument is always the instance of the type.  This argument should always be passed by reference to ensure that changes to the state are permanent and also to avoid unnecessary, time-consuming, copying.

To add a new method you simply add a new function or sub following those rules.

You can easily implement composition of objects but inheritance in the usually expected ways can't be done.  You can extend classes simply by defining new functions elsewhere that take arguments of the class type.   If the original class defines all of its methods as overloaded you can even create new methods of the same name so long as they have different signatures.


Here is the example code:
%%(freebasic)
'-----------------------------------------------------------------------------
' Classes without built in oop.

' Define a struct for the properties and a sub or function for each
' method.  Pass the struct as the first argument in all calls.

' By convention the argument will be Me as in VB Classic

' Strings in FB are so fast that a string builder class is 
' not needed most of the time but if you are concatenating 
' thousands of strings to build web pages for instance this might be useful.

' And please don't start complaining about the lack of inheritance; that
' is not a requirement for the use of objects.  There is no legal definition of 
' Object Oriented Programming but the most important part of any definition 
' is the close association between the data and the code that manipulates it.

'You can easily extend this class to provide more methods.
'-----------------------------------------------------------------------------


type StringB
  len as integer ' used length
  allocated as integer
  s as zstring ptr   ' buffer of at least len characters
end type


'-----------------------------------------------------------------------------
' Create a new StringB by calling one of these constructors.
'-----------------------------------------------------------------------------
public function StringB_New overload (byval InitialSize as integer) as StringB
  dim sb as StringB
  sb.allocated = InitialSize
  sb.s = allocate(InitialSize)
  *sb.s = ""
  StringB_New = sb
end function


public function StringB_New(byref InitialValue as string) as StringB
  dim sb as StringB
  sb = StringB_New(len(InitialValue))
  *sb.s = InitialValue
  sb.len = len(InitialValue)
  StringB_New = sb
end function

public sub StringB_Dispose(byref Me as StringB)
  deallocate Me.s
end sub

  
public function StringB_ToString(byref Me as StringB) as string 
  StringB_ToString = *Me.s
end function


sub StringB_Append overload(byref Me as StringB, byref s as string)

  dim i as integer = Me.len
  Me.len += len(s)
  if Me.len >= Me.allocated then
	Me.allocated = 2*Me.len
	dim as zstring ptr p = reallocate(Me.s, Me.allocated )
	if p=0 then
	  ' failed to reallocate
	  print "StringB_Append failed to reallocate", Me.allocated
	  return 
	end if
	Me.s = p
  end if
  *(Me.s + i) = s
  
end sub


sub StringB_Append(byref Me as StringB, byref other as StringB)
  StringB_Append Me, StringB_ToString(other)
end sub
%%