2021-08-18 14:03:52 +00:00
/*************************************************************************/
/* wrapped.hpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
2022-03-15 09:17:53 +00:00
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
2021-08-18 14:03:52 +00:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2022-10-09 06:47:07 +00:00
# ifndef GODOT_WRAPPED_HPP
# define GODOT_WRAPPED_HPP
2021-08-18 14:03:52 +00:00
# include <godot_cpp/core/memory.hpp>
2022-08-19 07:30:06 +00:00
# include <godot_cpp/core/property_info.hpp>
# include <godot_cpp/templates/list.hpp>
2021-09-09 23:40:40 +00:00
# include <godot_cpp/godot.hpp>
2021-11-30 13:00:13 +00:00
2021-08-18 14:03:52 +00:00
namespace godot {
typedef void GodotObject ;
// Base for all engine classes, to contain the pointer to the engine instance.
class Wrapped {
friend class GDExtensionBinding ;
2021-11-30 13:00:13 +00:00
friend void postinitialize_handler ( Wrapped * ) ;
2021-08-18 14:03:52 +00:00
protected :
2022-10-22 17:42:48 +00:00
virtual const StringName * _get_extension_class_name ( ) const ; // This is needed to retrieve the class name before the godot object has its _extension and _extension_instance members assigned.
2021-11-30 13:00:13 +00:00
virtual const GDNativeInstanceBindingCallbacks * _get_bindings_callbacks ( ) const = 0 ;
2022-08-19 07:30:06 +00:00
void _notification ( int p_what ) { } ;
bool _set ( const StringName & p_name , const Variant & p_property ) { return false ; } ;
bool _get ( const StringName & p_name , Variant & r_property ) const { return false ; } ;
void _get_property_list ( List < PropertyInfo > * p_list ) const { } ;
bool _property_can_revert ( const StringName & p_name ) const { return false ; } ;
bool _property_get_revert ( const StringName & p_name , Variant & r_property ) const { return false ; } ;
String _to_string ( ) const { return " [ " + String ( get_class_static ( ) ) + " : " + itos ( get_instance_id ( ) ) + " ] " ; }
static void notification_bind ( GDExtensionClassInstancePtr p_instance , int32_t p_what ) { }
static GDNativeBool set_bind ( GDExtensionClassInstancePtr p_instance , const GDNativeStringNamePtr p_name , const GDNativeVariantPtr p_value ) { return false ; }
static GDNativeBool get_bind ( GDExtensionClassInstancePtr p_instance , const GDNativeStringNamePtr p_name , GDNativeVariantPtr r_ret ) { return false ; }
static const GDNativePropertyInfo * get_property_list_bind ( GDExtensionClassInstancePtr p_instance , uint32_t * r_count ) { return nullptr ; }
static void free_property_list_bind ( GDExtensionClassInstancePtr p_instance , const GDNativePropertyInfo * p_list ) { }
static GDNativeBool property_can_revert_bind ( GDExtensionClassInstancePtr p_instance , const GDNativeStringNamePtr p_name ) { return false ; }
static GDNativeBool property_get_revert_bind ( GDExtensionClassInstancePtr p_instance , const GDNativeStringNamePtr p_name , GDNativeVariantPtr r_ret ) { return false ; }
2022-10-23 12:45:57 +00:00
static void to_string_bind ( GDExtensionClassInstancePtr p_instance , GDNativeBool * r_is_valid , GDNativeStringPtr r_out ) { }
2022-08-19 07:30:06 +00:00
GDNativePropertyInfo * plist = nullptr ;
uint32_t plist_size = 0 ;
2021-11-30 13:00:13 +00:00
void _postinitialize ( ) ;
2022-10-22 11:46:59 +00:00
Wrapped ( const StringName p_godot_class ) ;
2021-11-30 13:00:13 +00:00
Wrapped ( GodotObject * p_godot_object ) ;
2021-08-18 14:03:52 +00:00
public :
2022-10-22 11:46:59 +00:00
static StringName get_class_static ( ) {
static StringName string_name = StringName ( " Wrapped " ) ;
return string_name ;
2022-02-20 19:23:14 +00:00
}
2022-08-19 07:30:06 +00:00
uint64_t get_instance_id ( ) const {
return 0 ;
}
2021-08-18 14:03:52 +00:00
// Must be public but you should not touch this.
GodotObject * _owner = nullptr ;
2021-09-09 23:40:40 +00:00
} ;
2021-08-18 14:03:52 +00:00
} // namespace godot
2022-08-19 07:30:06 +00:00
# define GDCLASS(m_class, m_inherits) \
private : \
void operator = ( const m_class & p_rval ) { } \
friend class : : godot : : ClassDB ; \
\
protected : \
2022-10-22 17:42:48 +00:00
virtual const StringName * _get_extension_class_name ( ) const override { \
2022-10-22 11:46:59 +00:00
static StringName string_name = get_class_static ( ) ; \
return & string_name ; \
2022-08-19 07:30:06 +00:00
} \
\
virtual const GDNativeInstanceBindingCallbacks * _get_bindings_callbacks ( ) const override { \
return & ___binding_callbacks ; \
} \
\
static void ( * _get_bind_methods ( ) ) ( ) { \
return & m_class : : _bind_methods ; \
} \
\
2022-09-01 02:11:23 +00:00
static void ( : : godot : : Wrapped : : * _get_notification ( ) ) ( int ) { \
return ( void ( : : godot : : Wrapped : : * ) ( int ) ) & m_class : : _notification ; \
2022-08-19 07:30:06 +00:00
} \
\
2022-09-01 02:11:23 +00:00
static bool ( : : godot : : Wrapped : : * _get_set ( ) ) ( const : : godot : : StringName & p_name , const : : godot : : Variant & p_property ) { \
return ( bool ( : : godot : : Wrapped : : * ) ( const : : godot : : StringName & p_name , const : : godot : : Variant & p_property ) ) & m_class : : _set ; \
2022-08-19 07:30:06 +00:00
} \
\
2022-09-15 22:46:50 +00:00
static bool ( : : godot : : Wrapped : : * _get_get ( ) ) ( const : : godot : : StringName & p_name , : : godot : : Variant & r_ret ) const { \
return ( bool ( : : godot : : Wrapped : : * ) ( const : : godot : : StringName & p_name , : : godot : : Variant & r_ret ) const ) & m_class : : _get ; \
2022-08-19 07:30:06 +00:00
} \
\
2022-09-15 22:46:50 +00:00
static void ( : : godot : : Wrapped : : * _get_get_property_list ( ) ) ( : : godot : : List < : : godot : : PropertyInfo > * p_list ) const { \
return ( void ( : : godot : : Wrapped : : * ) ( : : godot : : List < : : godot : : PropertyInfo > * p_list ) const ) & m_class : : _get_property_list ; \
2022-08-19 07:30:06 +00:00
} \
\
2022-09-01 02:11:23 +00:00
static bool ( : : godot : : Wrapped : : * _get_property_can_revert ( ) ) ( const : : godot : : StringName & p_name ) { \
return ( bool ( : : godot : : Wrapped : : * ) ( const : : godot : : StringName & p_name ) ) & m_class : : _property_can_revert ; \
2022-08-19 07:30:06 +00:00
} \
\
2022-09-01 02:11:23 +00:00
static bool ( : : godot : : Wrapped : : * _get_property_get_revert ( ) ) ( const : : godot : : StringName & p_name , : : godot : : Variant & ) { \
return ( bool ( : : godot : : Wrapped : : * ) ( const : : godot : : StringName & p_name , : : godot : : Variant & ) ) & m_class : : _property_get_revert ; \
2022-08-19 07:30:06 +00:00
} \
\
2022-09-01 02:11:23 +00:00
static : : godot : : String ( : : godot : : Wrapped : : * _get_to_string ( ) ) ( ) { \
return ( : : godot : : String ( : : godot : : Wrapped : : * ) ( ) ) & m_class : : _to_string ; \
2022-08-19 07:30:06 +00:00
} \
\
2022-09-19 23:29:39 +00:00
template < class T , class B > \
2022-08-19 07:30:06 +00:00
static void register_virtuals ( ) { \
2022-09-19 23:29:39 +00:00
m_inherits : : register_virtuals < T , B > ( ) ; \
2022-08-19 07:30:06 +00:00
} \
\
public : \
static void initialize_class ( ) { \
static bool initialized = false ; \
if ( initialized ) { \
return ; \
} \
m_inherits : : initialize_class ( ) ; \
if ( m_class : : _get_bind_methods ( ) ! = m_inherits : : _get_bind_methods ( ) ) { \
_bind_methods ( ) ; \
2022-09-19 23:29:39 +00:00
m_inherits : : register_virtuals < m_class , m_inherits > ( ) ; \
2022-08-19 07:30:06 +00:00
} \
initialized = true ; \
} \
\
2022-10-22 11:46:59 +00:00
static StringName get_class_static ( ) { \
static StringName string_name = StringName ( # m_class ) ; \
return string_name ; \
2022-08-19 07:30:06 +00:00
} \
\
2022-10-22 17:42:48 +00:00
static StringName get_parent_class_static ( ) { \
2022-08-19 07:30:06 +00:00
return m_inherits : : get_class_static ( ) ; \
} \
\
static GDNativeObjectPtr create ( void * data ) { \
m_class * new_object = memnew ( m_class ) ; \
return new_object - > _owner ; \
} \
\
static void notification_bind ( GDExtensionClassInstancePtr p_instance , int32_t p_what ) { \
if ( p_instance & & m_class : : _get_notification ( ) ) { \
if ( m_class : : _get_notification ( ) ! = m_inherits : : _get_notification ( ) ) { \
m_class * cls = reinterpret_cast < m_class * > ( p_instance ) ; \
return cls - > _notification ( p_what ) ; \
} \
m_inherits : : notification_bind ( p_instance , p_what ) ; \
} \
} \
\
static GDNativeBool set_bind ( GDExtensionClassInstancePtr p_instance , const GDNativeStringNamePtr p_name , const GDNativeVariantPtr p_value ) { \
if ( p_instance & & m_class : : _get_set ( ) ) { \
if ( m_class : : _get_set ( ) ! = m_inherits : : _get_set ( ) ) { \
m_class * cls = reinterpret_cast < m_class * > ( p_instance ) ; \
2022-09-01 02:11:23 +00:00
return cls - > _set ( * reinterpret_cast < const : : godot : : StringName * > ( p_name ) , * reinterpret_cast < const : : godot : : Variant * > ( p_value ) ) ; \
2022-08-19 07:30:06 +00:00
} \
return m_inherits : : set_bind ( p_instance , p_name , p_value ) ; \
} \
return false ; \
} \
\
static GDNativeBool get_bind ( GDExtensionClassInstancePtr p_instance , const GDNativeStringNamePtr p_name , GDNativeVariantPtr r_ret ) { \
if ( p_instance & & m_class : : _get_get ( ) ) { \
if ( m_class : : _get_get ( ) ! = m_inherits : : _get_get ( ) ) { \
m_class * cls = reinterpret_cast < m_class * > ( p_instance ) ; \
2022-09-01 02:11:23 +00:00
return cls - > _get ( * reinterpret_cast < const : : godot : : StringName * > ( p_name ) , * reinterpret_cast < : : godot : : Variant * > ( r_ret ) ) ; \
2022-08-19 07:30:06 +00:00
} \
return m_inherits : : get_bind ( p_instance , p_name , r_ret ) ; \
} \
return false ; \
} \
\
static const GDNativePropertyInfo * get_property_list_bind ( GDExtensionClassInstancePtr p_instance , uint32_t * r_count ) { \
if ( p_instance & & m_class : : _get_get_property_list ( ) ) { \
if ( m_class : : _get_get_property_list ( ) ! = m_inherits : : _get_get_property_list ( ) ) { \
m_class * cls = reinterpret_cast < m_class * > ( p_instance ) ; \
2022-09-01 02:11:23 +00:00
: : godot : : List < : : godot : : PropertyInfo > list ; \
2022-08-19 07:30:06 +00:00
cls - > _get_property_list ( & list ) ; \
ERR_FAIL_COND_V_MSG ( cls - > plist ! = nullptr | | cls - > plist_size ! = 0 , nullptr , " Internal error, property list was not freed by engine! " ) ; \
cls - > plist = reinterpret_cast < GDNativePropertyInfo * > ( memalloc ( sizeof ( GDNativePropertyInfo ) * list . size ( ) ) ) ; \
cls - > plist_size = 0 ; \
2022-09-01 02:11:23 +00:00
for ( const : : godot : : PropertyInfo & E : list ) { \
2022-10-09 21:21:32 +00:00
cls - > plist [ cls - > plist_size ] . type = static_cast < GDNativeVariantType > ( E . type ) ; \
2022-10-22 17:42:48 +00:00
cls - > plist [ cls - > plist_size ] . name = E . name . _native_ptr ( ) ; \
2022-08-19 07:30:06 +00:00
cls - > plist [ cls - > plist_size ] . hint = E . hint ; \
2022-10-22 17:42:48 +00:00
cls - > plist [ cls - > plist_size ] . hint_string = E . hint_string . _native_ptr ( ) ; \
cls - > plist [ cls - > plist_size ] . class_name = E . class_name . _native_ptr ( ) ; \
2022-08-19 07:30:06 +00:00
cls - > plist [ cls - > plist_size ] . usage = E . usage ; \
cls - > plist_size + + ; \
} \
if ( r_count ) \
* r_count = cls - > plist_size ; \
return cls - > plist ; \
} \
return m_inherits : : get_property_list_bind ( p_instance , r_count ) ; \
} \
return nullptr ; \
} \
\
static void free_property_list_bind ( GDExtensionClassInstancePtr p_instance , const GDNativePropertyInfo * p_list ) { \
if ( p_instance ) { \
m_class * cls = reinterpret_cast < m_class * > ( p_instance ) ; \
ERR_FAIL_COND_MSG ( cls - > plist = = nullptr , " Internal error, property list double free! " ) ; \
memfree ( cls - > plist ) ; \
cls - > plist = nullptr ; \
cls - > plist_size = 0 ; \
} \
} \
\
static GDNativeBool property_can_revert_bind ( GDExtensionClassInstancePtr p_instance , const GDNativeStringNamePtr p_name ) { \
if ( p_instance & & m_class : : _get_property_can_revert ( ) ) { \
if ( m_class : : _get_property_can_revert ( ) ! = m_inherits : : _get_property_can_revert ( ) ) { \
m_class * cls = reinterpret_cast < m_class * > ( p_instance ) ; \
2022-09-01 02:11:23 +00:00
return cls - > _property_can_revert ( * reinterpret_cast < const : : godot : : StringName * > ( p_name ) ) ; \
2022-08-19 07:30:06 +00:00
} \
return m_inherits : : property_can_revert_bind ( p_instance , p_name ) ; \
} \
return false ; \
} \
\
static GDNativeBool property_get_revert_bind ( GDExtensionClassInstancePtr p_instance , const GDNativeStringNamePtr p_name , GDNativeVariantPtr r_ret ) { \
if ( p_instance & & m_class : : _get_property_get_revert ( ) ) { \
if ( m_class : : _get_property_get_revert ( ) ! = m_inherits : : _get_property_get_revert ( ) ) { \
m_class * cls = reinterpret_cast < m_class * > ( p_instance ) ; \
2022-09-01 02:11:23 +00:00
return cls - > _property_get_revert ( * reinterpret_cast < const : : godot : : StringName * > ( p_name ) , * reinterpret_cast < : : godot : : Variant * > ( r_ret ) ) ; \
2022-08-19 07:30:06 +00:00
} \
return m_inherits : : property_get_revert_bind ( p_instance , p_name , r_ret ) ; \
} \
return false ; \
} \
\
2022-10-23 12:45:57 +00:00
static void to_string_bind ( GDExtensionClassInstancePtr p_instance , GDNativeBool * r_is_valid , GDNativeStringPtr r_out ) { \
2022-08-19 07:30:06 +00:00
if ( p_instance & & m_class : : _get_to_string ( ) ) { \
if ( m_class : : _get_to_string ( ) ! = m_inherits : : _get_to_string ( ) ) { \
m_class * cls = reinterpret_cast < m_class * > ( p_instance ) ; \
2022-09-01 02:11:23 +00:00
* reinterpret_cast < : : godot : : String * > ( r_out ) = cls - > _to_string ( ) ; \
2022-10-23 12:45:57 +00:00
* r_is_valid = true ; \
2022-08-19 07:30:06 +00:00
return ; \
} \
2022-10-23 12:45:57 +00:00
m_inherits : : to_string_bind ( p_instance , r_is_valid , r_out ) ; \
2022-08-19 07:30:06 +00:00
} \
} \
\
static void free ( void * data , GDExtensionClassInstancePtr ptr ) { \
if ( ptr ) { \
m_class * cls = reinterpret_cast < m_class * > ( ptr ) ; \
cls - > ~ m_class ( ) ; \
: : godot : : Memory : : free_static ( cls ) ; \
} \
} \
\
static void * ___binding_create_callback ( void * p_token , void * p_instance ) { \
return nullptr ; \
} \
\
static void ___binding_free_callback ( void * p_token , void * p_instance , void * p_binding ) { \
} \
\
static GDNativeBool ___binding_reference_callback ( void * p_token , void * p_instance , GDNativeBool p_reference ) { \
return true ; \
} \
\
static constexpr GDNativeInstanceBindingCallbacks ___binding_callbacks = { \
___binding_create_callback , \
___binding_free_callback , \
___binding_reference_callback , \
} ;
// Don't use this for your classes, use GDCLASS() instead.
# define GDNATIVE_CLASS(m_class, m_inherits) \
2021-11-30 13:00:13 +00:00
private : \
void operator = ( const m_class & p_rval ) { } \
\
protected : \
virtual const GDNativeInstanceBindingCallbacks * _get_bindings_callbacks ( ) const override { \
return & ___binding_callbacks ; \
} \
\
2022-08-19 07:30:06 +00:00
m_class ( const char * p_godot_class ) : m_inherits ( p_godot_class ) { } \
m_class ( GodotObject * p_godot_object ) : m_inherits ( p_godot_object ) { } \
2021-11-30 13:00:13 +00:00
\
2022-08-19 07:30:06 +00:00
static void ( * _get_bind_methods ( ) ) ( ) { \
return nullptr ; \
2021-11-30 13:00:13 +00:00
} \
\
2022-08-19 07:30:06 +00:00
static void ( Wrapped : : * _get_notification ( ) ) ( int ) { \
return nullptr ; \
2021-11-30 13:00:13 +00:00
} \
\
2022-08-19 07:30:06 +00:00
static bool ( Wrapped : : * _get_set ( ) ) ( const StringName & p_name , const Variant & p_property ) { \
return nullptr ; \
2021-11-30 13:00:13 +00:00
} \
\
2022-09-15 22:46:50 +00:00
static bool ( Wrapped : : * _get_get ( ) ) ( const StringName & p_name , Variant & r_ret ) const { \
2022-08-19 07:30:06 +00:00
return nullptr ; \
2021-11-30 13:00:13 +00:00
} \
\
2022-09-15 22:46:50 +00:00
static void ( Wrapped : : * _get_get_property_list ( ) ) ( List < PropertyInfo > * p_list ) const { \
2022-08-19 07:30:06 +00:00
return nullptr ; \
2021-11-30 13:00:13 +00:00
} \
\
2022-08-19 07:30:06 +00:00
static bool ( Wrapped : : * _get_property_can_revert ( ) ) ( const StringName & p_name ) { \
2021-11-30 13:00:13 +00:00
return nullptr ; \
} \
\
2022-08-19 07:30:06 +00:00
static bool ( Wrapped : : * _get_property_get_revert ( ) ) ( const StringName & p_name , Variant & ) { \
return nullptr ; \
2021-11-30 13:00:13 +00:00
} \
\
2022-08-19 07:30:06 +00:00
static String ( Wrapped : : * _get_to_string ( ) ) ( ) { \
2021-11-30 13:00:13 +00:00
return nullptr ; \
} \
\
public : \
static void initialize_class ( ) { } \
\
2022-10-22 11:46:59 +00:00
static StringName get_class_static ( ) { \
static StringName string_name = StringName ( # m_class ) ; \
return string_name ; \
2021-11-30 13:00:13 +00:00
} \
\
2022-10-22 11:46:59 +00:00
static StringName get_parent_class_static ( ) { \
2022-02-20 19:23:14 +00:00
return m_inherits : : get_class_static ( ) ; \
2021-11-30 13:00:13 +00:00
} \
\
static void * ___binding_create_callback ( void * p_token , void * p_instance ) { \
2022-07-28 23:20:07 +00:00
/* Do not call memnew here, we don't want the postinitializer to be called */ \
return new ( " " ) m_class ( ( GodotObject * ) p_instance ) ; \
2021-11-30 13:00:13 +00:00
} \
static void ___binding_free_callback ( void * p_token , void * p_instance , void * p_binding ) { \
2022-07-28 23:20:07 +00:00
/* Explicitly call the deconstructor to ensure proper lifecycle for non-trivial members */ \
reinterpret_cast < m_class * > ( p_binding ) - > ~ m_class ( ) ; \
2021-11-30 13:00:13 +00:00
Memory : : free_static ( reinterpret_cast < m_class * > ( p_binding ) ) ; \
} \
static GDNativeBool ___binding_reference_callback ( void * p_token , void * p_instance , GDNativeBool p_reference ) { \
return true ; \
} \
static constexpr GDNativeInstanceBindingCallbacks ___binding_callbacks = { \
___binding_create_callback , \
___binding_free_callback , \
___binding_reference_callback , \
} ; \
m_class ( ) : m_class ( # m_class ) { }
2021-08-18 14:03:52 +00:00
2022-10-09 06:47:07 +00:00
# endif // GODOT_WRAPPED_HPP