C++ Any getValue

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

C++ Any getValue

Post 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("");
      }
}
OpenOffice 3.4.0
Mac OS X 10.5.8
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: C++ Any getValue

Post 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.
Apache OpenOffice 4.1.1
Windows XP
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: C++ Any getValue

Post by saleem145 »

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

Re: C++ Any getValue

Post 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?
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

Post 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.
Apache OpenOffice 4.1.1
Windows XP
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: C++ Any getValue

Post 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
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
Post Reply