There are people who think that writing codes without Cocoa is impossible, or at least it is meaningless. Although many articles on Objective-C mentions that it is a superset of the C, many people really don’t understand it.
Some even ask, “Is it possible to use functions from libraries written in C with Objective-C code?”.
Not so few people are curious about why Objective-C didn’t become popular, and some answer that it is because of NeXT’s falling down. Their reasoning is based on the NeXT’s failure in making their system popular. Is it really so?
I don’t think, so. Just like standard C libary or C++ library, anyone who are interested in making Objective-C standard library could build their own.
However, it didn’t take place. You can use standard C library in Objective-C code. So, probably the original writers of the Objective-C thought it would be OK not to have “standard” library for the Objective-C.
Anyway, truth is that you can write codes with the Objective-C and standard C library. You can write your own Objective-C class which is analogous to the NSObject. Or you can inherit from the Object. (Actually, the Object was the root class before when NSObject became so. )
The gcc provides some header files to help coding in Objective-C :
- encoding.h
- hash.h
- NXConstStr.h
- objc.h
- objc-api.h
- objc-list.h
- Object.h
- Protocol.h
- sarray.h
- thr.h
- typedstream.h
Among them, application programmers will be interested in Object.h, Protocol.h, thr.h, hash.h, and NXConstStr.h. The Object.h contains a class, Object, like the NSObject. Thus, it has messages like, init, initialize, free, alloc, and so on. thr.h is for threading.
So, with those header files, you can write codes in Objective-C.
The Objective-C is really a little addition to the C, while it has the genuine power of the Objected Oriented Programming.
Although it doesn’t support operator overloading, meta programming, it supports dynamism, message forwarding, remote messaging, flexible expansion of class.
However, as for reusability, I personally think that Objective-C is better than the C++.
Now, here is a simple Objective-C code without Cocoa or Foundation.
To compile it, issue :
gcc -g -O -c MyClass.m main.m
gcc -fgnu-runtime -fobjc-exceptions MyClass.o main.o -lobjc
File : MyClass.h
#import <objc/Object.h>
#import <objc/objc-api.h>
#import <objc/NXConstStr.h>
#import <objc/thr.h>
int gWait;
@interface MyClass : Object
{
objc_mutex_t pMutex;
NXConstantString *description;
int value;
}
- (void) setDescription:(NXConstantString *)theText;
- (void) description;
- (void) doSomethingAtomically;
- (void) doSomethingAtomically1;
- free;
@end
File : MyClass.m
#import "MyClass.h"
#import <stdio.h>
#include <windows.h>
extern int gWait;
@implementation MyClass
- (void)setDescription:(NXConstantString *)theText
{
description = [theText copy];
value = 0;
}
- (void)description
{
printf("%s", [description cString]);
}
- (void) doSomethingAtomically
{
int i;
printf("in Thread function\n" );
objc_mutex_lock( pMutex );
for( i = 0; i < 5; i++ )
{
value++;
printf("1st : Thread %d's value = %d\n", objc_thread_id(), value );
}
printf("\n");
objc_mutex_unlock( pMutex );
Sleep( 3 );
objc_mutex_lock( pMutex );
for( i = 0; i < 5; i++ )
{
value++;
printf("2nd : Thread %d's value = %d\n", objc_thread_id(), value );
}
printf("\n");
objc_mutex_unlock( pMutex );
Sleep( 3 );
objc_mutex_lock( pMutex );
for( i = 0; i < 5; i++ )
{
value++;
printf("3rd : Thread %d's value = %d\n", objc_thread_id(), value );
}
printf("\n");
objc_mutex_unlock( pMutex );
gWait++;
}
- (void) doSomethingAtomically1
{
int i;
printf("\t in Thread function 2\n" );
objc_mutex_lock( pMutex );
for( i = 0; i < 5; i++ )
{
value++;
printf("\t 2-1st : Thread %d's value = %d\n", objc_thread_id(), value );
}
printf("\n");
objc_mutex_unlock( pMutex );
Sleep( 3 );
objc_mutex_lock( pMutex );
for( i = 0; i < 5; i++ )
{
value++;
printf("\t 2-2nd : Thread %d's value = %d\n", objc_thread_id(), value );
}
printf("\n");
objc_mutex_unlock( pMutex );
Sleep( 3 );
objc_mutex_lock( pMutex );
for( i = 0; i < 5; i++ )
{
value++;
printf("\t 2-3rd : Thread %d's value = %d\n", objc_thread_id(), value );
}
printf("\n");
objc_mutex_unlock( pMutex );
gWait++;
}
- (id)init
{
if( [super init] != nil )
{
pMutex = objc_mutex_allocate();
description = nil;
}
gWait = 0;
return self;
}
- free
{
printf("being deallocated..\n");
objc_mutex_deallocate( pMutex );
if( description )
{
[description free];
}
}
@end
File : main.m
#import <stdio.h>
#import "MyClass.h"
#include <windows.h>
void isMultithreaded(void)
{
printf("The program is multithreaded\n");
}
int main( void )
{
MyClass *myObject = [[MyClass alloc] init];
[myObject setDescription:@"Hello, World\n"];
[myObject description];
objc_set_thread_callback( isMultithreaded);
objc_thread_detach( @selector(doSomethingAtomically), myObject, nil);
objc_thread_detach( @selector(doSomethingAtomically1), myObject, nil);
objc_thread_detach( @selector(doSomethingAtomically), myObject, nil);
objc_thread_yield();
while( gWait < 2)
{
//printf("main : Shall I sleep?\n");
Sleep(5);
}
[myObject free];
return 0;
}
NOTE : Above codes were written in MingW environment.
Recent Comments