473,890 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Specifying data types inside functions

Here is my problem:

I have a certain set of well-defined manipulations that I have to apply
to different types of data. In all cases the manipulations are exactly the
same, and are to be performed on the different types of data. Currently I
have a collection of functions that do exactly the same - the only
difference between them is the type of data they act on. Let me present a
toy example:

I have the following two data types:

typedef struct { typedef struct {
int x ; int x ;
int y ; char * p ;
char * s ; } data_two ;
} data_one ;

Thus, I know that both data_one and data_two have an integer field, x.
The remaining fields can be arbitrarily different.

I want to have a function that takes as an argument a pointer to one of
those structures, increments the x member and returns a pointer to the
same structure. Currently I have two functions:

data_one * f_one(data_one * data) data_two * f_two(data_two * data)
{ {
data->x += 1 ; data->x += 1 ;

return data ; return data ;
} }

The bodies of the functions are identical - which is a waste.
I'd rather use a single function, with a prototype similar to:

void * f(data_type type, void * data)

where "type" would specify the actual type of the data we are dealing with.

Can something along these lines be done? Please remember that the above
is just a toy example.




Nov 14 '05 #1
4 2276

"No One" <No******@nowhe re.net> wrote in message
I have the following two data types:

typedef struct { typedef struct {
int x ; int x ;
int y ; char * p ;
char * s ; } data_two ;
} data_one ;

I'd rather use a single function, with a prototype similar to:

void * f(data_type type, void * data)

where "type" would specify the actual type of the data we are
dealing with.

You very rapidly get into a murky area of the C standard here. There are
some guarantees about structure layout in memory, but it is generally not a
good idea to use extendable structures or to try to convert one type of
structure to another.

If two structures have a common header then do this

struct common
{
int x;
...
};

typedef struct
{
struct common header;
...
}data_one;

typedef struct
{
struct common header;
...
} data_two;

Now when you write f

void f(data_type type, void *data)
{
data_one *data1;
data_two *data2;
switch(type)
{
case 1:
data1 = data;
foo( data1->header);
break;
case 2:
data2 = data;
foo(data2->header);
break;
}
}

Theoretically you could cast data to a struct common * directly, but this is
asking for trouble. For instance, if someone inserts another member at the
beginning of one of the structs the code will break without warning.
Nov 14 '05 #2
On Sat, 10 Apr 2004 19:39:38 +0100, Malcolm wrote:

You very rapidly get into a murky area of the C standard here. There are
some guarantees about structure layout in memory, but it is generally not
a good idea to use extendable structures or to try to convert one type of
structure to another.

If two structures have a common header then do this

struct common
{
int x;
...
};

typedef struct
{
struct common header;
...
}data_one;

typedef struct
{
struct common header;
...
} data_two;

Now when you write f

void f(data_type type, void *data)
{
data_one *data1;
data_two *data2;
switch(type)
{
case 1:
data1 = data;
foo( data1->header);
break;
case 2:
data2 = data;
foo(data2->header);
break;
}
}
}
Theoretically you could cast data to a struct common * directly, but this
is asking for trouble. For instance, if someone inserts another member at
the beginning of one of the structs the code will break without warning.


Thanks for your feedback. The solution that you propose is similar to
what I am using. My actual problem is even worse than I presented, for the
common fields might not always be the first ones in the structures :-( It
seems to me that I am stuck with ugly code.

Nov 14 '05 #3
No One wrote:
Here is my problem:

I have a certain set of well-defined manipulations that I have to apply
to different types of data. In all cases the manipulations are exactly the
same, and are to be performed on the different types of data. Currently I
have a collection of functions that do exactly the same - the only
difference between them is the type of data they act on. Let me present a
toy example:

I have the following two data types:

typedef struct { typedef struct {
int x ; int x ;
int y ; char * p ;
char * s ; } data_two ;
} data_one ;

Thus, I know that both data_one and data_two have an integer field, x.
The remaining fields can be arbitrarily different.

I want to have a function that takes as an argument a pointer to one of
those structures, increments the x member and returns a pointer to the
same structure. Currently I have two functions:

data_one * f_one(data_one * data) data_two * f_two(data_two * data)
{ {
data->x += 1 ; data->x += 1 ;

return data ; return data ;
} }

The bodies of the functions are identical - which is a waste.
I'd rather use a single function, with a prototype similar to:

void * f(data_type type, void * data)

where "type" would specify the actual type of the data we are dealing with.

Can something along these lines be done? Please remember that the above
is just a toy example.


You can use the preprocessor.

#define func(type) type* f_##type (type* data) {\
data->x+=1; \
return data; \
}

func(data_one)
func(data_two)
With C++, you'd use templates.

-Peter

--
Pull out a splinter to reply.
Nov 14 '05 #4

Where is Cheema these days?

--
Munawar Akram
-=-
This message was posted via two or more anonymous remailing services.


Nov 14 '05 #5

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

Similar topics

2
7674
by: Enigman O'Maly | last post by:
I'm still somewhat new to object style programming (as will become evident), using VBA in Excel 2000 to automate some previously manual functions. I've defined a class module so that I can create objects that reflect data gathered in web inquiries. The module has a bunch of Get/Let routines such as this pair: Private m_dYield As Double
21
4554
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
13
3700
by: Shailesh Humbad | last post by:
I wrote a short page as a quick reference to c++ integer data types. Any feedback welcome: http://www.somacon.com/blog/page11.php
4
3873
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system that doesn't offer dynamic memory allocation (to be clear: no malloc, no realloc), and with rather tight memory constraints. Writing my own malloc to do dynamic allocation from some static pool isn't really an option, for various reasons, not...
4
1268
by: No One | last post by:
Here is my problem: I have a certain set of well-defined manipulations that I have to apply to different types of data. In all cases the manipulations are exactly the same, and are to be performed on the different types of data. Currently I have a collection of functions that do exactly the same - the only difference between them is the type of data they act on. Let me present a toy example: I have the following two data types:
11
3197
by: theshowmecanuck | last post by:
As a matter of academic interest only, is there a way to programmatically list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to accomplish it. For example: int main void() { while there are more data types { print next data type; }
41
4748
by: laimis | last post by:
Hey guys, I just recently got introduced to data mappers (DTO mapper). So now I have a SqlHelper being used by DTOMapper and then business layer is using DTOMapper when it needs to persist object to database or load them back. Everything is working nicely so far. My question is, is it OK practice to use DTOMapper rfom the presentation layer? For instance, if I want to present in HTML format the list of entries in my database, should I...
15
3546
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and you want to make a deque which can contain any objects of any of those types. Normally what you would have to do is to make a deque or vector of pointers of the base class type and then allocate each object dynamically with 'new' and store the...
2
7651
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means mentioning the class members(functions, typedefs, data) under the access control labels : public, protected, private. Encapsulation means providing the implementation of class member
0
10811
jinu1996
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...
1
10919
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
9625
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
8015
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
5846
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
6041
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4674
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
2
4270
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3275
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.