C++ casting: object type -> char *

P

pallav

I'm using an old sparse matrix C library in my C++ code and I'd like
to know how to downcast a boost::shared_ptr to char *. The sparse
matrix data structure is like this:

struct sm_element_struct {

/* other fields here */
.......
char *user_word; /* user-defined word */
};

struct sm_row_struct {
.....
char *user_word; /* user-defined word */
};

struct sm_col_struct {
........
char *user_word; /* user-defined word */
};


In my c++ function, I do this:

typedef boost::shared_ptr<class Node> NodePtr; // where Node is a C++
class

static sm_matrix* convertNodeToSM(const NodePtr& n)
{
sm_matrix *M;
sm_element *elem;
sm_col *col;


M = sm_alloc(); /* allocate a sparse matrix */

unsigned int j;
NodePtr fanin;

for (unsigned int i = 0; i < n->numCubes(); ++i)
{
/* loop over all the fanin of n)
foreach_fanin(n, j, fanin)
{
switch (fanin->getValue())
{
case ZERO:
elem = sm_insert(M, i, j); // create an element at
i,j
sm_put(elem, 1); // set elem->user_word = 1
col = sm_get_col(M, static_cast<int>(j)); get the jth
column
sm_put(col, boost::lexical_cast<char *>(fanin));
break;
}

}
}
}


basically i need to store fanin (type NodePtr) into the "user_word" of
col which takes a char *. so i need to type cast. in C, i would have
just done

(char *) fanin;

but i want to know how to do this properly in C++ is
boost::lexical_cast the way to go?

and then to convert from the user word -> NodePtr I'd do this:

boost::lexical_cast<NodePtr> (col->user_word);

is this correct?

i would have preferred to use boost::ublas::mapped_matrix but i'm
finding it is not suitable to my program (minimization of Boolean
functions) so i have to use an old sparse library which is general
enough but has some support for this application domain.

i'd appreciate any help on the casting. thanks for your time.

pallav
 
D

David Harmon

On 26 Apr 2007 10:56:33 -0700 in comp.lang.c++, pallav
I'm using an old sparse matrix C library in my C++ code and I'd like
to know how to downcast a boost::shared_ptr to char *.

You would use shared_ptr::get(). But beware, if you start storing those
char* pointers in the matrix class or elsewhere, you have pretty much
given up any advantage you may have gotten from shared_ptr.
 
P

pallav

actually i've been working on this and have made some progress:

static sm_matrix* convertNodeToSM(const NodePtr& n)
{
sm_matrix *sm;
sm_element *elem;
sm_col *col;

sm = sm_alloc();

unsigned int j;
NodePtr fanin;
for (unsigned int i = 0; i < n->numCubes(); ++i)
{
foreach_fanin(n, j, fanin)
{

switch (n->getLiteral(i, j))
{
case VZERO:
elem = sm_insert(sm, i, j);
sm_put(elem, 1);
col = sm_get_col(sm, static_cast<int>(j));
sm_put(col, reinterpret_cast<char *>(fanin.get())); <-- put
Node * in matrix
break;
case VONE:
elem = sm_insert(sm, i, j);
sm_put(elem, 0);
col = sm_get_col(sm, static_cast<int>(j));
sm_put(col, reinterpret_cast<char *>(fanin.get())); <-- put
Node * in matrix
break;
case VDASH:
break;
default:
bailOut("convertNodeToSM()", ERROR_INVALID_INPUT_VALUE);
}
}
}
return sm;
}

static NodePtr convertSMToNode(const sm_matrix *sm)
{
NodePtr fanin, lit, orn = Node::constant(0);

sm_row *row;
sm_col *col;
sm_element *elem;

sm_foreach_row(sm, row)
{
NodePtr andn = Node::constant(1);
sm_foreach_row_element(row, elem)
{
col = sm_get_col(sm, elem->col_num);
Node *tmp = reinterpret_cast<Node *>(sm_get(char *, col));
<-- get it back from matrix;

do stuff here..

}


this seems to work OK. i think i've got the casts right now. unless
i'm still doing it wrong, please ignore. thanks
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,096
Latest member
ThurmanCre

Latest Threads

Top