Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #403535 > unrolled thread

OOP in C...

Started by"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
First post2026-10-25 13:58 -0700
Last post2026-10-29 13:58 -0500
Articles 5 — 4 participants

Back to article view | Back to comp.lang.c


Contents

  OOP in C... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-10-25 13:58 -0700
    Re: OOP in C... steve g <Sgonedes1977@gmail.com> - 2026-10-28 01:21 -0400
      Re: OOP in C... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-10-28 14:47 -0700
        Re: OOP in C... David Brown <david.brown@hesbynett.no> - 2026-10-29 08:56 +0100
    Re: OOP in C... Lynn McGuire <lynnmcguire5@gmail.com> - 2026-10-29 13:58 -0500

#403535 — OOP in C...

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2026-10-25 13:58 -0700
SubjectOOP in C...
Message-ID<11blqhs$3vf3k$1@dont-email.me>
Fwiw, google groups seems to be working again! Found an older OOP thing 
I made:

https://pastebin.com/raw/f52a443b1
(no ads, no shit...)


/* Interfaces
____________________________________________________________________*/
#include <stddef.h>


struct object_prv_vtable {
   int (*fp_destroy) (void* const);
};


struct device_prv_vtable {
   int (*fp_read) (void* const, void*, size_t);
   int (*fp_write) (void* const, void const*, size_t);
};


struct device_vtable {
   struct object_prv_vtable const object;
   struct device_prv_vtable const device;
};


struct device {
   struct device_vtable const* vtable;
};


#define object_destroy(mp_self) ( \
   (mp_self)->vtable->object.fp_destroy((mp_self)) \
)


#define device_read(mp_self, mp_buf, mp_size) ( \
   (mp_self)->vtable->device.fp_read((mp_self), (mp_buf), (mp_size)) \
)


#define device_write(mp_self, mp_buf, mp_size) ( \
   (mp_self)->vtable->device.fp_write((mp_self), (mp_buf), (mp_size)) \
)






/* Sample Header (usb_drive.h)
____________________________________________________________________*/
#if ! defined(USB_HEADER_H)
#define USB_HEADER_H


extern int usb_drive_create(struct device** const);


#endif







/* Sample Impl (usb_drive.c)
____________________________________________________________________*/
/* #include "usb_drive.c" */
#include <stdio.h>
#include <stdlib.h>


struct usb_drive {
   struct device device;
   /* whatever */
};


static int usb_drive_object_destroy(void* const);
static int usb_drive_device_read(void* const, void*, size_t);
static int usb_drive_device_write(void* const, void const*, size_t);


static struct device_vtable const g_table = {
   { /* object */
     usb_drive_object_destroy
   },

   { /* device */
     usb_drive_device_read,
     usb_drive_device_write
   }
};


int usb_drive_create(
  struct device** const pself
) {
   struct usb_drive* const self = malloc(sizeof(*self));
   if (self) {
     self->device.vtable = &g_table;
     *pself = &self->device;
     return 0;
   }
   return -1;
}


int usb_drive_object_destroy(
  void* const self_
) {
   struct usb_drive* const self = self_;
   printf("usb_drive_object_destroy(%p)\n", (void*)self);
   free(self_);
   return 0;
}


int usb_drive_device_read(
  void* const self_,
  void* buf,
  size_t size
) {
   struct usb_drive* const self = self_;
   printf("usb_drive_device_read(%p, %p, %lu)\n",
     (void*)self, buf, (unsigned long)size);
   return 0;
}


int usb_drive_device_write(
  void* const self_,
  void const* buf,
  size_t size
) {
   struct usb_drive* const self = self_;
   printf("usb_drive_device_write(%p, %p, %lu)\n",
     (void*)self, buf, (unsigned long)size);
   return 0;
}







/* Sample Application
____________________________________________________________________*/
void read_write(
  struct device* const self
) {
   char buf[100];

   device_read(self, buf, 50);

   device_write(self, buf, 5);
}


int main(void) {
   struct device* a_device;

   if (! usb_drive_create(&a_device)) {
     read_write(a_device);

     object_destroy(a_device);
   }

   return 0;
}

[toc] | [next] | [standalone]


#403680

Fromsteve g <Sgonedes1977@gmail.com>
Date2026-10-28 01:21 -0400
Message-ID<875wympfi7.fsf@gmail.com>
In reply to#403535
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes:

> Fwiw, google groups seems to be working again! Found an older OOP thing I
> made:

Oh good. For OOP gcc supports empty structures...?

> https://pastebin.com/raw/f52a443b1
> (no ads, no shit...)


thanx

[toc] | [prev] | [next] | [standalone]


#403746

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2026-10-28 14:47 -0700
Message-ID<11btqh3$2qqmv$1@dont-email.me>
In reply to#403680
On 10/27/2026 10:21 PM, steve g wrote:
> "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes:
> 
>> Fwiw, google groups seems to be working again! Found an older OOP thing I
>> made:
> 
> Oh good. For OOP gcc supports empty structures...?

Iirc, it sure does. But its non std.



>> https://pastebin.com/raw/f52a443b1
>> (no ads, no shit...)

It was an old attempt Steve. Take close note: I used macros for the 
"pseudo" duck typing, but they have a double eval. That can be cleaned 
up for sure:
______________
#define object_destroy(mp_self) ( \
   (mp_self)->vtable->object.fp_destroy((mp_self)) \
)


#define device_read(mp_self, mp_buf, mp_size) ( \
   (mp_self)->vtable->device.fp_read((mp_self), (mp_buf), (mp_size)) \
)


#define device_write(mp_self, mp_buf, mp_size) ( \
   (mp_self)->vtable->device.fp_write((mp_self), (mp_buf), (mp_size)) \
)
______________


using them:
_______________
/* Sample Application
____________________________________________________________________*/
void read_write(
  struct device* const self
) {
   char buf[100];

   device_read(self, buf, 50);

   device_write(self, buf, 5);
}


int main(void) {
   struct device* a_device;

   if (! usb_drive_create(&a_device)) {
     read_write(a_device);

     object_destroy(a_device);
   }

   return 0;
}
_______________


> thanx

No problem. Just one way to do it.

[toc] | [prev] | [next] | [standalone]


#403772

FromDavid Brown <david.brown@hesbynett.no>
Date2026-10-29 08:56 +0100
Message-ID<11buu7e$34ll3$1@dont-email.me>
In reply to#403746
On 28/10/2026 22:47, Chris M. Thomasson wrote:
> On 10/27/2026 10:21 PM, steve g wrote:
>> "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes:
>>
>>> Fwiw, google groups seems to be working again! Found an older OOP 
>>> thing I
>>> made:
>>
>> Oh good. For OOP gcc supports empty structures...?
> 
> Iirc, it sure does. But its non std.
> 
And remember of course that an empty struct has size 1, not size 0. 
(This applies to C++ as well - though it can be hidden by empty base 
class optimisation or use of [[no_unique_address]].)

[toc] | [prev] | [next] | [standalone]


#403794

FromLynn McGuire <lynnmcguire5@gmail.com>
Date2026-10-29 13:58 -0500
Message-ID<11c050b$3kejn$1@dont-email.me>
In reply to#403535
On 10/25/2026 3:58 PM, Chris M. Thomasson wrote:
> Fwiw, google groups seems to be working again! Found an older OOP thing 

news.blueworldhosting.com has usenet back to 1992 for free and for 
viewing only, no posting or replying to messages on the server.

comp.lang.c has 599,802 headers in it.  Not all of the headers are good 
though, many are corrupt.

All of the newsgroups have been run through SpamAssassin to cut the 
noise down.

Lynn

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web