From 5dbf1809c6e3e905b94b8764e99491e608122261 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Verschelde?= Date: Sun, 5 Mar 2017 16:44:50 +0100 Subject: A Whole New World (clang-format edition) I can show you the code Pretty, with proper whitespace Tell me, coder, now when did You last write readable code? I can open your eyes Make you see your bad indent Force you to respect the style The core devs agreed upon A whole new world A new fantastic code format A de facto standard With some sugar Enforced with clang-format A whole new world A dazzling style we all dreamed of And when we read it through It's crystal clear That now we're in a whole new world of code --- core/object.cpp | 1175 +++++++++++++++++++++++++------------------------------ 1 file changed, 528 insertions(+), 647 deletions(-) (limited to 'core/object.cpp') diff --git a/core/object.cpp b/core/object.cpp index 79905a6be6..e9b332fafa 100644 --- a/core/object.cpp +++ b/core/object.cpp @@ -28,14 +28,14 @@ /*************************************************************************/ #include "object.h" -#include "print_string.h" #include "class_db.h" -#include "script_language.h" -#include "message_queue.h" #include "core_string_names.h" -#include "translation.h" +#include "message_queue.h" #include "os/os.h" +#include "print_string.h" #include "resource.h" +#include "script_language.h" +#include "translation.h" #ifdef DEBUG_ENABLED @@ -44,7 +44,7 @@ struct _ObjectDebugLock { Object *obj; _ObjectDebugLock(Object *p_obj) { - obj=p_obj; + obj = p_obj; obj->_lock_index.ref(); } ~_ObjectDebugLock() { @@ -60,48 +60,44 @@ struct _ObjectDebugLock { #endif - PropertyInfo::operator Dictionary() const { Dictionary d; - d["name"]=name; - d["type"]=type; - d["hint"]=hint; - d["hint_string"]=hint_string; - d["usage"]=usage; + d["name"] = name; + d["type"] = type; + d["hint"] = hint; + d["hint_string"] = hint_string; + d["usage"] = usage; return d; - } -PropertyInfo PropertyInfo::from_dict(const Dictionary& p_dict) { +PropertyInfo PropertyInfo::from_dict(const Dictionary &p_dict) { PropertyInfo pi; if (p_dict.has("type")) - pi.type=Variant::Type(int(p_dict["type"])); + pi.type = Variant::Type(int(p_dict["type"])); if (p_dict.has("name")) - pi.name=p_dict["name"]; + pi.name = p_dict["name"]; if (p_dict.has("hint")) - pi.hint=PropertyHint(int(p_dict["hint"])); + pi.hint = PropertyHint(int(p_dict["hint"])); if (p_dict.has("hint_string")) - pi.hint_string=p_dict["hint_string"]; + pi.hint_string = p_dict["hint_string"]; if (p_dict.has("usage")) - pi.usage=p_dict["usage"]; + pi.usage = p_dict["usage"]; return pi; } - -Array convert_property_list(const List * p_list) { +Array convert_property_list(const List *p_list) { Array va; - for (const List::Element *E=p_list->front();E;E=E->next()) { - + for (const List::Element *E = p_list->front(); E; E = E->next()) { va.push_back(Dictionary(E->get())); } @@ -111,201 +107,198 @@ Array convert_property_list(const List * p_list) { MethodInfo::operator Dictionary() const { - Dictionary d; - d["name"]=name; - d["args"]=convert_property_list(&arguments); + d["name"] = name; + d["args"] = convert_property_list(&arguments); Array da; - for(int i=0;i *p_parents) { - - } void Object::_get_valid_parents_static(List *p_parents) { - - } #if 0 //old style set, deprecated @@ -393,26 +379,25 @@ void Object::set(const String& p_name, const Variant& p_value) { } #endif -void Object::set(const StringName& p_name, const Variant& p_value, bool *r_valid) { +void Object::set(const StringName &p_name, const Variant &p_value, bool *r_valid) { #ifdef TOOLS_ENABLED - _edited=true; + _edited = true; #endif if (script_instance) { - if (script_instance->set(p_name,p_value)) { + if (script_instance->set(p_name, p_value)) { if (r_valid) - *r_valid=true; + *r_valid = true; return; } - } //try built-in setgetter { - if (ClassDB::set_property(this,p_name,p_value,r_valid)) { + if (ClassDB::set_property(this, p_name, p_value, r_valid)) { /* if (r_valid) *r_valid=true; @@ -421,85 +406,76 @@ void Object::set(const StringName& p_name, const Variant& p_value, bool *r_valid } } - - if (p_name==CoreStringNames::get_singleton()->_script) { + if (p_name == CoreStringNames::get_singleton()->_script) { set_script(p_value); if (r_valid) - *r_valid=true; + *r_valid = true; return; - } else if (p_name==CoreStringNames::get_singleton()->_meta) { + } else if (p_name == CoreStringNames::get_singleton()->_meta) { //set_meta(p_name,p_value); - metadata=p_value; + metadata = p_value; if (r_valid) - *r_valid=true; + *r_valid = true; return; } else { //something inside the object... :| - bool success = _setv(p_name,p_value); + bool success = _setv(p_name, p_value); if (success) { if (r_valid) - *r_valid=true; + *r_valid = true; return; } - setvar(p_name,p_value,r_valid); + setvar(p_name, p_value, r_valid); } - } -Variant Object::get(const StringName& p_name, bool *r_valid) const{ - +Variant Object::get(const StringName &p_name, bool *r_valid) const { Variant ret; if (script_instance) { - if (script_instance->get(p_name,ret)) { + if (script_instance->get(p_name, ret)) { if (r_valid) - *r_valid=true; + *r_valid = true; return ret; } - } - //try built-in setgetter { - if (ClassDB::get_property(const_cast(this),p_name,ret)) { + if (ClassDB::get_property(const_cast(this), p_name, ret)) { if (r_valid) - *r_valid=true; + *r_valid = true; return ret; } } - - if (p_name==CoreStringNames::get_singleton()->_script) { + if (p_name == CoreStringNames::get_singleton()->_script) { ret = get_script(); if (r_valid) - *r_valid=true; + *r_valid = true; return ret; - } else if (p_name==CoreStringNames::get_singleton()->_meta) { + } else if (p_name == CoreStringNames::get_singleton()->_meta) { ret = metadata; if (r_valid) - *r_valid=true; + *r_valid = true; return ret; } else { //something inside the object... :| - bool success = _getv(p_name,ret); + bool success = _getv(p_name, ret); if (success) { if (r_valid) - *r_valid=true; + *r_valid = true; return ret; } //if nothing else, use getvar - return getvar(p_name,r_valid); + return getvar(p_name, r_valid); } - - } - #if 0 //old style get, deprecated Variant Object::get(const String& p_name) const { @@ -528,86 +504,78 @@ Variant Object::get(const String& p_name) const { } #endif -void Object::get_property_list(List *p_list,bool p_reversed) const { +void Object::get_property_list(List *p_list, bool p_reversed) const { if (script_instance && p_reversed) { - p_list->push_back( PropertyInfo(Variant::NIL,"Script Variables",PROPERTY_HINT_NONE,String(),PROPERTY_USAGE_CATEGORY)); + p_list->push_back(PropertyInfo(Variant::NIL, "Script Variables", PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY)); script_instance->get_property_list(p_list); } - _get_property_listv(p_list,p_reversed); - + _get_property_listv(p_list, p_reversed); if (!is_class("Script")) // can still be set, but this is for userfriendlyness - p_list->push_back( PropertyInfo( Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script",PROPERTY_USAGE_DEFAULT|PROPERTY_USAGE_STORE_IF_NONZERO)); + p_list->push_back(PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NONZERO)); if (!metadata.empty()) - p_list->push_back( PropertyInfo( Variant::DICTIONARY, "__meta__", PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR|PROPERTY_USAGE_STORE_IF_NONZERO)); + p_list->push_back(PropertyInfo(Variant::DICTIONARY, "__meta__", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_STORE_IF_NONZERO)); if (script_instance && !p_reversed) { - p_list->push_back( PropertyInfo(Variant::NIL,"Script Variables",PROPERTY_HINT_NONE,String(),PROPERTY_USAGE_CATEGORY)); + p_list->push_back(PropertyInfo(Variant::NIL, "Script Variables", PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY)); script_instance->get_property_list(p_list); } - } -void Object::_validate_property(PropertyInfo& property) const { - +void Object::_validate_property(PropertyInfo &property) const { } void Object::get_method_list(List *p_list) const { - ClassDB::get_method_list(get_class_name(),p_list); + ClassDB::get_method_list(get_class_name(), p_list); if (script_instance) { script_instance->get_method_list(p_list); } } +Variant Object::_call_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error) { - -Variant Object::_call_bind(const Variant** p_args, int p_argcount, Variant::CallError& r_error) { - - if (p_argcount<1) { - r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.argument=0; + if (p_argcount < 1) { + r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 0; return Variant(); } - if (p_args[0]->get_type()!=Variant::STRING) { - r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument=0; - r_error.expected=Variant::STRING; + if (p_args[0]->get_type() != Variant::STRING) { + r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 0; + r_error.expected = Variant::STRING; return Variant(); } StringName method = *p_args[0]; - return call(method,&p_args[1],p_argcount-1,r_error); - - + return call(method, &p_args[1], p_argcount - 1, r_error); } -Variant Object::_call_deferred_bind(const Variant** p_args, int p_argcount, Variant::CallError& r_error) { +Variant Object::_call_deferred_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error) { - if (p_argcount<1) { - r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.argument=0; + if (p_argcount < 1) { + r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 0; return Variant(); } - if (p_args[0]->get_type()!=Variant::STRING) { - r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument=0; - r_error.expected=Variant::STRING; + if (p_args[0]->get_type() != Variant::STRING) { + r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 0; + r_error.expected = Variant::STRING; return Variant(); } - r_error.error=Variant::CallError::CALL_OK; + r_error.error = Variant::CallError::CALL_OK; StringName method = *p_args[0]; - MessageQueue::get_singleton()->push_call(get_instance_ID(),method,&p_args[1],p_argcount-1); + MessageQueue::get_singleton()->push_call(get_instance_ID(), method, &p_args[1], p_argcount - 1); return Variant(); - } #if 0 @@ -627,10 +595,9 @@ void Object::_call_deferred_bind(const StringName& p_name, const Variant& p_arg1 }; #endif #ifdef DEBUG_ENABLED -static bool _test_call_error(const StringName& p_func,const Variant::CallError& error) { - +static bool _test_call_error(const StringName &p_func, const Variant::CallError &error) { - switch(error.error) { + switch (error.error) { case Variant::CallError::CALL_OK: return true; @@ -638,37 +605,36 @@ static bool _test_call_error(const StringName& p_func,const Variant::CallError& return false; case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT: { - ERR_EXPLAIN("Error Calling Function: "+String(p_func)+" - Invalid type for argument "+itos(error.argument)+", expected "+Variant::get_type_name(error.expected)); + ERR_EXPLAIN("Error Calling Function: " + String(p_func) + " - Invalid type for argument " + itos(error.argument) + ", expected " + Variant::get_type_name(error.expected)); ERR_FAIL_V(true); } break; case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: { - ERR_EXPLAIN("Error Calling Function: "+String(p_func)+" - Too many arguments, expected "+itos(error.argument)); + ERR_EXPLAIN("Error Calling Function: " + String(p_func) + " - Too many arguments, expected " + itos(error.argument)); ERR_FAIL_V(true); } break; case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: { - ERR_EXPLAIN("Error Calling Function: "+String(p_func)+" - Too few arguments, expected "+itos(error.argument)); + ERR_EXPLAIN("Error Calling Function: " + String(p_func) + " - Too few arguments, expected " + itos(error.argument)); ERR_FAIL_V(true); } break; - case Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL: {} //? - + case Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL: { + } //? } return true; } #else -#define _test_call_error(m_str,m_err) ((m_err.error==Variant::CallError::CALL_ERROR_INVALID_METHOD)?false:true) +#define _test_call_error(m_str, m_err) ((m_err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) ? false : true) #endif -void Object::call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount) { - +void Object::call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount) { - if (p_method==CoreStringNames::get_singleton()->_free) { + if (p_method == CoreStringNames::get_singleton()->_free) { #ifdef DEBUG_ENABLED if (cast_to()) { ERR_EXPLAIN("Can't 'free' a reference."); @@ -676,8 +642,7 @@ void Object::call_multilevel(const StringName& p_method,const Variant** p_args,i return; } - - if (_lock_index.get()>1) { + if (_lock_index.get() > 1) { ERR_EXPLAIN("Object is locked and can't be freed."); ERR_FAIL(); return; @@ -695,59 +660,51 @@ void Object::call_multilevel(const StringName& p_method,const Variant** p_args,i Variant::CallError error; if (script_instance) { - script_instance->call_multilevel(p_method,p_args,p_argcount); + script_instance->call_multilevel(p_method, p_args, p_argcount); //_test_call_error(p_method,error); - } - MethodBind *method=ClassDB::get_method(get_class_name(),p_method); + MethodBind *method = ClassDB::get_method(get_class_name(), p_method); if (method) { - method->call(this,p_args,p_argcount,error); - _test_call_error(p_method,error); + method->call(this, p_args, p_argcount, error); + _test_call_error(p_method, error); } - } -void Object::call_multilevel_reversed(const StringName& p_method,const Variant** p_args,int p_argcount) { +void Object::call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount) { - - MethodBind *method=ClassDB::get_method(get_class_name(),p_method); + MethodBind *method = ClassDB::get_method(get_class_name(), p_method); Variant::CallError error; OBJ_DEBUG_LOCK if (method) { - method->call(this,p_args,p_argcount,error); - _test_call_error(p_method,error); + method->call(this, p_args, p_argcount, error); + _test_call_error(p_method, error); } //Variant ret; - - if (script_instance) { - script_instance->call_multilevel_reversed(p_method,p_args,p_argcount); + script_instance->call_multilevel_reversed(p_method, p_args, p_argcount); //_test_call_error(p_method,error); - } - } -bool Object::has_method(const StringName& p_method) const { +bool Object::has_method(const StringName &p_method) const { - if (p_method==CoreStringNames::get_singleton()->_free) { + if (p_method == CoreStringNames::get_singleton()->_free) { return true; } - if (script_instance && script_instance->has_method(p_method)) { return true; } - MethodBind *method=ClassDB::get_method(get_class_name(),p_method); + MethodBind *method = ClassDB::get_method(get_class_name(), p_method); if (method) { return true; @@ -756,42 +713,39 @@ bool Object::has_method(const StringName& p_method) const { return false; } - -Variant Object::getvar(const Variant& p_key, bool *r_valid) const { +Variant Object::getvar(const Variant &p_key, bool *r_valid) const { if (r_valid) - *r_valid=false; + *r_valid = false; return Variant(); } -void Object::setvar(const Variant& p_key, const Variant& p_value,bool *r_valid) { +void Object::setvar(const Variant &p_key, const Variant &p_value, bool *r_valid) { if (r_valid) - *r_valid=false; + *r_valid = false; } +Variant Object::callv(const StringName &p_method, const Array &p_args) { -Variant Object::callv(const StringName& p_method,const Array& p_args) { - - if (p_args.size()==0) { + if (p_args.size() == 0) { return call(p_method); } Vector args; args.resize(p_args.size()); - Vector argptrs; + Vector argptrs; argptrs.resize(p_args.size()); - for(int i=0;i_free) { #ifdef DEBUG_ENABLED @@ -843,23 +797,22 @@ Variant Object::call(const StringName& p_name, VARIANT_ARG_DECLARE) { VARIANT_ARGPTRS; - int argc=0; - for(int i=0;iget_type()==Variant::NIL) + int argc = 0; + for (int i = 0; i < VARIANT_ARG_MAX; i++) { + if (argptr[i]->get_type() == Variant::NIL) break; argc++; } Variant::CallError error; - Variant ret = call(p_name,argptr,argc,error); + Variant ret = call(p_name, argptr, argc, error); return ret; #endif - } -void Object::call_multilevel(const StringName& p_name, VARIANT_ARG_DECLARE) { +void Object::call_multilevel(const StringName &p_name, VARIANT_ARG_DECLARE) { #if 0 if (p_name==CoreStringNames::get_singleton()->_free) { #ifdef DEBUG_ENABLED @@ -904,62 +857,58 @@ void Object::call_multilevel(const StringName& p_name, VARIANT_ARG_DECLARE) { VARIANT_ARGPTRS; - int argc=0; - for(int i=0;iget_type()==Variant::NIL) + int argc = 0; + for (int i = 0; i < VARIANT_ARG_MAX; i++) { + if (argptr[i]->get_type() == Variant::NIL) break; argc++; } //Variant::CallError error; - call_multilevel(p_name,argptr,argc); + call_multilevel(p_name, argptr, argc); #endif - } +Variant Object::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) { + r_error.error = Variant::CallError::CALL_OK; -Variant Object::call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error) { - - r_error.error=Variant::CallError::CALL_OK; - - if (p_method==CoreStringNames::get_singleton()->_free) { - //free must be here, before anything, always ready + if (p_method == CoreStringNames::get_singleton()->_free) { +//free must be here, before anything, always ready #ifdef DEBUG_ENABLED - if (p_argcount!=0) { - r_error.argument=0; - r_error.error=Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; + if (p_argcount != 0) { + r_error.argument = 0; + r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; return Variant(); } if (cast_to()) { - r_error.argument=0; - r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD; + r_error.argument = 0; + r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD; ERR_EXPLAIN("Can't 'free' a reference."); ERR_FAIL_V(Variant()); } - if (_lock_index.get()>1) { - r_error.argument=0; - r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD; + if (_lock_index.get() > 1) { + r_error.argument = 0; + r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD; ERR_EXPLAIN("Object is locked and can't be freed."); ERR_FAIL_V(Variant()); - } #endif //must be here, must be before everything, memdelete(this); - r_error.error=Variant::CallError::CALL_OK; + r_error.error = Variant::CallError::CALL_OK; return Variant(); } Variant ret; OBJ_DEBUG_LOCK if (script_instance) { - ret = script_instance->call(p_method,p_args,p_argcount,r_error); + ret = script_instance->call(p_method, p_args, p_argcount, r_error); //force jumptable - switch(r_error.error) { + switch (r_error.error) { case Variant::CallError::CALL_OK: return ret; @@ -969,45 +918,41 @@ Variant Object::call(const StringName& p_method,const Variant** p_args,int p_arg case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: return ret; - case Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL: {} - + case Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL: { + } } } - MethodBind *method=ClassDB::get_method(get_class_name(),p_method); + MethodBind *method = ClassDB::get_method(get_class_name(), p_method); if (method) { - ret=method->call(this,p_args,p_argcount,r_error); + ret = method->call(this, p_args, p_argcount, r_error); } else { - r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD; + r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD; } return ret; } +void Object::notification(int p_notification, bool p_reversed) { -void Object::notification(int p_notification,bool p_reversed) { - - - _notificationv(p_notification,p_reversed); + _notificationv(p_notification, p_reversed); if (script_instance) { script_instance->notification(p_notification); } } -void Object::_changed_callback(Object *p_changed,const char *p_prop) { - - +void Object::_changed_callback(Object *p_changed, const char *p_prop) { } -void Object::add_change_receptor( Object *p_receptor ) { +void Object::add_change_receptor(Object *p_receptor) { change_receptors.insert(p_receptor); } -void Object::remove_change_receptor( Object *p_receptor ) { +void Object::remove_change_receptor(Object *p_receptor) { change_receptors.erase(p_receptor); } @@ -1019,47 +964,45 @@ void Object::property_list_changed_notify() { void Object::cancel_delete() { - _predelete_ok=true; + _predelete_ok = true; } -void Object::set_script(const RefPtr& p_script) { +void Object::set_script(const RefPtr &p_script) { - if (script==p_script) + if (script == p_script) return; if (script_instance) { memdelete(script_instance); - script_instance=NULL; + script_instance = NULL; } - script=p_script; + script = p_script; Ref