diff --git a/src/env.cpp b/src/env.cpp new file mode 100644 index 0000000..f2e8719 --- /dev/null +++ b/src/env.cpp @@ -0,0 +1,61 @@ +/* + * This file is a part of EZC -- Easy templating in C++ library + * and is distributed under the 2-Clause BSD licence. + * Author: Tomasz Sowa + */ + +/* + * Copyright (c) 2026, Tomasz Sowa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "ezc.h" +#include "generator.h" + + +namespace Ezc +{ + + +bool Env::EvaluateParameter(size_t index) +{ + bool ok = false; + + if( generator && index < item.function.parameters.size() && index < params.size() ) + { + const Item::Function & fun_child = *item.function.parameters[index]; + Val & par = params[index]; + ok = generator->EvaluateParameter(fun_child, par, &out); + } + + return ok; +} + + + +} // namespace Ezc + + diff --git a/src/env.h b/src/env.h index 1693739..ba1dbcf 100644 --- a/src/env.h +++ b/src/env.h @@ -46,6 +46,8 @@ namespace Ezc { +class Generator; + /* a variable @@ -145,9 +147,11 @@ struct Stack struct Env { - // a result consists of a string and a boolean value // output stream - pt::Stream & out; // OLD INTERFACE a reference to res.stream + // DEPRACATED + // an old interface + // it will be removed in the future + pt::Stream & out; // table of parameters // the table can be empty @@ -217,15 +221,18 @@ struct Env // an item from the tree const Item & item; + // current Generator - at the moment used for evaluating individual parameters + Generator * generator; // arguments: output_stream, table_of_parameters, the_first_parameter - Env(pt::Stream & o, + Env(Generator * generator, + pt::Stream & out_stream, std::vector & pars, Val & res, const pt::Stream & input_stream, Stack & s, - const Item & item_) : out(o), params(pars), res(res), in(input_stream), stack(s), item(item_) + const Item & item_) : generator(generator), out(out_stream), params(pars), res(res), in(input_stream), stack(s), item(item_) { Clear(); } @@ -306,6 +313,8 @@ struct Env } + bool EvaluateParameter(size_t index); + }; diff --git a/src/generator.h b/src/generator.h index 18465b4..b8e40ac 100644 --- a/src/generator.h +++ b/src/generator.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2007-2025, Tomasz Sowa + * Copyright (c) 2007-2026, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -135,13 +135,12 @@ public: private: - struct FindHelper { FindHelper( - Item::Function & item_fun, + const Item::Function & item_fun, const std::wstring & fun_name, - std::vector & fields, + const std::vector & fields, std::vector & parameters, Val & result, pt::Stream & out_stream, @@ -171,9 +170,9 @@ private: this->currentval = val; } - Item::Function & item_fun; + const Item::Function & item_fun; const std::wstring & fun_name; - std::vector & fields; + const std::vector & fields; ValWrapper * valwrapper; Val * currentval; std::vector & parameters; @@ -377,7 +376,6 @@ private: bool CallMethod(morm::Model * model, Val::ModelMethod4 method); bool CallBlock(FindHelper & find_helper); - bool CallPodType(FindHelper & find_helper); void CallObject(BaseObj & base_obj, int method_index, Env & info); @@ -399,18 +397,21 @@ private: bool CallSpace(FindHelper & find_helper); #ifdef EZC_HAS_MORM_LIBRARY - //bool CallModelField(FindHelper & find_helper, morm::Model & model); - bool CallModel(FindHelper & find_helper, morm::Model & model); - bool CallModel(FindHelper & find_helper); - bool CallModelContainerWrapper(FindHelper & find_helper); + + bool TryEvaluateFunctionOrMethod(FindHelper & find_helper, ValWrapper & wrapper, bool is_for_alias); + bool FindLastModelWrapperA(FindHelper & find_helper, ValWrapper & child_valwrapper, bool is_for_alias); + bool FindLastModelWrapperB(FindHelper & find_helper, const std::wstring & field, bool is_for_alias); + bool TryEvaluateUntilLastModel(FindHelper & find_helper, bool is_for_alias); + #endif - bool FindLastModelWrapper(FindHelper & find_helper, bool is_for_alias); - bool FindLastSpace(FindHelper & find_helper); + + bool TryEvaluateUntilLastSpace(FindHelper & find_helper); bool EvaluateFunctions(pt::Stream & out_stream, Val & val, std::vector & parameters, Val & result); bool EvaluateFunctions(pt::Stream & out_stream, ValWrapper & valwrapper, std::vector & parameters); bool EvaluateFunctions(FindHelper & find_helper, bool use_parameters, ValWrapper & valwrapper); + bool TryEvaluateFirstFunction(FindHelper & find_helper, bool is_for_alias); void CallObject(BaseObj & base_obj, int method_index, @@ -426,15 +427,15 @@ private: // pt::Stream & out_stream, // const pt::Stream & in_stream); - void EvaluateParameters(FindHelper & find_helper); + bool EvaluateParameter(const Item::Function & par, Val & parameter, pt::Stream * out_stream); + bool EvaluateParameters(FindHelper & find_helper, std::vector & out_streams); //bool Evaluate(FindHelper & find_helper); - bool CallValue(FindHelper & find_helper); - void CleanupParameters(std::vector & parameters); + bool TryEvaluateSimpleType(FindHelper & find_helper); - bool Call(Item::Function & item_fun, - std::wstring * fun_name, - Val & result, pt::Stream & out_stream, - const pt::Stream & in_stream); + void PrepareParametersStreams(FindHelper & find_helper, std::vector & out_streams); + void CleanupParametersStreams(std::vector & out_streams); + + bool Call(const Item::Function & item_fun, const std::wstring * fun_name, Val & result, pt::Stream & out_stream, const pt::Stream & in_stream); wchar_t CreateSpecialChar(wchar_t c); const wchar_t * PrintSpecialChar(const wchar_t * start, const wchar_t * end); @@ -456,10 +457,10 @@ private: pt::Stream * FindAddOutputStream(const std::wstring & name); void CopyTmpStreamToOutputStreams(Item::Function & fun, pt::Stream & ezc_out_tmp_stream); - void CreateMsg(const wchar_t * type, const std::wstring & model_name, std::vector & fields, size_t how_many_fields_print, - const wchar_t * arg = nullptr, const wchar_t * arg2 = nullptr, const wchar_t * arg3 = nullptr); + void CreateMsg(const wchar_t * type, const std::wstring & model_name, const std::vector & fields, size_t how_many_fields_print, + const wchar_t * arg = nullptr, const wchar_t * arg2 = nullptr, const wchar_t * arg3 = nullptr); - void CreateMsg(const wchar_t * type, const std::wstring & model_name, std::vector & fields, + void CreateMsg(const wchar_t * type, const std::wstring & model_name, const std::vector & fields, const wchar_t * arg = nullptr, const wchar_t * arg2 = nullptr, const wchar_t * arg3 = nullptr); void CreateMsg(const wchar_t * type, const wchar_t * arg = nullptr); @@ -501,6 +502,7 @@ private: void Generate(); + friend Env; }; // class Generator @@ -1483,7 +1485,7 @@ void Generator::PrintDate(FindHelper & find_helper, pt::Date * date) bool Generator::PrintDatePart(FindHelper & find_helper, pt::Date * date) { bool is_test = IsTestingFunctionExistence(); - std::wstring & field = find_helper.fields[find_helper.field_index]; + const std::wstring & field = find_helper.fields[find_helper.field_index]; if( field == L"year" ) { @@ -1540,8 +1542,6 @@ bool Generator::PrintDatePart(FindHelper & find_helper, pt::Date * date) bool Generator::CallDate(FindHelper & find_helper) { bool found = true; - bool all_fields_known = (find_helper.field_index == find_helper.fields.size()); - bool last_field_not_known = (find_helper.field_index + 1 == find_helper.fields.size()); pt::Date * date = nullptr; if( find_helper.currentval->type == Val::TYPE_DATE && find_helper.currentval->pointer ) @@ -1549,7 +1549,7 @@ bool Generator::CallDate(FindHelper & find_helper) date = reinterpret_cast(find_helper.currentval->pointer); } - if( all_fields_known ) + if( find_helper.all_fields_known() ) { if( date ) { @@ -1557,7 +1557,7 @@ bool Generator::CallDate(FindHelper & find_helper) } } else - if( last_field_not_known ) + if( find_helper.last_field_not_known() ) { if( date ) { @@ -1590,7 +1590,7 @@ bool Generator::CallDate(FindHelper & find_helper) pt::Space * Generator::CallSpaceObject(FindHelper & find_helper, pt::Space * space) { - std::wstring & field = find_helper.fields[find_helper.field_index]; + const std::wstring & field = find_helper.fields[find_helper.field_index]; space = space->get_space(field); if( find_helper.is_last_field() ) @@ -1709,7 +1709,7 @@ pt::Space * Generator::CallSpaceTable(FindHelper & find_helper, pt::Space * spac if( space && !space->is_object() ) { - std::wstring & next_field = find_helper.fields[find_helper.field_index]; + const std::wstring & next_field = find_helper.fields[find_helper.field_index]; if( next_field == L"this" ) { @@ -1816,168 +1816,6 @@ bool Generator::CallSpace(FindHelper & find_helper) } - - -#ifdef EZC_HAS_MORM_LIBRARY - - -/* -bool Generator::CallModelField(FindHelper & find_helper, morm::Model & model) -{ - bool found = false; - const std::wstring & field = find_helper.fields[find_helper.field_index]; - bool no_escape = HasParam(find_helper.parameters, L"raw", L"noescape"); - - find_helper.result.output_stream->escape_input(!no_escape); - - if( find_helper.parameters.empty() ) - { - // what about an empty input parameter? - - Env env(find_helper.out_stream, find_helper.parameters, find_helper.result, find_helper.in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); - PrepareEnvStruct(env); - found = model.get_raw_value(nullptr, field.c_str(), nullptr, env, find_helper.out_stream, false); - } - else - { - Env env(find_helper.out_stream, find_helper.parameters, find_helper.result, find_helper.in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); - PrepareEnvStruct(env); - found = model.get_raw_value(nullptr, field.c_str(), nullptr, env, find_helper.out_stream, false); - } - - if( found ) - { - // set type to TYPE_OUTPUT_STREAM? - // find_helper.result.type == - } - - return found; -} -*/ - - -bool Generator::CallModel(FindHelper & find_helper, morm::Model & model) -{ - bool found = true; - - if( find_helper.field_index == find_helper.fields.size() ) - { - // all fields items are models or models containers - // at the moment we do not set last_res - // we don't know whether the last object has properties (fields) or not - if( !IsTestingFunctionExistence() ) - { - DumpModelIfNeeded(find_helper, model); - } - } - else - if( find_helper.field_index + 1 == find_helper.fields.size() ) - { - // last field is not a model nor a models container - /* - if( !CallModelField(find_helper, model) ) - { - if( !IsTestingFunctionExistence() ) - { - CreateMsg(L"cannot find ", find_helper.fun_name, find_helper.fields, L", unknown property ", - find_helper.fields[find_helper.field_index].c_str(), L" of a model object"); - } - - found = false; - } - */ - } - else - { - if( !IsTestingFunctionExistence() ) - { - CreateMsg(L"cannot find ", find_helper.fun_name, find_helper.fields, L", ", find_helper.fields[find_helper.field_index].c_str(), - L" is not a model nor a model container nor a space"); - } - - found = false; - } - - return found; -} - -/* -Val * Generator::FindLastModelWrapperEvaluateMiddleFunction(FindHelper & find_helper) -{ - -} - - -Val * Generator::FindLastModelWrapperEvalueateMiddleModel(FindHelper & find_helper) -{ - Val * child_val = nullptr; - morm::Model * model = nullptr; - std::wstring & field = find_helper.fields[find_helper.field_index]; - - if( find_helper.currentval->pointer ) - { - if( find_helper.currentval->type == Val::TYPE_MODEL ) - { - model = reinterpret_cast(find_helper.currentval->pointer); - } - else - if( find_helper.currentval->type == Val::TYPE_MODEL_CONTAINER_WRAPPER ) - { - morm::ModelContainerWrapper * model_container_wrapper = reinterpret_cast(find_helper.currentval->pointer); - model = model_container_wrapper->get_model(); - // this can return null for lists/vectors in a case when the iterator is not pointing to a valid item - } - } - - if( model ) - { - Val val = model->get_ezc_val(nullptr, field.c_str()); - - if( val.has_object() ) - { - child_val = find_helper.currentval->add_child(field, val); - } - } - - return child_val; -} - - -Val * Generator::FindLastModelWrapperEvalueateFunctionsRenameMe(FindHelper & find_helper, Val * child_val) -{ - std::wstring & field = find_helper.fields[find_helper.field_index]; - size_t function_call_limit = 2; // set me to 16; - size_t current_call = 1; - Val val; - - while( child_val && child_val->has_function() ) - { - if( current_call > function_call_limit ) - { - // IMPROVEME put some error msg - return nullptr; - } - - Val::UserFunction function = reinterpret_cast(child_val->pointer); - val.clear(); - val.set_output_stream(&find_helper.out_stream); - - // a current find_helper.in_stream used here? or rather an empty_stream of some kind? - Env env(find_helper.out_stream, find_helper.parameters, val, find_helper.in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); - CallFunction(function, env); - - child_val = &val; - current_call += 1; - } - - child_val = find_helper.currentval->add_child(field, val); - - return child_val; -} -*/ - - - bool Generator::EvaluateFunctions(pt::Stream & out_stream, Val & val, std::vector & parameters, Val & result) { size_t max_calls = 2; // change me to something reasnoable @@ -2001,7 +1839,7 @@ bool Generator::EvaluateFunctions(pt::Stream & out_stream, Val & val, std::vecto * the result is correclty cleared in Env cctor so we have to copy current_val->pointer now */ Val::UserFunction function = reinterpret_cast(current_val->pointer); - Env env(out_stream, parameters, result, *empty_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); + Env env(this, out_stream, parameters, result, *empty_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); CallFunction(function, env); } #ifdef EZC_HAS_MORM_LIBRARY @@ -2013,7 +1851,7 @@ bool Generator::EvaluateFunctions(pt::Stream & out_stream, Val & val, std::vecto if( current_val->model_method1 ) { Val::ModelMethod1 method = current_val->model_method1; // current_val can point to the result so copy the value (Env cctor calls a clear() method on the result) - Env env(out_stream, parameters, result, *empty_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); + Env env(this, out_stream, parameters, result, *empty_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); CallMethod(model, method, env); } else @@ -2084,126 +1922,187 @@ bool Generator::EvaluateFunctions(pt::Stream & out_stream, ValWrapper & valwrapp bool Generator::EvaluateFunctions(FindHelper & find_helper, bool use_parameters, ValWrapper & valwrapper) { - if( !no_parameters.empty() ) + if( use_parameters ) { - no_parameters.clear(); + return EvaluateFunctions(find_helper.out_stream, valwrapper, find_helper.parameters); } + else + { + /* + * make sure the no_parameters table is empty + */ + if( !no_parameters.empty() ) + { + no_parameters.clear(); + } - std::vector * parameters = (use_parameters) ? &find_helper.parameters : &no_parameters; - - return EvaluateFunctions(find_helper.out_stream, valwrapper, *parameters); + return EvaluateFunctions(find_helper.out_stream, valwrapper, no_parameters); + } } -// RENAMEME -bool Generator::FindLastModelWrapper(FindHelper & find_helper, bool is_for_alias) +bool Generator::TryEvaluateFirstFunction(FindHelper & find_helper, bool is_for_alias) { - while( find_helper.field_index < find_helper.fields.size() && find_helper.currentval->has_model_object() ) + bool status = true; + + if( !is_for_alias || find_helper.field_index < find_helper.fields.size() ) { - std::wstring & field = find_helper.fields[find_helper.field_index]; - ValWrapper * child_valwrapper = find_helper.valwrapper->find_child(field); - - if( child_valwrapper ) + if( find_helper.valwrapper->val.has_function() ) { - // evaluate fun or method in a case they have set a non_cache attr + /* + * here we are using parameters when the ezc tag has such a form: + * [my_function] + * and not in such a case: + * [my_function.other_value] + * + */ + bool use_parameters = find_helper.fields.empty(); - if( child_valwrapper->has_evaluated_val ) + if( EvaluateFunctions(find_helper, use_parameters, *find_helper.valwrapper) ) { - if( child_valwrapper->evaluated_str ) + if( find_helper.valwrapper->has_evaluated_val ) { - find_helper.out_stream.escape_input(false); - find_helper.out_stream << *child_valwrapper->evaluated_str; + find_helper.currentval = &find_helper.valwrapper->evaluated_val; } } else { - // the same as below - if( child_valwrapper->val.has_function() || child_valwrapper->val.has_method() ) - { - if( (find_helper.field_index + 1 < find_helper.fields.size()) || !is_for_alias ) - { - bool use_parameters = (find_helper.field_index + 1 == find_helper.fields.size()); - - if( !EvaluateFunctions(find_helper, use_parameters, *child_valwrapper) ) - { - return false; - } - } - } + status = false; } - - find_helper.set_valwrapper(child_valwrapper); - find_helper.currentval->set_output_stream(find_helper.out_stream); } - else + } + + return status; +} + + +#ifdef EZC_HAS_MORM_LIBRARY + +bool Generator::TryEvaluateFunctionOrMethod(FindHelper & find_helper, ValWrapper & wrapper, bool is_for_alias) +{ + if( wrapper.val.has_function() || wrapper.val.has_method() ) + { + if( (find_helper.field_index + 1 < find_helper.fields.size()) || !is_for_alias ) { - morm::Model * model = nullptr; + /* + * we are using parameters only for the last function/method + */ + bool use_parameters = (find_helper.field_index + 1 == find_helper.fields.size()); - if( find_helper.currentval->pointer ) + if( !EvaluateFunctions(find_helper, use_parameters, wrapper) ) { - if( find_helper.currentval->type == Val::TYPE_MODEL ) - { - model = reinterpret_cast(find_helper.currentval->pointer); - } - else - if( find_helper.currentval->type == Val::TYPE_MODEL_CONTAINER_WRAPPER ) - { - morm::ModelContainerWrapper * model_container_wrapper = reinterpret_cast(find_helper.currentval->pointer); - model = model_container_wrapper->get_model(); - // this can return null for lists/vectors in a case when the iterator is not pointing to a valid item - } - } - - if( model ) - { - ValWrapper valwrapper; - valwrapper.val = model->get_ezc_val(nullptr, field.c_str()); // may it would be better to provide a pointer to get_ezc_val() method? - - // the same block as above - if( valwrapper.val.has_function() || valwrapper.val.has_method() ) - { - if( (find_helper.field_index + 1 < find_helper.fields.size()) || !is_for_alias ) - { - bool use_parameters = (find_helper.field_index + 1 == find_helper.fields.size()); - - if( !EvaluateFunctions(find_helper, use_parameters, valwrapper) ) - { - return false; - } - } - } - - if( (valwrapper.has_evaluated_val && valwrapper.evaluated_val.has_object()) || - (!valwrapper.has_evaluated_val && valwrapper.val.has_object()) ) - { - child_valwrapper = find_helper.valwrapper->add_child(field, valwrapper); - find_helper.set_valwrapper(child_valwrapper); - find_helper.currentval->set_output_stream(find_helper.out_stream); - - if( child_valwrapper->has_evaluated_val && !child_valwrapper->evaluated_val.is_allowed_to_cache() ) - { - child_valwrapper->has_evaluated_val = false; - - /* - * but find_helper.currentval let point to the evaluated_val (it is set by set_valwrapper() method) - */ - } - } - } - - if( !child_valwrapper ) return false; + } } - - find_helper.field_index += 1; } return true; } +bool Generator::FindLastModelWrapperA(FindHelper & find_helper, ValWrapper & child_valwrapper, bool is_for_alias) +{ + // evaluate fun or method in a case they have set a non_cache attr -bool Generator::FindLastSpace(FindHelper & find_helper) + if( child_valwrapper.has_evaluated_val ) + { + if( child_valwrapper.evaluated_str ) + { + find_helper.out_stream.escape_input(false); + find_helper.out_stream << *child_valwrapper.evaluated_str; + } + } + else + { + if( !TryEvaluateFunctionOrMethod(find_helper, child_valwrapper, is_for_alias) ) + return false; + } + + find_helper.set_valwrapper(&child_valwrapper); + find_helper.currentval->set_output_stream(find_helper.out_stream); + + return true; +} + + +bool Generator::FindLastModelWrapperB(FindHelper & find_helper, const std::wstring & field, bool is_for_alias) +{ + morm::Model * model = nullptr; + ValWrapper * child_valwrapper = nullptr; + + if( find_helper.currentval->pointer ) + { + if( find_helper.currentval->type == Val::TYPE_MODEL ) + { + model = reinterpret_cast(find_helper.currentval->pointer); + } + else + if( find_helper.currentval->type == Val::TYPE_MODEL_CONTAINER_WRAPPER ) + { + morm::ModelContainerWrapper * model_container_wrapper = reinterpret_cast(find_helper.currentval->pointer); + model = model_container_wrapper->get_model(); + // this can return null for lists/vectors in a case when the iterator is not pointing to a valid item + } + } + + if( model ) + { + ValWrapper valwrapper; + valwrapper.val = model->get_ezc_val(nullptr, field.c_str()); // may it would be better to provide a pointer to get_ezc_val() method? + + if( !TryEvaluateFunctionOrMethod(find_helper, valwrapper, is_for_alias) ) + return false; + + if( (valwrapper.has_evaluated_val && valwrapper.evaluated_val.has_object()) || + (!valwrapper.has_evaluated_val && valwrapper.val.has_object()) ) + { + child_valwrapper = find_helper.valwrapper->add_child(field, valwrapper); + find_helper.set_valwrapper(child_valwrapper); + find_helper.currentval->set_output_stream(find_helper.out_stream); + + if( child_valwrapper->has_evaluated_val && !child_valwrapper->evaluated_val.is_allowed_to_cache() ) + { + child_valwrapper->has_evaluated_val = false; + + /* + * but find_helper.currentval let point to the evaluated_val (it is set by set_valwrapper() method) + */ + } + } + } + + return child_valwrapper != nullptr; +} + + +bool Generator::TryEvaluateUntilLastModel(FindHelper & find_helper, bool is_for_alias) +{ + bool ok = true; + + while( ok && find_helper.field_index < find_helper.fields.size() && find_helper.currentval->has_model_object() ) + { + const std::wstring & field = find_helper.fields[find_helper.field_index]; + ValWrapper * child_valwrapper = find_helper.valwrapper->find_child(field); + + if( child_valwrapper ) + { + ok = FindLastModelWrapperA(find_helper, *child_valwrapper, is_for_alias); + } + else + { + ok = FindLastModelWrapperB(find_helper, field, is_for_alias); + } + + find_helper.field_index += 1; + } + + return ok; +} + +#endif // #ifdef EZC_HAS_MORM_LIBRARY + + +bool Generator::TryEvaluateUntilLastSpace(FindHelper & find_helper) { if( find_helper.currentval->type == Val::TYPE_SPACE ) { @@ -2214,59 +2113,6 @@ bool Generator::FindLastSpace(FindHelper & find_helper) } -bool Generator::CallModel(FindHelper & find_helper) -{ - morm::Model * model = reinterpret_cast(find_helper.currentval->pointer); - return CallModel(find_helper, *model); -} - - - -bool Generator::CallModelContainerWrapper(FindHelper & find_helper) -{ - bool found = true; - //last_res = false; - - morm::ModelContainerWrapper * model_container_wrapper = reinterpret_cast(find_helper.currentval->pointer); - morm::Model * model = model_container_wrapper->get_model(); - - if( model ) - { - found = CallModel(find_helper, *model); - } - else - { - if( find_helper.last_field_not_known() ) - { - // can be printed even for [if-def...] - CreateMsg(L"model ", find_helper.fun_name, find_helper.fields, L" is not initialized, have you forgotten to use [for ...] statement?"); - found = false; - } - } - - if( find_helper.all_fields_known() ) - { - find_helper.result.set_pointer_to(model_container_wrapper); - - if( is_generating_for ) - { - // model_container_wrapper->increment_iterator(); - // find_helper.currentval->clear_childs(); - // find_helper.result.set(model_container_wrapper->is_iterator_correct()); - //last_res = find_helper.wrapper->model_container_wrapper->is_iterator_correct(); - } - else - { - // for [if-def...] last_res will be set later - //last_res = !find_helper.wrapper->model_container_wrapper->is_container_empty(); - //find_helper.result.set(model_container_wrapper->is_container_empty()); - } - } - - return found; -} - -#endif @@ -2279,7 +2125,7 @@ void Generator::CallObject(BaseObj & base_obj, { if( !IsTestingFunctionExistence() ) { - Env env(out_stream, parameters, result, in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); + Env env(this, out_stream, parameters, result, in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); CallObject(base_obj, method_index, env); } } @@ -2343,47 +2189,34 @@ bool Generator::CallBlock(FindHelper & find_helper) // } -bool Generator::CallPodType(FindHelper & find_helper) +bool Generator::EvaluateParameter(const Item::Function & fun_child, Val & parameter, pt::Stream * out_stream) { - if( !IsTestingFunctionExistence() ) + bool ok = true; + + if( fun_child.is_function ) { - find_helper.currentval->put_pod_type_to_stream(); + ok = Call(fun_child, nullptr, parameter, *out_stream, *empty_stream); + } + else + { + parameter.set_pointer_to(&fun_child.name); } - return true; + return ok; } - -void Generator::EvaluateParameters(FindHelper & find_helper) +bool Generator::EvaluateParameters(FindHelper & find_helper, std::vector & out_streams) { - find_helper.parameters.resize(find_helper.item_fun.parameters.size()); + bool ok = true; - for(size_t i=0 ; i < find_helper.item_fun.parameters.size() ; ++i) + for(size_t i=0 ; ok && i < find_helper.parameters.size() ; ++i) { - pt::Stream * tmp_stream = main_stream->new_empty(); - tmp_stream->escape_input(true); - find_helper.parameters[i].set_output_stream(tmp_stream); - Item::Function & fun_child = *find_helper.item_fun.parameters[i]; - - if( fun_child.is_function ) - { - Call(fun_child, nullptr, find_helper.parameters[i], *tmp_stream, *empty_stream); - - //CopyStreamToString(*local_temp_stream, parameters[i].str); - //parameters[i].res = last_res; - } - else - { - // parameters[i].str = fun_child.name; - // parameters[i].res = ConvertToBool(fun_child.name); - - - //find_helper.parameters[i] << fun_child.name; - find_helper.parameters[i].set_pointer_to(&fun_child.name); - } + ok = EvaluateParameter(*find_helper.item_fun.parameters[i], find_helper.parameters[i], out_streams[i]); } + + return ok; } @@ -2402,7 +2235,7 @@ void Generator::EvaluateParameters(FindHelper & find_helper) // } -bool Generator::CallValue(FindHelper & find_helper) +bool Generator::TryEvaluateSimpleType(FindHelper & find_helper) { if( find_helper.currentval->pointer ) { @@ -2415,6 +2248,7 @@ bool Generator::CallValue(FindHelper & find_helper) else if( find_helper.currentval->type == Val::TYPE_MODEL_CONTAINER_WRAPPER ) { + find_helper.result = *find_helper.currentval; //return CallModelContainerWrapper(find_helper); } else @@ -2448,25 +2282,52 @@ bool Generator::CallValue(FindHelper & find_helper) // if( find_helper.variable ) // return CallVariable(item_fun, *find_helper.variable, parameters, result, out_stream, in_stream); } + else + { + if( find_helper.currentval->type == Val::TYPE_SPACE_LOCAL || + find_helper.currentval->type == Val::TYPE_SPACE_LOCAL_ULLONG || + find_helper.currentval->type == Val::TYPE_SPACE_LOCAL_CHAR || + find_helper.currentval->type == Val::TYPE_SPACE_LOCAL_UCHAR || + find_helper.currentval->type == Val::TYPE_SPACE_LOCAL_WCHAR ) + { + find_helper.result = *find_helper.currentval; + } + } // if( find_helper.currentval->has_pod_type() ) // return CallPodType(find_helper); - find_helper.result = *find_helper.currentval; // IMPROVEME this makes a copy + //find_helper.result = *find_helper.currentval; // IMPROVEME this makes a copy return true; } -void Generator::CleanupParameters(std::vector & parameters) +void Generator::PrepareParametersStreams(FindHelper & find_helper, std::vector & out_streams) { - for(Val & val : parameters) + size_t params_size = find_helper.item_fun.parameters.size(); + + find_helper.parameters.resize(params_size); + out_streams.resize(params_size, nullptr); + + for(size_t i=0 ; i < params_size ; ++i) { - delete val.output_stream; - val.output_stream = nullptr; + out_streams[i] = main_stream->new_empty(); + find_helper.parameters[i].set_output_stream(out_streams[i]); } } +void Generator::CleanupParametersStreams(std::vector & out_streams) +{ + for(pt::Stream * stream : out_streams) + { + delete stream; + } + + out_streams.clear(); +} + + /* * fun_name can be null, it is used only with [let ...] statements * and if not null then means: as a funcion name we are not using item_fun.name but fun_name @@ -2474,71 +2335,46 @@ void Generator::CleanupParameters(std::vector & parameters) * return: true if a function, variable or block was found and called (evaluated) */ -bool Generator::Call(Item::Function & item_fun, - std::wstring * fun_name, +bool Generator::Call(const Item::Function & item_fun, + const std::wstring * fun_name, Val & result, pt::Stream & out_stream, const pt::Stream & in_stream) { - //Val current(&out_stream); std::vector parameters; + std::vector parameters_out_streams; if( !fun_name ) fun_name = &item_fun.name; FindHelper find_helper(item_fun, *fun_name, item_fun.fields, parameters, result, out_stream, in_stream); - find_helper.field_index = 0; - - - // if( clear_out_stream ) - // ClearStream(out_stream); if( !Find(find_helper) ) return false; out_stream.escape_input(true); - - bool is_for_alias = false; - - EvaluateParameters(find_helper); - - if( !is_for_alias || find_helper.field_index < find_helper.fields.size() ) - { - bool use_parameters = find_helper.fields.empty(); - - if( EvaluateFunctions(find_helper, use_parameters, *find_helper.valwrapper) ) - { - if( find_helper.valwrapper->has_evaluated_val ) - { - find_helper.currentval = &find_helper.valwrapper->evaluated_val; - } - } - else - { - return false; - } - } - - if( !FindLastModelWrapper(find_helper, false) ) - return false; - - if( !FindLastSpace(find_helper) ) - return false; - - bool status = false; + bool is_for_alias = false; /* temporarily */ + bool status = true; try { - status = CallValue(find_helper); + PrepareParametersStreams(find_helper, parameters_out_streams); + status = status && EvaluateParameters(find_helper, parameters_out_streams); + status = status && TryEvaluateFirstFunction(find_helper, is_for_alias); + + #ifdef EZC_HAS_MORM_LIBRARY + status = status && TryEvaluateUntilLastModel(find_helper, false); + #endif + + status = status && TryEvaluateUntilLastSpace(find_helper); + status = status && TryEvaluateSimpleType(find_helper); } catch(...) { - // rethrow or just generate a log? - CleanupParameters(parameters); - return false; + status = false; } - CleanupParameters(parameters); + CleanupParametersStreams(parameters_out_streams); return status; } @@ -2795,8 +2631,8 @@ void Generator::CreateMsg(const wchar_t * type, const wchar_t * arg) -void Generator::CreateMsg(const wchar_t * type, const std::wstring & model_name, std::vector & fields, size_t how_many_fields_print, - const wchar_t * arg, const wchar_t * arg2, const wchar_t * arg3) +void Generator::CreateMsg(const wchar_t * type, const std::wstring & model_name, const std::vector & fields, + size_t how_many_fields_print, const wchar_t * arg, const wchar_t * arg2, const wchar_t * arg3) { if( plog ) { @@ -2828,7 +2664,7 @@ void Generator::CreateMsg(const wchar_t * type, const std::wstring & model_name, -void Generator::CreateMsg(const wchar_t * type, const std::wstring & model_name, std::vector & fields, +void Generator::CreateMsg(const wchar_t * type, const std::wstring & model_name, const std::vector & fields, const wchar_t * arg, const wchar_t * arg2, const wchar_t * arg3) { @@ -2976,7 +2812,6 @@ void Generator::MakeTextContainer(Item & item) - void Generator::MakeTextFunction(Item & item) { is_generating_normal = true; @@ -2991,7 +2826,6 @@ void Generator::MakeTextFunction(Item & item) { Val result(output_stream); - // does we need both seperate: result and output_stream? if( Call(item.function, nullptr, result, *output_stream, *empty_stream) ) { output_stream->escape_input(true); diff --git a/src/models.cpp b/src/models.cpp index 3d51a9c..c557d43 100644 --- a/src/models.cpp +++ b/src/models.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2021-2025, Tomasz Sowa + * Copyright (c) 2021-2026, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -61,65 +61,124 @@ void Models::Clear() void Models::Add(const std::wstring & name, morm::Model & model) { - ValWrapper valcache; - valcache.val.set_pointer_to(&model); - models_map[name] = valcache; + add_set_pointer_to_generic(name, &model); } void Models::Add(const std::wstring & name, morm::Model * model) { - ValWrapper valcache; - valcache.val.set_pointer_to(model); - models_map[name] = valcache; + add_set_pointer_to_generic(name, model); } +#ifdef EZC_HAS_MORM_LIBRARY + void Models::Add(const std::wstring & name, pt::Space & space) { - ValWrapper valcache; - valcache.val.set_pointer_to(new morm::SpaceWrapper(&space)); - models_map[name] = valcache; + add_set_pointer_to_generic(name, new morm::SpaceWrapper(&space)); } void Models::Add(const std::wstring & name, pt::Space * space) { - ValWrapper valcache; - valcache.val.set_pointer_to(new morm::SpaceWrapper(space)); - models_map[name] = valcache; + add_set_pointer_to_generic(name, new morm::SpaceWrapper(space)); } +#endif + void Models::Add(const std::wstring & name, pt::Date & date) { - ValWrapper valcache; - valcache.val.set_pointer_to(&date); - models_map[name] = valcache; + add_set_pointer_to_generic(name, &date); +} + + +void Models::Add(const std::wstring & name, char value) +{ + add_set_generic(name, value); +} + + +void Models::Add(const std::wstring & name, unsigned char value) +{ + add_set_generic(name, value); +} + + +void Models::Add(const std::wstring & name, wchar_t value) +{ + add_set_generic(name, value); +} + + +void Models::Add(const std::wstring & name, short value) +{ + add_set_generic(name, value); +} + + +void Models::Add(const std::wstring & name, int value) +{ + add_set_generic(name, value); +} + + +void Models::Add(const std::wstring & name, long value) +{ + add_set_generic(name, value); +} + + +void Models::Add(const std::wstring & name, long long value) +{ + add_set_generic(name, value); +} + + +void Models::Add(const std::wstring & name, unsigned short value) +{ + add_set_generic(name, value); +} + + +void Models::Add(const std::wstring & name, unsigned int value) +{ + add_set_generic(name, value); +} + +void Models::Add(const std::wstring & name, unsigned long value) +{ + add_set_generic(name, value); +} + + +void Models::Add(const std::wstring & name, unsigned long long value) +{ + add_set_generic(name, value); } void Models::Add(const std::wstring & name, pt::Date * date) { - ValWrapper valcache; - valcache.val.set_pointer_to(date); - models_map[name] = valcache; + add_set_pointer_to_generic(name, date); +} + + +void Models::Add(const std::wstring & name, const std::string & value) +{ + add_set_generic(name, value); } void Models::Add(const std::wstring & name, const std::wstring & value) { - ValWrapper valcache; - valcache.val.set(value); - models_map[name] = valcache; + add_set_generic(name, value); } void Models::Add(const std::wstring & name, Val::UserFunction ufunction) { - ValWrapper valcache; - valcache.val.set_pointer_to(ufunction); - models_map[name] = valcache; + add_set_pointer_to_generic(name, ufunction); } diff --git a/src/models.h b/src/models.h index 9a5c814..6c78bda 100644 --- a/src/models.h +++ b/src/models.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2021-2025, Tomasz Sowa + * Copyright (c) 2021-2026, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -62,15 +62,30 @@ public: void Add(const std::wstring & name, morm::Model & model); void Add(const std::wstring & name, morm::Model * model); +#ifdef EZC_HAS_MORM_LIBRARY + void Add(const std::wstring & name, pt::Space & space); void Add(const std::wstring & name, pt::Space * space); - void Add(const std::wstring & name, pt::Date & space); - void Add(const std::wstring & name, pt::Date * space); +#endif - void Add(const std::wstring & name, const std::wstring & value); + void Add(const std::wstring & name, pt::Date & space); // ok + void Add(const std::wstring & name, pt::Date * space); // ok + void Add(const std::wstring & name, char value); // ok + void Add(const std::wstring & name, unsigned char value); // ok + void Add(const std::wstring & name, wchar_t value); // ok + void Add(const std::wstring & name, short value); // ok + void Add(const std::wstring & name, int value); // ok + void Add(const std::wstring & name, long value); // ok + void Add(const std::wstring & name, long long value); // ok + void Add(const std::wstring & name, unsigned short value); // ok + void Add(const std::wstring & name, unsigned int value); // ok + void Add(const std::wstring & name, unsigned long value); // ok + void Add(const std::wstring & name, unsigned long long value); // ok + void Add(const std::wstring & name, const std::string & value); // ok + void Add(const std::wstring & name, const std::wstring & value); // ok - void Add(const std::wstring & name, Val::UserFunction ufunction); + void Add(const std::wstring & name, Val::UserFunction ufunction); // ok template void Add(const std::wstring & name, std::vector & container) @@ -146,6 +161,24 @@ public: protected: + template + void add_set_pointer_to_generic(const std::wstring & name, GenericType * value) + { + ValWrapper valcache; + valcache.val.set_pointer_to(value); + models_map[name] = valcache; + } + + + template + void add_set_generic(const std::wstring & name, GenericType & value) + { + ValWrapper valcache; + valcache.val.set(value); + models_map[name] = valcache; + } + + ModelsMap models_map; diff --git a/src/val.cpp b/src/val.cpp index b4d7ef6..e24e4bb 100644 --- a/src/val.cpp +++ b/src/val.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2024-2025, Tomasz Sowa + * Copyright (c) 2024-2026, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -77,17 +77,23 @@ void Val::copy(const Val & val) type = val.type; pointer = val.pointer; + if( type == Type::TYPE_SPACE_LOCAL || + type == Type::TYPE_SPACE_LOCAL_CHAR || + type == Type::TYPE_SPACE_LOCAL_UCHAR || + type == Type::TYPE_SPACE_LOCAL_WCHAR || + type == Type::TYPE_SPACE_LOCAL_ULLONG ) + { + space_local = val.space_local; + } + +#ifdef EZC_HAS_MORM_LIBRARY + if( type == Type::TYPE_SPACE ) { space_wrapper = val.space_wrapper; space_table_index = val.space_table_index; } - if( type == Type::TYPE_SPACE_LOCAL ) - { - space_local = val.space_local; - } - if( type == Type::TYPE_MODEL_METHOD ) { model_method1 = val.model_method1; @@ -95,6 +101,8 @@ void Val::copy(const Val & val) model_method4 = val.model_method4; } +#endif + // user_function = val.user_function; // model = val.model; // model_container_wrapper = val.model_container_wrapper; @@ -104,16 +112,21 @@ void Val::copy(const Val & val) // stream = val.stream; // space = val.space; - output_stream = val.output_stream; allow_to_cache_value = val.allow_to_cache_value; + output_stream = nullptr; // we do not copy the output stream +#ifdef EZC_HAS_MORM_LIBRARY increment_model_container_wrapper_ref(); increment_space_wrapper_ref(); +#endif + // childs_map don't need to be copied } +#ifdef EZC_HAS_MORM_LIBRARY + void Val::increment_model_container_wrapper_ref() { if( type == Type::TYPE_MODEL_CONTAINER_WRAPPER && pointer ) @@ -132,6 +145,8 @@ void Val::increment_space_wrapper_ref() } } +#endif + Val::~Val() { @@ -156,11 +171,15 @@ void Val::initialize_empty() { type = TYPE_VOID; pointer = nullptr; + +#ifdef EZC_HAS_MORM_LIBRARY model_method1 = nullptr; model_method3 = nullptr; model_method4 = nullptr; space_wrapper = nullptr; space_table_index = 0; +#endif + allow_to_cache_value = false; } @@ -174,6 +193,8 @@ void Val::clear() } +#ifdef EZC_HAS_MORM_LIBRARY + void Val::clear_model_container_wrapper() { if( type == Type::TYPE_MODEL_CONTAINER_WRAPPER && pointer ) @@ -188,6 +209,8 @@ void Val::clear_model_container_wrapper() } } +#endif + void Val::clear_space_wrapper() { @@ -232,8 +255,10 @@ bool Val::has_model_object() #endif +/* bool Val::has_pod_type() { + // what about floats, doubles here? return (type == Type::TYPE_CHAR || type == Type::TYPE_SHORT || type == Type::TYPE_INT || @@ -245,12 +270,7 @@ bool Val::has_pod_type() type == Type::TYPE_UNSIGNED_LONG || type == Type::TYPE_UNSIGNED_LONG_LONG); } - - -// bool Val::has_space_object() -// { -// return (type == Type::TYPE_SPACE && pointer) || (type == Type::TYPE_SPACE_LOCAL && space_local.is_object()); -// } +*/ bool Val::to_bool() const @@ -258,31 +278,133 @@ bool Val::to_bool() const switch(type) { case TYPE_VOID: + // we assume void is equal to false return false; case TYPE_SPACE_LOCAL: + case TYPE_SPACE_LOCAL_ULLONG: return space_local.to_bool(); + case TYPE_SPACE_LOCAL_CHAR: + return to_bool_space_local_char(); + + case TYPE_SPACE_LOCAL_UCHAR: + return to_bool_space_local_uchar(); + + case TYPE_SPACE_LOCAL_WCHAR: + return to_bool_space_local_wchar(); + case TYPE_STREAM: return to_bool_stream(); case TYPE_FUNCTION: - case TYPE_DATE: // date can be evaluated to true if it's different than the Unix epoch - case TYPE_MODEL: - case TYPE_ITEM_BLOCK: break; + case TYPE_DATE: + return to_bool_date(); + +#ifdef EZC_HAS_MORM_LIBRARY + + case TYPE_MODEL: + break; + + case TYPE_MODEL_CONTAINER_WRAPPER: + return to_bool_model_container_wrapper(); + + case TYPE_MODEL_METHOD: + break; + +#endif + case TYPE_SPACE: return to_bool_space(); - case TYPE_MODEL_CONTAINER_WRAPPER: - return to_bool_model_container_wrapper(); + case TYPE_ITEM_BLOCK: + break; + + case TYPE_CHAR: + return to_bool_char(); + + case TYPE_WCHAR: + return to_bool_wchar(); + + case TYPE_BOOL: + return to_bool_bool(); + + case TYPE_SHORT: + return to_bool_numeric(); + + case TYPE_INT: + return to_bool_numeric(); + + case TYPE_LONG: + return to_bool_numeric(); + + case TYPE_LONG_LONG: + return to_bool_numeric(); + + case TYPE_UNSIGNED_CHAR: + return to_bool_uchar(); + + case TYPE_UNSIGNED_SHORT: + return to_bool_numeric(); + + case TYPE_UNSIGNED_INT: + return to_bool_numeric(); + + case TYPE_UNSIGNED_LONG: + return to_bool_numeric(); + + case TYPE_UNSIGNED_LONG_LONG: + return to_bool_numeric(); + + case TYPE_FLOAT: + return to_bool_numeric(); + + case TYPE_DOUBLE: + return to_bool_numeric(); + + case TYPE_LONG_DOUBLE: + return to_bool_numeric(); + + case TYPE_STRING: + return to_bool_string(); + + case TYPE_WSTRING: + return to_bool_wstring(); } return false; } +bool Val::char_to_bool(wchar_t c) +{ + return c == 't'; +} + + +bool Val::to_bool_space_local_char() const +{ + char val = static_cast(space_local.to_llong()); + return char_to_bool(static_cast(val)); +} + + +bool Val::to_bool_space_local_uchar() const +{ + unsigned char val = static_cast(space_local.to_llong()); + return char_to_bool(static_cast(val)); +} + + +bool Val::to_bool_space_local_wchar() const +{ + wchar_t val = static_cast(space_local.to_llong()); + return char_to_bool(val); +} + + bool Val::to_bool_stream() const { pt::Stream * stream = reinterpret_cast(pointer); @@ -297,12 +419,65 @@ bool Val::to_bool_space() const } +bool Val::to_bool_date() const +{ + pt::Date * date = reinterpret_cast(pointer); + return !date->IsUnixEpoch(); +} + + +#ifdef EZC_HAS_MORM_LIBRARY + bool Val::to_bool_model_container_wrapper() const { morm::ModelContainerWrapper * model_container_wrapper = reinterpret_cast(pointer); return !model_container_wrapper->is_container_empty(); } +#endif + + +bool Val::to_bool_char() const +{ + char * c = reinterpret_cast(pointer); + return *c == 't'; +} + + +bool Val::to_bool_wchar() const +{ + wchar_t * c = reinterpret_cast(pointer); + return *c == 't'; +} + + +bool Val::to_bool_bool() const +{ + bool * c = reinterpret_cast(pointer); + return *c; +} + + +bool Val::to_bool_uchar() const +{ + unsigned char * c = reinterpret_cast(pointer); + return *c == 't'; +} + + +bool Val::to_bool_string() const +{ + std::string * c = reinterpret_cast(pointer); + return !c->empty(); +} + + +bool Val::to_bool_wstring() const +{ + std::wstring * c = reinterpret_cast(pointer); + return !c->empty(); +} + void Val::set(const char * str) { @@ -336,6 +511,30 @@ void Val::set(const std::wstring & str) } +void Val::set(char val) +{ + clear(); + type = TYPE_SPACE_LOCAL_CHAR; + space_local.set(static_cast(val)); +} + + +void Val::set(unsigned char val) +{ + clear(); + type = TYPE_SPACE_LOCAL_UCHAR; + space_local.set(static_cast(val)); +} + + +void Val::set(wchar_t val) +{ + clear(); + type = TYPE_SPACE_LOCAL_WCHAR; + space_local.set(static_cast(val)); +} + + void Val::set(bool val) { clear(); @@ -378,32 +577,26 @@ void Val::set(long long val) void Val::set(unsigned short val) { - clear(); - type = TYPE_SPACE_LOCAL; - space_local.set(val); + set(static_cast(val)); } void Val::set(unsigned int val) { - clear(); - type = TYPE_SPACE_LOCAL; - space_local.set(val); + set(static_cast(val)); } void Val::set(unsigned long val) { - clear(); - type = TYPE_SPACE_LOCAL; - space_local.set(val); + set(static_cast(val)); } void Val::set(unsigned long long val) { clear(); - type = TYPE_SPACE_LOCAL; + type = TYPE_SPACE_LOCAL_ULLONG; space_local.set(val); } @@ -432,6 +625,115 @@ void Val::set(long double val) } + +Val & Val::operator=(char val) +{ + set(val); + return *this; +} + + +Val & Val::operator=(unsigned char val) +{ + set(val); + return *this; +} + + +Val & Val::operator=(wchar_t val) +{ + set(val); + return *this; +} + + +Val & Val::operator=(bool val) +{ + set(val); + return *this; +} + + +Val & Val::operator=(short val) +{ + set(val); + return *this; +} + + +Val & Val::operator=(int val) +{ + set(val); + return *this; +} + + +Val & Val::operator=(long val) +{ + set(val); + return *this; +} + + +Val & Val::operator=(long long val) +{ + set(val); + return *this; +} + + +Val & Val::operator=(unsigned short val) +{ + set(val); + return *this; +} + + +Val & Val::operator=(unsigned int val) +{ + set(val); + return *this; +} + + +Val & Val::operator=(unsigned long val) +{ + set(val); + return *this; +} + + +Val & Val::operator=(unsigned long long val) +{ + set(val); + return *this; +} + + +Val & Val::operator=(float val) +{ + set(val); + return *this; +} + + +Val & Val::operator=(double val) +{ + set(val); + return *this; +} + + +Val & Val::operator=(long double val) +{ + set(val); + return *this; +} + + + + + void Val::set_pointer_to(pt::Stream * str) { clear(); @@ -448,7 +750,22 @@ void Val::set_pointer_to(UserFunction user_function) } +Val & Val::operator=(pt::Stream * str) +{ + set_pointer_to(str); + return *this; +} + + +Val & Val::operator=(UserFunction user_function) +{ + set_pointer_to(user_function); + return *this; +} + + #ifdef EZC_HAS_MORM_LIBRARY + void Val::set_pointer_to(morm::Model * model, ModelMethod1 model_method1) { clear(); @@ -474,15 +791,6 @@ void Val::set_pointer_to(morm::Model * model, ModelMethod4 model_method4) this->pointer = model; this->model_method4 = model_method4; } -#endif - - -void Val::set_pointer_to(pt::Date * date) -{ - clear(); - type = TYPE_DATE; - this->pointer = date; -} void Val::set_pointer_to(morm::Model * model) @@ -527,6 +835,19 @@ void Val::set_pointer_to(pt::Space * space, bool create_wrapper) } } +#endif + + +void Val::set_pointer_to(pt::Date * date) +{ + clear(); + type = TYPE_DATE; + this->pointer = date; +} + + + + // void Val::set(Val & val) // { @@ -594,12 +915,6 @@ void Val::set_pointer_to(Item * item_block) } -void Val::set_pointer_to(bool * val) -{ - // IMPLEMENT ME -} - - void Val::set_pointer_to(char * val) { clear(); @@ -616,6 +931,14 @@ void Val::set_pointer_to(wchar_t * val) } +void Val::set_pointer_to(bool * val) +{ + clear(); + type = Val::TYPE_BOOL; + pointer = val; +} + + void Val::set_pointer_to(short * val) { clear(); @@ -712,19 +1035,19 @@ void Val::set_pointer_to(long double * val) } -void Val::set_pointer_to(std::string * val) +void Val::set_pointer_to(const std::string * val) { clear(); type = TYPE_STRING; - pointer = val; + pointer = const_cast(val); // is it correct to drop const here? } -void Val::set_pointer_to(std::wstring * val) +void Val::set_pointer_to(const std::wstring * val) { clear(); type = TYPE_WSTRING; - pointer = val; + pointer = const_cast(val); // is it correct to drop const here? } @@ -810,97 +1133,99 @@ void Val::set_pointer_to(std::wstring * val) -// bool Val::is_equal_bool(const char * str) const -// { -// if( space_local.to_bool() ) -// { -// return str[0] != 0; -// } -// else -// { -// return str[0] == 0; -// } -// } - - - -// bool Val::is_equal_string(const char * str) const -// { -// if( space_local.is_str() ) -// { -// return space_local.is_equal(str); -// } -// else -// if( space_local.is_wstr() ) -// { -// std::string space_str_utf8; -// pt::wide_to_utf8(*space_local.get_wstr(), space_str_utf8); -// return space_str_utf8 == str; -// } - -// return false; -// } - - - - -// bool Val::is_equal_bool(const wchar_t * str) const -// { -// if( space_local.to_bool() ) -// { -// return str[0] != 0; -// } -// else -// { -// return str[0] == 0; -// } -// } - - - bool Val::is_equal_string(const wchar_t * str) const { - bool found = false; + bool is_equal = false; - if( type == Type::TYPE_SPACE_LOCAL ) + if( type == Type::TYPE_SPACE_LOCAL || + type == Type::TYPE_SPACE_LOCAL_ULLONG ) { - found = is_space_equal_string(space_local, str); + is_equal = is_space_equal_string(space_local, str); + } + else + if( type == Type::TYPE_SPACE_LOCAL_CHAR ) + { + wchar_t val = (wchar_t)(unsigned char)(char)(space_local.to_llong()); + is_equal = (*str != 0 && *str == val && *(str+1) == 0); + } + else + if( type == Type::TYPE_SPACE_LOCAL_UCHAR ) + { + wchar_t val = (wchar_t)(unsigned char)(space_local.to_llong()); + is_equal = (*str != 0 && *str == val && *(str+1) == 0); + } + else + if( type == Type::TYPE_SPACE_LOCAL_WCHAR ) + { + wchar_t val = (wchar_t)(space_local.to_llong()); + is_equal = (*str != 0 && *str == val && *(str+1) == 0); } else if( type == Type::TYPE_SPACE && pointer ) { pt::Space * space = reinterpret_cast(pointer); - found = is_space_equal_string(*space, str); + is_equal = is_space_equal_string(*space, str); } else if( type == Type::TYPE_STREAM && pointer ) { pt::Stream * stream = reinterpret_cast(pointer); - found = is_stream_equal_string(*stream, str); + is_equal = is_stream_equal_string(*stream, str); + } + else + if( type == Type::TYPE_CHAR ) + { + char * c = reinterpret_cast(pointer); + is_equal = is_char_equal_string(static_cast(*c), str); + } + else + if( type == Type::TYPE_WCHAR ) + { + wchar_t * c = reinterpret_cast(pointer); + is_equal = is_wchar_equal_string(*c, str); + } + else + if( type == Type::TYPE_UNSIGNED_CHAR ) + { + unsigned char * c = reinterpret_cast(pointer); + is_equal = is_char_equal_string(*c, str); + } + else + if( type == Type::TYPE_STRING ) + { + std::string * loc_str = reinterpret_cast(pointer); + size_t len = wcslen(str); + int compare_status = pt::compare_utf8_to_wide(loc_str->c_str(), loc_str->size(), str, len); + is_equal = (compare_status == 0); + } + else + if( type == Type::TYPE_WSTRING ) + { + std::wstring * loc_str = reinterpret_cast(pointer); + is_equal = (*loc_str == str); } - return found; + return is_equal; } - bool Val::is_space_equal_string(const pt::Space & space, const wchar_t * str) const { - bool found = false; + bool is_equal = false; if( space.is_wstr() ) { - found = space.is_equal(str); + is_equal = space.is_equal(str); } else if( space.is_str() ) { - std::string str_utf8; + std::string str_utf8; ///////////////////////////////// poprawic z uzyciem compare pt::wide_to_utf8(str, str_utf8); - found = space.is_equal(str_utf8); + is_equal = space.is_equal(str_utf8); } - return found; + return is_equal; } @@ -941,6 +1266,17 @@ bool Val::is_stream_equal_string(const pt::Stream & stream, const wchar_t * str) } +bool Val::is_char_equal_string(unsigned char c, const wchar_t * str) const +{ + return (*str != 0) && (*(str+1) == static_cast(c)) && (*(str+2) == 0); +} + + +bool Val::is_wchar_equal_string(wchar_t c, const wchar_t * str) const +{ + return (*str != 0) && (*(str+1) == c) && (*(str+2) == 0); +} + void Val::serialize_to(pt::Stream & str) { @@ -950,6 +1286,22 @@ void Val::serialize_to(pt::Stream & str) space_local.serialize_to_string(str); break; + case TYPE_SPACE_LOCAL_ULLONG: + str << space_local.to_ullong(); + break; + + case TYPE_SPACE_LOCAL_CHAR: + str << static_cast(space_local.to_llong()); + break; + + case TYPE_SPACE_LOCAL_UCHAR: + str << static_cast(space_local.to_llong()); + break; + + case TYPE_SPACE_LOCAL_WCHAR: + str << static_cast(space_local.to_llong()); + break; + case TYPE_STREAM: serialize_stream_to(str); break; @@ -958,6 +1310,7 @@ void Val::serialize_to(pt::Stream & str) serialize_date_to(str); break; +#ifdef EZC_HAS_MORM_LIBRARY case TYPE_MODEL: serialize_model_to(str); break; @@ -965,6 +1318,7 @@ void Val::serialize_to(pt::Stream & str) case TYPE_MODEL_CONTAINER_WRAPPER: serialize_model_container_wrapper_to(str); break; +#endif case TYPE_SPACE: serialize_space_to(str); @@ -979,37 +1333,55 @@ void Val::serialize_to(pt::Stream & str) break; case TYPE_CHAR: - serialize_pod_type_to(str); + serialize_generic_to(str); break; case TYPE_WCHAR: - serialize_pod_type_to(str); + serialize_generic_to(str); + break; + case TYPE_BOOL: + serialize_generic_to(str); break; case TYPE_SHORT: - serialize_pod_type_to(str); + serialize_generic_to(str); break; case TYPE_INT: - serialize_pod_type_to(str); + serialize_generic_to(str); break; case TYPE_LONG: - serialize_pod_type_to(str); + serialize_generic_to(str); break; case TYPE_LONG_LONG: - serialize_pod_type_to(str); + serialize_generic_to(str); break; case TYPE_UNSIGNED_CHAR: - serialize_pod_type_to(str); + serialize_generic_to(str); break; case TYPE_UNSIGNED_SHORT: - serialize_pod_type_to(str); + serialize_generic_to(str); break; case TYPE_UNSIGNED_INT: - serialize_pod_type_to(str); + serialize_generic_to(str); break; case TYPE_UNSIGNED_LONG: - serialize_pod_type_to(str); + serialize_generic_to(str); break; case TYPE_UNSIGNED_LONG_LONG: - serialize_pod_type_to(str); + serialize_generic_to(str); + break; + case TYPE_FLOAT: + serialize_generic_to(str); + break; + case TYPE_DOUBLE: + serialize_generic_to(str); + break; + case TYPE_LONG_DOUBLE: + serialize_generic_to(str); + break; + case TYPE_STRING: + serialize_generic_to(str); + break; + case TYPE_WSTRING: + serialize_generic_to(str); break; } } @@ -1029,6 +1401,8 @@ void Val::serialize_date_to(pt::Stream & str) } +#ifdef EZC_HAS_MORM_LIBRARY + void Val::serialize_model_to(pt::Stream & str) { morm::Model * model = reinterpret_cast(pointer); @@ -1047,6 +1421,8 @@ void Val::serialize_model_container_wrapper_to(pt::Stream & str) } } +#endif + void Val::serialize_space_to(pt::Stream & str) { @@ -1275,80 +1651,6 @@ Val & Val::operator<<(const pt::Stream & str) } -Val & Val::operator=(bool res) -{ - set(res); - return *this; -} - - -void Val::put_pod_type_to_stream() -{ - if( pointer ) - { - if( type == Type::TYPE_CHAR ) - { - char * val = reinterpret_cast(pointer); - (*output_stream) << *val; - } - - if( type == Type::TYPE_SHORT ) - { - short * val = reinterpret_cast(pointer); - (*output_stream) << *val; - } - - if( type == Type::TYPE_INT ) - { - int * val = reinterpret_cast(pointer); - (*output_stream) << *val; - } - - if( type == Type::TYPE_LONG ) - { - long * val = reinterpret_cast(pointer); - (*output_stream) << *val; - } - - if( type == Type::TYPE_LONG_LONG ) - { - long long * val = reinterpret_cast(pointer); - (*output_stream) << *val; - } - - if( type == Type::TYPE_UNSIGNED_CHAR ) - { - unsigned char * val = reinterpret_cast(pointer); - (*output_stream) << *val; - } - - if( type == Type::TYPE_UNSIGNED_SHORT ) - { - unsigned short * val = reinterpret_cast(pointer); - (*output_stream) << *val; - } - - if( type == Type::TYPE_UNSIGNED_INT ) - { - unsigned int * val = reinterpret_cast(pointer); - (*output_stream) << *val; - } - - if( type == Type::TYPE_UNSIGNED_LONG ) - { - unsigned long * val = reinterpret_cast(pointer); - (*output_stream) << *val; - } - - if( type == Type::TYPE_UNSIGNED_LONG_LONG ) - { - unsigned long long * val = reinterpret_cast(pointer); - (*output_stream) << *val; - } - } -} - - void Val::allow_to_cache(bool allow_to_cache) { this->allow_to_cache_value = allow_to_cache; diff --git a/src/val.h b/src/val.h index b334545..c2916b1 100644 --- a/src/val.h +++ b/src/val.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2024, Tomasz Sowa + * Copyright (c) 2024-2026, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -39,27 +39,27 @@ #include "date/date.h" #include "item.h" #include "modelcontainerwrapper.h" -#include "textstream/textstream.h" +#ifdef EZC_HAS_MORM_LIBRARY namespace morm { class Model; } - +#endif namespace Ezc { - struct Env; /* - -*/ + * + * + */ class Val { public: @@ -76,33 +76,38 @@ public: enum Type { TYPE_VOID, - TYPE_SPACE_LOCAL, - TYPE_STREAM, - TYPE_FUNCTION, - TYPE_DATE, + TYPE_SPACE_LOCAL, // used: space_local + TYPE_SPACE_LOCAL_CHAR, // used: space_local, a char saved as a long long in the space_local + TYPE_SPACE_LOCAL_UCHAR, // used: space_local, an unsigned char saved as a long long in the space_local + TYPE_SPACE_LOCAL_WCHAR, // used: space_local, a wchar_t saved as a long long in the space_local + TYPE_SPACE_LOCAL_ULLONG, // used: space_local, an unsigned long long saved as a long long in the space_local + TYPE_STREAM, // used: pointer + TYPE_FUNCTION, // used: pointer + TYPE_DATE, // used: pointer #ifdef EZC_HAS_MORM_LIBRARY - TYPE_MODEL, - TYPE_MODEL_CONTAINER_WRAPPER, - TYPE_MODEL_METHOD, + TYPE_MODEL, // used: pointer + TYPE_MODEL_CONTAINER_WRAPPER, // used: pointer + TYPE_MODEL_METHOD, // used: pointer and (model_method1 or model_method3 or model_method4) #endif - TYPE_SPACE, - TYPE_ITEM_BLOCK, - TYPE_CHAR, - TYPE_WCHAR, // - TYPE_SHORT, - TYPE_INT, - TYPE_LONG, - TYPE_LONG_LONG, - TYPE_UNSIGNED_CHAR, - TYPE_UNSIGNED_SHORT, - TYPE_UNSIGNED_INT, - TYPE_UNSIGNED_LONG, - TYPE_UNSIGNED_LONG_LONG, - TYPE_FLOAT, - TYPE_DOUBLE, - TYPE_LONG_DOUBLE, - TYPE_STRING, - TYPE_WSTRING, + TYPE_SPACE, // used: pointer, space_wrapper and space_table_index + TYPE_ITEM_BLOCK, // used: pointer + TYPE_CHAR, // used: pointer + TYPE_WCHAR, // used: pointer + TYPE_BOOL, // used: pointer + TYPE_SHORT, // used: pointer + TYPE_INT, // used: pointer + TYPE_LONG, // used: pointer + TYPE_LONG_LONG, // used: pointer + TYPE_UNSIGNED_CHAR, // used: pointer + TYPE_UNSIGNED_SHORT, // used: pointer + TYPE_UNSIGNED_INT, // used: pointer + TYPE_UNSIGNED_LONG, // used: pointer + TYPE_UNSIGNED_LONG_LONG, // used: pointer + TYPE_FLOAT, // used: pointer + TYPE_DOUBLE, // used: pointer + TYPE_LONG_DOUBLE, // used: pointer + TYPE_STRING, // used: pointer + TYPE_WSTRING, // used: pointer }; @@ -124,8 +129,8 @@ public: bool has_model_object(); #endif - bool has_pod_type(); - //bool has_space_object(); + + //bool has_pod_type(); void clear(); @@ -138,6 +143,9 @@ public: void set(const std::string & str); void set(const std::wstring & str); + void set(char val); + void set(unsigned char val); + void set(wchar_t val); void set(bool val); void set(short val); void set(int val); @@ -147,22 +155,43 @@ public: void set(unsigned int val); void set(unsigned long val); void set(unsigned long long val); - void set(float val); void set(double val); void set(long double val); + Val & operator=(char val); + Val & operator=(unsigned char val); + Val & operator=(wchar_t val); + Val & operator=(bool val); + Val & operator=(short val); + Val & operator=(int val); + Val & operator=(long val); + Val & operator=(long long val); + Val & operator=(unsigned short val); + Val & operator=(unsigned int val); + Val & operator=(unsigned long val); + Val & operator=(unsigned long long val); + Val & operator=(float val); + Val & operator=(double val); + Val & operator=(long double val); void set_pointer_to(pt::Stream * str); void set_pointer_to(UserFunction user_function); + + Val & operator=(pt::Stream * str); + Val & operator=(UserFunction user_function); + #ifdef EZC_HAS_MORM_LIBRARY void set_pointer_to(morm::Model * model, ModelMethod1 model_method1); void set_pointer_to(morm::Model * model, ModelMethod3 model_method3); void set_pointer_to(morm::Model * model, ModelMethod4 model_method4); -#endif - void set_pointer_to(pt::Date * date); void set_pointer_to(morm::Model * model); void set_pointer_to(morm::ModelContainerWrapper * model_container_wrapper); + void set_pointer_to(morm::SpaceWrapper * space_wrapper); + void set_pointer_to(pt::Space * space, bool create_wrapper = true); +#endif + + void set_pointer_to(pt::Date * date); template void set_pointer_to(std::vector * model_container); @@ -176,8 +205,6 @@ public: template void set_pointer_to(std::list * model_container); - void set_pointer_to(morm::SpaceWrapper * space_wrapper); - void set_pointer_to(pt::Space * space, bool create_wrapper = true); //void set(Val & val); @@ -201,8 +228,8 @@ public: void set_pointer_to(double * val); void set_pointer_to(long double * val); - void set_pointer_to(std::string * val); - void set_pointer_to(std::wstring * val); + void set_pointer_to(const std::string * val); + void set_pointer_to(const std::wstring * val); // bool is_equal(const char * str) const; @@ -235,12 +262,6 @@ public: Val & operator<<(long double val); Val & operator<<(const pt::Stream & str); - Val & operator=(bool res); - - - void put_pod_type_to_stream(); - - void allow_to_cache(bool allow_to_cache); bool is_allowed_to_cache(); @@ -253,92 +274,93 @@ public: ModelMethod1 model_method1; ModelMethod3 model_method3; ModelMethod4 model_method4; -#endif - // used when pointer is pointing to a pt::Space // but can be null morm::SpaceWrapper * space_wrapper; size_t space_table_index; +#endif + bool allow_to_cache_value; - // UserFunction user_function; - - // // Wrapper - // morm::Model * model; - // morm::ModelContainerWrapper * model_container_wrapper; - // pt::Date * date; - // morm::SpaceWrapper * space_wrapper; - // // - - // pt::Space * space; - // pt::Stream * stream; - - // Item * item_block; - - - - // output stream - pt::Stream * output_stream; bool is_equal_string(const wchar_t * str) const; + static bool char_to_bool(wchar_t c); + private: + // output stream, can be null + pt::Stream * output_stream; void initialize_empty(); void copy(const Val & val); + +#ifdef EZC_HAS_MORM_LIBRARY void increment_model_container_wrapper_ref(); void increment_space_wrapper_ref(); +#endif + +#ifdef EZC_HAS_MORM_LIBRARY void clear_model_container_wrapper(); +#endif + void clear_space_wrapper(); - // bool is_equal_bool(const char * str) const; - // bool is_equal_string(const char * str) const; - - // bool is_equal_bool(const wchar_t * str) const; - bool is_space_equal_string(const pt::Space & space, const wchar_t * str) const; bool is_stream_equal_string(const pt::Stream & stream, const wchar_t * str) const; + bool is_char_equal_string(unsigned char c, const wchar_t * str) const; + bool is_wchar_equal_string(wchar_t c, const wchar_t * str) const; + bool to_bool_space_local_char() const; + bool to_bool_space_local_uchar() const; + bool to_bool_space_local_wchar() const; bool to_bool_stream() const; bool to_bool_space() const; + bool to_bool_date() const; + +#ifdef EZC_HAS_MORM_LIBRARY bool to_bool_model_container_wrapper() const; +#endif + + bool to_bool_char() const; + bool to_bool_wchar() const; + bool to_bool_bool() const; + bool to_bool_uchar() const; + bool to_bool_string() const; + bool to_bool_wstring() const; + + template + bool to_bool_numeric() const + { + NumericType * val = reinterpret_cast(pointer); + return *val != static_cast(0); + } void serialize_stream_to(pt::Stream & str); void serialize_date_to(pt::Stream & str); + +#ifdef EZC_HAS_MORM_LIBRARY + void serialize_model_to(pt::Stream & str); void serialize_model_container_wrapper_to(pt::Stream & str); + +#endif + void serialize_space_to(pt::Stream & str); - template - void serialize_pod_type_to(pt::Stream & str) + template + void serialize_generic_to(pt::Stream & str) { - PODType * val = reinterpret_cast(pointer); + PointerType * val = reinterpret_cast(pointer); str << *val; } - - /* - * old - */ - - - /* - * if true then means 'str' is a function name and should be called (res is ignored) - * - * if false then means 'str' is a string value and res is a boolean value - */ - //bool is_function; - - //std::wstring str; // a string value - //bool res; // a boolean value - };