Page 1 of 1

C++ Any getValue

Posted: Wed Dec 19, 2012 8:08 pm
by saleem145
Hello,

I am trying to determine the type of information present in an any variable and extract it.The problem is I do not know how to use getValue. It returns a void *, but how to I convert it to string or double. Is it as simple as deferencing it??

The code I have below -- need the code for the ???

Saleem

Code: Select all

value AnyToValue( const Any &val )
{
      value result;

      if( val.hasValue() )
      {
            string value_type ==OUStringToString( val.getValueTypeName() );

            if( value_type == "string" )
            {
                    return ??????;
            }
            else
            {
                     return ?????;
            }
      }
      else
      {
             return value("");
      }
}

Re: C++ Any getValue

Posted: Wed Dec 19, 2012 9:08 pm
by Charlie Young
saleem145 wrote:Hello,

I am trying to determine the type of information present in an any variable and extract it.The problem is I do not know how to use getValue. It returns a void *, but how to I convert it to string or double. Is it as simple as deferencing it??

The code I have below -- need the code for the ???

Saleem

Code: Select all

value AnyToValue( const Any &val )
{
      value result;

      if( val.hasValue() )
      {
            string value_type ==OUStringToString( val.getValueTypeName() );

            if( value_type == "string" )
            {

                    return ??????;
            }
            else
            {
                     return ?????;
            }
      }
      else
      {
             return value("");
      }
}
Your code isn't quite clear to me. The return value has to be the same type as "value," whatever that is.

You extract the value of an any to a given type using an overloaded shift right assignment operator

Code: Select all

if( value_type == "string" )
 {
         OUString StrVal;
        val >>= SrrVal

        return StrVal;
}
This example will only work if the return type is OUString.

You insert into an Any by

Code: Select all

anyvar <<= vartoinsert
Where vartoinsert may be any number of things.

Re: C++ Any getValue

Posted: Wed Dec 19, 2012 9:43 pm
by saleem145
What do I if type is "byte" -- I tried to extract it into char but that doesn't work.....Saleem

Re: C++ Any getValue

Posted: Thu Dec 20, 2012 12:01 am
by Charlie Young
saleem145 wrote:What do I if type is "byte" -- I tried to extract it into char but that doesn't work.....Saleem
I can't get it to work straightaway either. BYTE is just a typedef for unsigned char, but if I insert a BYTE into an Any, I then get a valueTypeName of "boolean," which is strange. I can make a one character OUString(BYTE), then insert it into an Any and extract it out again as expected. What exactly do you need to do here?

Re: C++ Any getValue

Posted: Thu Dec 20, 2012 12:49 am
by Charlie Young
This works, if you really need to be stingy with bytes:

Code: Select all

BYTE myByte = (BYTE) 'A';
Any AnyByte;

AnyByte <<= (sal_Int8) myByte;
sal_Int8 ByteChar;
AnyByte >>= ByteChar;
		
Neither sal_Char nor sal_uInt8 work, however.

Re: C++ Any getValue

Posted: Thu Dec 20, 2012 8:05 am
by hanya
saleem145 wrote:

Code: Select all

string value_type ==OUStringToString( val.getValueTypeName() );

            if( value_type == "string" )
It should be done using TypeClass: http://www.openoffice.org/api/docs/cpp/ ... Class-2624