Hello all,
I am creating a linked list implementation which will be used in a
number of contexts. As a result, I am defining its value node as type
(void *). I hope to pass something in to its "constructo r" so that I
will be able to manipulate my list without the need for constant
casting; some sort of runtime type-safety mechanism.
For example, I want a linked lists of ints. I want to be able to say:
Newguy = ll_new(int); //so that in the future i can say:
Newguy.next.val ++;
Or alternatively a list of bools:
Newerguy = ll_new([type]bool) //so that in the future i can say:
Newerguy.next.v al = false;
Anyhow, I'm looking for ways to enforce runtime type-safety. Any
thoughts are appreciated, especially since I know for a fact this can
be done (and without unions...).
Dave 2 2434
On Sun, 28 Sep 2003, Dave wrote: I am creating a linked list implementation which will be used in a number of contexts. As a result, I am defining its value node as type (void *). I hope to pass something in to its "constructo r" so that I will be able to manipulate my list without the need for constant casting; some sort of runtime type-safety mechanism.
Can't be done easily. If you have a variable of type (void *), then
you're going to have to cast it to something else in order to dereference
it. That's just the way C works. But read on.
For example, I want a linked lists of ints. I want to be able to say:
Newguy = ll_new(int); //so that in the future i can say: Newguy.next.val ++;
Well, here are two things I've thought of. You can go the whole hog
and create a big dispatch table inside your linked list class, so
that one can write
struct llnode {
void *val; /* data stored through here */
};
struct my_crazy_llist {
struct llnode *head;
struct llnode *tail;
struct llnode *current; /* for iteration */
[...dispatch stuff...]
};
int ll_add_dispatch (struct llist *who, const char *method,
void *(*how)(void *, va_list));
void *ll_dispatch(st ruct llist *who, struct llnode *where,
const char *method, ...);
void *int_inc(void *val, va_list ap)
{
++ *(int *)val;
}
void *int_asgn(void *val, va_list ap)
{
*(int *)val = va_arg(ap, int);
return 0;
}
[...]
my_crazy_llist *Newguy = ll_new();
ll_add_dispatch (Newguy, "++", int_inc);
ll_add_dispatch (Newguy, "=", int_asgn);
[...]
ll_dispatch(New guy, Newguy->current->val, "++");
ll_dispatch(New guy, Newguy->current->val, "=", 42);
Now, whether you want to go this route is completely up to you
and your asylum warden. A simpler route might be to create
just a few generic routines via #define, and use those for the
commonplace things -- write your own for the complicated things.
E.g.,
#define ASGN(var, type, val) ((*(type *)(var)) = (val))
ASGN(Newguy->current->val, int, 42);
Anyhow, I'm looking for ways to enforce runtime type-safety. Any thoughts are appreciated, especially since I know for a fact this can be done (and without unions...).
And how, exactly, *do* you know this can be done? Have you seen
it done before? (Why not just copy that implementation, then?)
My recommendation: Use C++ and templates for this stuff. Don't
mess around with polymorphism in C, because the language simply
isn't designed for that. (Not that hacking around like this isn't
fun; it's just not very productive in the long run.)
-Arthur da**********@ya hoo.com (Dave) wrote in message news:<3e******* *************** ****@posting.go ogle.com>... Hello all,
I am creating a linked list implementation which will be used in a number of contexts. As a result, I am defining its value node as type (void *). I hope to pass something in to its "constructo r" so that I will be able to manipulate my list without the need for constant casting; some sort of runtime type-safety mechanism.
For example, I want a linked lists of ints. I want to be able to say:
Newguy = ll_new(int); //so that in the future i can say: Newguy.next.val ++;
Or alternatively a list of bools:
Newerguy = ll_new([type]bool) //so that in the future i can say: Newerguy.next.v al = false;
You should, if you really need this, listen to Authers advice
and use C++ with templates (in fact, dont "create" anything, just
use the stl to store whatever you want to).
if you are doing it purely for the fun aspect, then a way around
the "dont know what to cast this to" in the code (like the
Newguy = ll_new(int)
above) is to use macros combined with a function that implements
a lookup table based on the type.
----hw.c----
#include <stdio.h>
#include <stdlib.h>
/* the following two arrays must be
kept in synch with one another.
*/
enum the_type_t {
INT,
CHAR,
FLOAT,
DOUBLE,
UNKNOWN
};
char *all_type_strin gs[] = {
"int",
"char",
"float",
"double",
NULL
};
/* your structure for a single node in a ll */
struct node_t {
void *data;
struct node_t *next;
};
enum the_type_t get_type (char *the_type_strin g) {
enum the_type_t retvalue = UNKNOWN;
int i;
for (i=0; all_type_string s[i]!=NULL; i++) {
if (strcmp (all_type_strin gs[i], the_type_string )==0) {
retvalue = i;
break;
}
}
return retvalue;
}
struct node_t *fll_new (enum the_type_t the_type) {
void *data;
struct node_t *node = malloc (sizeof *node);
if (!node) {
return NULL;
}
switch (the_type) {
case INT: data = malloc (sizeof (int)); break;
case CHAR: data = malloc (sizeof (char)); break;
case FLOAT: data = malloc (sizeof (float)); break;
case DOUBLE: data = malloc (sizeof (double)); break;
case UNKNOWN:
default:
data = NULL;
};
node->data = data;
return node;
}
#define ll_new(the_type ) fll_new (get_type (#the_type))
int main (void) {
struct node_t *MyData = ll_new (int);
if (!MyData) {
printf ("no mem ?\n");
return EXIT_FAILURE;
}
*(int *)MyData->data = 42;
printf ("data stored = %i\n", *(int *)MyData->data);
/* this can possibly be wrapped into a macro as well */
free (MyData->data);
free (MyData);
return EXIT_SUCCESS;
}
----end of hw.c----
its not too hard to massage the above (rather ugly) code into
a linked list, as the non-intuitive parts of it is already
done. If you know how to implement a normal linked list, then
you could do it.
Anyhow, I'm looking for ways to enforce runtime type-safety. Any thoughts are appreciated, especially since I know for a fact this can be done (and without unions...).
I dont think that that (runtime safety) *can* be done properly. but
good luck anyway.
goose,
post a link to the finished goods, will you? i'm interested ;-) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: H Jansen |
last post by:
I try to work out how to use __new__ and metaclass (or __metaclass__)
mechanisms in order to change the base class at runtime. I haven't been
successful so far, however, even after also reading most of the relevant
documentation.
The most simple representation (an oversimplification indeed) of what
I'm trying to accomplish is this:
class Base1:
pass
|
by: web1110 |
last post by:
Hi y'all,
I'm playing with C# again, trying to implement a remoted calculator
exercise.
If I include:
using System.Runtime.Remoting.Channels.Http;
I get the error:
|
by: mra |
last post by:
I want to cast an object that I have created from a typename to the
corresponding type. Can anycone tell me how to do this?
Example:
//Here, Create the object of type "MyClass"
object obj=Activator.CreateInstance(strAssemblyName, "MyClass");
//Now, I want to do something like ((MyClass)obj).Method
//Can I do this?
|
by: Samuel R. Neff |
last post by:
We just started getting NullReferenceException in one of our
applications on our demo server.
This is occuring in a .NET Windows Service that is using binary
remoting over TCP to talk to another .NET Windows Service on the same
machine.
..NET 1.1 SP1
Any suggestions?
|
by: kumar.senthil |
last post by:
Hi,
I would like to know whether we can create a generic object at
runtime using the type obtained using reflection.
Type myType = Type.GetType("Namespace.Class");
MyClass<myType> test1 = new MyClass<myType>();
Basically, i need to create the generic object at run time and the
type is decided at runtime.
| |
by: Rich |
last post by:
I want to replace CSomeObject class with some kind of runtime method
that returns type CSomeObject that I can use as cast.
How do I specify type of explicit cast at runtime?
eg:
object object1 = new CSomeObject(22);
object object2 = new CSomeObject(2);
|
by: John |
last post by:
Hi Everyone,
I'm having this extremely annoying problem with Internet Explorer 6,
giving me an error message saying "unknown runtime error" whenever I
try to alter the contents of a <divelement using innerHTML.
Now, I've researched this problem on the web, and found many references
to it, but none of them quite addressed my specific situation, and
since my experience with JavaScript is limited, I was not able to adapt
the solutions I...
|
by: =?Utf-8?B?R3JhaGFt?= |
last post by:
I've added 2 tracking services to the wf runtime; one is the standard
SqlTrackingService:
trackingService = new
SqlTrackingService(<trackingConnectionString>);
<workflow Runtime>.AddService(trackingService);
trackingService.IsTransactional = false;
trackingService.UseDefaultProfile = true;
This works just fine.
|
by: desktop |
last post by:
I have read that using templates makes types know at compile time and
using inheritance the types are first decided at runtime. The use of
pointers and casts also indicates that the types will first be know at
runtime.
But is there some strict definitions that defines runtime code and
compile time code that can be used in general?
|
by: rn5a |
last post by:
The different Page events in the page life cycle like Page_PreInit,
Page_Init, Page_Load etc. - are they different stages of the runtime
process?
Does a server send back the HTML output of an ASPX page to the browser
immediately after the runtime or are there any processes involved in
between the runtime & the time when the server sends the HTML output
back to the browser?
Thanks
|
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...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |