diff --git a/src/projectM-engine/BuiltinFuncs.cpp b/src/projectM-engine/BuiltinFuncs.cpp index ca302b844..19f8ddbff 100644 --- a/src/projectM-engine/BuiltinFuncs.cpp +++ b/src/projectM-engine/BuiltinFuncs.cpp @@ -127,7 +127,7 @@ int BuiltinFuncs::init_builtin_func_db() { int retval; builtin_func_tree = - SplayTree::create_splaytree((int (*)(void*,void*))compare_string, (void*(*)(void*))copy_string, (void(*)(void*))free_string); + SplayTree::create_splaytree((int (*)(void*,void*))SplayKeyFunctions::compare_string, (void*(*)(void*))SplayKeyFunctions::copy_string, (void(*)(void*))SplayKeyFunctions::free_string); if (builtin_func_tree == NULL) return PROJECTM_OUTOFMEM_ERROR; diff --git a/src/projectM-engine/BuiltinParams.cpp b/src/projectM-engine/BuiltinParams.cpp index e502b254d..dd74e28ca 100644 --- a/src/projectM-engine/BuiltinParams.cpp +++ b/src/projectM-engine/BuiltinParams.cpp @@ -82,7 +82,8 @@ int BuiltinParams::load_builtin_param_float(char * name, void * engine_val, void Generally, do this on projectm exit */ int BuiltinParams::destroy_builtin_param_db() { - builtin_param_tree->splay_traverse((void (*)(void*))delete); + builtin_param_tree->traverse + >(); delete builtin_param_tree; builtin_param_tree = NULL; return PROJECTM_SUCCESS; @@ -184,7 +185,7 @@ int BuiltinParams::insert_builtin_param( Param *param ) { int BuiltinParams::init_builtin_param_db(const PresetInputs & presetInputs, PresetOutputs & presetOutputs) { /* Create the builtin parameter splay tree (go Sleator...) */ - if ((this->builtin_param_tree = SplayTree::create_splaytree((int (*)(void*,void*))compare_string,(void* (*)(void*)) copy_string, (void (*)(void*))free_string)) == NULL) { + if ((this->builtin_param_tree = SplayTree::create_splaytree((int (*)(void*,void*))SplayKeyFunctions::compare_string,(void* (*)(void*)) SplayKeyFunctions::copy_string, (void (*)(void*))SplayKeyFunctions::free_string)) == NULL) { if (BUILTIN_PARAMS_DEBUG) printf("init_builtin_param_db: failed to initialize database (FATAL)\n"); return PROJECTM_OUTOFMEM_ERROR; } diff --git a/src/projectM-engine/CustomShape.cpp b/src/projectM-engine/CustomShape.cpp index e9d5f8545..81b8168c5 100755 --- a/src/projectM-engine/CustomShape.cpp +++ b/src/projectM-engine/CustomShape.cpp @@ -35,6 +35,7 @@ #include "Preset.hpp" #include "SplayTree.hpp" #include "ParamUtils.hpp" +#include "InitCondUtils.hpp" #include "wipemalloc.h" @@ -199,20 +200,18 @@ CustomShape::~CustomShape() { return; abort(); - per_frame_eqn_tree->traverse(); - init_cond_tree->trave - param_tree; - per_frame_init_eqn_tree; + per_frame_eqn_tree->traverse >(); + init_cond_tree->traverse >(); + param_tree->traverse > (); + per_frame_init_eqn_tree->traverse > (); return; } - - void CustomShape::load_custom_shape_init() { load_unspecified_init_conds_shape(); - } +} void CustomShape::eval_custom_shape_init_conds() { per_frame_init_eqn_tree->splay_traverse((void (*)(void*))eval_init_cond_helper ); @@ -220,91 +219,8 @@ void CustomShape::eval_custom_shape_init_conds() { void CustomShape::load_unspecified_init_conds_shape() { - param_tree->splay_traverse((void (*)(void*))load_unspec_init_cond_shape_helper); - - } - - - -Param * CustomShape::findParam(char * name, bool create_flag) { - - assert(name); - - Param * param = NULL; - - - /* First look in the builtin database */ - param = (Param *)splay_find(name); - - - if (((param = (Param *)param_tree->splay_find(name)) == NULL) && (create_flag == TRUE)) { - - /* Check if string is valid */ - if (!param->is_valid_param_string(name)) - return NULL; - - /* Now, create the user defined parameter given the passed name */ - if ((param = Param::create_user_param(name)) == NULL) - return NULL; - - /* Finally, insert the new parameter into this preset's proper splaytree */ - if (splay_insert(param, param->name) < 0) { - delete param; - return NULL; - } - - } - - /* Return the found (or created) parameter. Note that this could be null */ - return param; + InitCondUtils::LoadUnspecInitCond fun(*this->init_cond_tree, *this->per_frame_init_eqn_tree); -} - -/// @bug BROKEN: FIX ME (carm) -void CustomShape::load_unspec_init_cond_shape() { -abort(); -#if 0 - InitCond * init_cond; - CValue init_val; - - /* Don't count read only parameters as initial conditions */ - if (flags & P_FLAG_READONLY) - return; - if (flags & P_FLAG_QVAR) - return; - if (flags & P_FLAG_TVAR) - return; - if (flags & P_FLAG_USERDEF) - return; - - /* If initial condition was not defined by the preset file, force a default one - with the following code */ - if ((init_cond =(InitCond*)CustomShape::interface_shape->init_cond_tree->splay_find(name)) == NULL) { - - /* Make sure initial condition does not exist in the set of per frame initial equations */ - if ((init_cond = (InitCond*)CustomShape::interface_shape->per_frame_init_eqn_tree->splay_find(name)) != NULL) - return; - - if (type == P_TYPE_BOOL) - init_val.bool_val = 0; - - else if (type == P_TYPE_INT) - init_val.int_val = *(int*)engine_val; - - else if (type == P_TYPE_DOUBLE) - init_val.float_val = *(float*)engine_val; - - //printf("%s\n", param->name); - /* Create new initial condition */ - if ((init_cond = new InitCond(this, init_val)) == NULL) - return; - - /* Insert the initial condition into this presets tree */ - if (CustomShape::interface_shape->init_cond_tree->splay_insert(init_cond, init_cond->param->name) < 0) { - delete init_cond; - return; - } - - } -#endif + param_tree->traverse(fun); + } diff --git a/src/projectM-engine/CustomWave.cpp b/src/projectM-engine/CustomWave.cpp index 9d38986a7..38325f3f8 100755 --- a/src/projectM-engine/CustomWave.cpp +++ b/src/projectM-engine/CustomWave.cpp @@ -38,6 +38,7 @@ #include "Preset.hpp" #include "SplayTree.hpp" #include "ParamUtils.hpp" +#include "InitCondUtils.hpp" #include "wipemalloc.h" #define MAX_SAMPLE_SIZE 4096 @@ -550,59 +551,11 @@ void CustomWave::evalPerPointEqns() { void CustomWave::load_unspecified_init_conds() { - LoadUnspecInitCond fun(*this); + InitCondUtils::LoadUnspecInitCond fun(*this->init_cond_tree, *this->per_frame_init_eqn_tree); param_tree->traverse(fun); } -CustomWave::LoadUnspecInitCond::LoadUnspecInitCond(CustomWave & customWave):m_customWave(customWave) {} - -void CustomWave::LoadUnspecInitCond::operator() (Param * param) { - InitCond * init_cond; - CValue init_val; - - /* Don't count these parameters as initial conditions */ - if (param->flags & P_FLAG_READONLY) - return; - if (param->flags & P_FLAG_QVAR) - return; - if (param->flags & P_FLAG_TVAR) - return; - if (param->flags & P_FLAG_USERDEF) - return; - - /* If initial condition was not defined by the preset file, force a default one - with the following code */ - if ((init_cond = (InitCond*)m_customWave.init_cond_tree->splay_find(param->name)) == NULL) { - - /* Make sure initial condition does not exist in the set of per frame initial equations */ - if ((init_cond = (InitCond*)m_customWave.per_frame_init_eqn_tree->splay_find(param->name)) != NULL) - return; - - if (param->type == P_TYPE_BOOL) - init_val.bool_val = 0; - - else if (param->type == P_TYPE_INT) - init_val.int_val = *(int*)param->engine_val; - - else if (param->type == P_TYPE_DOUBLE) - init_val.float_val = *(float*)param->engine_val; - - //printf("%s\n", param->name); - /* Create new initial condition */ - if ((init_cond = new InitCond(param, init_val)) == NULL) - return; - - /* Insert the initial condition into this presets tree */ - if (m_customWave.init_cond_tree->splay_insert(init_cond, init_cond->param->name) < 0) { - delete init_cond; - return; - } - } - - -} - diff --git a/src/projectM-engine/CustomWave.h b/src/projectM-engine/CustomWave.h index 4f65e4872..a97d6dbf3 100755 --- a/src/projectM-engine/CustomWave.h +++ b/src/projectM-engine/CustomWave.h @@ -61,10 +61,6 @@ public: /** Destructor is necessary so we can free the per point matrices **/ ~CustomWave(); - /** Search for parameter 'name' in 'database', if create_flag is true, then generate the parameter - and insert it into 'database' */ - Param * findParam(char * name, bool create_flag); - /* Numerical id */ int id; int per_frame_count; @@ -131,7 +127,7 @@ public: /* Per point equation array */ GenExpr * per_point_eqn_array[NUM_POINT_OPS]; - void reset_per_point_eqn_array(CustomWave *custom_wave ); + void reset_per_point_eqn_array(CustomWave *custom_wave); int add_per_point_eqn(char * name, GenExpr * gen_expr); void evalCustomWaveInitConditions(Preset *preset); @@ -148,17 +144,8 @@ public: void destroy_init_cond_tree(SplayTree * tree); void evalPerPointEqn(PerPointEqn * per_point_eqn); - class LoadUnspecInitCond { - public: - LoadUnspecInitCond(CustomWave & customWave) ; - - void operator()(Param * param); - - private: - CustomWave & m_customWave; }; - }; /** Splaytree traversal helpers */ inline void free_custom_wave_helper( void *custom_wave ) { diff --git a/src/projectM-engine/InitCondUtils.hpp b/src/projectM-engine/InitCondUtils.hpp new file mode 100644 index 000000000..e18a08b15 --- /dev/null +++ b/src/projectM-engine/InitCondUtils.hpp @@ -0,0 +1,68 @@ +#ifndef _INIT_COND_UTILS_HPP +#define _INIT_COND_UTILS_HPP +#include "SplayTree.hpp" +#include "InitCond.h" + +namespace InitCondUtils { +class LoadUnspecInitCond { + public: + + LoadUnspecInitCond(SplayTree & initCondTree, SplayTree & perFrameInitEqnTree): + m_initCondTree(initCondTree), m_perFrameInitEqnTree(perFrameInitEqnTree) {} + + void operator()(Param * param); + + private: + SplayTree & m_initCondTree; + SplayTree & m_perFrameInitEqnTree; +}; + + +inline void LoadUnspecInitCond::operator() (Param * param) { + + InitCond * init_cond; + CValue init_val; + + /* Don't count these parameters as initial conditions */ + if (param->flags & P_FLAG_READONLY) + return; + if (param->flags & P_FLAG_QVAR) + return; + if (param->flags & P_FLAG_TVAR) + return; + if (param->flags & P_FLAG_USERDEF) + return; + + /* If initial condition was not defined by the preset file, force a default one + with the following code */ + if ((init_cond = m_initCondTree.splay_find(param->name)) == NULL) { + + /* Make sure initial condition does not exist in the set of per frame initial equations */ + if ((init_cond = m_perFrameInitEqnTree.splay_find(param->name)) != NULL) + return; + + if (param->type == P_TYPE_BOOL) + init_val.bool_val = 0; + + else if (param->type == P_TYPE_INT) + init_val.int_val = *(int*)param->engine_val; + + else if (param->type == P_TYPE_DOUBLE) + init_val.float_val = *(float*)param->engine_val; + + //printf("%s\n", param->name); + /* Create new initial condition */ + if ((init_cond = new InitCond(param, init_val)) == NULL) + return; + + /* Insert the initial condition into this presets tree */ + if (m_initCondTree.splay_insert(init_cond, init_cond->param->name) < 0) { + delete init_cond; + return; + } + } + + +} +} +#endif diff --git a/src/projectM-engine/ParamUtils.hpp b/src/projectM-engine/ParamUtils.hpp index 32b0d2ec4..73ca086a0 100644 --- a/src/projectM-engine/ParamUtils.hpp +++ b/src/projectM-engine/ParamUtils.hpp @@ -20,6 +20,7 @@ public: } static const int AUTO_CREATE = 1; + static const int NO_CREATE = 0; template static Param * find(char * name, SplayTree * paramTree) { diff --git a/src/projectM-engine/Parser.cpp b/src/projectM-engine/Parser.cpp index 039c9a858..7c1db6b15 100755 --- a/src/projectM-engine/Parser.cpp +++ b/src/projectM-engine/Parser.cpp @@ -758,18 +758,17 @@ GenExpr * Parser::parse_gen_expr ( FILE * fs, TreeExpr * tree_expr, Preset * pre /* CASE 5: custom wave variable */ if (current_wave != NULL) { - if ((param = current_wave->findParam(string, FALSE)) == NULL) { + if ((param = ParamUtils::find(string, current_wave->param_tree)) == NULL) { if ((param = preset->builtinParams.find_builtin_param(string)) == NULL) - if ((param = current_wave->findParam(string, TRUE)) == NULL) { + if ((param = ParamUtils::find(string, current_wave->param_tree)) == NULL) { delete tree_expr; return NULL; } - } if (PARSE_DEBUG) { DWRITE("parse_gen_expr: custom wave parameter (name = %s)... \n", param->name); - + } /* Convert parameter to an expression */ @@ -782,12 +781,12 @@ GenExpr * Parser::parse_gen_expr ( FILE * fs, TreeExpr * tree_expr, Preset * pre /* Parse the rest of the line */ return parse_infix_op(fs, token, insert_gen_expr(gen_expr, &tree_expr), preset); - + } /* CASE 6: regular parameter. Will be created if necessary and the string has no invalid characters */ if ((param = Param::find_param(string, preset, P_CREATE)) != NULL) { - + if (PARSE_DEBUG) { DWRITE("parse_gen_expr: parameter (name = %s)...\n", param->name); diff --git a/src/projectM-engine/SplayTree.hpp b/src/projectM-engine/SplayTree.hpp index b777549dd..a6814bdfe 100755 --- a/src/projectM-engine/SplayTree.hpp +++ b/src/projectM-engine/SplayTree.hpp @@ -53,7 +53,7 @@ public: static SplayTree *create_splaytree(int (*compare)(void*,void*), void * (*copy_key)(void*), void (*free_key)(void*)); ~SplayTree(); - void *splay_find(void * key); + Data *splay_find(void * key); int splay_insert(Data * data, void * key); int splay_insert_node( SplayNode *node ); int splay_insert_link(void * alias_key, void * orig_key); @@ -119,7 +119,7 @@ SplayTree * SplayTree::create_splaytree(int (*compare)(void *,void*) splaytree->compare = compare; splaytree->copy_key = copy_key; splaytree->free_key = free_key; - + /* Return instantiated splay tree */ return splaytree; } @@ -235,7 +235,7 @@ void SplayTree::traverseRec (Fun & fun, SplayNode * splaynode) { } /* Find the node corresponding to the given key in splaytree, return its data pointer */ template -void * SplayTree::splay_find(void * key) { +Data * SplayTree::splay_find(void * key) { SplayNode * splaynode; int match_type; @@ -427,7 +427,7 @@ int SplayTree::splay_insert_link(void * alias_key, void * orig_key) { return SPLAYTREE_FAILURE; /* Create a new splay node of symbolic link type */ - if ((splaynode = new SplayNode(SYMBOLIC_NODE_TYPE, (key_clone = copy_key(alias_key)), data_node, this)) == NULL) { + if ((splaynode = new SplayNode >(SYMBOLIC_NODE_TYPE, (key_clone = copy_key(alias_key)), data_node, free_key)) == NULL) { free_key(key_clone); return PROJECTM_OUTOFMEM_ERROR; }