joyhughes / Jen

Image processing, generative photography, cellular automata
https://discord.gg/GQQHUbkf
23 stars 20 forks source link

Improve function pointer retrieval #92

Closed joyhughes closed 4 months ago

joyhughes commented 4 months ago

Scene file error will throw exemption rather than causing segfault. Change function wrapper classes, use macro for template specialization.

Refactor code to retrieve function pointer by adding function to scene class:

    template< class T, class F > std::shared_ptr< F > get_fn_ptr( const std::string& name ) {
        if( !functions.contains( name ) ) {
            throw std::runtime_error( "function " + name + " not found in scene" ); 
            return nullptr;
        }
        else {
            if( !std::holds_alternative< any_fn< T > >( functions[ name ] ) ) {
                throw std::runtime_error( "function " + name + " not of return type " + typeid( T ).name() );
                return nullptr;
            }
            else {
                auto fn = std::get< any_fn< T > >( functions[ name ] );
                if( !std::holds_alternative< std::shared_ptr< F > >( fn.any_fn_ptr ) ) {
                    throw std::runtime_error( "function " + name + " not of type " + typeid( F ).name() );
                    return nullptr;
                }
                else {
                    return std::get< std::shared_ptr< F > >( fn.any_fn_ptr );
                }
            }
        }
    }