From 79591c49b2a83553651dcc449b25d60230cdea29 Mon Sep 17 00:00:00 2001 From: w1z7ard Date: Sun, 24 Jun 2007 01:23:47 +0000 Subject: [PATCH] endless trudge of refactoring git-svn-id: https://projectm.svn.sourceforge.net/svnroot/projectm/personal/carm/dev-1.0@218 6778bc44-b910-0410-a7a0-be141de4315d --- src/projectM-engine/BuiltinParams.cpp | 5 +- src/projectM-engine/BuiltinParams.hpp | 9 ++ src/projectM-engine/CustomShape.cpp | 127 ++++++------------ src/projectM-engine/CustomWave.cpp | 178 +++++++------------------- src/projectM-engine/CustomWave.h | 11 ++ src/projectM-engine/Param.cpp | 46 ------- src/projectM-engine/Param.h | 1 - src/projectM-engine/ParamUtils.hpp | 127 +++++++++++++----- src/projectM-engine/Preset.cpp | 31 +++-- src/projectM-engine/SplayTree.hpp | 32 ++++- src/projectM-engine/compare.h | 7 +- 11 files changed, 256 insertions(+), 318 deletions(-) diff --git a/src/projectM-engine/BuiltinParams.cpp b/src/projectM-engine/BuiltinParams.cpp index 0978753cf..e502b254d 100644 --- a/src/projectM-engine/BuiltinParams.cpp +++ b/src/projectM-engine/BuiltinParams.cpp @@ -82,7 +82,7 @@ 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*))free_param_helper); + builtin_param_tree->splay_traverse((void (*)(void*))delete); delete builtin_param_tree; builtin_param_tree = NULL; return PROJECTM_SUCCESS; @@ -207,9 +207,6 @@ int BuiltinParams::init_builtin_param_db(const PresetInputs & presetInputs, Pres } -void BuiltinParams::traverse(void (*func_ptr)(void*)) { - builtin_param_tree->splay_traverse(func_ptr); -} /* Loads all builtin parameters, limits are also defined here */ int BuiltinParams::load_all_builtin_param(const PresetInputs & presetInputs, PresetOutputs & presetOutputs) { diff --git a/src/projectM-engine/BuiltinParams.hpp b/src/projectM-engine/BuiltinParams.hpp index b19409cc3..08ebbf9e4 100644 --- a/src/projectM-engine/BuiltinParams.hpp +++ b/src/projectM-engine/BuiltinParams.hpp @@ -62,6 +62,15 @@ public: int init_val, char *alt_name ); int insert_builtin_param( Param *param ); + template + void traverse(Fun & fun) { + builtin_param_tree->traverse(fun); + } + +//void BuiltinParams::traverse(void (*func_ptr)(void*)) { +// builtin_param_tree->splay_traverse(func_ptr); +//} + void traverse(void (*func_ptr)(void*)); private: diff --git a/src/projectM-engine/CustomShape.cpp b/src/projectM-engine/CustomShape.cpp index 36f50cadb..e9d5f8545 100755 --- a/src/projectM-engine/CustomShape.cpp +++ b/src/projectM-engine/CustomShape.cpp @@ -34,7 +34,7 @@ #include "PerFrameEqn.h" #include "Preset.hpp" #include "SplayTree.hpp" - +#include "ParamUtils.hpp" #include "wipemalloc.h" @@ -42,10 +42,6 @@ void eval_custom_shape_init_conds(CustomShape * custom_shape); void load_unspec_init_cond_shape(Param * param); -void destroy_param_db_tree_shape(SplayTree * tree); -void destroy_per_frame_eqn_tree_shape(SplayTree * tree); -void destroy_per_frame_init_eqn_tree_shape(SplayTree * tree); -void destroy_init_cond_tree_shape(SplayTree * tree); CustomShape::CustomShape( int id ) { @@ -58,196 +54,155 @@ CustomShape::CustomShape( int id ) { /* Initialize tree data structures */ this->param_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); this->per_frame_eqn_tree = - SplayTree::create_splaytree((int (*)(void*, void*))compare_int, (void* (*)(void*))copy_int,(void (*)(void*)) free_int); + SplayTree::create_splaytree((int (*)(void*, void*))SplayKeyFunctions::compare_int, (void* (*)(void*))SplayKeyFunctions::copy_int,(void (*)(void*))SplayKeyFunctions::free_int); this->init_cond_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); this->per_frame_init_eqn_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); /* Start: Load custom shape parameters */ param = Param::new_param_float("r", P_FLAG_NONE, &this->r, NULL, 1.0, 0.0, 0.5); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("g", P_FLAG_NONE, &this->g, NULL, 1.0, 0.0, .5); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("b", P_FLAG_NONE, &this->b, NULL, 1.0, 0.0, .5); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("a", P_FLAG_NONE, &this->a, NULL, 1.0, 0.0, .5); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("border_r", P_FLAG_NONE, &this->border_r, NULL, 1.0, 0.0, .5); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("border_g", P_FLAG_NONE, &this->border_g, NULL, 1.0, 0.0, .5); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("border_b", P_FLAG_NONE, &this->border_b, NULL, 1.0, 0.0, .5); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("border_a", P_FLAG_NONE, &this->border_a, NULL, 1.0, 0.0, .5); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("r2", P_FLAG_NONE, &this->r2, NULL, 1.0, 0.0, .5); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("g2", P_FLAG_NONE, &this->g2, NULL, 1.0, 0.0, .5); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("b2", P_FLAG_NONE, &this->b2, NULL, 1.0, 0.0, .5); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("a2", P_FLAG_NONE, &this->a2, NULL, 1.0, 0.0, .5); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("x", P_FLAG_NONE, &this->x, NULL, 1.0, 0.0, .5); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("y", P_FLAG_NONE, &this->y, NULL, 1.0, 0.0, .5); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_bool("thickOutline", P_FLAG_NONE, &this->thickOutline, 1, 0, 0); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_bool("enabled", P_FLAG_NONE, &this->enabled, 1, 0, 0); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_int("sides", P_FLAG_NONE, &this->sides, 100, 3, 3); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_bool("additive", P_FLAG_NONE, &this->additive, 1, 0, 0); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_bool("textured", P_FLAG_NONE, &this->textured, 1, 0, 0); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("rad", P_FLAG_NONE, &this->radius, NULL, MAX_DOUBLE_SIZE, 0, 0.0); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("ang", P_FLAG_NONE, &this->ang, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("tex_zoom", P_FLAG_NONE, &this->tex_zoom, NULL, MAX_DOUBLE_SIZE, .00000000001, 0.0); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("tex_ang", P_FLAG_NONE, &this->tex_ang, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("t1", P_FLAG_TVAR, &this->t1, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("t2", P_FLAG_TVAR, &this->t2, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("t3", P_FLAG_TVAR, &this->t3, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("t4", P_FLAG_TVAR, &this->t4, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("t5", P_FLAG_TVAR, &this->t5, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("t6", P_FLAG_TVAR, &this->t6, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("t7", P_FLAG_TVAR, &this->t7, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } param = Param::new_param_float("t8", P_FLAG_TVAR, &this->t8, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0); - if ( param_tree->insert_param( param ) < 0 ) { + if ( ParamUtils::insert(param, this->param_tree) < 0 ) { DWRITE( "%s\n", "failed to insert param!" ); } } -void destroy_per_frame_init_eqn_tree_shape(SplayTree * tree) { - - if (!tree) - return; - - tree->splay_traverse((void (*)(void*))free_init_cond_helper); - delete tree; - -} - -void destroy_init_cond_tree_shape(SplayTree * tree) { - - if (!tree) - return; - - tree->splay_traverse((void (*)(void*))free_init_cond_helper); - delete tree; - -} - -void destroy_per_frame_eqn_tree_shape(SplayTree * tree) { - - - if (!tree) - return; - - tree->splay_traverse((void (*)(void*))free_per_frame_eqn_helper); - delete tree; - -} - - -void destroy_param_db_tree_shape(SplayTree * tree) { - - if (!tree) - return; - - tree->splay_traverse((void (*)(void*))free_param_helper); - delete tree; - -} - /* Frees a custom shape form object */ CustomShape::~CustomShape() { if (param_tree == NULL) return; - destroy_per_frame_eqn_tree_shape(per_frame_eqn_tree); - destroy_init_cond_tree_shape(init_cond_tree); - destroy_param_db_tree_shape(param_tree); - destroy_per_frame_init_eqn_tree_shape(per_frame_init_eqn_tree); + abort(); + per_frame_eqn_tree->traverse(); + init_cond_tree->trave + param_tree; + per_frame_init_eqn_tree; return; diff --git a/src/projectM-engine/CustomWave.cpp b/src/projectM-engine/CustomWave.cpp index b42997076..9d38986a7 100755 --- a/src/projectM-engine/CustomWave.cpp +++ b/src/projectM-engine/CustomWave.cpp @@ -37,7 +37,7 @@ #include "PerPointEqn.h" #include "Preset.hpp" #include "SplayTree.hpp" - +#include "ParamUtils.hpp" #include "wipemalloc.h" #define MAX_SAMPLE_SIZE 4096 @@ -79,32 +79,32 @@ CustomWave::CustomWave(int id):id(id) /* Initialize tree data structures */ if ((this->param_tree = - SplayTree::create_splaytree((int (*)(void*, void*))compare_string, (void* (*)(void*))copy_string, (void (*)(void*))free_string)) == NULL) { + SplayTree::create_splaytree((int (*)(void*, void*))SplayKeyFunctions::compare_string, (void* (*)(void*))SplayKeyFunctions::copy_string, (void (*)(void*))SplayKeyFunctions::free_string)) == NULL) { delete(this); abort(); } if ((this->per_point_eqn_tree = - SplayTree::create_splaytree((int (*)(void*, void*))compare_int, (void* (*)(void*))copy_int, (void (*)(void*))free_int)) == NULL) { + SplayTree::create_splaytree((int (*)(void*, void*))SplayKeyFunctions::compare_int, (void* (*)(void*))SplayKeyFunctions::copy_int, (void (*)(void*))SplayKeyFunctions::free_int)) == NULL) { delete(this); abort(); } if ((this->per_frame_eqn_tree = - SplayTree::create_splaytree((int (*)(void*, void*))compare_int,(void* (*)(void*)) copy_int,(void (*)(void*)) free_int)) == NULL) { + SplayTree::create_splaytree((int (*)(void*, void*))SplayKeyFunctions::compare_int,(void* (*)(void*)) SplayKeyFunctions::copy_int,(void (*)(void*)) SplayKeyFunctions::free_int)) == NULL) { delete(this); abort(); } if ((this->init_cond_tree = - SplayTree::create_splaytree((int (*)(void*, void*))compare_string, (void*(*)(void*))copy_string,(void (*)(void*)) free_string)) == NULL) { + SplayTree::create_splaytree((int (*)(void*, void*))SplayKeyFunctions::compare_string, (void*(*)(void*))SplayKeyFunctions::copy_string,(void (*)(void*)) SplayKeyFunctions::free_string)) == NULL) { delete(this); /// @bug make exception abort(); } if ((this->per_frame_init_eqn_tree = - SplayTree::create_splaytree((int (*)(void*, void*))compare_string, (void*(*)(void*))copy_string, (void (*)(void*))free_string)) == NULL) { + SplayTree::create_splaytree((int (*)(void*, void*))SplayKeyFunctions::compare_string, (void*(*)(void*))SplayKeyFunctions::copy_string, (void (*)(void*))SplayKeyFunctions::free_string)) == NULL) { delete(this); /// @bug make exception abort(); @@ -119,7 +119,8 @@ CustomWave::CustomWave(int id):id(id) abort(); } - if (this->param_tree->insert_param(param) < 0) { + + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); /// @bug make exception abort(); @@ -132,11 +133,10 @@ CustomWave::CustomWave(int id):id(id) } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, param_tree) < 0) { delete(this); /// @bug make exception abort(); - } if ((param = Param::new_param_float("b", P_FLAG_DONT_FREE_MATRIX | P_FLAG_PER_POINT, &this->b, this->b_mesh, 1.0, 0.0, .5)) == NULL){ @@ -146,7 +146,7 @@ CustomWave::CustomWave(int id):id(id) } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); /// @bug make exception abort(); @@ -161,7 +161,7 @@ CustomWave::CustomWave(int id):id(id) } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); /// @bug make exception abort(); @@ -176,7 +176,7 @@ CustomWave::CustomWave(int id):id(id) } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); /// @bug make exception abort(); @@ -189,7 +189,7 @@ CustomWave::CustomWave(int id):id(id) abort(); } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); /// @bug make exception @@ -205,7 +205,7 @@ CustomWave::CustomWave(int id):id(id) } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); /// @bug make exception @@ -221,7 +221,7 @@ CustomWave::CustomWave(int id):id(id) } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); /// @bug make exception abort(); @@ -237,7 +237,7 @@ CustomWave::CustomWave(int id):id(id) } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); /// @bug make exception abort(); @@ -252,7 +252,7 @@ CustomWave::CustomWave(int id):id(id) } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); /// @bug make exception abort(); @@ -265,7 +265,7 @@ CustomWave::CustomWave(int id):id(id) abort(); } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); abort(); } @@ -275,7 +275,7 @@ CustomWave::CustomWave(int id):id(id) abort(); } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); abort(); } @@ -285,7 +285,7 @@ CustomWave::CustomWave(int id):id(id) abort(); } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); abort(); } @@ -296,7 +296,7 @@ CustomWave::CustomWave(int id):id(id) abort(); } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { printf("failed to insert sample\n"); delete(this); abort(); @@ -307,7 +307,7 @@ CustomWave::CustomWave(int id):id(id) abort(); } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); abort(); } @@ -317,7 +317,7 @@ CustomWave::CustomWave(int id):id(id) abort(); } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); abort(); } @@ -327,7 +327,7 @@ CustomWave::CustomWave(int id):id(id) abort(); } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); abort(); } @@ -337,7 +337,7 @@ CustomWave::CustomWave(int id):id(id) abort(); } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); abort(); } @@ -347,7 +347,7 @@ CustomWave::CustomWave(int id):id(id) abort(); } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); abort(); } @@ -357,7 +357,7 @@ CustomWave::CustomWave(int id):id(id) abort(); } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); abort(); } @@ -367,7 +367,7 @@ CustomWave::CustomWave(int id):id(id) abort(); } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); abort(); } @@ -376,7 +376,7 @@ CustomWave::CustomWave(int id):id(id) abort(); } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); abort(); } @@ -385,7 +385,7 @@ CustomWave::CustomWave(int id):id(id) abort(); } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); abort(); } @@ -394,7 +394,7 @@ CustomWave::CustomWave(int id):id(id) abort(); } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); abort(); } @@ -403,7 +403,7 @@ CustomWave::CustomWave(int id):id(id) abort(); } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); abort(); } @@ -413,7 +413,7 @@ CustomWave::CustomWave(int id):id(id) abort(); } - if (this->param_tree->insert_param(param) < 0) { + if (ParamUtils::insert(param, this->param_tree) < 0) { delete(this); abort(); } @@ -471,49 +471,6 @@ void CustomWave::destroy_per_frame_init_eqn_tree() { } -void CustomWave::destroy_per_point_eqn_tree(SplayTree * tree) { - - if (!tree) - return; - - tree->splay_traverse((void (*)(void*))free_per_point_eqn_helper); - delete tree; - -} - -void CustomWave::destroy_init_cond_tree(SplayTree * tree) { - - if (!tree) - return; - - tree->splay_traverse((void (*)(void*))free_init_cond_helper); - delete tree; - -} - -void CustomWave::destroy_per_frame_eqn_tree(SplayTree * tree) { - - - if (!tree) - return; - - tree->splay_traverse((void (*)(void*))free_per_frame_eqn_helper); - delete tree; - -} - - -void CustomWave::destroy_param_db_tree(SplayTree * tree) { - - if (!tree) - return; - - tree->splay_traverse((void (*)(void*))free_param_helper); - delete tree; - -} - - int CustomWave::add_per_point_eqn(char * name, GenExpr * gen_expr) { @@ -531,7 +488,7 @@ int CustomWave::add_per_point_eqn(char * name, GenExpr * gen_expr) { /* Search for the parameter so we know what matrix the per pixel equation is referencing */ - if ((param = param_tree->find_param_db(name, TRUE)) == NULL) { + if ((param = ParamUtils::find(name,param_tree)) == NULL) { if (CUSTOM_WAVE_DEBUG) printf("add_per_point_eqn: failed to allocate a new parameter!\n"); return PROJECTM_FAILURE; @@ -592,14 +549,15 @@ void CustomWave::evalPerPointEqns() { } void CustomWave::load_unspecified_init_conds() { -// interface_wave = this; - param_tree->traverse(this->load_unspec_init_cond); -// interface_wave = NULL; + + LoadUnspecInitCond fun(*this); + param_tree->traverse(fun); + } +CustomWave::LoadUnspecInitCond::LoadUnspecInitCond(CustomWave & customWave):m_customWave(customWave) {} -void CustomWave::load_unspec_init_cond(Param * param) { - +void CustomWave::LoadUnspecInitCond::operator() (Param * param) { InitCond * init_cond; @@ -617,28 +575,28 @@ void CustomWave::load_unspec_init_cond(Param * param) { /* If initial condition was not defined by the preset file, force a default one with the following code */ - if ((init_cond = (InitCond*)init_cond_tree>splay_find(param->name)) == NULL) { + 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*)per_frame_init_eqn_tree->splay_find(param->name)) != NULL) + if ((init_cond = (InitCond*)m_customWave.per_frame_init_eqn_tree->splay_find(param->name)) != NULL) return; - if (type == P_TYPE_BOOL) + if (param->type == P_TYPE_BOOL) init_val.bool_val = 0; - else if (type == P_TYPE_INT) + else if (param->type == P_TYPE_INT) init_val.int_val = *(int*)param->engine_val; - else if (type == P_TYPE_DOUBLE) + 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(this, init_val)) == NULL) + if ((init_cond = new InitCond(param, init_val)) == NULL) return; /* Insert the initial condition into this presets tree */ - if (init_cond_tree->splay_insert(init_cond, init_cond->param->name) < 0) { + if (m_customWave.init_cond_tree->splay_insert(init_cond, init_cond->param->name) < 0) { delete init_cond; return; } @@ -647,46 +605,4 @@ void CustomWave::load_unspec_init_cond(Param * param) { } -/* Inserts a parameter into the builtin database */ -int SplayTree::insert_param(Param * param) { - - if (param == NULL) - return PROJECTM_FAILURE; - - return splay_insert(param, param->name); -} - -Param * CustomWave::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; - -} diff --git a/src/projectM-engine/CustomWave.h b/src/projectM-engine/CustomWave.h index d27015b4d..4f65e4872 100755 --- a/src/projectM-engine/CustomWave.h +++ b/src/projectM-engine/CustomWave.h @@ -147,6 +147,17 @@ public: void destroy_per_frame_init_eqn_tree(); 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 */ diff --git a/src/projectM-engine/Param.cpp b/src/projectM-engine/Param.cpp index 1defda375..973b4660b 100755 --- a/src/projectM-engine/Param.cpp +++ b/src/projectM-engine/Param.cpp @@ -249,51 +249,5 @@ Param * Param::new_param_bool(char * name, short int flags, void * engine_val, /* Finished, return success */ return param; } - -/// @bug this is fucked -void Param::load_init_cond() { - - // bullshit - int type; - void *engine_val; - InitCond * init_cond; - CValue init_val; - - /* Don't count read only parameters as initial conditions */ - if (flags & P_FLAG_READONLY) - return; - - /* If initial condition was not defined by the preset file, force a default one - with the following code */ -abort(); - -// if ((init_cond = (InitCond*)(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*)(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; - - /* Create new initial condition */ - if ((init_cond = new InitCond(this, init_val)) == NULL) - return; - - /* Insert the initial condition into this presets tree */ -// if (this->init_cond_tree->splay_insert(init_cond, init_cond->param->name) < 0) { - delete init_cond; - return; - // } - -// } - -} diff --git a/src/projectM-engine/Param.h b/src/projectM-engine/Param.h index 652479aa3..2e0b69473 100755 --- a/src/projectM-engine/Param.h +++ b/src/projectM-engine/Param.h @@ -87,7 +87,6 @@ public: int remove_param(); static Param * find_param( char *name, Preset *preset, int flags ); //int insert_param( SplayTree *database ); - void load_init_cond(); void load_unspec_init_cond(); void load_unspec_init_cond_shape(); diff --git a/src/projectM-engine/ParamUtils.hpp b/src/projectM-engine/ParamUtils.hpp index feada7d3e..32b0d2ec4 100644 --- a/src/projectM-engine/ParamUtils.hpp +++ b/src/projectM-engine/ParamUtils.hpp @@ -10,44 +10,103 @@ class ParamUtils { public: + static int insert(Param * param, SplayTree * paramTree) { -static const int AUTO_CREATE = 1; - -template - static Param * find(char * name, SplayTree * paramTree) { - - assert(name); - - Param * param = NULL; - - - /* First look in the builtin database */ - param = (Param *)paramTree->splay_find(name); - - - if (((FLAGS == AUTO_CREATE) && ((param = (Param *)paramTree->splay_find(name)) == NULL))) { - - /* 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 = new Param(name)) == NULL) - return NULL; - - /* Finally, insert the new parameter into this preset's proper splaytree */ - if (paramTree->splay_insert(param, param->name) < 0) { - delete param; - return NULL; - } - - } - - /* Return the found (or created) parameter. Note that this could be null */ - return param; + assert(param); + assert(paramTree); + return paramTree->splay_insert(param, param->name); } + static const int AUTO_CREATE = 1; + + template + static Param * find(char * name, SplayTree * paramTree) { + + assert(name); + + Param * param = NULL; + + + /* First look in the builtin database */ + param = (Param *)paramTree->splay_find(name); + + + if (((FLAGS == AUTO_CREATE) && ((param = (Param *)paramTree->splay_find(name)) == NULL))) { + + /* 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 = new Param(name)) == NULL) + return NULL; + + /* Finally, insert the new parameter into this preset's proper splaytree */ + if (paramTree->splay_insert(param, param->name) < 0) { + delete param; + return NULL; + } + + } + + /* Return the found (or created) parameter. Note that this could be null */ + return param; + + + } + + + +class LoadInitCondFunctor { +public: + LoadInitCondFunctor(Preset * preset) :m_preset(preset) {} + + void operator() (Param * param) { + + InitCond * init_cond; + CValue init_val; + + /* Don't count read only parameters as initial conditions */ + if (param->flags & P_FLAG_READONLY) + return; + + /* If initial condition was not defined by the preset file, force a default one + with the following code */ + if ((init_cond = (InitCond*)(m_preset->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_preset->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; + + /* Create new initial condition */ + if ((init_cond = new InitCond(param, init_val)) == NULL) + return; + + /* Insert the initial condition into this presets tree */ + if (m_preset->init_cond_tree->splay_insert(init_cond, init_cond->param->name) < 0) { + delete init_cond; + return; + } + + } + + } + +private : + Preset * m_preset; }; + +}; + #endif diff --git a/src/projectM-engine/Preset.cpp b/src/projectM-engine/Preset.cpp index 0ec0fa2cc..f188412f7 100755 --- a/src/projectM-engine/Preset.cpp +++ b/src/projectM-engine/Preset.cpp @@ -31,6 +31,7 @@ #include "Preset.hpp" #include "Parser.h" +#include "ParamUtils.hpp" Preset::Preset() {} @@ -49,6 +50,8 @@ Preset::~Preset() { DWRITE( "~preset(): in\n" ); #endif + + init_cond_tree->splay_traverse((void (*)(void*))free_init_cond_helper); delete init_cond_tree; @@ -60,8 +63,8 @@ Preset::~Preset() { per_frame_eqn_tree->splay_traverse((void (*)(void*))free_per_frame_eqn_helper); delete per_frame_eqn_tree; - - user_param_tree->splay_traverse((void (*)(void*))free_param_helper); + + user_param_tree->traverse >(); delete user_param_tree; custom_wave_tree->splay_traverse((void (*)(void*))free_custom_wave_helper); @@ -124,7 +127,8 @@ void Preset::reloadPerPixel(char *s) { /* Clear previous per pixel equations */ per_pixel_eqn_tree->splay_traverse((void (*)(void*))free_per_pixel_eqn_helper); delete per_pixel_eqn_tree; - per_pixel_eqn_tree = SplayTree::create_splaytree((int (*)(void*,void*))compare_int, (void* (*)(void*))copy_int, (void (*)(void*))free_int); + per_pixel_eqn_tree = SplayTree::create_splaytree + ((int (*)(void*,void*))SplayKeyFunctions::compare_int, (void* (*)(void*))SplayKeyFunctions::copy_int, (void (*)(void*))SplayKeyFunctions::free_int); /* Convert string to a stream */ #if !defined(MACOS) && !defined(WIN32) @@ -176,7 +180,7 @@ void Preset::reloadPerFrame(char * s) { /* Clear previous per frame equations */ per_frame_eqn_tree->splay_traverse((void (*)(void*))free_per_frame_eqn_helper ); delete per_frame_eqn_tree; - per_frame_eqn_tree = SplayTree::create_splaytree((int (*)(void*,void*))compare_int,(void* (*)(void*)) copy_int, (void (*)(void*))free_int); + per_frame_eqn_tree = SplayTree::create_splaytree((int (*)(void*,void*))SplayKeyFunctions::compare_int,(void* (*)(void*)) SplayKeyFunctions::copy_int, (void (*)(void*))SplayKeyFunctions::free_int); /* Convert string to a stream */ //FIXME @@ -221,13 +225,13 @@ printf( "reloadPerFrame()\n" ); void Preset::initialize(const std::string & pathname) { /* Initialize equation trees */ - init_cond_tree = SplayTree::create_splaytree((int (*)(void*,void*))compare_string, (void* (*)(void*))copy_string, (void (*)(void*))free_string); - this->user_param_tree = SplayTree::create_splaytree((int (*)(void*,void*))compare_string,(void* (*)(void*)) copy_string, (void (*)(void*))free_string); - this->per_frame_eqn_tree = SplayTree::create_splaytree((int (*)(void*,void*))compare_int,(void* (*)(void*)) copy_int, (void (*)(void*)) free_int); - this->per_pixel_eqn_tree = SplayTree::create_splaytree((int (*)(void*,void*))compare_int,(void* (*)(void*)) copy_int, (void (*)(void*)) free_int); - this->per_frame_init_eqn_tree = SplayTree::create_splaytree((int (*)(void*,void*))compare_string,(void* (*)(void*)) copy_string, (void (*)(void*)) free_string); - this->custom_wave_tree = SplayTree::create_splaytree((int (*)(void*,void*))compare_int, (void* (*)(void*))copy_int, (void (*)(void*)) free_int); - this->custom_shape_tree = SplayTree::create_splaytree((int (*)(void*,void*))compare_int, (void* (*)(void*))copy_int, (void (*)(void*)) free_int); + init_cond_tree = SplayTree::create_splaytree((int (*)(void*,void*))SplayKeyFunctions::compare_string, (void* (*)(void*))SplayKeyFunctions::copy_string, (void (*)(void*))SplayKeyFunctions::free_string); + this->user_param_tree = SplayTree::create_splaytree((int (*)(void*,void*))SplayKeyFunctions::compare_string,(void* (*)(void*)) SplayKeyFunctions::copy_string, (void (*)(void*))SplayKeyFunctions::free_string); + this->per_frame_eqn_tree = SplayTree::create_splaytree((int (*)(void*,void*))SplayKeyFunctions::compare_int,(void* (*)(void*)) SplayKeyFunctions::copy_int, (void (*)(void*)) SplayKeyFunctions::free_int); + this->per_pixel_eqn_tree = SplayTree::create_splaytree((int (*)(void*,void*))SplayKeyFunctions::compare_int,(void* (*)(void*)) SplayKeyFunctions::copy_int, (void (*)(void*))SplayKeyFunctions::free_int); + this->per_frame_init_eqn_tree = SplayTree::create_splaytree((int (*)(void*,void*))SplayKeyFunctions::compare_string,(void* (*)(void*)) SplayKeyFunctions::copy_string, (void (*)(void*)) SplayKeyFunctions::free_string); + this->custom_wave_tree = SplayTree::create_splaytree((int (*)(void*,void*))SplayKeyFunctions::compare_int, (void* (*)(void*))SplayKeyFunctions::copy_int, (void (*)(void*)) SplayKeyFunctions::free_int); + this->custom_shape_tree = SplayTree::create_splaytree((int (*)(void*,void*))SplayKeyFunctions::compare_int, (void* (*)(void*))SplayKeyFunctions::copy_int, (void (*)(void*))SplayKeyFunctions::free_int); memset(this->per_pixel_flag, 0, sizeof(int)*NUM_OPS); @@ -667,7 +671,10 @@ int Preset::load_preset_file(const char * pathname) { void Preset::load_init_conditions() { - builtinParams.traverse(load_init_cond_helper); + + ParamUtils::LoadInitCondFunctor functor(this); + builtinParams.traverse(functor); + } diff --git a/src/projectM-engine/SplayTree.hpp b/src/projectM-engine/SplayTree.hpp index 2e78859c2..b777549dd 100755 --- a/src/projectM-engine/SplayTree.hpp +++ b/src/projectM-engine/SplayTree.hpp @@ -82,9 +82,18 @@ public: Data *splay_find_min(); Data *splay_find_max(); + /** Traverses the splay tree at each node in order with the passed in functor */ template void traverse(Fun & functor); + /** Traverses the splay tree at each node in order by constructing a functor on the fly + * and using it to traverse the entire tree. This is a convenience function for functors that dont return + * any useful state to the caller. Note that the functor is assumed to take one template type which + * matches the datatype of the splay tree. See implementation for more details. + */ + template + void traverse(); + private: void splay_traverse_helper (void (*func_ptr)(void*), SplayNode * splaynode); @@ -126,7 +135,6 @@ SplayTree::~SplayTree() { } -/* Traverses the entire splay tree with the given function func_ptr */ template template void SplayTree::traverse(Fun & functor) { @@ -137,6 +145,18 @@ void SplayTree::traverse(Fun & functor) { return; } + +template +template +void SplayTree::traverse() { + + Fun functor; + /* Call recursive helper function */ + traverseRec(functor, root); + + return; +} + /* Traverses the entire splay tree with the given function func_ptr */ template void SplayTree::splay_traverse(void (*func_ptr)(void*)) { @@ -680,5 +700,15 @@ int SplayTree::splay_rec_size(SplayNode * splaynode) { } +namespace SplayTreeFunctors { +template +class Delete { +public: +void operator() (Data * data) { + delete(data); +} + +}; +} #endif /** !_SPLAYTREE_HPP */ diff --git a/src/projectM-engine/compare.h b/src/projectM-engine/compare.h index e8e64adc7..fa06657e4 100755 --- a/src/projectM-engine/compare.h +++ b/src/projectM-engine/compare.h @@ -30,6 +30,7 @@ /// @bug this will be ripped away when splaytree is more standardly written or its removed in favor of stl-esque data structure class SplayKeyFunctions { +public: static int compare_int(int * num1, int * num2); static int compare_string(char * string1, char * string2); @@ -43,7 +44,7 @@ static int compare_string_version(char * str1, char * str2); /** tree_types.cpp */ /* Compares integer value numbers in 32 bit range */ -int SplayKeyFunctions::compare_int(int * num1, int * num2) { +inline int SplayKeyFunctions::compare_int(int * num1, int * num2) { if ((*num1) < (*num2)) return -1; @@ -54,7 +55,7 @@ int SplayKeyFunctions::compare_int(int * num1, int * num2) { } /* Compares strings in lexographical order */ -int SplayKeyFunctions::compare_string(char * str1, char * str2) { +inline int SplayKeyFunctions::compare_string(char * str1, char * str2) { // printf("comparing \"%s\" to \"%s\"\n", str1, str2); //return strcmp(str1, str2); @@ -63,7 +64,7 @@ int SplayKeyFunctions::compare_string(char * str1, char * str2) { } /* Compares a string in version order. That is, file1 < file2 < file10 */ -int SplayKeyFunctions::compare_string_version(char * str1, char * str2) { +inline int SplayKeyFunctions::compare_string_version(char * str1, char * str2) { return strcmp( str1, str2 ); #ifdef PANTS