473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

New OOP Framework using C

I have looked at many object-oriented programming frameworks out there
for C. Though the ideas presented are intriguing, and I've used some
of them in my own work, they all suffered some drawback or another.

I have created a new method of doing OOP with C, something that fits
my needs particularly well. I haven't encountered anything else like
it, so I'm tossing it out to the Internet for comment.

http://www.planet-source-code.com/vb...=6820&lngWId=3

Here's a bit more info about it, in case you're interested:

This framework allows for object oriented processing in C.
Instead of structs with hard-coded fields, every object is a
hash table that can contain an infinite number of name/value pairs,
called 'stats' from here on out. So - instead of this:

struct PERSON_T
{
char *name; // filled with 'Harry
int age; // filled with '17'
}

you'd have this instead:

HASHTABLE (list of name/value pairs)
StatName StatValue
-------------- --------------
name Harry
age 17

(The stat names are actually stored as enums, for maximum lookup
speed.)

Each type of object that can exist, such a 'square' or 'circle,'
must have an entry in the master table, that sets the 'default field
values' for that type of object. This entry is called a 'master item.'
Each master item in the master table is itself a hash table, so the
master table can be considered a 'table of tables.'

When accessing the fields of an object, use GetStat or SetStat.
They work in the following manner:

* GetStat - Retrieves the value from the object, if that
name/value pair exists.
If it doesn't, retrieves the value from the
master table for that object type.

* SetStat - Sets the value in the object only -
never modifies the master table!

This is an 'auto-optimizing' memory scheme. Only the differences
between the object, and the 'master object' for that type, are stored.
This allows you to create millions of items in memory, with thousands
of stats each, in a very small amount of space.
If the stat exists in the object, it is pulled from the object.
If it doesn't, it assumes 'the default value' from the master is
good enough, and uses that instead.

Constructing objects is easy - just call the Create routine with the
name of the master object, plus all the stats you want to override.

In addition to storing the default stats, the master table stores
function pointers to event handlers for that object.

Look at the following master item example:

CIRCLE
StatName StatValue
---------- --------------
Type 200
Name CIRCLE
Inherits SHAPE
Radius 1
OnDraw circle_draw
OnSetRadius circle_setradius

This master item describes a new object type called 'CIRCLE', that
has a type of 200, a name of CIRCLE, and a default radius of 1.
When events are sent to the object, it looks for the corresponding
'OnEvent' stat for the specified event, and calls the function
through that function pointer. For instance - when the 'draw'
command is sent to it, it pulls up the 'OnDraw' stat, and picks out
the function pointer. Then, it executes the 'circle_draw' routine
through that function pointer. When a 'setradius' command is sent
to it, it executes the 'circle_setradius' routine.

It also illustrates the 'inheritance' feature. Inheritance comes into
play only when loading the master file. When an 'Inherits' stat is
encountered, the master loading routine pulls up the specified master
item, and pulls all ITS stats out and adds it to the current master
item.
In this case, the master table entry for 'SHAPE' looks like this:

SHAPE
StatName StatValue
---------- --------------
Type 100
Name SHAPE
Inherits BASE
OnMove shape_move
OnSetName shape_setname

All stats except type and name will be added to the CIRCLE master
item.
Though not illustrated, you can inherit from multiple base types.
Simply specify multiple INHERITS stats when loading the master file.
The stats will be pulled in order, with newer entries overlaying older
entries, so be careful to specify the inheritance loading order
accordingly!

After all the inheritance is performed, the CIRCLE item actually looks
like this in memory:

CIRCLE
StatName StatValue
---------- --------------
Type 200
Name CIRCLE
Radius 1
OnDraw circle_draw
OnSetRadius circle_setradius
OnMove shape_move
OnSetName shape_setname

As you can see, 'draw' and 'setradius' are specific to the circle
master item. 'move' and 'setname' are inherited from the 'shape'
master item. (The 'inherits' stat is missing entirely, as that is
only used when loading the master table into memory.)

All method calls are done via 'Do'. Do takes the item,
the event, and any additional arguments. It looks up which stat is
associated with the event, and retrieves that function pointer.
If the function pointer exists, it executes it with the supplied
arguments. If it doesn't exist, it returns an ERR_UNHANDLED_EVENT
error.

All possible events must be listed in the 'events.txt' file, and
all possible stats must be listed in the 'stats.txt' file.
All the errors are listed in the 'errors.txt' file.
A stringizing macro STRINGIZE is used to turn these into the
appropriate enum and arrays in code.

That's about it. I've stripped this down from my own code to its
bare essentials. In my code, I load the master table from disk, and
so should you! This makes it easy to modify functionality of objects
without cutting any code. I also have specialized 'load from disk' and
'save to disk' handling for the objects, not implemented here.
As you might guess, it's only necessary to store the stats from the
object hash table, not the master item hash table.
The master table should only ever be modified 'by hand'.

I hope you find some of these concepts useful in your own work!
If you do any enhancements, drop me a line.

--Kamilche, ka******@mad.scientist.com
Nov 13 '05 #1
0 2796

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Cameron Laird | last post by:
QOTW: "... So I started profiling the code and the slowdown was actually taking place at places where I didn't expect it." -- Guyon Mor?e (and about twenty-three thousand others) " suggestion...
2
by: Tomas Vera | last post by:
Hello All, I'm running into a problem that I don't know how to solve (properly, at least). Our web servers are running Win2K and have Framework v1.0.3705 running on them. We have a DLL...
4
by: RM | last post by:
Had VS .Net 2002 installed on W2k Server SP3 and supported a number of web sites. Installed VS .Net 2003 on Friday and now all web sites using .Net & MS ACCESS get this strange error upon open. ...
6
by: Joseph Geretz | last post by:
I recently upgraded my server to Windows 2003. The first thing I noticed is that my sample WebService pages no longer worked. The Invoke test button is missing. This is addresed by the following KB...
9
by: Tim D | last post by:
Hi, I originally posted this as a reply to a rather old thread in dotnet.framework.general and didn't get any response. I thought it might be more relevant here; anyone got any ideas? My...
11
by: xenophon | last post by:
I have a web site with forms authentication and a single logon page. I have 4 subdirectories, each that should be protected by a different username/password combination. For testing purposes, the...
2
by: Mr Utkal Ranjan Pradhan | last post by:
Hi Friends I've developped a vb.net application. Now i want to deploy the application to an Windows 98/Me/2000/XP machine which donot have .NET Framework installed. Now I want to package my...
0
by: ikhan | last post by:
Hello, I am using an activex control on my website. It was running fine with framework 1.1, when I try to run it on framework 2.0 it's not showing up. It's not showing any error message, just a...
29
by: =?Utf-8?B?SGVybWF3aWg=?= | last post by:
Hello, Please anybody help me. I have only a little experience with web development. I created simple project using ASP NET 2.0 (VS 2005) It works fine on local computer. When I tried to run...
13
by: dancer | last post by:
I have made a new post because when I try to respond to another, I get the error, "Article Rejected -- Ill-formed message id" This is in response to the advice of Juan Libre to install Net...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.