473,766 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Very Simple, Minimalist Technique For OOP in C...

Here is the example code:

- http://appcore.home.comcast.net/vzoo.../interface.zip
which is an analog of the following technique:

- http://groups.google.com/group/comp....f857051ca4029b
It's definitely not full-blown OOP is any sense of the term, however imho, I
think it could be fairly useful in certain scenarios... Now, let me try to
briefly describe it:

The provided example shows how a minimalist abstract interface for a "shape"
object might look. It also includes an implementation of simple circle and
square objects that are compatible with the abstract shape interface. If you
want to try and add another shape to get a feel for how "cumbersome " the
interface method is, well, here is a quick example of that:
<pseudo-code for adding, lets say, a triangle>
_______________ ___________

triangle.h
_______________ ___
/* Put include guard here */

#include "ishape.h"

/* Return values that are non-zero indicate error's */

/* Create a new triangle */
extern int Triangle_Create (IShape_t* const, /* [triangle params] */);

triangle.c
_______________ ___
#include "triangle.h "
typedef struct Triangle_s Triangle_t;

struct Triangle_s {
/* [triangle members] */
};
static int Triangle_IObjec t_Destroy(void* const);
static int Triangle_IObjec t_Status(void* const);
static int Triangle_IShape _Draw(void* const);
/* Triangle IShape Interface VTable */
static IShape_VTable_t const Triangle_IShape _VTable = {
Triangle_IObjec t_Destroy,
Triangle_IObjec t_Status,
Triangle_IShape _Draw
};
/* Triangle Interface Functions */
int
Triangle_Create (
IShape_t* const IThis,
/* [triangle params] */) {
Triangle_t* const This = malloc(sizeof(* This));
if (This) {
/* [init This triangle] */
IOBJECT_INIT(IT his, This, &Triangle_IShap e_VTable);
return 0;
}
return -1;
}
/* Triangle IObject Interface Functions */
int
Triangle_IObjec t_Destroy(void* const ThisState) {
Triangle_t* const This = ThisState;
free(This);
return 0;
}
int
Triangle_IObjec t_Status(void* const ThisState) {
Triangle_t* const This = ThisState;
/* [determine This triangle state; this is optional] */
return 0;
}
/* Triangle IShape Interface Functions */
int
Triangle_IShape _Draw(void* const ThisState) {
Triangle_t* const This = ThisState;
/* [draw This triangle] */
return 0;
}
_______________ ___________


IMHO, that should be "fairly" straight forward... Now, here is how you could
use your Triangle:
_______________ ___________

#include "triangle.h "

int Example(void) {
IShape_t MyShape;

int rStatus = Triangle_Create (&MyShape, /* [triangle params] */);
if (! rStatus) {

rStatus = IShape_Draw(&My Shape);
if (rStatus) {

rStatus = IObject_Status( &MyShape);
}

rStatus = IObject_Destroy (&MyShape);
}

return rStatus;
}
_______________ ___________
I was wondering if you have come across any superior methods for very basic
OOP in C?
Any thoughts? Is this method total crap?

;^)
Thanks.

Jun 21 '07 #1
10 2247
"Chris Thomasson" <cr*****@comcas t.netwrote in message
news:h_******** *************** *******@comcast .com...
[...]
It's definitely not full-blown OOP is any sense of the term, however imho,
I think it could be fairly useful in certain scenarios... Now, let me try
to briefly describe it:
[...]

Actually, I think the example code should be fairly self-explanatory...
Jun 21 '07 #2
"Chris Thomasson" <cr*****@comcas t.netwrote in message
news:h_******** *************** *******@comcast .com...
Here is the example code:

- http://appcore.home.comcast.net/vzoo.../interface.zip
which is an analog of the following technique:

- http://groups.google.com/group/comp....f857051ca4029b
It's definitely not full-blown OOP is any sense of the term, however imho,
I think it could be fairly useful in certain scenarios...
[...]

I just remembered that a library called Nobel uses same basic technique:
You can create an abstract interface in C by declaring a vtable structure
and the interface structure self. First we setup a type for the VTable's
first parameter (e.g., 'this' in c++):
_______________
typedef void* IObject_This_t;
typedef IObject_This_t const VTable_This_t;
_______________


Now, lets say that our new interface will be for a thread/process
mutual-exclusion synchronization object. So, we need to declare the
following structs:
_______________
typedef struct IMutex_s IMutex_t;
typedef struct IMutex_VTable_s IMutex_VTable_t ;
_______________

And then we define the struct for our mutex interface:
_______________
struct IMutex_s {
IObject_This_t This;
IMutex_VTable_t const *VTable;
};

/*Take note of the names of the members of IMutex_t: (This, VTable). Its
import that you keep the names you choose consistent because they are going
to be depended upon. More on that later.
*/
_______________


An abstract mutex interface needs at least the following abstract functions
(Destroy, Lock, Unlock), so we define the following vtable struct:
_______________
struct IMutex_VTable_s {
int (*fp_IObject_De stroy) (VTable_This_t) ;
int (*fp_Lock) (VTable_This_t) ;
int (*fp_Unlock) (VTable_This_t) ;

/*Take note of the names of the members of IMutex_VTable_t :
(fp_IObject_Des troy, fp_Lock, fp_Unlock). Its import that you keep the names
you choose consistent because they are going to be depended upon. More on
that later.
*/
};
_______________


Now, we need to define the abstract interface that makes use of all the
above. We start be defining a helper function and an abstract function that
can init/destroy any object:
_______________
#define IObject_Initial ize(__IThis, __ThisPtr, __VTablePtr) \
(__IThis)->This = (__ThisPtr); \
(__IThis)->VTable = (__VTablePtr)

#define IObject_Destroy (__IThis) \
(__IThis)->VTable->fp_IObject_Des troy((__IThis)->This)
_______________

Now we define the abstract functions of IMutex_t:
_______________
#define IMutex_Lock(__I This) \
(__IThis)->VTable->fp_Lock((__ITh is)->This)

#define IMutex_Unlock(_ _IThis) \
(__IThis)->VTable->fp_Unlock((__I This)->This)
_______________


Stick all the above in a header file called (imutex.h) or whatever and
that's it for the abstract interface part. Now you create an object that
makes use of the interface:
your_mutex.h
_______________
/* include guard */
#include "imutex.h"
extern int Your_Mutex_Crea te(IMutex_t* const);

your_mutex.c
_______________
#include "your_mutex .h"
#include <stdlib.h/* malloc/free */
/* declare/define your mutexs impl struct */
typedef struct Your_Mutex_s Your_Mutex_t;
typedef Your_Mutex_t* const Your_Mutex_This _t;

struct Your_Mutex_s {
/* [impl specific mutex data] */
};
/* declare your mutexs interface functions */
static int Your_Mutex_IObj ect_Destroy(VTa ble_This_t);
static int Your_Mutex_Lock (VTable_This_t) ;
static int Your_Mutex_Unlo ck(VTable_This_ t);
/* define your mutex interface vtable */
static IMutex_VTable_t const Your_Mutex_VTab le = {
Your_Mutex_IObj ect_Destroy,
Your_Mutex_Lock ,
Your_Mutex_Unlo ck
};
/* define mutex create function */
int Your_Mutex_Crea te(IMutex_t* const IThis) {
Your_Mutex_This _t This = malloc(sizeof(* This));
if (This) {
/* [init impl This specific mutex data] */

/* init the IThis interface */
IObject_Initial ize(IThis, This, &Your_Mutex_VTa ble);
return 0;
}
return -1;
}
/* define mutex object destroy */
int Your_Mutex_IObj ect_Destroy(VTa ble_This_t ThisState) {
Your_Mutex_This _t This = ThisState;
/* [teardown impl This specific mutex data] */
free(This);
return -1;
}
/* define mutex interface */
int Your_Mutex_Lock (VTable_This_t ThisState) {
Your_Mutex_This _t This = ThisState;
/* [lock impl This specific mutex] */
return -1;
}
int Your_Mutex_Unlo ck(VTable_This_ t ThisState) {
Your_Mutex_This _t This = ThisState;
/* [unlock impl This specific mutex] */
return -1;
}
_______________
That's it for the interface and impl... Now you can use it like:
_______________
#include "your_mutex .h"
int Locked_Callback (
IMutex_t* const IThis,
int (*fp_Callback) (void*, IMutex* const),
void *CallbackState) {

int rStatus = IMutex_Lock(ITh is);
if (! rStatus) {

rStatus = fp_Callback(Cal lbackState, IThis);
if (! rStatus) {
rStatus = IMutex_Unlock(I This);
}
}

return rStatus;
}

static IMutex_t The_Lock;
int Critical_Sectio n(void *ThisState, IMutex_t* const Lock) {
/* [your in a critical-section locked by 'Lock'] */
return 0;
}
void Some_Threads(/* [...] */) {
/* [...] */
Locked_Callback (&The_Lock, Critical_Sectio n, /* [ptr to state */);
}
int main(void) {
int rStatus = Your_Mutex_Crea te(&The_Lock);
if (! rStatus) {
/* [create Some_Threads] */
}
return rStatus ;
}
_______________

Well, barring any typos, that's about it. ;^)
Jun 21 '07 #3
int main(void) {
int rStatus = Your_Mutex_Crea te(&The_Lock);
if (! rStatus) {
/* [create Some_Threads] */
/* [join threads] */

rStatus = IObject_Destroy (&The_Lock);
}
return rStatus ;
}
_______________

Jun 21 '07 #4
On Jun 21, 5:30 am, "Chris Thomasson" <cris...@comcas t.netwrote:
Here is the example code:

-http://appcore.home.co mcast.net/vzoom/example/interface.zip

which is an analog of the following technique:

-http://groups.google.c om/group/comp.lang.c/msg/6cf857051ca4029 b

It's definitely not full-blown OOP is any sense of the term, however imho, I
think it could be fairly useful in certain scenarios... Now, let me try to
briefly describe it:

The provided example shows how a minimalist abstract interface for a "shape"
object might look. It also includes an implementation of simple circle and
square objects that are compatible with the abstract shape interface. If you
want to try and add another shape to get a feel for how "cumbersome " the
interface method is, well, here is a quick example of that:

<pseudo-code for adding, lets say, a triangle>
_______________ ___________

triangle.h
_______________ ___
/* Put include guard here */

#include "ishape.h"

/* Return values that are non-zero indicate error's */

/* Create a new triangle */
extern int Triangle_Create (IShape_t* const, /* [triangle params] */);

triangle.c
_______________ ___
#include "triangle.h "

typedef struct Triangle_s Triangle_t;

struct Triangle_s {
/* [triangle members] */

};

static int Triangle_IObjec t_Destroy(void* const);
static int Triangle_IObjec t_Status(void* const);
static int Triangle_IShape _Draw(void* const);

/* Triangle IShape Interface VTable */
static IShape_VTable_t const Triangle_IShape _VTable = {
Triangle_IObjec t_Destroy,
Triangle_IObjec t_Status,
Triangle_IShape _Draw

};

/* Triangle Interface Functions */
int
Triangle_Create (
IShape_t* const IThis,
/* [triangle params] */) {
Triangle_t* const This = malloc(sizeof(* This));
if (This) {
/* [init This triangle] */
IOBJECT_INIT(IT his, This, &Triangle_IShap e_VTable);
return 0;
}
return -1;

}

/* Triangle IObject Interface Functions */
int
Triangle_IObjec t_Destroy(void* const ThisState) {
Triangle_t* const This = ThisState;
free(This);
return 0;

}

int
Triangle_IObjec t_Status(void* const ThisState) {
Triangle_t* const This = ThisState;
/* [determine This triangle state; this is optional] */
return 0;

}

/* Triangle IShape Interface Functions */
int
Triangle_IShape _Draw(void* const ThisState) {
Triangle_t* const This = ThisState;
/* [draw This triangle] */
return 0;}

_______________ ___________

IMHO, that should be "fairly" straight forward... Now, here is how you could
use your Triangle:
_______________ ___________

#include "triangle.h "

int Example(void) {
IShape_t MyShape;

int rStatus = Triangle_Create (&MyShape, /* [triangle params] */);
if (! rStatus) {

rStatus = IShape_Draw(&My Shape);
if (rStatus) {

rStatus = IObject_Status( &MyShape);
}

rStatus = IObject_Destroy (&MyShape);
}

return rStatus;}

_______________ ___________

I was wondering if you have come across any superior methods for very basic
OOP in C?

Any thoughts? Is this method total crap?

;^)

Thanks.
The following article also covers OO programming in C:
http://www.eventhelix.com/RealtimeMa...mming_in_c.htm

--
EventStudio 4.0 - http://www.EventHelix.com/EventStudio
Sequence Diagram based System Modeling Tool

Jun 22 '07 #5

"Chris Thomasson" <cr*****@comcas t.netwrote in message
news:h_******** *************** *******@comcast .com...
I was wondering if you have come across any superior methods for very
basic OOP in C?
typedef struct object
{
void *ptr;
void *(*query)(struc t object *obj, char *interface);
void (*kill)(struct object *obj)
} OBJECT;

Every object you query for an interface. An example would be

typedef struct
{
double (*length)(OBJEC T *obj);
int (*getpos)(OBJEC T *obj, double t, double *x, double *y);
} LINE;

So we are passed a line

void drawline(OBJECT *obj)
{
LINE *line;
int len;
int i;

line = obj->query(obj, "line");
if(!line)
fprintf(stderr, "Must be passed a line\n"):
len = (int) line->length(obj) + 1;
for(i=0;i<len;i ++)
{
line->getpos(obj, ((double)i)/len, &x, &y);
drawpixel(x, y);
}
}

However it really is a lot of trouble. The syntactical support isn't really
there, so code quickly becomes a complete mess.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 22 '07 #6
On Thu, 21 Jun 2007 19:52:19 -0700, "EventHelix.com "
<ev********@gma il.comwrote:
snip
>
The following article also covers OO programming in C:
http://www.eventhelix.com/RealtimeMa...mming_in_c.htm
Did you really need to quote 130+ lines just to provide a reference.
Remove del for email
Jun 22 '07 #7
Barry Schwarz <sc******@doezl .netwrote:
On Thu, 21 Jun 2007 19:52:19 -0700, "EventSpam. com"
<ev*******@gmai l.comwrote:
The following article also covers OO programming in C:
http://www.eventspam.com/spam.htm

Did you really need to quote 130+ lines just to provide a reference.
No; but did you really need to quote a known spammer without munging his
links?

Richard
Jun 22 '07 #8
On Fri, 22 Jun 2007 10:39:10 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote:

>
No; but did you really need to quote a known spammer without munging his
links?
He's not one of the spammers I recognized and the link actually
contained reasonable C code.
Remove del for email
Jun 22 '07 #9
"Malcolm McLean" <re*******@btin ternet.comwrote in message
news:kZ******** *************@b t.com...
>
"Chris Thomasson" <cr*****@comcas t.netwrote in message
news:h_******** *************** *******@comcast .com...
>I was wondering if you have come across any superior methods for very
basic OOP in C?
[...]
However it really is a lot of trouble. The syntactical support isn't
really there, so code quickly becomes a complete mess.
Yeah, that seems to attempt to support more OOP techniques than the
"minimalist " method posted, which only provides an abstract interface
technique.

Jun 24 '07 #10

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

Similar topics

42
9744
by: Steven O. | last post by:
I am seeking some kind of tool that I can use for GUI prototyping. I know how to use Visual Basic, but since a lot of software is being coded in Java or C++, I'd like to learn a Java or C++ -based tool. Back when I took my Java and C++ classes (two or three years ago), the available tools -- at least the ones I could find -- were still not as easy, not as "drag-and-drop", as Visual Basic. Has that changed? Is there some software out...
8
6088
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that uses message queues to pass pointers to Events between threads. In my app there are simple events that can be defined using an enum (for example an event called NETWORK_TIMEOUT) and more complex events that contain data (for example an event called...
11
17571
by: DJJ | last post by:
I am using the MySQL ODBC 3.51 driver to link three relatively small MySQL tables to a Microsoft Access 2003 database. I am finding that the data from the MySQL tables takes a hell of a long time to load making any kind linkage with my Access data virtually useless. I have the MySQL driver setup in as a USER DSN. The MySQL data is sitting out on a server and the Access database is running locally. The network connection is very...
3
16775
by: Douwe | last post by:
I try to build my own version of printf which just passes all arguments to the original printf. As long as I keep it with the single argument version everything is fine. But their is also a version which uses the "..." as the last parameter how can I pass them to the orignal printf ? void myprintf(char *txt, ...) printf(txt, ???????); }
12
20703
by: John | last post by:
I can't get my head around this! I have the following code: <% .... Code for connection to the database ... .... Code for retrieving recordset ... If Not rs.EOF Then ... Do something...
14
5616
by: rtk | last post by:
I'm looking for information on building a tiny/small/minimalist/ vanilla python interpreter. One that implements the core language and a few of the key modules but isn't tied to any specific operating system. I guess I'm asking for the smallest subset of the standard Python source code files that is necessary to get a working interpreter using a plain C compiler. Is this even possible? If so, has someone done it already? I've
112
4755
by: Prisoner at War | last post by:
Friends, your opinions and advice, please: I have a very simple JavaScript image-swap which works on my end but when uploaded to my host at http://buildit.sitesell.com/sunnyside.html does not work. To rule out all possible factors, I made up a dummy page for an index.html to upload, along the lines of <html><head><title></title></ head><body></body></html>.; the image-swap itself is your basic <img src="blah.png"...
126
4413
by: jacob navia | last post by:
Buffer overflows are a fact of life, and, more specifically, a fact of C. All is not lost however. In the book "Value Range Analysis of C programs" Axel Simon tries to establish a theoretical framework for analyzing C programs. In contrast to other books where the actual technical difficulties are "abstracted away", this books tries to analyze real C programs taking into account pointers, stack frames, etc.
17
5817
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /* Simple Thread Object ______________________________________________________________*/ #include <pthread.h> extern "C" void* thread_entry(void*);
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9837
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.