Cast operation or string conversion?

G

Giuseppe:G:

Hi,

I have a function defined as follows:

bool observe(const WordID* dataword);
bool observe(const Word* dataword);

where Word and WordID are respectively

typedef std::string Word;
typedef uint32_t WordID;

My purpose is to use this function with some different data I already
have: the problem is, this data is NOT in the WordID/Word format.

What I have is *pairs* of values, that I would like to input together
into observe(). In other words, I have pairs of numbers whose type is size_t

(size_t, size_t), (size_t, size_t), ... etc.

How can I input one of these pairs into observe()? I was thinking of
creating a pair like in

X = make_pair(a,b)

and then use some kind of cast in order to have

WordID* dataword = dynamic_cast<WordID*>(X)

But I'm not really sure if what I'm saying sounds reasonable or whether
there are better ways to achieve this?Such as chaining the two size_t
into a string and returning a pointer maybe?

Thanks!

Giuseppe
 
A

acehreli

bool observe(const WordID* dataword);
bool observe(const Word* dataword);

where Word and WordID are respectively

typedef std::string Word;
typedef uint32_t WordID;

My purpose is to use this function with some different data I already
have: the problem is, this data is NOT in the WordID/Word format.

What I have is *pairs* of values, that I would like to input together
into observe(). In other words, I have pairs of numbers whose type is size_t

(size_t, size_t), (size_t, size_t), ... etc.

Would a unique conversion to Word work? Without knowing whether
observe() cares about the format of Word, it's difficult to say, but
this might work:

So if you have (0x123, 0x42), create a Word that contains the string
representation of the two value (assuming that size_t is 32 bits):

"0000012300000042"
How can I input one of these pairs into observe()? I was thinking of
creating a pair like in

X = make_pair(a,b)

and then use some kind of cast in order to have

WordID* dataword = dynamic_cast<WordID*>(X)

You can't use dynamic_cast, it's for dynamic class hierarchies with
virtual functions.

What you are saying is using the address of the pair as WordID. Yes,
it can work too but you must take care of the lifetimes of the pairs.
Insert them in a vector that lives long enough, perhaps?
But I'm not really sure if what I'm saying sounds reasonable or whether
there are better ways to achieve this?Such as chaining the two size_t
into a string and returning a pointer maybe?

It sounds like either can work.

Ali
 
A

acehreli

I forgot to say what would work:

WordID* dataword = reinterpret_cast<WordID*>(&X)

Ali
 
G

Giuseppe:G:

Hi Victor, thanks. Please find my replies below.

Victor said:
A pair of size_t objects is *most likely* bigger than an unsigned int.
You cannot dynamic_cast anything except pointers to polymorphic classes.
That means the approach you're describing is not going to work.

I would say convert your tuple into a string using hexadecimal or some
other notation and pass the pointer to that string to your 'observe'
function.

Would you be so kind to show me a practical example on how I achieve this?
BTW, what's the reason you're passing pointers and not references?

Well, to be honest observe() it's not my function, I just need to
integrate it in my code. After looking at it I've seen that the only
thing it does is incrementing some global count value leaving the
dataword untouched.


Thanks
Giuseppe
 
G

Giuseppe:G:

Hi Ali, thanks very much. Below are some further questions.

Would a unique conversion to Word work? Without knowing whether
observe() cares about the format of Word, it's difficult to say, but
this might work:

So if you have (0x123, 0x42), create a Word that contains the string
representation of the two value (assuming that size_t is 32 bits):

"0000012300000042"
Well I guess it would because, observe basically just increments some
global value and I don't think it parses dataword to get content
information.
I'm not familiar with C++, would you be so kind to show me some way to
pratically do this?


Thanks Giuseppe
 
D

Dijkstra

Well, to be honest observe() it's not my function, I just need to
integrate it in my code. After looking at it I've seen that the only
thing it does is incrementing some global count value leaving the
dataword untouched.

So, if you're pretty sure it does not touch the dataword and only do
some counting, why don't you pass NULL?

observe( (const WordID *)NULL );

If you "observe" by the way some crashing or core dumps you can pretty
sure the "observe" function touched the WordID. :)

Cheers,
Dijkstra.
 
G

Giuseppe:G:

Dijkstra said:
So, if you're pretty sure it does not touch the dataword and only do
some counting, why don't you pass NULL?

observe( (const WordID *)NULL );

If you "observe" by the way some crashing or core dumps you can pretty
sure the "observe" function touched the WordID. :)

Cheers,
Dijkstra.

hehe yeah I'm observing everything working thanks :D
 
G

Giuseppe:G:

Victor said:
A pair of size_t objects is *most likely* bigger than an unsigned int.

HI, Victor, I studied better my code and realised that I can use that
observe function only by passing the uint32 pointer to it (the *string
version does other things). So I guess now I have a more serious
problem, that of uniquely mapping this pair of (size_t size_t) to and
uint32. What do you propose to do?

Is it possible to cast

An idea is to go redefine the observe function using uint64_t instead of
uint32

observe(uint64_t* wordid);

and then convert each size_t into a uint32 and chain them into a uint64.
Does it sound reasonable?Something like this

uint64_t chain(uint32_t one, uint32_t two) {
// get two 32bit rands and concatenate
uint64_t longrand = one;
longrand <<= 32;
longrand |= two;
return longrand;
}

but I have no clue on how to get a uint32 out of a size_t and if it's
feasible.
Can you suggest other ways?

Thanks so much again

Giuseppe
 

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,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top