home made handle

  • Thread starter Christopher Pisz
  • Start date
C

Christopher Pisz

How would you go about giving some unique identifier back to a user, that
they can use to make calls to your module with, that is associated with a
pointer to an object in your multithreaded module?

I thought about something like

typedef ModuleObject * ID;

where ModuleObject is a class in my multithreaded module.
objects of the ModuleObject type frequently get allocated and released from
memory.
and I have a private list of ModuleObject pointers in my module.

, but I don't really want them to have access to the methods of the object
since it is multithreaded and more importantly, they have no business
calling methods directly, but should be calling the functions in the module.
 
V

Victor Bazarov

Christopher said:
How would you go about giving some unique identifier back to a user,
that they can use to make calls to your module with, that is
associated with a pointer to an object in your multithreaded module?

There are many different ways. A pointer to the object is one.
I thought about something like

typedef ModuleObject * ID;

Sure. That's the simplest way, AFAICT.
where ModuleObject is a class in my multithreaded module.
objects of the ModuleObject type frequently get allocated and
released from memory.
and I have a private list of ModuleObject pointers in my module.

I don't think this is relevant.
, but I don't really want them to have access to the methods of the
object since it is multithreaded and more importantly, they have no
business calling methods directly, but should be calling the
functions in the module.

Make your 'ID' a void pointer instead.

V
 
R

red floyd

Christopher said:
How would you go about giving some unique identifier back to a user, that
they can use to make calls to your module with, that is associated with a
pointer to an object in your multithreaded module?

I thought about something like

typedef ModuleObject * ID;
[redacted]

, but I don't really want them to have access to the methods of the object
since it is multithreaded and more importantly, they have no business
calling methods directly, but should be calling the functions in the module.

Have two headers.

Module.h:

class ModuleObject;
typedef ModuleObject* ID;

ModulePriv.h

#include "Module.h"
class ModuleObject {
// actual class definition here.
};

That way, you don't have to do any casting internally, and you have type
safety amongst your various opaque types.
 
J

James Kanze

How would you go about giving some unique identifier back to a user, that
they can use to make calls to your module with, that is associated with a
pointer to an object in your multithreaded module?
I thought about something like
typedef ModuleObject * ID;
where ModuleObject is a class in my multithreaded module.
objects of the ModuleObject type frequently get allocated and
released from memory. and I have a private list of
ModuleObject pointers in my module.
, but I don't really want them to have access to the methods of the object
since it is multithreaded and more importantly, they have no business
calling methods directly, but should be calling the functions in the module.

There are several solutions. The simplest is the one you just
described. If all the user sees is an incomplete declaration of
ModuleObject, they won't be able to misuse it too much.

If you don't want to do that, there's always void*. And if even
revealing the fact that it is a pointer is too much for you,
then you can keep pointers to the objects in a vector, and
return an index to that. In practice, I don't see any reason to
go with the additional complexity, but this solution may be
useful in cases where you keep the objects themselves in the
vector (reusing them as needed), rather than a pointer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top