changed how models from morm library are used

now we are using morm::ModelWrapper... classes as wrappers on models and list/vector of models
and Models class is using these wrappers
this allows us to iterate through list/vectors in [for...] statements
This commit is contained in:
Tomasz Sowa 2021-06-16 14:16:49 +02:00
parent e6fd9aad37
commit 9022d4a5fc
4 changed files with 339 additions and 184 deletions

View File

@ -1,41 +1,23 @@
# DO NOT DELETE # DO NOT DELETE
blocks.o: blocks.h item.h cache.h functions.h ../../pikotools/src/utf8/utf8.h blocks.o: blocks.h item.h cache.h functions.h ../../pikotools/src/utf8/utf8.h
blocks.o: ../../pikotools/src/textstream/textstream.h
blocks.o: ../../pikotools/src/space/space.h
blocks.o: ../../pikotools/src/textstream/types.h
blocks.o: ../../pikotools/src/convert/inttostr.h
blocks.o: ../../pikotools/src/date/date.h
blocks.o: ../../pikotools/src/membuffer/membuffer.h
blocks.o: ../../pikotools/src/textstream/types.h
blocks.o: ../../pikotools/src/utf8/utf8_templates.h blocks.o: ../../pikotools/src/utf8/utf8_templates.h
blocks.o: ../../pikotools/src/utf8/utf8_private.h funinfo.h objects.h blocks.o: ../../pikotools/src/utf8/utf8_private.h funinfo.h objects.h
cache.o: cache.h item.h functions.h ../../pikotools/src/utf8/utf8.h cache.o: cache.h item.h functions.h ../../pikotools/src/utf8/utf8.h
cache.o: ../../pikotools/src/textstream/textstream.h
cache.o: ../../pikotools/src/space/space.h
cache.o: ../../pikotools/src/textstream/types.h
cache.o: ../../pikotools/src/convert/inttostr.h
cache.o: ../../pikotools/src/date/date.h
cache.o: ../../pikotools/src/membuffer/membuffer.h
cache.o: ../../pikotools/src/textstream/types.h
cache.o: ../../pikotools/src/utf8/utf8_templates.h cache.o: ../../pikotools/src/utf8/utf8_templates.h
cache.o: ../../pikotools/src/utf8/utf8_private.h funinfo.h objects.h blocks.h cache.o: ../../pikotools/src/utf8/utf8_private.h funinfo.h objects.h blocks.h
item.o: item.h item.o: item.h
models.o: models.h models.o: models.h
pattern.o: pattern.h item.h cache.h functions.h pattern.o: pattern.h item.h cache.h functions.h
pattern.o: ../../pikotools/src/utf8/utf8.h pattern.o: ../../pikotools/src/utf8/utf8.h
pattern.o: ../../pikotools/src/textstream/textstream.h
pattern.o: ../../pikotools/src/space/space.h
pattern.o: ../../pikotools/src/textstream/types.h
pattern.o: ../../pikotools/src/convert/inttostr.h
pattern.o: ../../pikotools/src/date/date.h
pattern.o: ../../pikotools/src/membuffer/membuffer.h
pattern.o: ../../pikotools/src/textstream/types.h
pattern.o: ../../pikotools/src/utf8/utf8_templates.h pattern.o: ../../pikotools/src/utf8/utf8_templates.h
pattern.o: ../../pikotools/src/utf8/utf8_private.h funinfo.h objects.h pattern.o: ../../pikotools/src/utf8/utf8_private.h funinfo.h objects.h
pattern.o: blocks.h pattern.o: blocks.h
patternparser.o: patternparser.h blocks.h item.h cache.h functions.h patternparser.o: patternparser.h blocks.h item.h cache.h functions.h
patternparser.o: ../../pikotools/src/utf8/utf8.h patternparser.o: ../../pikotools/src/utf8/utf8.h
patternparser.o: ../../pikotools/src/utf8/utf8_templates.h
patternparser.o: ../../pikotools/src/utf8/utf8_private.h funinfo.h objects.h
patternparser.o: pattern.h ../../pikotools/src/log/log.h
patternparser.o: ../../pikotools/src/textstream/textstream.h patternparser.o: ../../pikotools/src/textstream/textstream.h
patternparser.o: ../../pikotools/src/space/space.h patternparser.o: ../../pikotools/src/space/space.h
patternparser.o: ../../pikotools/src/textstream/types.h patternparser.o: ../../pikotools/src/textstream/types.h
@ -43,9 +25,6 @@ patternparser.o: ../../pikotools/src/convert/inttostr.h
patternparser.o: ../../pikotools/src/date/date.h patternparser.o: ../../pikotools/src/date/date.h
patternparser.o: ../../pikotools/src/membuffer/membuffer.h patternparser.o: ../../pikotools/src/membuffer/membuffer.h
patternparser.o: ../../pikotools/src/textstream/types.h patternparser.o: ../../pikotools/src/textstream/types.h
patternparser.o: ../../pikotools/src/utf8/utf8_templates.h
patternparser.o: ../../pikotools/src/utf8/utf8_private.h funinfo.h objects.h
patternparser.o: pattern.h ../../pikotools/src/log/log.h
patternparser.o: ../../pikotools/src/log/filelog.h patternparser.o: ../../pikotools/src/log/filelog.h
patternparser.o: ../../pikotools/src/convert/convert.h patternparser.o: ../../pikotools/src/convert/convert.h
patternparser.o: ../../pikotools/src/convert/inttostr.h patternparser.o: ../../pikotools/src/convert/inttostr.h

View File

@ -50,6 +50,9 @@
#include "expressionparser.h" #include "expressionparser.h"
#include "models.h" #include "models.h"
#ifdef EZC_HAS_MORM_LIBRARY
#include "model.h"
#endif
namespace Ezc namespace Ezc
{ {
@ -75,7 +78,10 @@ public:
void SetFunctions(Functions<StreamType> & functions); void SetFunctions(Functions<StreamType> & functions);
void SetObjects(Objects<StreamType> & objects); void SetObjects(Objects<StreamType> & objects);
void SetVariables(Vars & variables); // [def] and [let] void SetVariables(Vars & variables); // [def] and [let]
#ifdef EZC_HAS_MORM_LIBRARY
void SetModels(Models & models); void SetModels(Models & models);
#endif
void SetMax(size_t max_items_, size_t max_for_items_); void SetMax(size_t max_items_, size_t max_for_items_);
@ -133,6 +139,53 @@ public:
private: private:
struct FindHelper
{
#ifdef EZC_HAS_MORM_LIBRARY
morm::ModelWrapper * model_wrapper;
#endif
BaseObj<StreamType> * base_obj;
int method_index;
typename Functions<StreamType>::UserFunction * function;
Item * item_block;
Var * variable;
FindHelper()
{
#ifdef EZC_HAS_MORM_LIBRARY
model_wrapper = nullptr;
#endif
base_obj = nullptr;
method_index = -1;
function = nullptr;
item_block = nullptr;
variable = nullptr;
}
};
#ifdef EZC_HAS_MORM_LIBRARY
struct CallModelHelper
{
size_t field_index;
morm::ModelWrapper * model_wrapper;
morm::Model * model;
CallModelHelper()
{
field_index = 0;
model_wrapper = nullptr;
model = nullptr;
}
};
#endif
struct BlockStack struct BlockStack
{ {
std::vector<Var> args; std::vector<Var> args;
@ -152,7 +205,9 @@ private:
Blocks * pblocks; Blocks * pblocks;
Functions<StreamType> * pfunctions; Functions<StreamType> * pfunctions;
Objects<StreamType> * pobjects; Objects<StreamType> * pobjects;
#ifdef EZC_HAS_MORM_LIBRARY
Models * pmodels; Models * pmodels;
#endif
Vars * pvars; Vars * pvars;
// pointer to the output streams map (can be null) // pointer to the output streams map (can be null)
@ -238,38 +293,25 @@ private:
CharType ToLower(CharType c); CharType ToLower(CharType c);
bool CheckBlockArgument(int arg_index, Var ** variable); bool CheckBlockArgument(int arg_index, FindHelper & find_helper);
bool FindInCache(Item::Function & item_fun, bool FindInCache(Item::Function & item_fun, FindHelper & find_helper);
BaseObj<StreamType> ** base_obj,
int * method_index,
typename Functions<StreamType>::UserFunction ** function,
Item ** item_block);
bool FindInModels(const std::wstring & name, morm::Model ** model); #ifdef EZC_HAS_MORM_LIBRARY
bool FindInModels(const std::wstring & name, FindHelper & find_helper);
#endif
bool FindInFunctionsAndBlocks(const std::wstring & name, bool FindInFunctionsAndBlocks(const std::wstring & name, FindHelper & find_helper);
BaseObj<StreamType> ** base_obj,
int * method_index,
typename Functions<StreamType>::UserFunction ** function,
Item ** item_block);
bool FindInVariables(const std::wstring & name, bool FindInVariables(const std::wstring & name, FindHelper & find_helper);
Var ** variable);
bool Find(Item::Function & item_fun, bool Find(Item::Function & item_fun,
std::wstring * fun_name, std::wstring & fun_name,
BaseObj<StreamType> ** base_obj, FindHelper & find_helper);
int * method_index,
typename Functions<StreamType>::UserFunction ** function,
Item ** item_block,
Var ** variable);
void CallFunction(typename Functions<StreamType>::UserFunction * function, void CallFunction(typename Functions<StreamType>::UserFunction & function, FunInfo<StreamType> & info);
FunInfo<StreamType> & info);
void CallFunction(typename Functions<StreamType>::UserFunction * function, void CallFunction(typename Functions<StreamType>::UserFunction & function,
std::vector<Var> & parameters, std::vector<Var> & parameters,
StreamType & out_stream, StreamType & out_stream,
const StreamType & in_stream); const StreamType & in_stream);
@ -278,18 +320,22 @@ private:
std::vector<Var> & parameters, std::vector<Var> & parameters,
StreamType & out_stream); StreamType & out_stream);
void CallObject(BaseObj<StreamType> * base_obj, int method_index, FunInfo<StreamType> & info); void CallObject(BaseObj<StreamType> & base_obj, int method_index, FunInfo<StreamType> & info);
void CallModel(morm::Model & model, std::vector<std::wstring> & fields, std::vector<Var> parameters, StreamType & out_stream, const StreamType & in_stream); #ifdef EZC_HAS_MORM_LIBRARY
void CallModel(morm::Model & model, const std::wstring & field, std::vector<Var> parameters, StreamType & out_stream, const StreamType & in_stream);
CallModelHelper FindLastModelWrapper(morm::ModelWrapper & models_base, std::vector<std::wstring> & fields);
void CallModelWrapper(morm::ModelWrapper & models_base, std::vector<std::wstring> & fields, std::vector<Var> parameters, StreamType & out_stream, const StreamType & in_stream);
#endif
void CallObject(BaseObj<StreamType> * base_obj, void CallObject(BaseObj<StreamType> & base_obj,
int method_index, int method_index,
std::vector<Var> & parameters, std::vector<Var> & parameters,
StreamType & out_stream, StreamType & out_stream,
const StreamType & in_stream); const StreamType & in_stream);
bool CallVariable(Item::Function & item_fun, bool CallVariable(Item::Function & item_fun,
Var * variable, Var & variable,
std::vector<Var> & parameters, std::vector<Var> & parameters,
StreamType & out_stream, StreamType & out_stream,
const StreamType & in_stream); const StreamType & in_stream);
@ -362,7 +408,10 @@ Generator<StreamType>::Generator() : empty_stream()
pfunctions = nullptr; pfunctions = nullptr;
pobjects = nullptr; pobjects = nullptr;
pvars = nullptr; pvars = nullptr;
#ifdef EZC_HAS_MORM_LIBRARY
pmodels = nullptr; pmodels = nullptr;
#endif
max_items = 50000; max_items = 50000;
max_for_items = 5000; max_for_items = 5000;
@ -397,7 +446,10 @@ Generator<StreamType> & Generator<StreamType>::operator=(const Generator<StreamT
pfunctions = n.pfunctions; pfunctions = n.pfunctions;
pobjects = n.pobjects; pobjects = n.pobjects;
pvars = n.pvars; pvars = n.pvars;
#ifdef EZC_HAS_MORM_LIBRARY
pmodels = n.pmodels; pmodels = n.pmodels;
#endif
max_items = n.max_items; max_items = n.max_items;
max_for_items = n.max_for_items; max_for_items = n.max_for_items;
@ -515,12 +567,13 @@ void Generator<StreamType>::SetVariables(Vars & variables)
} }
#ifdef EZC_HAS_MORM_LIBRARY
template<class StreamType> template<class StreamType>
void Generator<StreamType>::SetModels(Models & models) void Generator<StreamType>::SetModels(Models & models)
{ {
pmodels = &models; pmodels = &models;
} }
#endif
template<class StreamType> template<class StreamType>
void Generator<StreamType>::CanUseCache(bool can_use_cache) void Generator<StreamType>::CanUseCache(bool can_use_cache)
@ -834,7 +887,7 @@ void Generator<StreamType>::Generate(OutStreams<StreamType> & out_streams)
template<class StreamType> template<class StreamType>
bool Generator<StreamType>::CheckBlockArgument(int arg_index, Var ** variable) bool Generator<StreamType>::CheckBlockArgument(int arg_index, FindHelper & find_helper)
{ {
if( arg_index < 0 ) if( arg_index < 0 )
return false; return false;
@ -846,7 +899,7 @@ bool Generator<StreamType>::CheckBlockArgument(int arg_index, Var ** variable)
if( size_t(arg_index) < block_stack.args.size() ) if( size_t(arg_index) < block_stack.args.size() )
{ {
*variable = &block_stack.args[arg_index]; find_helper.variable = &block_stack.args[arg_index];
return true; return true;
} }
} }
@ -857,30 +910,26 @@ bool Generator<StreamType>::CheckBlockArgument(int arg_index, Var ** variable)
template<class StreamType> template<class StreamType>
bool Generator<StreamType>::FindInCache(Item::Function & item_fun, bool Generator<StreamType>::FindInCache(Item::Function & item_fun, FindHelper & find_helper)
BaseObj<StreamType> ** base_obj,
int * method_index,
typename Functions<StreamType>::UserFunction ** function,
Item ** item_block)
{ {
if( can_find_in_cache ) if( can_find_in_cache )
{ {
if( item_fun.base_obj ) if( item_fun.base_obj )
{ {
*base_obj = reinterpret_cast<BaseObj<StreamType> * >(item_fun.base_obj); find_helper.base_obj = reinterpret_cast<BaseObj<StreamType> * >(item_fun.base_obj);
*method_index = item_fun.method_index; find_helper.method_index = item_fun.method_index;
return true; return true;
} }
if( item_fun.fun_cache ) if( item_fun.fun_cache )
{ {
*function = reinterpret_cast<typename Functions<StreamType>::UserFunction*>(item_fun.fun_cache); find_helper.function = reinterpret_cast<typename Functions<StreamType>::UserFunction*>(item_fun.fun_cache);
return true; return true;
} }
if( item_fun.item_block ) if( item_fun.item_block )
{ {
*item_block = item_fun.item_block; find_helper.item_block = item_fun.item_block;
return true; return true;
} }
} }
@ -890,19 +939,18 @@ return false;
#ifdef EZC_HAS_MORM_LIBRARY
template<class StreamType> template<class StreamType>
bool Generator<StreamType>::FindInModels(const std::wstring & name, morm::Model ** model) bool Generator<StreamType>::FindInModels(const std::wstring & name, FindHelper & find_helper)
{ {
*model = nullptr;
if( pmodels ) if( pmodels )
{ {
morm::Model * m = pmodels->Find(name); morm::ModelWrapper * m = pmodels->Find(name);
if( m ) if( m )
{ {
*model = m; find_helper.model_wrapper = m;
return true; return true;
} }
} }
@ -910,22 +958,19 @@ bool Generator<StreamType>::FindInModels(const std::wstring & name, morm::Model
return false; return false;
} }
#endif
template<class StreamType> template<class StreamType>
bool Generator<StreamType>::FindInFunctionsAndBlocks(const std::wstring & name, bool Generator<StreamType>::FindInFunctionsAndBlocks(const std::wstring & name, FindHelper & find_helper)
BaseObj<StreamType> ** base_obj,
int * method_index,
typename Functions<StreamType>::UserFunction ** function,
Item ** item_block)
{ {
if( pobjects ) if( pobjects )
{ {
typename Objects<StreamType>::Iterator i = pobjects->Find(name, *method_index); typename Objects<StreamType>::Iterator i = pobjects->Find(name, find_helper.method_index);
if( i != pobjects->End() ) if( i != pobjects->End() )
{ {
*base_obj = *i; find_helper.base_obj = *i;
return true; return true;
} }
} }
@ -936,7 +981,7 @@ bool Generator<StreamType>::FindInFunctionsAndBlocks(const std::wstring & name,
if( i != pfunctions->End() ) if( i != pfunctions->End() )
{ {
*function = &i->second; find_helper.function = &i->second;
return true; return true;
} }
} }
@ -947,7 +992,7 @@ bool Generator<StreamType>::FindInFunctionsAndBlocks(const std::wstring & name,
if( i != pblocks->End() ) if( i != pblocks->End() )
{ {
*item_block = &i->second; find_helper.item_block = &i->second;
return true; return true;
} }
} }
@ -957,8 +1002,7 @@ return false;
template<class StreamType> template<class StreamType>
bool Generator<StreamType>::FindInVariables(const std::wstring & name, bool Generator<StreamType>::FindInVariables(const std::wstring & name, FindHelper & find_helper)
Var ** variable)
{ {
if( pvars ) if( pvars )
{ {
@ -966,7 +1010,7 @@ bool Generator<StreamType>::FindInVariables(const std::wstring & name,
if( i != pvars->end() ) if( i != pvars->end() )
{ {
*variable = &(i->second); find_helper.variable = &(i->second);
return true; return true;
} }
} }
@ -975,49 +1019,41 @@ bool Generator<StreamType>::FindInVariables(const std::wstring & name,
} }
/* /*
* fun_name can be null, it is used only with [let ...] statements * 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 * and if not null then means: as a funcion name we are not using item_fun.name but fun_name
*/ */
template<class StreamType> template<class StreamType>
bool Generator<StreamType>::Find(Item::Function & item_fun, bool Generator<StreamType>::Find(Item::Function & item_fun,
std::wstring * fun_name, std::wstring & fun_name,
BaseObj<StreamType> ** base_obj, FindHelper & find_helper)
int * method_index,
typename Functions<StreamType>::UserFunction ** function,
Item ** item_block,
Var ** variable)
{ {
*base_obj = nullptr; #ifdef EZC_HAS_MORM_LIBRARY
*method_index = -1; if( FindInModels(fun_name, find_helper) )
*function = nullptr; return true;
*item_block = nullptr; #endif
*variable = nullptr;
if( CheckBlockArgument(item_fun.arg, variable) ) if( CheckBlockArgument(item_fun.arg, find_helper) )
return true; return true;
if( FindInCache(item_fun, base_obj, method_index, function, item_block) ) if( FindInCache(item_fun, find_helper) )
return true; return true;
if( !fun_name ) if( FindInFunctionsAndBlocks(fun_name, find_helper) )
fun_name = &item_fun.name;
if( FindInFunctionsAndBlocks(*fun_name, base_obj, method_index, function, item_block) )
return true; return true;
if( FindInVariables(*fun_name, variable) ) if( FindInVariables(fun_name, find_helper) )
return true; return true;
CreateUnknownMsg(*fun_name); CreateUnknownMsg(fun_name);
return false;
return false;
} }
template<class StreamType> template<class StreamType>
void Generator<StreamType>::CallFunction(typename Functions<StreamType>::UserFunction * function, FunInfo<StreamType> & info) void Generator<StreamType>::CallFunction(typename Functions<StreamType>::UserFunction & function, FunInfo<StreamType> & info)
{ {
info.Clear(); info.Clear();
@ -1026,10 +1062,10 @@ void Generator<StreamType>::CallFunction(typename Functions<StreamType>::UserFun
info.is_normal = is_generating_normal; info.is_normal = is_generating_normal;
info.is_filter = is_generating_filter; info.is_filter = is_generating_filter;
info.iter = info.stack.iter; info.iter = info.stack.iter;
info.stack_tab = &stack_tab[0];//stack_tab.data();///////////////////////////////////////////////////////// info.stack_tab = stack_tab.data();
info.stack_index = stack_index-1; info.stack_index = stack_index-1;
(*function)(info); (function)(info);
last_res = info.res; last_res = info.res;
} }
@ -1037,7 +1073,7 @@ void Generator<StreamType>::CallFunction(typename Functions<StreamType>::UserFun
template<class StreamType> template<class StreamType>
void Generator<StreamType>::CallFunction(typename Functions<StreamType>::UserFunction * function, void Generator<StreamType>::CallFunction(typename Functions<StreamType>::UserFunction & function,
std::vector<Var> & parameters, std::vector<Var> & parameters,
StreamType & out_stream, StreamType & out_stream,
const StreamType & in_stream) const StreamType & in_stream)
@ -1061,7 +1097,7 @@ void Generator<StreamType>::CallFunction(typename Functions<StreamType>::UserFun
template<class StreamType> template<class StreamType>
void Generator<StreamType>::CallObject(BaseObj<StreamType> * base_obj, int method_index, FunInfo<StreamType> & info) void Generator<StreamType>::CallObject(BaseObj<StreamType> & base_obj, int method_index, FunInfo<StreamType> & info)
{ {
info.Clear(); info.Clear();
@ -1070,10 +1106,10 @@ void Generator<StreamType>::CallObject(BaseObj<StreamType> * base_obj, int metho
info.is_normal = is_generating_normal; info.is_normal = is_generating_normal;
info.is_filter = is_generating_filter; info.is_filter = is_generating_filter;
info.iter = info.stack.iter; info.iter = info.stack.iter;
info.stack_tab = &stack_tab[0];//stack_tab.data();///////////////////////////////////////////////////////// info.stack_tab = stack_tab.data();
info.stack_index = stack_index-1; info.stack_index = stack_index-1;
base_obj->CallFun(method_index, info); base_obj.CallFun(method_index, info);
last_res = info.res; last_res = info.res;
} }
@ -1093,7 +1129,15 @@ void Generator<StreamType>::CopyStream(pt::TextStream src_stream, StreamType & d
} }
else else
{ {
dst_stream.write(&(*i), 1); if constexpr(sizeof(pt::TextStream::char_type) == sizeof(typename StreamType::char_type))
{
dst_stream.write(&(*i), 1);
}
else
{
wchar_t buf = static_cast<unsigned char>(*i);
dst_stream.write(&buf, 1);
}
} }
++i; ++i;
@ -1121,60 +1165,136 @@ bool Generator<StreamType>::ShouldEscapeValue(std::vector<Var> parameters)
#ifdef EZC_HAS_MORM_LIBRARY
template<class StreamType> template<class StreamType>
void Generator<StreamType>::CallModel(morm::Model & model, std::vector<std::wstring> & fields, std::vector<Var> parameters, StreamType & out_stream, const StreamType & in_stream) void Generator<StreamType>::CallModel(morm::Model & model, const std::wstring & field, std::vector<Var> parameters, StreamType & out_stream, const StreamType & in_stream)
{ {
bool should_escape = ShouldEscapeValue(parameters); /*
* if 'field' is a POD type then 'str' will be used in get_raw_value()
* if 'field' is a getter method with pt::TextStream then 'str' will be used too
* if 'field' is a getter method which takes FunInfo<> then out_stream will be used and 'str' will be empty
*
*/
pt::TextStream str;
if( fields.empty() ) if( parameters.empty() )
{ {
pt::TextStream str; FunInfo<StreamType> info(out_stream, parameters, empty, in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item);
model.to_text(str, false, false); model.get_raw_value(nullptr, field.c_str(), nullptr, info, str, false);
CopyStream(str, out_stream, should_escape); last_res = info.res;
} }
else else
{ {
morm::Model * m = &model; FunInfo<StreamType> info(out_stream, parameters, parameters[0].str, in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item);
model.get_raw_value(nullptr, field.c_str(), nullptr, info, str, false);
last_res = info.res;
}
for(size_t i=0 ; i<fields.size() ; ++i) bool should_escape = ShouldEscapeValue(parameters);
CopyStream(str, out_stream, should_escape);
}
template<class StreamType>
Generator<StreamType>::CallModelHelper Generator<StreamType>::FindLastModelWrapper(morm::ModelWrapper & models_base, std::vector<std::wstring> & fields)
{
CallModelHelper helper;
helper.field_index = 0;
helper.model_wrapper = &models_base;
helper.model = nullptr;
for(; helper.field_index < fields.size() ; ++helper.field_index)
{
std::wstring & field = fields[helper.field_index];
morm::ModelWrapper * child_models_base = helper.model_wrapper->find_child(field);
if( !child_models_base )
{ {
std::wstring & field = fields[i]; helper.model = helper.model_wrapper->get_model();
// this can return null for lists/vectors in a case when the iterator is not pointing to a valid item
if( i+1 < fields.size() ) if( helper.model )
{ {
m = m->get_field_model(field.c_str(), field.c_str()); child_models_base = helper.model->get_model_wrapper(nullptr, field.c_str(), false);
if( !m ) if( child_models_base )
{ {
// put some log helper.model_wrapper->add_child(field, child_models_base);
break;
} }
} }
else }
{
// last field
pt::TextStream str;
if( parameters.empty() ) if( !child_models_base )
{ break;
FunInfo<StreamType> info(out_stream, parameters, empty, in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item);
m->put_field_value(field.c_str(), nullptr, info, str, false);
}
else
{
FunInfo<StreamType> info(out_stream, parameters, parameters[0].str, in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item);
m->put_field_value(field.c_str(), nullptr, info, str, false);
}
CopyStream(str, out_stream, should_escape); helper.model_wrapper = child_models_base;
} helper.model = nullptr;
}
return helper;
}
template<class StreamType>
void Generator<StreamType>::CallModelWrapper(morm::ModelWrapper & models_base_root, std::vector<std::wstring> & fields,
std::vector<Var> parameters, StreamType & out_stream, const StreamType & in_stream)
{
last_res = false;
CallModelHelper helper = FindLastModelWrapper(models_base_root, fields);
// if:
// helper.field_index == fields.size() - all fields items are models or models containers
// helper.field_index == fields.size()-1 - last field is not a model nor a models container
// helper.field_index < fields.size()-1 - incorrect
if( helper.field_index + 1 < fields.size() )
{
// put some log
// at the end there are some fields which are not models nor models containers
last_res = false;
return;
}
if( helper.field_index + 1 == fields.size() )
{
// last field is not a model nor a models container
if( helper.model )
{
CallModel(*helper.model, fields[helper.field_index], parameters, out_stream, in_stream);
}
else
{
last_res = false;
// put log something like:
// model not set, have you forgotten to use [for ...] statement?
}
}
else
{
// all fields items are models or models containers
// this is usualy in [for...] or [if ...] statements
// helper.model_wrapper is always set
if( stack_tab[stack_index-1].is_for )
{
helper.model_wrapper->increment_iterator();
helper.model_wrapper->clear_childs();
last_res = helper.model_wrapper->is_iterator_correct();
}
else
{
last_res = !helper.model_wrapper->is_container_empty();
} }
} }
} }
#endif
template<class StreamType> template<class StreamType>
void Generator<StreamType>::CallObject(BaseObj<StreamType> * base_obj, void Generator<StreamType>::CallObject(BaseObj<StreamType> & base_obj,
int method_index, int method_index,
std::vector<Var> & parameters, std::vector<Var> & parameters,
StreamType & out_stream, StreamType & out_stream,
@ -1236,16 +1356,16 @@ return true;
template<class StreamType> template<class StreamType>
bool Generator<StreamType>::CallVariable(Item::Function & item_fun, Var * variable, std::vector<Var> & parameters, StreamType & out_stream, const StreamType & in_stream) bool Generator<StreamType>::CallVariable(Item::Function & item_fun, Var & variable, std::vector<Var> & parameters, StreamType & out_stream, const StreamType & in_stream)
{ {
if( variable->is_function ) if( variable.is_function )
{ {
return Call(item_fun, &variable->str, out_stream, false, in_stream); return Call(item_fun, &variable.str, out_stream, false, in_stream);
} }
else else
{ {
out_stream << variable->str; out_stream << variable.str;
last_res = variable->res; last_res = variable.res;
return true; return true;
} }
} }
@ -1264,13 +1384,8 @@ bool Generator<StreamType>::Call(Item::Function & item_fun,
bool clear_out_stream, bool clear_out_stream,
const StreamType & in_stream) const StreamType & in_stream)
{ {
BaseObj<StreamType> * base_obj; FindHelper find_helper;
int method_index;
typename Functions<StreamType>::UserFunction * fun;
Item * item_block;
Var * variable;
std::vector<Var> parameters; std::vector<Var> parameters;
morm::Model * model;
if( clear_out_stream ) if( clear_out_stream )
ClearStream(out_stream); ClearStream(out_stream);
@ -1278,12 +1393,8 @@ morm::Model * model;
if( !fun_name ) if( !fun_name )
fun_name = &item_fun.name; fun_name = &item_fun.name;
// add macro if( !Find(item_fun, *fun_name, find_helper) )
if( !FindInModels(*fun_name, &model) ) return false;
{
if( !Find(item_fun, fun_name, &base_obj, &method_index, &fun, &item_block, &variable) )
return false;
}
parameters.resize(item_fun.parameters.size()); parameters.resize(item_fun.parameters.size());
@ -1311,20 +1422,22 @@ morm::Model * model;
} }
} }
if( model ) #ifdef EZC_HAS_MORM_LIBRARY
CallModel(*model, item_fun.fields, parameters, out_stream, in_stream); if( find_helper.model_wrapper )
CallModelWrapper(*find_helper.model_wrapper, item_fun.fields, parameters, out_stream, in_stream);
else else
if( base_obj ) #endif
CallObject(base_obj, method_index, parameters, out_stream, in_stream); if( find_helper.base_obj )
CallObject(*find_helper.base_obj, find_helper.method_index, parameters, out_stream, in_stream);
else else
if( fun ) if( find_helper.function )
CallFunction(fun, parameters, out_stream, in_stream); CallFunction(*find_helper.function, parameters, out_stream, in_stream);
else else
if( item_block ) if( find_helper.item_block )
return CallBlock(*item_block, parameters, out_stream); return CallBlock(*find_helper.item_block, parameters, out_stream);
else else
if( variable ) if( find_helper.variable )
return CallVariable(item_fun, variable, parameters, out_stream, in_stream); return CallVariable(item_fun, *find_helper.variable, parameters, out_stream, in_stream);
return true; return true;
} }

View File

@ -38,22 +38,56 @@
#include "models.h" #include "models.h"
#ifdef EZC_HAS_MORM_LIBRARY
namespace Ezc namespace Ezc
{ {
Models::Models()
{
}
Models::~Models()
{
Clear();
}
void Models::Clear()
{
for(auto & map_item : models_map)
{
if( map_item.second->should_be_auto_removed() )
{
delete map_item.second;
map_item.second = nullptr;
}
}
models_map.clear();
}
void Models::Add(const std::wstring & name, morm::Model & model) void Models::Add(const std::wstring & name, morm::Model & model)
{ {
models_map[name] = &model; morm::ModelWrapper * models_base = new morm::ModelWrapperModel(&model);
models_map[name] = models_base;
} }
void Models::Add(const std::wstring & name, morm::Model * model) void Models::Add(const std::wstring & name, morm::Model * model)
{ {
models_map[name] = model; morm::ModelWrapper * models_base = new morm::ModelWrapperModel(model);
models_map[name] = models_base;
} }
morm::Model * Models::Find(const std::wstring & name) morm::ModelWrapper * Models::Find(const std::wstring & name)
{ {
auto iterator = models_map.find(name); auto iterator = models_map.find(name);
@ -66,14 +100,9 @@ morm::Model * Models::Find(const std::wstring & name)
} }
void Models::Clear()
{
models_map.clear();
}
} }
#endif

View File

@ -38,35 +38,69 @@
#ifndef headerfile_ezc_models #ifndef headerfile_ezc_models
#define headerfile_ezc_models #define headerfile_ezc_models
#include <map> #ifdef EZC_HAS_MORM_LIBRARY
#include <string>
// put some macros #include "modelwrapper.h"
#include "model.h" #include "funinfo.h"
namespace Ezc namespace Ezc
{ {
class Models class Models
{ {
public: public:
Models();
~Models();
void Add(const std::wstring & name, morm::Model & model); void Add(const std::wstring & name, morm::Model & model);
void Add(const std::wstring & name, morm::Model * model); void Add(const std::wstring & name, morm::Model * model);
morm::Model * Find(const std::wstring & name); template<typename VectorType>
void Add(const std::wstring & name, std::vector<VectorType> & container)
{
morm::ModelWrapper * models_base = new morm::ModelWrapperVector<VectorType>(&container);
models_map[name] = models_base;
}
template<typename VectorType>
void Add(const std::wstring & name, std::vector<VectorType> * container)
{
morm::ModelWrapper * models_base = new morm::ModelWrapperVector<VectorType>(container);
models_map[name] = models_base;
}
template<typename ListType>
void Add(const std::wstring & name, std::list<ListType> & container)
{
morm::ModelWrapper * models_base = new morm::ModelWrapperList<ListType>(&container);
models_map[name] = models_base;
}
template<typename ListType>
void Add(const std::wstring & name, std::list<ListType> * container)
{
morm::ModelWrapper * models_base = new morm::ModelWrapperList<ListType>(container);
models_map[name] = models_base;
}
morm::ModelWrapper * Find(const std::wstring & name);
void Clear(); void Clear();
protected: protected:
std::map<std::wstring, morm::ModelWrapper*> models_map;
std::map<std::wstring, morm::Model*> models_map;
}; };
} }
#endif
#endif #endif