azraelly / ticpp

Automatically exported from code.google.com/p/ticpp
0 stars 0 forks source link

GetAttribute with return Value #22

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi, i would like to have a funtion like this in ticpp::element

    /**
        Returns an attribute of @a name from an element.
        Uses FromString to convert the string to the type of choice.

        @param name             The name of the attribute you are querying.
        @param throwIfNotFound  [DEF]   If true, will throw an exception if the
attribute doesn't exist
        @throws Exception When the attribute doesn't exist and throwIfNotFound is
true

        @see GetAttributeOrDefault
        */
        template< class T >
            T GetAttribute( const std::string& name, bool throwIfNotFound = true ) const
        {
            // Get the attribute's value as a std::string
            std::string temp;
            T value;
            if ( !GetAttributeImp( name, &temp ) )
            {
                if ( throwIfNotFound )
                {
                    TICPPTHROW( "Attribute does not exist" );
                }
            }
            else
            {
                // Stream the value from the string to T
                FromString( temp, &value );
            }

            return value;
        }

I some Situations, this is quite useful, so i think it should be added to
ticpp. Maybe with a better note in the documentation, that the Object has
to be copied, and you should prefer the "old" version, where you can.

But if you want to call a function with a Parameter from a Attribute, this
little function is very useful.

Original issue reported on code.google.com by PerryHop...@googlemail.com on 21 Jun 2008 at 6:48

GoogleCodeExporter commented 9 years ago
thanks, that cleaned up my code a bit, too
applied in svn rev 89

Original comment by rjmy...@gmail.com on 8 Jul 2008 at 12:50