473,387 Members | 1,517 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,387 software developers and data experts.

a good example why it is a pity that sizeof() is not overloadable?!

Hi,

I'm trying to build an _efficient_, _purely_ _abstract_ API for inter
process(or) communication, _completely_ hiding any implementation
details. The core components are "Buffer", "Address" and "Process".
They may be instantiated by a "Factory". Here is a simplified, but
compilable version:

// Abstract Interface
enum OsSelector {
POSIX,
WIN32
};

class Buffer {
public:
virtual void *payload(void) const = 0;
virtual void release(void) = 0;
};

struct Address;
extern const unsigned int addressSize;

class Process {
public:
virtual Address *address(void) = 0;
virtual void release(void) = 0;
};
class Callback {
public:
virtual void receive(Buffer *buffer) = 0;
Process *process;
};

class Factory {
public:
static Factory *create(OsSelector osSelector = POSIX);
virtual Buffer *buffer(unsigned int size,
void *data = 0) = 0;
virtual Process *process(const char *processName,
Callback *callback,
int prio = 0,
unsigned int stackSize = 0xffff) = 0;
virtual Address *lookup(const char *processName,
const char *processorName = 0) = 0;
virtual void send(Address *address, Buffer *buffer) = 0;
};

A simple application, sending a message to another "Process",
may look like this:

// Application
#include <string.h>
struct Data {
unsigned int data1;
unsigned int data2;
};

class TaskCallback: public Callback {
void receive(Buffer *buffer) {
this->process->release();
}
};

int main(void) {
TaskCallback taskCallback;
Factory *factory = Factory::create(POSIX);
Process *process = factory->process("process", &taskCallback);
Buffer *buffer = factory->buffer(addressSize + sizeof(Data));
char *sender = (char *) buffer->payload();
Data *data = (Data *) (sender + addressSize);
memcpy(sender, process->address(), addressSize);
data->data1 = 1;
data->data2 = 2;
factory->send(process->address(), buffer);
}

While this interface fulfills the outlined needs, it is somewhat
cumbersome to use when "Address"es should be sent to another
"Process" intermixed with other data: Typically, one would like to
define a structure containing the "Address"es and the other data
members to easily fill the payload of the "Buffer" to be sent. But
since "Address" is an incomplete type, this is not possible. Instead,
filling "Address"es into the payload requires employing pointer
arithmetic, using the variable "addressSize" provided by the interface.

Finally coming to my point: I know that there are good reasons why
the "sizeof()" operator cannot be overloaded, most notably because
the sizes of structure members must be known at compile time already.
But having seen many discussions that simply state that one can not
even imagine only _one_ example where it would be nice to overload
sizeof(), could you agree that here, it would be perfect if one
could replace the incomplete type "Address" by something like:

class Address {
public:
virtual size_t operator sizeof() = 0;
};

Assuming that types may then be created at runtime, it
would be possible to use the comfort of a structure even when
"Address"es are involved, provided that the implementation of
the overloaded sizeof() operator returns the size of the implementation
for "Address".

As I don't expect that sizeof() will become overloadable soon
just because I wish it could be ;-), does anyone know a more elegant
approach than my pointer arithmetics to fill "Buffer"s with huge
amount of ordinary data intermixed with the abstract "Address"es?
Restricting the presence of "Address"es to fixed locations in the
payload is not an option as the processor the SW is running on is
part of a network with legacy code that makes extensive use of
sending (fixed size) addresses.

Many thanks for the patience to read until this point, and even more
thanks for any good idea!

Regards,

Christof

Oct 14 '05 #1
3 2184

Christof Warlich wrote:
Hi,

I'm trying to build an _efficient_, _purely_ _abstract_ API for inter
process(or) communication, _completely_ hiding any implementation
details. The core components are "Buffer", "Address" and "Process".
They may be instantiated by a "Factory". Here is a simplified, but
compilable version:
[snip]
I'm sure that you could come with a simpler example.

While this interface fulfills the outlined needs, it is somewhat
cumbersome to use when "Address"es should be sent to another
"Process" intermixed with other data: Typically, one would like to
define a structure containing the "Address"es and the other data
members to easily fill the payload of the "Buffer" to be sent. But
since "Address" is an incomplete type, this is not possible. Instead,
filling "Address"es into the payload requires employing pointer
arithmetic, using the variable "addressSize" provided by the interface.

Finally coming to my point: I know that there are good reasons why
the "sizeof()" operator cannot be overloaded, most notably because
the sizes of structure members must be known at compile time already.
But having seen many discussions that simply state that one can not
even imagine only _one_ example where it would be nice to overload
sizeof(), could you agree that here, it would be perfect if one
could replace the incomplete type "Address" by something like:

class Address {
public:
virtual size_t operator sizeof() = 0;
};

Assuming that types may then be created at runtime, it
would be possible to use the comfort of a structure even when
"Address"es are involved, provided that the implementation of
the overloaded sizeof() operator returns the size of the implementation
for "Address".

As I don't expect that sizeof() will become overloadable soon
just because I wish it could be ;-), does anyone know a more elegant
approach than my pointer arithmetics to fill "Buffer"s with huge
amount of ordinary data intermixed with the abstract "Address"es?
Restricting the presence of "Address"es to fixed locations in the
payload is not an option as the processor the SW is running on is
part of a network with legacy code that makes extensive use of
sending (fixed size) addresses.

Many thanks for the patience to read until this point, and even more
thanks for any good idea!

Regards,

Christof


class Address
{
public:
virtual size_t SizeOf() = 0;
};

class DerivedAddress : public Address
{
public:
virtual size_t SizeOf()
{
return sizeof(*this);
}
};

/dan

Oct 14 '05 #2
Christof Warlich wrote:

Finally coming to my point: I know that there are good reasons why
the "sizeof()" operator cannot be overloaded


Just to be nitpicky, it's the sizeof operator, not sizeof(). The ()'s
are only necessary when the operand is a type.

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Oct 14 '05 #3
ben
The beauty of C++ is, if you want an extensible typeof operator, you can
write your own version (with a different name of course.) For example,
you can do:

template <typename T>
std::size_t size_of(void)
{
return sizeof(T);
}

template <typename T>
std::size_t size_of(T t)
{
return size_of<T>();
}
So if you want that to behave differently for type X, you make a
specialization, like so:

template <>
std::size_t size_of<X>(void)
{
return 1024;
}

If a lot of that specializations take place, you can use a macro:

#define TYPE_OF_SPEC(TYPE, SIZE)\
template<>\
std::size_t size_of<(_TYPE_)>(void)\
{return (SIZE);}
TYPE_OF_SPEC(Y, 2046)
TYPE_OF_SPEC(Z, 2500)
// ...

Ben

Oct 15 '05 #4

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

Similar topics

226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
14
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators...
8
by: Sims | last post by:
Hi, I have some small questions that have never been any problems, (for my compiler?), but always make me curious. So here goes... what does the standard sday about the 'if' statement? for...
19
by: Raposa Velha | last post by:
Hello to all! Does any of you want to comment the approach I implement for instantiating a form? A description and an example follow. Cheers, RV jmclopesAThotmail.com replace the AT with the...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
3
by: TC | last post by:
I'm not a C programmer. This is a good C? how check the return value of getch or scanf? and how check for the \n at the end of the text read? FEOF do not return true if the file (text file) is...
9
by: johan.tibell | last post by:
I'm in the process of writing an interpreter for lambda calculus (i.e. a small functional programming language) in C. I've previously written one in Haskell so I understand at least some of the...
75
by: Amkcoder | last post by:
http://amkcoder.fileave.com/L_BitWise.zip http://amkcoder.fileave.com/L_ptr2.zip
0
by: TamusJRoyce | last post by:
Thought of solution, but any thoughts would be welcome... I am curious about how I could know the type of an object I am working with from a base class. It would be nice if VB .Net supported...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
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
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...

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.