solstice

Compute collected power and efficiencies of a solar plant
git clone git://git.meso-star.com/solstice.git
Log | Files | Refs | README | LICENSE

commit b46cd90f6eff3156781a85522ade3cca5ff5d344
parent 279eb7e28144ca978f8563ca463dea85a0454c4d
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Fri,  2 Dec 2016 15:23:25 +0100

Update the solstice material internal API

Diffstat:
Msrc/solstice.c | 37+++++++++++++++++++++++++++++++++++++
Msrc/solstice_c.h | 17+++--------------
Msrc/solstice_material.c | 57+++++++++++++++++++++++++++++++++------------------------
3 files changed, 73 insertions(+), 38 deletions(-)

diff --git a/src/solstice.c b/src/solstice.c @@ -19,6 +19,41 @@ #include <solstice/ssol.h> /******************************************************************************* + * Helper functions + ******************************************************************************/ +static void +clear_materials(struct htable_material* materials) +{ + struct htable_material_iterator it, end; + ASSERT(materials); + + htable_material_begin(materials, &it); + htable_material_end(materials, &end); + while(!htable_material_iterator_eq(&it, &end)) { + struct ssol_material* mtl = *htable_material_iterator_data_get(&it); + SSOL(material_ref_put(mtl)); + htable_material_iterator_next(&it); + } + htable_material_clear(materials); +} + +static void +clear_objects(struct htable_object* objects) +{ + struct htable_object_iterator it, end; + ASSERT(objects); + + htable_object_begin(objects, &it); + htable_object_end(objects, &end); + while(!htable_object_iterator_eq(&it, &end)) { + struct ssol_object* obj = *htable_object_iterator_data_get(&it); + SSOL(object_ref_put(obj)); + htable_object_iterator_next(&it); + } + htable_object_clear(objects); +} + +/******************************************************************************* * Solstice local functions ******************************************************************************/ res_T @@ -55,6 +90,8 @@ void solstice_release(struct solstice* solstice) { ASSERT(solstice); + clear_materials(&solstice->materials); + clear_objects(&solstice->objects); if(solstice->ssol) SSOL(device_ref_put(solstice->ssol)); if(solstice->parser) solparser_ref_put(solstice->parser); htable_material_release(&solstice->materials); diff --git a/src/solstice_c.h b/src/solstice_c.h @@ -19,22 +19,11 @@ #include "solstice.h" #include "parser/solparser.h" -extern LOCAL_SYM res_T -solstice_create_ssol_material - (struct solstice* solstice, - const struct solparser_material_id mtl_id); - -/* Return NULL if the material does not exist */ -static FINLINE struct ssol_material* +extern FINLINE res_T solstice_get_ssol_material (struct solstice* solstice, - const struct solparser_material_id mtl_id) -{ - struct ssol_material** pmtl; - ASSERT(solstice); - pmtl = htable_material_find(&solstice->materials, &mtl_id.i); - return pmtl ? *pmtl : NULL; -} + const struct solparser_material_id mtl_id, + struct ssol_material** mtl); #endif /* SOLSTICE_C_H */ diff --git a/src/solstice_material.c b/src/solstice_material.c @@ -145,39 +145,48 @@ error: * Local functions ******************************************************************************/ res_T -solstice_create_ssol_material +solstice_get_ssol_material (struct solstice* solstice, - const struct solparser_material_id mtl_id) + const struct solparser_material_id mtl_id, + struct ssol_material** out_ssol_mtl) { - const struct solparser_material* mtl; - const struct solparser_material_matte* matte; - const struct solparser_material_mirror* mirror; struct ssol_material* ssol_mtl = NULL; + struct ssol_material** pssol_mtl = NULL; res_T res = RES_OK; ASSERT(solstice); - mtl = solparser_get_material(solstice->parser, mtl_id); - ASSERT(mtl); - - switch(mtl->type) { - case SOLPARSER_MATERIAL_MIRROR: - mirror = solparser_get_material_mirror(solstice->parser, mtl->data.mirror); - res = create_material_mirror(solstice, mirror, &ssol_mtl); - break; - case SOLPARSER_MATERIAL_MATTE: - matte = solparser_get_material_matte(solstice->parser, mtl->data.matte); - res = create_material_matte(solstice, matte, &ssol_mtl); - default: FATAL("Unreachable code.\n"); break; - } - if(res != RES_OK) goto error; - - res = htable_material_set(&solstice->materials, &mtl_id.i, &ssol_mtl); - if(res != RES_OK) { - fprintf(stderr, "Could not register the material into solstice.\n"); - goto error; + pssol_mtl = htable_material_find(&solstice->materials, &mtl_id.i); + if(pssol_mtl) { + ssol_mtl = *pssol_mtl; + } else { + const struct solparser_material* mtl; + const struct solparser_material_matte* matte; + const struct solparser_material_mirror* mirror; + + mtl = solparser_get_material(solstice->parser, mtl_id); + ASSERT(mtl); + + switch(mtl->type) { + case SOLPARSER_MATERIAL_MIRROR: + mirror = solparser_get_material_mirror(solstice->parser, mtl->data.mirror); + res = create_material_mirror(solstice, mirror, &ssol_mtl); + break; + case SOLPARSER_MATERIAL_MATTE: + matte = solparser_get_material_matte(solstice->parser, mtl->data.matte); + res = create_material_matte(solstice, matte, &ssol_mtl); + default: FATAL("Unreachable code.\n"); break; + } + if(res != RES_OK) goto error; + + res = htable_material_set(&solstice->materials, &mtl_id.i, &ssol_mtl); + if(res != RES_OK) { + fprintf(stderr, "Could not register the material into solstice.\n"); + goto error; + } } exit: + *out_ssol_mtl = ssol_mtl; return res; error: if(ssol_mtl) SSOL(material_ref_put(ssol_mtl));