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:
parent
e6fd9aad37
commit
9022d4a5fc
|
@ -1,41 +1,23 @@
|
|||
# DO NOT DELETE
|
||||
|
||||
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_private.h funinfo.h objects.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_private.h funinfo.h objects.h blocks.h
|
||||
item.o: item.h
|
||||
models.o: models.h
|
||||
pattern.o: pattern.h item.h cache.h functions.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_private.h funinfo.h objects.h
|
||||
pattern.o: blocks.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_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/space/space.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/membuffer/membuffer.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/convert/convert.h
|
||||
patternparser.o: ../../pikotools/src/convert/inttostr.h
|
||||
|
|
401
src/generator.h
401
src/generator.h
|
@ -50,6 +50,9 @@
|
|||
#include "expressionparser.h"
|
||||
#include "models.h"
|
||||
|
||||
#ifdef EZC_HAS_MORM_LIBRARY
|
||||
#include "model.h"
|
||||
#endif
|
||||
|
||||
namespace Ezc
|
||||
{
|
||||
|
@ -75,7 +78,10 @@ public:
|
|||
void SetFunctions(Functions<StreamType> & functions);
|
||||
void SetObjects(Objects<StreamType> & objects);
|
||||
void SetVariables(Vars & variables); // [def] and [let]
|
||||
|
||||
#ifdef EZC_HAS_MORM_LIBRARY
|
||||
void SetModels(Models & models);
|
||||
#endif
|
||||
|
||||
void SetMax(size_t max_items_, size_t max_for_items_);
|
||||
|
||||
|
@ -133,6 +139,53 @@ public:
|
|||
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
|
||||
{
|
||||
std::vector<Var> args;
|
||||
|
@ -152,7 +205,9 @@ private:
|
|||
Blocks * pblocks;
|
||||
Functions<StreamType> * pfunctions;
|
||||
Objects<StreamType> * pobjects;
|
||||
#ifdef EZC_HAS_MORM_LIBRARY
|
||||
Models * pmodels;
|
||||
#endif
|
||||
Vars * pvars;
|
||||
|
||||
// pointer to the output streams map (can be null)
|
||||
|
@ -238,38 +293,25 @@ private:
|
|||
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,
|
||||
BaseObj<StreamType> ** base_obj,
|
||||
int * method_index,
|
||||
typename Functions<StreamType>::UserFunction ** function,
|
||||
Item ** item_block);
|
||||
bool FindInCache(Item::Function & item_fun, FindHelper & find_helper);
|
||||
|
||||
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,
|
||||
BaseObj<StreamType> ** base_obj,
|
||||
int * method_index,
|
||||
typename Functions<StreamType>::UserFunction ** function,
|
||||
Item ** item_block);
|
||||
bool FindInFunctionsAndBlocks(const std::wstring & name, FindHelper & find_helper);
|
||||
|
||||
bool FindInVariables(const std::wstring & name,
|
||||
Var ** variable);
|
||||
|
||||
bool FindInVariables(const std::wstring & name, FindHelper & find_helper);
|
||||
|
||||
bool Find(Item::Function & item_fun,
|
||||
std::wstring * fun_name,
|
||||
BaseObj<StreamType> ** base_obj,
|
||||
int * method_index,
|
||||
typename Functions<StreamType>::UserFunction ** function,
|
||||
Item ** item_block,
|
||||
Var ** variable);
|
||||
std::wstring & fun_name,
|
||||
FindHelper & find_helper);
|
||||
|
||||
void CallFunction(typename Functions<StreamType>::UserFunction * function,
|
||||
FunInfo<StreamType> & info);
|
||||
void CallFunction(typename Functions<StreamType>::UserFunction & function, FunInfo<StreamType> & info);
|
||||
|
||||
void CallFunction(typename Functions<StreamType>::UserFunction * function,
|
||||
void CallFunction(typename Functions<StreamType>::UserFunction & function,
|
||||
std::vector<Var> & parameters,
|
||||
StreamType & out_stream,
|
||||
const StreamType & in_stream);
|
||||
|
@ -278,18 +320,22 @@ private:
|
|||
std::vector<Var> & parameters,
|
||||
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,
|
||||
std::vector<Var> & parameters,
|
||||
StreamType & out_stream,
|
||||
const StreamType & in_stream);
|
||||
|
||||
bool CallVariable(Item::Function & item_fun,
|
||||
Var * variable,
|
||||
Var & variable,
|
||||
std::vector<Var> & parameters,
|
||||
StreamType & out_stream,
|
||||
const StreamType & in_stream);
|
||||
|
@ -362,7 +408,10 @@ Generator<StreamType>::Generator() : empty_stream()
|
|||
pfunctions = nullptr;
|
||||
pobjects = nullptr;
|
||||
pvars = nullptr;
|
||||
|
||||
#ifdef EZC_HAS_MORM_LIBRARY
|
||||
pmodels = nullptr;
|
||||
#endif
|
||||
|
||||
max_items = 50000;
|
||||
max_for_items = 5000;
|
||||
|
@ -397,7 +446,10 @@ Generator<StreamType> & Generator<StreamType>::operator=(const Generator<StreamT
|
|||
pfunctions = n.pfunctions;
|
||||
pobjects = n.pobjects;
|
||||
pvars = n.pvars;
|
||||
|
||||
#ifdef EZC_HAS_MORM_LIBRARY
|
||||
pmodels = n.pmodels;
|
||||
#endif
|
||||
|
||||
max_items = n.max_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>
|
||||
void Generator<StreamType>::SetModels(Models & models)
|
||||
{
|
||||
pmodels = ⊧
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
template<class StreamType>
|
||||
void Generator<StreamType>::CanUseCache(bool can_use_cache)
|
||||
|
@ -834,7 +887,7 @@ void Generator<StreamType>::Generate(OutStreams<StreamType> & out_streams)
|
|||
|
||||
|
||||
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 )
|
||||
return false;
|
||||
|
@ -846,7 +899,7 @@ bool Generator<StreamType>::CheckBlockArgument(int arg_index, Var ** variable)
|
|||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -857,30 +910,26 @@ bool Generator<StreamType>::CheckBlockArgument(int arg_index, Var ** variable)
|
|||
|
||||
|
||||
template<class StreamType>
|
||||
bool Generator<StreamType>::FindInCache(Item::Function & item_fun,
|
||||
BaseObj<StreamType> ** base_obj,
|
||||
int * method_index,
|
||||
typename Functions<StreamType>::UserFunction ** function,
|
||||
Item ** item_block)
|
||||
bool Generator<StreamType>::FindInCache(Item::Function & item_fun, FindHelper & find_helper)
|
||||
{
|
||||
if( can_find_in_cache )
|
||||
{
|
||||
if( item_fun.base_obj )
|
||||
{
|
||||
*base_obj = reinterpret_cast<BaseObj<StreamType> * >(item_fun.base_obj);
|
||||
*method_index = item_fun.method_index;
|
||||
find_helper.base_obj = reinterpret_cast<BaseObj<StreamType> * >(item_fun.base_obj);
|
||||
find_helper.method_index = item_fun.method_index;
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if( item_fun.item_block )
|
||||
{
|
||||
*item_block = item_fun.item_block;
|
||||
find_helper.item_block = item_fun.item_block;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -890,19 +939,18 @@ return false;
|
|||
|
||||
|
||||
|
||||
#ifdef EZC_HAS_MORM_LIBRARY
|
||||
|
||||
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 )
|
||||
{
|
||||
morm::Model * m = pmodels->Find(name);
|
||||
morm::ModelWrapper * m = pmodels->Find(name);
|
||||
|
||||
if( m )
|
||||
{
|
||||
*model = m;
|
||||
find_helper.model_wrapper = m;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -910,22 +958,19 @@ bool Generator<StreamType>::FindInModels(const std::wstring & name, morm::Model
|
|||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
template<class StreamType>
|
||||
bool Generator<StreamType>::FindInFunctionsAndBlocks(const std::wstring & name,
|
||||
BaseObj<StreamType> ** base_obj,
|
||||
int * method_index,
|
||||
typename Functions<StreamType>::UserFunction ** function,
|
||||
Item ** item_block)
|
||||
bool Generator<StreamType>::FindInFunctionsAndBlocks(const std::wstring & name, FindHelper & find_helper)
|
||||
{
|
||||
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() )
|
||||
{
|
||||
*base_obj = *i;
|
||||
find_helper.base_obj = *i;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -936,7 +981,7 @@ bool Generator<StreamType>::FindInFunctionsAndBlocks(const std::wstring & name,
|
|||
|
||||
if( i != pfunctions->End() )
|
||||
{
|
||||
*function = &i->second;
|
||||
find_helper.function = &i->second;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -947,7 +992,7 @@ bool Generator<StreamType>::FindInFunctionsAndBlocks(const std::wstring & name,
|
|||
|
||||
if( i != pblocks->End() )
|
||||
{
|
||||
*item_block = &i->second;
|
||||
find_helper.item_block = &i->second;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -957,8 +1002,7 @@ return false;
|
|||
|
||||
|
||||
template<class StreamType>
|
||||
bool Generator<StreamType>::FindInVariables(const std::wstring & name,
|
||||
Var ** variable)
|
||||
bool Generator<StreamType>::FindInVariables(const std::wstring & name, FindHelper & find_helper)
|
||||
{
|
||||
if( pvars )
|
||||
{
|
||||
|
@ -966,7 +1010,7 @@ bool Generator<StreamType>::FindInVariables(const std::wstring & name,
|
|||
|
||||
if( i != pvars->end() )
|
||||
{
|
||||
*variable = &(i->second);
|
||||
find_helper.variable = &(i->second);
|
||||
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
|
||||
* and if not null then means: as a funcion name we are not using item_fun.name but fun_name
|
||||
*/
|
||||
template<class StreamType>
|
||||
bool Generator<StreamType>::Find(Item::Function & item_fun,
|
||||
std::wstring * fun_name,
|
||||
BaseObj<StreamType> ** base_obj,
|
||||
int * method_index,
|
||||
typename Functions<StreamType>::UserFunction ** function,
|
||||
Item ** item_block,
|
||||
Var ** variable)
|
||||
std::wstring & fun_name,
|
||||
FindHelper & find_helper)
|
||||
{
|
||||
*base_obj = nullptr;
|
||||
*method_index = -1;
|
||||
*function = nullptr;
|
||||
*item_block = nullptr;
|
||||
*variable = nullptr;
|
||||
#ifdef EZC_HAS_MORM_LIBRARY
|
||||
if( FindInModels(fun_name, find_helper) )
|
||||
return true;
|
||||
#endif
|
||||
|
||||
if( CheckBlockArgument(item_fun.arg, variable) )
|
||||
if( CheckBlockArgument(item_fun.arg, find_helper) )
|
||||
return true;
|
||||
|
||||
if( FindInCache(item_fun, base_obj, method_index, function, item_block) )
|
||||
if( FindInCache(item_fun, find_helper) )
|
||||
return true;
|
||||
|
||||
if( !fun_name )
|
||||
fun_name = &item_fun.name;
|
||||
|
||||
if( FindInFunctionsAndBlocks(*fun_name, base_obj, method_index, function, item_block) )
|
||||
if( FindInFunctionsAndBlocks(fun_name, find_helper) )
|
||||
return true;
|
||||
|
||||
if( FindInVariables(*fun_name, variable) )
|
||||
if( FindInVariables(fun_name, find_helper) )
|
||||
return true;
|
||||
|
||||
CreateUnknownMsg(*fun_name);
|
||||
|
||||
return false;
|
||||
CreateUnknownMsg(fun_name);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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();
|
||||
|
||||
|
@ -1026,10 +1062,10 @@ void Generator<StreamType>::CallFunction(typename Functions<StreamType>::UserFun
|
|||
info.is_normal = is_generating_normal;
|
||||
info.is_filter = is_generating_filter;
|
||||
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;
|
||||
|
||||
(*function)(info);
|
||||
(function)(info);
|
||||
|
||||
last_res = info.res;
|
||||
}
|
||||
|
@ -1037,7 +1073,7 @@ void Generator<StreamType>::CallFunction(typename Functions<StreamType>::UserFun
|
|||
|
||||
|
||||
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,
|
||||
StreamType & out_stream,
|
||||
const StreamType & in_stream)
|
||||
|
@ -1061,7 +1097,7 @@ void Generator<StreamType>::CallFunction(typename Functions<StreamType>::UserFun
|
|||
|
||||
|
||||
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();
|
||||
|
||||
|
@ -1070,10 +1106,10 @@ void Generator<StreamType>::CallObject(BaseObj<StreamType> * base_obj, int metho
|
|||
info.is_normal = is_generating_normal;
|
||||
info.is_filter = is_generating_filter;
|
||||
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;
|
||||
|
||||
base_obj->CallFun(method_index, info);
|
||||
base_obj.CallFun(method_index, info);
|
||||
|
||||
last_res = info.res;
|
||||
}
|
||||
|
@ -1093,7 +1129,15 @@ void Generator<StreamType>::CopyStream(pt::TextStream src_stream, StreamType & d
|
|||
}
|
||||
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;
|
||||
|
@ -1121,60 +1165,136 @@ bool Generator<StreamType>::ShouldEscapeValue(std::vector<Var> parameters)
|
|||
|
||||
|
||||
|
||||
#ifdef EZC_HAS_MORM_LIBRARY
|
||||
|
||||
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;
|
||||
model.to_text(str, false, false);
|
||||
CopyStream(str, out_stream, should_escape);
|
||||
FunInfo<StreamType> info(out_stream, parameters, empty, 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;
|
||||
}
|
||||
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
|
||||
break;
|
||||
helper.model_wrapper->add_child(field, child_models_base);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// last field
|
||||
pt::TextStream str;
|
||||
}
|
||||
|
||||
if( parameters.empty() )
|
||||
{
|
||||
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);
|
||||
}
|
||||
if( !child_models_base )
|
||||
break;
|
||||
|
||||
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>
|
||||
void Generator<StreamType>::CallObject(BaseObj<StreamType> * base_obj,
|
||||
void Generator<StreamType>::CallObject(BaseObj<StreamType> & base_obj,
|
||||
int method_index,
|
||||
std::vector<Var> & parameters,
|
||||
StreamType & out_stream,
|
||||
|
@ -1236,16 +1356,16 @@ return true;
|
|||
|
||||
|
||||
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
|
||||
{
|
||||
out_stream << variable->str;
|
||||
last_res = variable->res;
|
||||
out_stream << variable.str;
|
||||
last_res = variable.res;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -1264,13 +1384,8 @@ bool Generator<StreamType>::Call(Item::Function & item_fun,
|
|||
bool clear_out_stream,
|
||||
const StreamType & in_stream)
|
||||
{
|
||||
BaseObj<StreamType> * base_obj;
|
||||
int method_index;
|
||||
typename Functions<StreamType>::UserFunction * fun;
|
||||
Item * item_block;
|
||||
Var * variable;
|
||||
FindHelper find_helper;
|
||||
std::vector<Var> parameters;
|
||||
morm::Model * model;
|
||||
|
||||
if( clear_out_stream )
|
||||
ClearStream(out_stream);
|
||||
|
@ -1278,12 +1393,8 @@ morm::Model * model;
|
|||
if( !fun_name )
|
||||
fun_name = &item_fun.name;
|
||||
|
||||
// add macro
|
||||
if( !FindInModels(*fun_name, &model) )
|
||||
{
|
||||
if( !Find(item_fun, fun_name, &base_obj, &method_index, &fun, &item_block, &variable) )
|
||||
return false;
|
||||
}
|
||||
if( !Find(item_fun, *fun_name, find_helper) )
|
||||
return false;
|
||||
|
||||
parameters.resize(item_fun.parameters.size());
|
||||
|
||||
|
@ -1311,20 +1422,22 @@ morm::Model * model;
|
|||
}
|
||||
}
|
||||
|
||||
if( model )
|
||||
CallModel(*model, item_fun.fields, parameters, out_stream, in_stream);
|
||||
#ifdef EZC_HAS_MORM_LIBRARY
|
||||
if( find_helper.model_wrapper )
|
||||
CallModelWrapper(*find_helper.model_wrapper, item_fun.fields, parameters, out_stream, in_stream);
|
||||
else
|
||||
if( base_obj )
|
||||
CallObject(base_obj, method_index, parameters, out_stream, in_stream);
|
||||
#endif
|
||||
if( find_helper.base_obj )
|
||||
CallObject(*find_helper.base_obj, find_helper.method_index, parameters, out_stream, in_stream);
|
||||
else
|
||||
if( fun )
|
||||
CallFunction(fun, parameters, out_stream, in_stream);
|
||||
if( find_helper.function )
|
||||
CallFunction(*find_helper.function, parameters, out_stream, in_stream);
|
||||
else
|
||||
if( item_block )
|
||||
return CallBlock(*item_block, parameters, out_stream);
|
||||
if( find_helper.item_block )
|
||||
return CallBlock(*find_helper.item_block, parameters, out_stream);
|
||||
else
|
||||
if( variable )
|
||||
return CallVariable(item_fun, variable, parameters, out_stream, in_stream);
|
||||
if( find_helper.variable )
|
||||
return CallVariable(item_fun, *find_helper.variable, parameters, out_stream, in_stream);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -38,22 +38,56 @@
|
|||
#include "models.h"
|
||||
|
||||
|
||||
#ifdef EZC_HAS_MORM_LIBRARY
|
||||
|
||||
|
||||
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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);
|
||||
|
||||
|
@ -66,14 +100,9 @@ morm::Model * Models::Find(const std::wstring & name)
|
|||
}
|
||||
|
||||
|
||||
void Models::Clear()
|
||||
{
|
||||
models_map.clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
|
46
src/models.h
46
src/models.h
|
@ -38,35 +38,69 @@
|
|||
#ifndef headerfile_ezc_models
|
||||
#define headerfile_ezc_models
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#ifdef EZC_HAS_MORM_LIBRARY
|
||||
|
||||
// put some macros
|
||||
#include "model.h"
|
||||
#include "modelwrapper.h"
|
||||
#include "funinfo.h"
|
||||
|
||||
|
||||
namespace Ezc
|
||||
{
|
||||
|
||||
|
||||
class Models
|
||||
{
|
||||
public:
|
||||
|
||||
Models();
|
||||
~Models();
|
||||
|
||||
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();
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
std::map<std::wstring, morm::ModelWrapper*> models_map;
|
||||
|
||||
std::map<std::wstring, morm::Model*> models_map;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Reference in New Issue