C++ Any getValue

Java, C++, C#, Delphi, ??? - Using the UNO bridges

C++ Any getValue

Postby saleem145 » Wed Dec 19, 2012 8:08 pm

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   Expand viewCollapse view
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("");
      }
}
OpenOffice 3.4.0
Mac OS X 10.5.8
saleem145
 
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: C++ Any getValue

Postby Charlie Young » Wed Dec 19, 2012 9:08 pm

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   Expand viewCollapse view
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   Expand viewCollapse view
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   Expand viewCollapse view
anyvar <<= vartoinsert


Where vartoinsert may be any number of things.
Apache OpenOffice 4.1.1
Windows XP
User avatar
Charlie Young
Volunteer
 
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: C++ Any getValue

Postby saleem145 » Wed Dec 19, 2012 9:43 pm

What do I if type is "byte" -- I tried to extract it into char but that doesn't work.....Saleem
OpenOffice 3.4.0
Mac OS X 10.5.8
saleem145
 
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: C++ Any getValue

Postby Charlie Young » Thu Dec 20, 2012 12:01 am

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?
Apache OpenOffice 4.1.1
Windows XP
User avatar
Charlie Young
Volunteer
 
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: C++ Any getValue

Postby Charlie Young » Thu Dec 20, 2012 12:49 am

This works, if you really need to be stingy with bytes:

Code: Select all   Expand viewCollapse view
BYTE myByte = (BYTE) 'A';
Any AnyByte;

AnyByte <<= (sal_Int8) myByte;
sal_Int8 ByteChar;
AnyByte >>= ByteChar;
      


Neither sal_Char nor sal_uInt8 work, however.
Apache OpenOffice 4.1.1
Windows XP
User avatar
Charlie Young
Volunteer
 
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: C++ Any getValue

Postby hanya » Thu Dec 20, 2012 8:05 am

saleem145 wrote:
Code: Select all   Expand viewCollapse view
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
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
hanya
Volunteer
 
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan


Return to External Programs

Who is online

Users browsing this forum: No registered users and 1 guest