diff options
author | Dennis Brakhane <brakhane@gmail.com> | 2016-05-26 23:35:33 +0200 |
---|---|---|
committer | Dennis Brakhane <brakhane@gmail.com> | 2016-06-05 20:00:54 +0200 |
commit | 842057e56f63c7dfc20b60615aa1a70fd5791d59 (patch) | |
tree | 54f58a23c221546588afa8a9c2a538e72488d42f | |
parent | 56348cbbfe82c2f5e2b95875e0687b664cc6e2d8 (diff) |
Move repetitive code to macros
In preparation for the following "allow negative indexing" commit,
replace the repetitive array "set index" and "get index" code with
macros.
no functional changes were made, the resulting machine code is unchanged.
-rw-r--r-- | core/variant_op.cpp | 281 |
1 files changed, 38 insertions, 243 deletions
diff --git a/core/variant_op.cpp b/core/variant_op.cpp index 6065094da7..2f522faf1f 100644 --- a/core/variant_op.cpp +++ b/core/variant_op.cpp @@ -969,6 +969,28 @@ Variant Variant::get_named(const StringName& p_index, bool *r_valid) const { return get(p_index.operator String(),r_valid); } + +#define DEFAULT_OP_ARRAY_CMD(m_name, m_type, skip_test, cmd)\ + case m_name: {\ + skip_test;\ +\ + if (p_index.get_type()==Variant::INT || p_index.get_type()==Variant::REAL) {\ + int index = p_index;\ + m_type *arr=reinterpret_cast<m_type* >(_data._mem);\ +\ + if (index>=0 && index<arr->size()) {\ + valid=true;\ + cmd;\ + }\ + }\ + } break; + +#define DEFAULT_OP_DVECTOR_SET(m_name, dv_type, skip_cond)\ + DEFAULT_OP_ARRAY_CMD(m_name, DVector<dv_type>, if(skip_cond) return;, arr->set(index, p_value);return) + +#define DEFAULT_OP_DVECTOR_GET(m_name, dv_type)\ + DEFAULT_OP_ARRAY_CMD(m_name, const DVector<dv_type>, 0, return arr->get(index)) + void Variant::set(const Variant& p_index, const Variant& p_value, bool *r_valid) { static bool _dummy=false; @@ -1786,145 +1808,14 @@ void Variant::set(const Variant& p_index, const Variant& p_value, bool *r_valid) valid=true; //always valid, i guess? should this really be ok? return; } break; // 20 - case ARRAY: { - - - if (p_index.get_type()==Variant::INT || p_index.get_type()==Variant::REAL) { - - int index = p_index; - Array *arr=reinterpret_cast<Array* >(_data._mem); - - if (index >=0 && index <arr->size()) { - valid=true; - (*arr)[index]=p_value; - return; - } - } - - } break; - case RAW_ARRAY: { - - if (p_value.type!=Variant::REAL && p_value.type!=Variant::INT) - return; - - if (p_index.get_type()==Variant::INT || p_index.get_type()==Variant::REAL) { - - int index = p_index; - DVector<uint8_t> *arr=reinterpret_cast<DVector<uint8_t>* >(_data._mem); - - if (index >=0 && index <arr->size()) { - valid=true; - arr->set(index,p_value); - return; - } - } - - } break; - case INT_ARRAY: { - if (p_value.type!=Variant::REAL && p_value.type!=Variant::INT) - return; - - if (p_index.get_type()==Variant::INT || p_index.get_type()==Variant::REAL) { - - int index = p_index; - DVector<int> *arr=reinterpret_cast<DVector<int>* >(_data._mem); - - if (index >=0 && index <arr->size()) { - valid=true; - arr->set(index,p_value); - return; - } - } - } break; - case REAL_ARRAY: { - - if (p_value.type!=Variant::REAL && p_value.type!=Variant::INT) - return; - - if (p_index.get_type()==Variant::INT || p_index.get_type()==Variant::REAL) { - - int index = p_index; - DVector<real_t> *arr=reinterpret_cast<DVector<real_t>* >(_data._mem); - - if (index >=0 && index <arr->size()) { - valid=true; - arr->set(index,p_value); - return; - } - } - - } break; - case STRING_ARRAY: { - - if (p_value.type!=Variant::STRING) - return; - - if (p_index.get_type()==Variant::INT || p_index.get_type()==Variant::REAL) { - - int index = p_index; - DVector<String> *arr=reinterpret_cast<DVector<String>* >(_data._mem); - - if (index >=0 && index <arr->size()) { - valid=true; - arr->set(index,p_value); - return; - } - } - - } break; //25 - case VECTOR2_ARRAY: { - - if (p_value.type!=Variant::VECTOR2) - return; - - if (p_index.get_type()==Variant::INT || p_index.get_type()==Variant::REAL) { - - int index = p_index; - DVector<Vector2> *arr=reinterpret_cast<DVector<Vector2>* >(_data._mem); - - if (index >=0 && index <arr->size()) { - valid=true; - arr->set(index,p_value); - return; - } - } - - } break; - case VECTOR3_ARRAY: { - - if (p_value.type!=Variant::VECTOR3) - return; - - if (p_index.get_type()==Variant::INT || p_index.get_type()==Variant::REAL) { - - int index = p_index; - DVector<Vector3> *arr=reinterpret_cast<DVector<Vector3>* >(_data._mem); - - if (index >=0 && index <arr->size()) { - valid=true; - arr->set(index,p_value); - return; - } - } - - } break; - case COLOR_ARRAY: { - - if (p_value.type!=Variant::COLOR) - return; - - if (p_index.get_type()==Variant::INT || p_index.get_type()==Variant::REAL) { - - int index = p_index; - DVector<Color> *arr=reinterpret_cast<DVector<Color>* >(_data._mem); - - if (index >=0 && index <arr->size()) { - valid=true; - arr->set(index,p_value); - return; - } - } - } break; + DEFAULT_OP_ARRAY_CMD(ARRAY, Array, ;, (*arr)[index]=p_value;return) + DEFAULT_OP_DVECTOR_SET(RAW_ARRAY, uint8_t, p_value.type != Variant::REAL && p_value.type != Variant::INT) + DEFAULT_OP_DVECTOR_SET(INT_ARRAY, int, p_value.type != Variant::REAL && p_value.type != Variant::INT) + DEFAULT_OP_DVECTOR_SET(REAL_ARRAY, real_t, p_value.type != Variant::REAL && p_value.type != Variant::INT) + DEFAULT_OP_DVECTOR_SET(STRING_ARRAY, String, p_value.type != Variant::STRING) // 25 + DEFAULT_OP_DVECTOR_SET(VECTOR2_ARRAY, Vector2, p_value.type != Variant::VECTOR2) + DEFAULT_OP_DVECTOR_SET(VECTOR3_ARRAY, Vector3, p_value.type != Variant::VECTOR3) + DEFAULT_OP_DVECTOR_SET(COLOR_ARRAY, Color, p_value.type != Variant::COLOR) default: return; } @@ -2489,110 +2380,14 @@ Variant Variant::get(const Variant& p_index, bool *r_valid) const { return *res; } } break; // 20 - case ARRAY: { - if (p_index.get_type()==Variant::INT || p_index.get_type()==Variant::REAL) { - - int index = p_index; - const Array *arr=reinterpret_cast<const Array* >(_data._mem); - - if (index >=0 && index <arr->size()) { - valid=true; - return (*arr)[index]; - } - } - - } break; - case RAW_ARRAY: { - if (p_index.get_type()==Variant::INT || p_index.get_type()==Variant::REAL) { - - int index = p_index; - const DVector<uint8_t> *arr=reinterpret_cast<const DVector<uint8_t>* >(_data._mem); - - if (index >=0 && index <arr->size()) { - valid=true; - return arr->get(index); - } - } - - } break; - case INT_ARRAY: { - if (p_index.get_type()==Variant::INT || p_index.get_type()==Variant::REAL) { - - int index = p_index; - const DVector<int> *arr=reinterpret_cast<const DVector<int>* >(_data._mem); - - if (index >=0 && index <arr->size()) { - valid=true; - return arr->get(index); - } - } - } break; - case REAL_ARRAY: { - - if (p_index.get_type()==Variant::INT || p_index.get_type()==Variant::REAL) { - - int index = p_index; - const DVector<real_t> *arr=reinterpret_cast<const DVector<real_t>* >(_data._mem); - - if (index >=0 && index <arr->size()) { - valid=true; - return arr->get(index); - } - } - - } break; - case STRING_ARRAY: { - if (p_index.get_type()==Variant::INT || p_index.get_type()==Variant::REAL) { - - int index = p_index; - const DVector<String> *arr=reinterpret_cast<const DVector<String>* >(_data._mem); - - if (index >=0 && index <arr->size()) { - valid=true; - return arr->get(index); - } - } - - } break; //25 - case VECTOR2_ARRAY: { - if (p_index.get_type()==Variant::INT || p_index.get_type()==Variant::REAL) { - - int index = p_index; - const DVector<Vector2> *arr=reinterpret_cast<const DVector<Vector2>* >(_data._mem); - - if (index >=0 && index <arr->size()) { - valid=true; - return arr->get(index); - } - } - - } break; - case VECTOR3_ARRAY: { - if (p_index.get_type()==Variant::INT || p_index.get_type()==Variant::REAL) { - - int index = p_index; - const DVector<Vector3> *arr=reinterpret_cast<const DVector<Vector3>* >(_data._mem); - - if (index >=0 && index <arr->size()) { - valid=true; - return arr->get(index); - } - } - - } break; - case COLOR_ARRAY: { - - if (p_index.get_type()==Variant::INT || p_index.get_type()==Variant::REAL) { - - int index = p_index; - const DVector<Color> *arr=reinterpret_cast<const DVector<Color>* >(_data._mem); - - if (index >=0 && index <arr->size()) { - valid=true; - return arr->get(index); - } - } - } break; + DEFAULT_OP_ARRAY_CMD(ARRAY, const Array, 0, return (*arr)[index]) + DEFAULT_OP_DVECTOR_GET(RAW_ARRAY, uint8_t) + DEFAULT_OP_DVECTOR_GET(INT_ARRAY, int) + DEFAULT_OP_DVECTOR_GET(REAL_ARRAY, real_t) + DEFAULT_OP_DVECTOR_GET(STRING_ARRAY, String) + DEFAULT_OP_DVECTOR_GET(VECTOR2_ARRAY, Vector2) + DEFAULT_OP_DVECTOR_GET(VECTOR3_ARRAY, Vector3) + DEFAULT_OP_DVECTOR_GET(COLOR_ARRAY, Color) default: return Variant(); } |