bada lecture - Osp::Base fundamental classes (1/2)

Uploaded by badadevelopers on 09.02.2011

In this lecture, I will introduce the base name space of bada.
First, I will explain the Object class and the frequently used String and Buffer classes.
After that, I will explain the 2-Phase construction that the bada's classes follow and then I
will explain the DateTime class. The most fundamental class of the base name
space is the Object class. The Object class is the base class of all
bada classes, except those with only static methods.
The Object class provides the Equal and the GetHashCode functions.
The Equal function returns true if two objects are the same.
The GetHashCode function returns the address of the object in the current implementation.
In the Base namespace, the wrapper classes for C++ types are implemented.
The following integral types have been implemented: Boolean, Int8, Short, Character, Integer,
Long and LongLong. The Float and Double floating point types
have been implemented. Additionally, the String, Buffer and ByteBuffer
types have been implemented so that they can be used for arrays.
Also, the DateTime, TimeSpan and UuId types have been implemented.
Let's have a look at the String type in more detail.
Basically, in Bada, a String is a sequence of Unicode characters.
The content and the length of the sequence can be changed.
If a String is created without specifications, 16 bytes are allocated by default.
After that, if the String length is changed, as much as 1.5 times the actually required
memory is allocated. The maximum length of a String is 2^32 -1
bytes. The primitive functions provided for the String
class includes functions to clear the string and to identify whether the string is empty
such as the Clear and IsEmpty functions. In addition, the GetCapacity function can
be used to get the size of the allocated memory, and the EnsureCapacity and SetCapacity functions
can be used to set the capacity of a String. Also, the GetLength and SetLength functions
to acquire and set the length of the actual string are provided.
Now, to use a String, you have to first initialize the String.
You can initialize a String with an existing ascii string or Unicode string.
In addition, if you want to create a String with a capacity greater than 16 bytes, you
can create a String by specifying the capacity of the string.
Now, I will explain the String functions that can be used to acquire a character pointer
or character. As described previously, since a String is
basically a Unicode character sequence, you can handle a String as an mchar type that
is used to save Unicode characters. For example, I declared an mchar type variable
"ch" and created the String using a C string made with an ASCII string, "ascii string".
Here, I retrieve the 7th character using the [ ] operator, and character "s" is saved
in the variable ch. You can use the GetCharAt function to perform
the same functionality as the [] operator. The GetPointer function is used to acquire
the mchar pointer to the string.
To change a character in a String, you can use SetCharAt or the [ ] operator.
In addition, the Format function is provided that corresponds to the sprintf() function
in the conventional C language. The first parameter specifies the maximum
length of the String, the other parameters are the same as those of the sprintf function.
In addition, you can assign a String to another String.
The Operators += and + are provided to allow appending a String to another String.
In addition, an Append function is provided which can be used to append a String to another
String, or to convert an integer value or a floating point type into a String type and
append the converted String to a String. In this example, I have created a String containing
"10 + 10". I append the "=" string to the string using
the operator += and append an integer type value to the String to complete the numeric
expression. In addition, you can perform a binary plus
operation for a String. The Insert function can be used to insert
a String into the middle of a String. The usage of this function is the same as
that of the Append function. Functions to handle sub strings within a string
are also provided. First, you can use the Substring function
to acquire a sub string from a string. You can use the Replace function to replace
a sub string with another sub string. You can replace a sub string with another
sub string by specifying the sub string to be replaced as the first parameter and the
new string required as the second parameter. The Remove function enables removing a sub
string from a string.
Functions to compare the contents of strings are provided such as the CompareTo, Equals,
StartsWith and EndsWith functions. In addition, the IndexOf and LastIndexOf functions
can be used to identify if a string contains a specific sub string or to find the position
of a sub string in a string. The ToLower and ToUpper functions can be used
to change the characters of a string to upper or lower case characters.
The Reverse function can be used to reverse the order of the characters of a string.
This example illustrates how to modify the contents of a string by using various string
functions. First, I created a string "a string" and changed
the first character of the string to the capital character "A".
Printing the result will display "A string" on the screen.
After that, I appended a sub string "is cool!" to the string using the Append function.
As a result, the string has been changed to "A string is cool!".
After that, I inserted a sub string "class" into the 9th character position of the string.
The sub string "class" is inserted between "string" and "is".
As a result, the string has been changed to "A string class is cool!".
Then I called a function to remove 2 characters from the 1st character position.
It removes the "A" and the blank space. After that, I inserted "The" into the 1st
character position using the Insert function.
After that, I have created another new string. I initialized this string using the Format
function. The initialized string is "5 + 2 is".
Then, I appended an integer type constant "3" to the string.
Then I called the IndexOf function to acquire the index of the "+" character in the string
and changed the character to "-". Then I found the "is" sub string and replaced
it with "=". As a result, the str2 string becomes "5 -
2 = 3". The last statement appends str2 to the str
by using the operator +=.