From e39311c290696a0250c610d97a358a765e151a9b Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Thu, 5 Dec 2024 17:48:06 +0100 Subject: [PATCH] WIP: use pt::Stream instead of a template argument --- src/blocks.cpp | 19 ++ src/blocks.h | 26 +- src/cache.cpp | 51 +++ src/cache.h | 71 +--- src/functions.cpp | 109 ++++++ src/functions.h | 85 +---- src/funinfo.h | 14 +- src/generator.h | 855 ++++++++++++++++++++++++---------------------- src/objects.cpp | 104 ++++++ src/objects.h | 80 +---- src/outstreams.h | 56 ++- src/pattern.h | 14 +- 12 files changed, 777 insertions(+), 707 deletions(-) create mode 100644 src/functions.cpp create mode 100644 src/objects.cpp diff --git a/src/blocks.cpp b/src/blocks.cpp index b8ca259..9eea8d2 100644 --- a/src/blocks.cpp +++ b/src/blocks.cpp @@ -85,6 +85,24 @@ void Blocks::ClearCache() +void Blocks::CacheObjects(Objects & obj) +{ + BlocksTable::iterator i = blocks_tab.begin(); + + for( ; i != blocks_tab.end() ; ++i) + Cache(obj, i->second); +} + + +void Blocks::CacheFunctions(Functions & fun) +{ + BlocksTable::iterator i = blocks_tab.begin(); + + for( ; i != blocks_tab.end() ; ++i) + Cache(fun, i->second); +} + + void Blocks::CacheBlocks(Blocks & blocks) { BlocksTable::iterator i = blocks_tab.begin(); @@ -95,4 +113,5 @@ void Blocks::CacheBlocks(Blocks & blocks) + } // namespace diff --git a/src/blocks.h b/src/blocks.h index 4d89605..98e9ee1 100644 --- a/src/blocks.h +++ b/src/blocks.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2014-2015, Tomasz Sowa + * Copyright (c) 2014-2024, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -62,8 +62,8 @@ public: size_t Size() const; void Clear(); - template void CacheObjects(Objects & obj); - template void CacheFunctions(Functions & fun); + void CacheObjects(Objects & obj); + void CacheFunctions(Functions & fun); void CacheBlocks(Blocks & blocks); void ClearCache(); @@ -75,26 +75,6 @@ private: }; -template -void Blocks::CacheObjects(Objects & obj) -{ - BlocksTable::iterator i = blocks_tab.begin(); - - for( ; i != blocks_tab.end() ; ++i) - Cache(obj, i->second); -} - - -template -void Blocks::CacheFunctions(Functions & fun) -{ - BlocksTable::iterator i = blocks_tab.begin(); - - for( ; i != blocks_tab.end() ; ++i) - Cache(fun, i->second); -} - - } // namespace diff --git a/src/cache.cpp b/src/cache.cpp index 79f12bd..7bf281e 100644 --- a/src/cache.cpp +++ b/src/cache.cpp @@ -58,6 +58,32 @@ void Cache(Blocks & blocks, Item::Function & function) } +void Cache(Functions & fun, Item::Function & function) +{ + function.fun_cache = 0; + + if( !function.name.empty() && function.arg < 0 ) + { + typename Functions::Iterator i = fun.Find(function.name); + + if( i != fun.End() ) + function.fun_cache = &i->second; + } + + for(size_t i=0 ; i < function.parameters.size() ; ++i) + Cache(fun, *function.parameters[i]); +} + + +void Cache(Functions & fun, Item & item) +{ + Cache(fun, item.function); + + for(size_t i=0; i < item.item_tab.size() ; ++i) + Cache(fun, *item.item_tab[i]); +} + + void Cache(Blocks & blocks, Item & item) { @@ -68,6 +94,31 @@ void Cache(Blocks & blocks, Item & item) } +void Cache(Objects & objects, Item::Function & function) +{ + function.base_obj = 0; + function.method_index = -1; + + if( !function.name.empty() && function.arg < 0 ) + { + typename Objects::Iterator i = objects.Find(function.name, function.method_index); + + if( i != objects.End() ) + function.base_obj = *i; + } + + for(size_t i=0 ; i < function.parameters.size() ; ++i) + Cache(objects, *function.parameters[i]); +} + + +void Cache(Objects & objects, Item & item) +{ + Cache(objects, item.function); + + for(size_t i=0; i < item.item_tab.size() ; ++i) + Cache(objects, *item.item_tab[i]); +} diff --git a/src/cache.h b/src/cache.h index 37e870a..7b1fa23 100644 --- a/src/cache.h +++ b/src/cache.h @@ -4,8 +4,8 @@ * Author: Tomasz Sowa */ -/* - * Copyright (c) 2014-2015, Tomasz Sowa +/* + * Copyright (c) 2014-2024, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -48,70 +48,11 @@ namespace Ezc class Blocks; - -template -void Cache(Functions & fun, Item::Function & function) -{ - function.fun_cache = 0; - - if( !function.name.empty() && function.arg < 0 ) - { - typename Functions::Iterator i = fun.Find(function.name); - - if( i != fun.End() ) - function.fun_cache = &i->second; - } - - for(size_t i=0 ; i < function.parameters.size() ; ++i) - Cache(fun, *function.parameters[i]); -} - - -template -void Cache(Functions & fun, Item & item) -{ - Cache(fun, item.function); - - for(size_t i=0; i < item.item_tab.size() ; ++i) - Cache(fun, *item.item_tab[i]); -} - - - +void Cache(Functions & fun, Item::Function & function); +void Cache(Functions & fun, Item & item); void Cache(Blocks & blocks, Item & item); - - - - -template -void Cache(Objects & objects, Item::Function & function) -{ - function.base_obj = 0; - function.method_index = -1; - - if( !function.name.empty() && function.arg < 0 ) - { - typename Objects::Iterator i = objects.Find(function.name, function.method_index); - - if( i != objects.End() ) - function.base_obj = *i; - } - - for(size_t i=0 ; i < function.parameters.size() ; ++i) - Cache(objects, *function.parameters[i]); -} - - - -template -void Cache(Objects & objects, Item & item) -{ - Cache(objects, item.function); - - for(size_t i=0; i < item.item_tab.size() ; ++i) - Cache(objects, *item.item_tab[i]); -} - +void Cache(Objects & objects, Item::Function & function); +void Cache(Objects & objects, Item & item); } // namespace Ezc diff --git a/src/functions.cpp b/src/functions.cpp new file mode 100644 index 0000000..d9217bc --- /dev/null +++ b/src/functions.cpp @@ -0,0 +1,109 @@ +/* + * 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) 2024, 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 "functions.h" + + +namespace Ezc +{ + + +void Functions::Insert(const char * key, UserFunction ufunction) +{ + pt::utf8_to_wide(key, temp_key); + functions_tab[temp_key] = ufunction; + temp_key.clear(); +} + + +void Functions::Insert(const std::string & key, UserFunction ufunction) +{ + Insert(key.c_str(), ufunction); +} + + +void Functions::Insert(const wchar_t * key, UserFunction ufunction) +{ + temp_key = key; + functions_tab[temp_key] = ufunction; + temp_key.clear(); +} + + +void Functions::Insert(const std::wstring & key, UserFunction ufunction) +{ + functions_tab[key] = ufunction; +} + + + + + + + + +typename Functions::Iterator Functions::Find(const std::wstring & key) +{ + return functions_tab.find(key); +} + + + +void Functions::Clear() +{ + functions_tab.clear(); +} + + +typename Functions::Iterator Functions::Begin() +{ + return functions_tab.begin(); +} + + +typename Functions::Iterator Functions::End() +{ + return functions_tab.end(); +} + + +size_t Functions::Size() const +{ + return functions_tab.size(); +} + + + +} // namespace Ezc + diff --git a/src/functions.h b/src/functions.h index e1cffb7..72e312b 100644 --- a/src/functions.h +++ b/src/functions.h @@ -4,8 +4,8 @@ * Author: Tomasz Sowa */ -/* - * Copyright (c) 2007-2015, Tomasz Sowa +/* + * Copyright (c) 2007-2024, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -48,12 +48,11 @@ namespace Ezc // functions or variables -template class Functions { public: - typedef void (*UserFunction)(FunInfo &); + typedef void (*UserFunction)(FunInfo &); typedef std::map FunctionsTable; typedef typename FunctionsTable::iterator Iterator; @@ -79,84 +78,6 @@ private: - - - -template -void Functions::Insert(const char * key, UserFunction ufunction) -{ - pt::utf8_to_wide(key, temp_key); - functions_tab[temp_key] = ufunction; - temp_key.clear(); -} - - -template -void Functions::Insert(const std::string & key, UserFunction ufunction) -{ - Insert(key.c_str(), ufunction); -} - - -template -void Functions::Insert(const wchar_t * key, UserFunction ufunction) -{ - temp_key = key; - functions_tab[temp_key] = ufunction; - temp_key.clear(); -} - - -template -void Functions::Insert(const std::wstring & key, UserFunction ufunction) -{ - functions_tab[key] = ufunction; -} - - - - - - - - -template -typename Functions::Iterator Functions::Find(const std::wstring & key) -{ - return functions_tab.find(key); -} - - - -template -void Functions::Clear() -{ - functions_tab.clear(); -} - - -template -typename Functions::Iterator Functions::Begin() -{ - return functions_tab.begin(); -} - - -template -typename Functions::Iterator Functions::End() -{ - return functions_tab.end(); -} - - -template -size_t Functions::Size() const -{ - return functions_tab.size(); -} - - - } // namespace Ezc diff --git a/src/funinfo.h b/src/funinfo.h index d53a04c..77700fb 100644 --- a/src/funinfo.h +++ b/src/funinfo.h @@ -4,8 +4,8 @@ * Author: Tomasz Sowa */ -/* - * Copyright (c) 2007-2023, Tomasz Sowa +/* + * Copyright (c) 2007-2024, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -39,6 +39,7 @@ #include #include #include "item.h" +#include "textstream/stream.h" @@ -143,12 +144,11 @@ struct Stack // !! IMPROVE ME // the name is bad // may it should be called Env (environment) or FunEnv -template struct FunInfo { // a result consists of a string and a boolean value // output stream - StreamType & out; + pt::Stream & out; // return value from a user's function (default false if not set directly by the function) bool res; @@ -163,7 +163,7 @@ struct FunInfo // an input stream used in [filter] statement // if there is other statement than [filter] then this is an empty stream - const StreamType & in; + const pt::Stream & in; // indicates that this function is from [for ...] statement bool is_for; @@ -207,10 +207,10 @@ struct FunInfo // arguments: output_stream, table_of_parameters, the_first_parameter - FunInfo(StreamType & o, + FunInfo(pt::Stream & o, std::vector & pars, const std::wstring & first_par, - const StreamType & input_stream, + const pt::Stream & input_stream, Stack & s, const Item & item_) : out(o), params(pars), par(first_par), in(input_stream), stack(s), item(item_) { diff --git a/src/generator.h b/src/generator.h index 4d9de98..73e067e 100644 --- a/src/generator.h +++ b/src/generator.h @@ -36,8 +36,9 @@ #ifndef headerfile_ezc_generator #define headerfile_ezc_generator -#include #include +#include +#include #include #include "blocks.h" #include "pattern.h" @@ -61,10 +62,9 @@ namespace Ezc /* -StreamType +pt::Stream we use only method write(const wchar_t * str, size_t len) from the stream */ -template class Generator { public: @@ -76,8 +76,8 @@ public: void SetPattern(Pattern & pattern); void SetBlocks(Blocks & blocks); - void SetFunctions(Functions & functions); - void SetObjects(Objects & objects); + void SetFunctions(Functions & functions); + void SetObjects(Objects & objects); void SetVariables(Vars & variables); // [def] and [let] void SetLogger(pt::Log & logger); @@ -138,9 +138,9 @@ public: void UseMainStream(bool use_main_stream); // the main methods for generating - void Generate(StreamType & out); - void Generate(StreamType & out, OutStreams & out_streams); - void Generate(OutStreams & out_streams); + void Generate(pt::Stream & out); + void Generate(pt::Stream & out, OutStreams & out_streams); + //void Generate(OutStreams & out_streams); @@ -151,9 +151,9 @@ private: { std::wstring * fun_name; - BaseObj * base_obj; + BaseObj * base_obj; int method_index; - typename Functions::UserFunction * function; + typename Functions::UserFunction * function; Item * item_block; Var * variable; @@ -183,7 +183,7 @@ private: struct BlockStack { std::vector args; - StreamType * out_stream; + pt::Stream * out_stream; bool was_return; }; @@ -192,13 +192,14 @@ private: size_t block_stack_size; // current output stream (can be null) - // at the beginning it is pointing to the main stream (to the StreamType argument passsed to Generate method) - StreamType * output_stream; + // at the beginning it is pointing to the main stream (to the pt::Stream argument passsed to Generate method) + pt::Stream * output_stream; + pt::Stream * main_stream; // it is pointing to the main stream (to the pt::Stream argument passsed to Generate method) Pattern * ppattern; Blocks * pblocks; - Functions * pfunctions; - Objects * pobjects; + Functions * pfunctions; + Objects * pobjects; #ifdef EZC_HAS_MORM_LIBRARY Models * pmodels; #endif @@ -208,7 +209,7 @@ private: // pointer to the output streams map (can be null) // output stream will be created when [ezc frame "stream_name"] statement is found - OutStreams * output_frames_streams; + OutStreams * output_frames_streams; // temporary error messages std::wstring temp_msg; @@ -231,13 +232,13 @@ private: size_t filter_size; // we have to use a pointers table because standard streams such // as std::wostringstream are not copyable - std::vector filter_tab; - std::vector ezc_frame_stack_tab; - const StreamType empty_stream; + std::vector filter_tab; + std::vector ezc_frame_stack_tab; + const pt::Stream * empty_stream; // temporary streams used in [if..] [for...] or [def ...] // or if output_stream is null and an ezc function should be called - StreamType stream_temp1, stream_temp_define; + pt::Stream * stream_temp1, * stream_temp_define; // last result from a user function (FunInfo::res) bool last_res; @@ -270,23 +271,25 @@ private: ExpressionParser * expression_parser; + void InitializeTmpStreams(); - void ResizeStreamStack(std::vector & stream_tab, size_t stream_tab_max_size); + void ResizeStreamStack(std::vector & stream_tab, size_t stream_tab_max_size); void ResizeFilterTab(); void ResizeStack(); void ResizeBlockStack(); void ResizeEzcFrameStack(); - void ClearStreamStack(std::vector & stream_tab); + void ClearStreamStack(std::vector & stream_tab); void ClearFilterTab(); void ClearForStack(); void ClearBlockStack(); void ClearEzcFrameTab(); + void ClearTmpStreams(); - void ClearStream(StreamType & str); - void CopyStreamToString(StreamType & src_stream, std::wstring & dst_string); - void CopyStream(StreamType & src_stream, StreamType & dst_stream); - void CopyStream(pt::WTextStream & src_stream, StreamType & dst_stream, bool should_escape); + void ClearStream(pt::Stream & str); + void CopyStreamToString(pt::Stream & src_stream, std::wstring & dst_string); + void CopyStream(pt::Stream & src_stream, pt::Stream & dst_stream); + void CopyStream(pt::WTextStream & src_stream, pt::Stream & dst_stream, bool should_escape); void RemoveStackFunData(Stack & sitem); @@ -310,62 +313,62 @@ private: bool FindInVariables(const std::wstring & name, FindHelper & find_helper); bool Find(Item::Function & item_fun, FindHelper & find_helper); - void PrepareEnvStruct(FunInfo & info); + void PrepareEnvStruct(FunInfo & info); - void CallFunction(typename Functions::UserFunction & function, FunInfo & info); + void CallFunction(typename Functions::UserFunction & function, FunInfo & info); - void CallFunction(typename Functions::UserFunction & function, + void CallFunction(typename Functions::UserFunction & function, std::vector & parameters, - StreamType & out_stream, - const StreamType & in_stream); + pt::Stream & out_stream, + const pt::Stream & in_stream); bool CallBlock(Item & item_block, std::vector & parameters, - StreamType & out_stream); + pt::Stream & out_stream); - void CallObject(BaseObj & base_obj, int method_index, FunInfo & info); + void CallObject(BaseObj & base_obj, int method_index, FunInfo & info); - void PrintDate(pt::Date * date, std::vector & parameters, StreamType & out_stream); - bool PrintDatePart(pt::Date * date, const std::wstring & field, std::vector & parameters, StreamType & out_stream); + void PrintDate(pt::Date * date, std::vector & parameters, pt::Stream & out_stream); + bool PrintDatePart(pt::Date * date, const std::wstring & field, std::vector & parameters, pt::Stream & out_stream); - bool CallDate(FindHelper & find_helper, std::vector & fields, std::vector & parameters, StreamType & out_stream); + bool CallDate(FindHelper & find_helper, std::vector & fields, std::vector & parameters, pt::Stream & out_stream); - void PrintLastSpaceField(pt::Space * space, std::vector & parameters, StreamType & out_stream); - void CallSpaceObjectForLastField(std::vector & parameters, StreamType & out_stream, pt::Space * space); + void PrintLastSpaceField(pt::Space * space, std::vector & parameters, pt::Stream & out_stream); + void CallSpaceObjectForLastField(std::vector & parameters, pt::Stream & out_stream, pt::Space * space); pt::Space * CallSpaceObjectForMiddleField(std::wstring & root_space_name, std::vector & fields, size_t field_index, pt::Space * space); - void CallSpaceTableForLastField(morm::SpaceWrapper & space_wrapper, std::vector & parameters, StreamType & out_stream, + void CallSpaceTableForLastField(morm::SpaceWrapper & space_wrapper, std::vector & parameters, pt::Stream & out_stream, pt::Space * space, size_t model_wrapper_space_table_index); pt::Space * CallSpaceTableForMiddleField(morm::SpaceWrapper & space_wrapper, std::wstring & root_space_name, std::vector & fields, size_t field_index, pt::Space * space, size_t model_wrapper_space_table_index); - bool CallSpace(FindHelper & find_helper, std::vector & fields, std::vector & parameters, StreamType & out_stream); + bool CallSpace(FindHelper & find_helper, std::vector & fields, std::vector & parameters, pt::Stream & out_stream); #ifdef EZC_HAS_MORM_LIBRARY - bool CallModelField(morm::Model & model, const std::wstring & field, std::vector & parameters, StreamType & out_stream, const StreamType & in_stream); - bool CallModel(morm::Model & model, FindHelper & find_helper, std::vector & fields, std::vector & parameters, StreamType & out_stream, const StreamType & in_stream); + bool CallModelField(morm::Model & model, const std::wstring & field, std::vector & parameters, pt::Stream & out_stream, const pt::Stream & in_stream); + bool CallModel(morm::Model & model, FindHelper & find_helper, std::vector & fields, std::vector & parameters, pt::Stream & out_stream, const pt::Stream & in_stream); void FindLastModelWrapper(FindHelper & find_helper, std::vector & fields); - bool CallWrapper(FindHelper & find_helper, std::vector & fields, std::vector & parameters, StreamType & out_stream, const StreamType & in_stream); + bool CallWrapper(FindHelper & find_helper, std::vector & fields, std::vector & parameters, pt::Stream & out_stream, const pt::Stream & in_stream); #endif - void CallObject(BaseObj & base_obj, + void CallObject(BaseObj & base_obj, int method_index, std::vector & parameters, - StreamType & out_stream, - const StreamType & in_stream); + pt::Stream & out_stream, + const pt::Stream & in_stream); bool CallVariable(Item::Function & item_fun, Var & variable, std::vector & parameters, - StreamType & out_stream, - const StreamType & in_stream); + pt::Stream & out_stream, + const pt::Stream & in_stream); bool Call(Item::Function & item_fun, std::wstring * fun_name, - StreamType & out_stream, + pt::Stream & out_stream, bool clear_out_stream, - const StreamType & in_stream); + const pt::Stream & in_stream); bool Call(Item::Function & item_fun); @@ -383,11 +386,11 @@ private: bool ShouldMakeJsonDump(std::vector & parameters); bool IsPrettyPrint(std::vector & parameters); - void DumpSpaceIfNeeded(std::vector & parameters, StreamType & out_stream, pt::Space * space); - void DumpModelIfNeeded(morm::Model & model, std::vector & parameters, StreamType & out_stream); + void DumpSpaceIfNeeded(std::vector & parameters, pt::Stream & out_stream, pt::Space * space); + void DumpModelIfNeeded(morm::Model & model, std::vector & parameters, pt::Stream & out_stream); - StreamType * FindAddOutputStream(const std::wstring & name); - void CopyTmpStreamToOutputStreams(Item::Function & fun, StreamType & ezc_out_tmp_stream); + 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); @@ -405,7 +408,7 @@ private: bool HasAskForSuchFrame(Item::Function & fun); bool HasAskForSuchFrame(const std::wstring & name); - StreamType * GetNewStreamForFrame(); + pt::Stream * GetNewStreamForFrame(); void DecrementFramesStackIndex(); void MakeTextIf_go(Item & item, bool result); @@ -440,8 +443,8 @@ private: -template -Generator::Generator() : empty_stream() + +Generator::Generator() : empty_stream() { ppattern = nullptr; pblocks = nullptr; @@ -474,17 +477,17 @@ Generator::Generator() : } -template -Generator::Generator(const Generator & n) + +Generator::Generator(const Generator & n) { operator=(n); } -template -Generator & -Generator::operator=(const Generator & n) + +Generator & +Generator::operator=(const Generator & n) { ppattern = n.ppattern; pblocks = n.pblocks; @@ -522,6 +525,7 @@ Generator::operator=(cons // don't copy stack // don't copy ezc_frame_stack_tab // don't copy output_stream and output_frames_streams + // don't copy tmp streams // !! CHECK ME // may copying should be denied when generator is working? @@ -533,8 +537,8 @@ return *this; -template -Generator::~Generator() + +Generator::~Generator() { ClearFilterTab(); ClearForStack(); @@ -544,8 +548,8 @@ Generator::~Generator() -template -void Generator::SetPattern(Pattern & pattern) + +void Generator::SetPattern(Pattern & pattern) { ppattern = &pattern; } @@ -553,36 +557,36 @@ void Generator::SetPatter -template -void Generator::SetBlocks(Blocks & blocks) + +void Generator::SetBlocks(Blocks & blocks) { pblocks = &blocks; } -template -void Generator::SetFunctions(Functions & functions) + +void Generator::SetFunctions(Functions & functions) { pfunctions = &functions; } -template -void Generator::SetObjects(Objects & objects) + +void Generator::SetObjects(Objects & objects) { pobjects = &objects; } -template -void Generator::SetVariables(Vars & variables) + +void Generator::SetVariables(Vars & variables) { pvars = &variables; } -template -void Generator::SetLogger(pt::Log & logger) + +void Generator::SetLogger(pt::Log & logger) { plog = &logger; } @@ -590,62 +594,62 @@ void Generator::SetLogger #ifdef EZC_HAS_MORM_LIBRARY -template -void Generator::SetModels(Models & models) + +void Generator::SetModels(Models & models) { pmodels = ⊧ } #endif -template -void Generator::CanUseCache(bool can_use_cache) + +void Generator::CanUseCache(bool can_use_cache) { can_find_in_cache = can_use_cache; } -template -void Generator::CanUseVars(bool can_use_variables) + +void Generator::CanUseVars(bool can_use_variables) { can_use_vars = can_use_variables; } -template -void Generator::SetProgramMode(bool set_program_mode) + +void Generator::SetProgramMode(bool set_program_mode) { this->program_mode = set_program_mode; } -template -void Generator::SetExpressionParser(ExpressionParser * expression_parser) + +void Generator::SetExpressionParser(ExpressionParser * expression_parser) { this->expression_parser = expression_parser; } -template -void Generator::OnlyFrames(const std::vector & frames) + +void Generator::OnlyFrames(const std::vector & frames) { this->only_frames = &frames; } -template -void Generator::OnlyFrames(const std::vector * frames) + +void Generator::OnlyFrames(const std::vector * frames) { this->only_frames = frames; } -template -void Generator::UseMainStream(bool use_main_stream) + +void Generator::UseMainStream(bool use_main_stream) { this->use_main_stream = use_main_stream; } -template -void Generator::ResizeStack() + +void Generator::ResizeStack() { if( stack_tab.size() != stack_size ) { @@ -661,8 +665,8 @@ void Generator::ResizeSta -template -void Generator::ResizeStreamStack(std::vector & stream_tab, size_t stream_tab_max_size) + +void Generator::ResizeStreamStack(std::vector & stream_tab, size_t stream_tab_max_size) { if( stream_tab.size() != stream_tab_max_size ) { @@ -672,7 +676,7 @@ void Generator::ResizeStr stream_tab.resize(stream_tab_max_size); for( ; inew_empty(); } else { @@ -685,22 +689,22 @@ void Generator::ResizeStr } -template -void Generator::ResizeFilterTab() + +void Generator::ResizeFilterTab() { ResizeStreamStack(filter_tab, filter_size); } -template -void Generator::ResizeEzcFrameStack() + +void Generator::ResizeEzcFrameStack() { ResizeStreamStack(ezc_frame_stack_tab, ezc_frames_stack_size); } -template -void Generator::ResizeBlockStack() + +void Generator::ResizeBlockStack() { if( block_stack_tab.size() != block_stack_size ) { @@ -710,7 +714,7 @@ void Generator::ResizeBlo block_stack_tab.resize(block_stack_size); for( ; inew_empty(); } else { @@ -724,8 +728,8 @@ void Generator::ResizeBlo -template -void Generator::ClearStreamStack(std::vector & stream_tab) + +void Generator::ClearStreamStack(std::vector & stream_tab) { for(size_t i=0 ; i::ClearStre } -template -void Generator::ClearFilterTab() + +void Generator::ClearFilterTab() { ClearStreamStack(filter_tab); } -template -void Generator::ClearEzcFrameTab() + +void Generator::ClearEzcFrameTab() { ClearStreamStack(ezc_frame_stack_tab); } -template -void Generator::ClearBlockStack() +void Generator::ClearTmpStreams() +{ + delete empty_stream; + delete stream_temp1; + delete stream_temp_define; +} + + +void Generator::ClearBlockStack() { for(size_t i=0 ; i::ClearBloc } -template -void Generator::ClearForStack() + +void Generator::ClearForStack() { for(size_t i=0 ; i -void Generator::ClearStream(StreamType & str) + +void Generator::ClearStream(pt::Stream & str) { - if constexpr(is_pikotools_stream) - { - str.clear(); - } - else - { - str.str(L""); - } + str.clear(); } -template -void Generator::CopyStreamToString(StreamType & src_stream, std::wstring & dst_string) + +void Generator::CopyStreamToString(pt::Stream & src_stream, std::wstring & dst_string) { - if constexpr (sizeof(wchar_t) == sizeof(typename StreamType::char_type)) - { - if constexpr(is_pikotools_stream) - { - src_stream.to_str(dst_string); - } - else - { - dst_string = src_stream.str(); - } - } - else - { - if constexpr(is_pikotools_stream) - { - src_stream.to_str(dst_string); - } - else - { - std::string tmp = src_stream.str(); - pt::utf8_to_wide(tmp, dst_string); - } - } -} + src_stream.to_str(dst_string); - -template -void Generator::CopyStream(StreamType & src_stream, StreamType & dst_stream) -{ - if constexpr(is_pikotools_stream) - { - dst_stream << src_stream; - } - else - { - dst_stream << src_stream.str(); - } -} - - -template -void Generator::CopyStream(pt::WTextStream & src_stream, StreamType & dst_stream, bool should_escape) -{ - if constexpr(is_autoescape_stream) - { - dst_stream.Escape(should_escape); - dst_stream << src_stream; - } - else - { - if constexpr(is_pikotools_stream) - { - dst_stream << src_stream; - } - else - { - if constexpr(sizeof(char) == sizeof(typename StreamType::char_type)) - { - wide_stream_to_utf8(src_stream, dst_stream, false); - } - else - { - dst_stream << src_stream.to_str(); - } - } - } + // if constexpr (sizeof(wchar_t) == sizeof(typename pt::Stream::char_type)) + // { + // if constexpr(is_pikotools_stream) + // { + // src_stream.to_str(dst_string); + // } + // else + // { + // dst_string = src_stream.str(); + // } + // } + // else + // { + // if constexpr(is_pikotools_stream) + // { + // src_stream.to_str(dst_string); + // } + // else + // { + // std::string tmp = src_stream.str(); + // pt::utf8_to_wide(tmp, dst_string); + // } + // } } -template -void Generator::RemoveStackFunData(Stack & s) +void Generator::CopyStream(pt::Stream & src_stream, pt::Stream & dst_stream) +{ + dst_stream << src_stream; + + // if constexpr(is_pikotools_stream) + // { + // dst_stream << src_stream; + // } + // else + // { + // dst_stream << src_stream.str(); + // } +} + + + +void Generator::CopyStream(pt::WTextStream & src_stream, pt::Stream & dst_stream, bool should_escape) +{ + dst_stream.escape_input(should_escape); + dst_stream << src_stream; + + // if constexpr(is_autoescape_stream) + // { + // dst_stream.Escape(should_escape); + // dst_stream << src_stream; + // } + // else + // { + // if constexpr(is_pikotools_stream) + // { + // dst_stream << src_stream; + // } + // else + // { + // if constexpr(sizeof(char) == sizeof(typename pt::Stream::char_type)) + // { + // wide_stream_to_utf8(src_stream, dst_stream, false); + // } + // else + // { + // dst_stream << src_stream.to_str(); + // } + // } + // } +} + + + + +void Generator::RemoveStackFunData(Stack & s) { if( s.fun_data && s.auto_remove ) { @@ -865,9 +876,9 @@ void Generator::RemoveSta } -template + template -CharType Generator::ToLower(CharType c) +CharType Generator::ToLower(CharType c) { if( c>='A' && c<='Z' ) return c - 'A' + 'a'; @@ -876,52 +887,52 @@ return c; } -template -bool Generator::ConvertToBool(const std::wstring & str) + +bool Generator::ConvertToBool(const std::wstring & str) { return !str.empty(); } -template -void Generator::SetMax(size_t max_items_, size_t max_for_items_) + +void Generator::SetMax(size_t max_items_, size_t max_for_items_) { max_items = max_items_; max_for_items = max_for_items_; } -template -void Generator::RecognizeSpecialChars(bool spec) + +void Generator::RecognizeSpecialChars(bool spec) { special_chars = spec; } -template -void Generator::TrimWhite(bool trim) + +void Generator::TrimWhite(bool trim) { trim_white = trim; } -template -void Generator::SkipNewLine(bool skip) + +void Generator::SkipNewLine(bool skip) { skip_new_line = skip; } -template -void Generator::SetMaxFilters(size_t new_len) + +void Generator::SetMaxFilters(size_t new_len) { // the table will be resized when Generate() method is called filter_size = new_len; } -template -void Generator::SetStackSize(size_t new_stack_size) + +void Generator::SetStackSize(size_t new_stack_size) { // the stack will be resized when Generate() method is called stack_size = new_stack_size; @@ -929,8 +940,8 @@ void Generator::SetStackS -template -void Generator::Generate() + +void Generator::Generate() { if( is_generator_working ) { @@ -975,9 +986,10 @@ void Generator::Generate( } -template -void Generator::Generate(StreamType & out) + +void Generator::Generate(pt::Stream & out) { + main_stream = &out; output_stream = &out; output_frames_streams = nullptr; Generate(); @@ -985,33 +997,40 @@ void Generator::Generate( -template -void Generator::Generate(StreamType & out, OutStreams & out_streams) + +void Generator::Generate(pt::Stream & out, OutStreams & out_streams) { + main_stream = &out; output_stream = &out; output_frames_streams = &out_streams; Generate(); } -template -void Generator::Generate(OutStreams & out_streams) +void Generator::InitializeTmpStreams() { - output_stream = nullptr; - output_frames_streams = &out_streams; - Generate(); + empty_stream = main_stream->new_empty(); + stream_temp1 = main_stream->new_empty(); + stream_temp_define = main_stream->new_empty(); } +// void Generator::Generate(OutStreams & out_streams) +// { +// output_stream = nullptr; +// output_frames_streams = &out_streams; +// Generate(); +// } -template -bool Generator::IsTestingFunctionExistence() + + +bool Generator::IsTestingFunctionExistence() { return is_generating_if_def || is_generating_if_not_def; } -template -bool Generator::CheckBlockArgument(int arg_index, FindHelper & find_helper) + +bool Generator::CheckBlockArgument(int arg_index, FindHelper & find_helper) { if( arg_index < 0 ) return false; @@ -1033,21 +1052,21 @@ bool Generator::CheckBloc -template -bool Generator::FindInCache(Item::Function & item_fun, FindHelper & find_helper) + +bool Generator::FindInCache(Item::Function & item_fun, FindHelper & find_helper) { if( can_find_in_cache ) { if( item_fun.base_obj ) { - find_helper.base_obj = reinterpret_cast * >(item_fun.base_obj); + find_helper.base_obj = reinterpret_cast(item_fun.base_obj); find_helper.method_index = item_fun.method_index; return true; } if( item_fun.fun_cache ) { - find_helper.function = reinterpret_cast::UserFunction*>(item_fun.fun_cache); + find_helper.function = reinterpret_cast(item_fun.fun_cache); return true; } @@ -1065,8 +1084,8 @@ return false; #ifdef EZC_HAS_MORM_LIBRARY -template -bool Generator::FindInModels(FindHelper & find_helper) + +bool Generator::FindInModels(FindHelper & find_helper) { if( pmodels ) { @@ -1085,12 +1104,12 @@ bool Generator::FindInMod #endif -template -bool Generator::FindInFunctionsAndBlocks(const std::wstring & name, FindHelper & find_helper) + +bool Generator::FindInFunctionsAndBlocks(const std::wstring & name, FindHelper & find_helper) { if( pobjects ) { - typename Objects::Iterator i = pobjects->Find(name, find_helper.method_index); + typename Objects::Iterator i = pobjects->Find(name, find_helper.method_index); if( i != pobjects->End() ) { @@ -1101,7 +1120,7 @@ bool Generator::FindInFun if( pfunctions ) { - typename Functions::Iterator i = pfunctions->Find(name); + typename Functions::Iterator i = pfunctions->Find(name); if( i != pfunctions->End() ) { @@ -1125,8 +1144,8 @@ return false; } -template -bool Generator::FindInVariables(const std::wstring & name, FindHelper & find_helper) + +bool Generator::FindInVariables(const std::wstring & name, FindHelper & find_helper) { if( pvars ) { @@ -1148,8 +1167,8 @@ bool Generator::FindInVar * 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 -bool Generator::Find(Item::Function & item_fun, FindHelper & find_helper) + +bool Generator::Find(Item::Function & item_fun, FindHelper & find_helper) { if( CheckBlockArgument(item_fun.arg, find_helper) ) return true; @@ -1182,8 +1201,8 @@ bool Generator::Find(Item } -template -void Generator::PrepareEnvStruct(FunInfo & info) + +void Generator::PrepareEnvStruct(FunInfo & info) { info.Clear(); @@ -1200,8 +1219,8 @@ void Generator::PrepareEn -template -void Generator::CallFunction(typename Functions::UserFunction & function, FunInfo & info) + +void Generator::CallFunction(typename Functions::UserFunction & function, FunInfo & info) { PrepareEnvStruct(info); (function)(info); @@ -1210,22 +1229,22 @@ void Generator::CallFunct -template -void Generator::CallFunction(typename Functions::UserFunction & function, + +void Generator::CallFunction(typename Functions::UserFunction & function, std::vector & parameters, - StreamType & out_stream, - const StreamType & in_stream) + pt::Stream & out_stream, + const pt::Stream & in_stream) { if( !IsTestingFunctionExistence() ) { if( parameters.empty() ) { - FunInfo info(out_stream, parameters, empty, in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); + FunInfo info(out_stream, parameters, empty, in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); CallFunction(function, info); } else { - FunInfo info(out_stream, parameters, parameters[0].str, in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); + FunInfo info(out_stream, parameters, parameters[0].str, in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); CallFunction(function, info); } } @@ -1234,8 +1253,8 @@ void Generator::CallFunct -template -void Generator::CallObject(BaseObj & base_obj, int method_index, FunInfo & info) + +void Generator::CallObject(BaseObj & base_obj, int method_index, FunInfo & info) { PrepareEnvStruct(info); base_obj.CallFun(method_index, info); @@ -1245,8 +1264,8 @@ void Generator::CallObjec -template -bool Generator::HasParam(std::vector & parameters, const wchar_t * param1, const wchar_t * param2) + +bool Generator::HasParam(std::vector & parameters, const wchar_t * param1, const wchar_t * param2) { for(Var & var : parameters) { @@ -1264,30 +1283,30 @@ bool Generator::HasParam( -template -bool Generator::ShouldMakeSpaceDump(std::vector & parameters) + +bool Generator::ShouldMakeSpaceDump(std::vector & parameters) { return HasParam(parameters, L"dump", L"dump_to_space"); } -template -bool Generator::ShouldMakeJsonDump(std::vector & parameters) + +bool Generator::ShouldMakeJsonDump(std::vector & parameters) { return HasParam(parameters, L"dump_to_json"); } -template -bool Generator::IsPrettyPrint(std::vector & parameters) + +bool Generator::IsPrettyPrint(std::vector & parameters) { return HasParam(parameters, L"pretty"); } -template -void Generator::DumpSpaceIfNeeded(std::vector & parameters, StreamType & out_stream, pt::Space * space) + +void Generator::DumpSpaceIfNeeded(std::vector & parameters, pt::Stream & out_stream, pt::Space * space) { bool dump_space = ShouldMakeSpaceDump(parameters); bool dump_json = ShouldMakeJsonDump(parameters); @@ -1309,8 +1328,8 @@ void Generator::DumpSpace } -template -void Generator::DumpModelIfNeeded(morm::Model & model, std::vector & parameters, StreamType & out_stream) + +void Generator::DumpModelIfNeeded(morm::Model & model, std::vector & parameters, pt::Stream & out_stream) { bool dump_space = ShouldMakeSpaceDump(parameters); bool dump_json = ShouldMakeJsonDump(parameters); @@ -1338,8 +1357,8 @@ void Generator::DumpModel -template -void Generator::PrintDate(pt::Date * date, std::vector & parameters, StreamType & out_stream) + +void Generator::PrintDate(pt::Date * date, std::vector & parameters, pt::Stream & out_stream) { bool is_roman = HasParam(parameters, L"roman"); bool is_no_sec = HasParam(parameters, L"no_sec"); @@ -1365,8 +1384,8 @@ void Generator::PrintDate } -template -bool Generator::PrintDatePart(pt::Date * date, const std::wstring & field, std::vector & parameters, StreamType & out_stream) + +bool Generator::PrintDatePart(pt::Date * date, const std::wstring & field, std::vector & parameters, pt::Stream & out_stream) { bool is_test = IsTestingFunctionExistence(); @@ -1421,8 +1440,8 @@ bool Generator::PrintDate } -template -bool Generator::CallDate(FindHelper & find_helper, std::vector & fields, std::vector & parameters, StreamType & out_stream) + +bool Generator::CallDate(FindHelper & find_helper, std::vector & fields, std::vector & parameters, pt::Stream & out_stream) { bool found = true; bool all_fields_known = (find_helper.field_index == fields.size()); @@ -1461,8 +1480,8 @@ bool Generator::CallDate( -template -void Generator::CallSpaceObjectForLastField(std::vector & parameters, StreamType & out_stream, pt::Space * space) + +void Generator::CallSpaceObjectForLastField(std::vector & parameters, pt::Stream & out_stream, pt::Space * space) { if( IsTestingFunctionExistence() ) { @@ -1476,8 +1495,8 @@ void Generator::CallSpace } -template -pt::Space * Generator::CallSpaceObjectForMiddleField(std::wstring & root_space_name, std::vector & fields, size_t field_index, pt::Space * space) + +pt::Space * Generator::CallSpaceObjectForMiddleField(std::wstring & root_space_name, std::vector & fields, size_t field_index, pt::Space * space) { std::wstring & next_field = fields[field_index]; space = space->get_space(next_field); @@ -1492,9 +1511,9 @@ pt::Space * Generator::Ca -template -void Generator::CallSpaceTableForLastField( - morm::SpaceWrapper & space_wrapper, std::vector & parameters, StreamType & out_stream, pt::Space * space, size_t model_wrapper_space_table_index) + +void Generator::CallSpaceTableForLastField( + morm::SpaceWrapper & space_wrapper, std::vector & parameters, pt::Stream & out_stream, pt::Space * space, size_t model_wrapper_space_table_index) { pt::Space::TableType * table = space->get_table(); @@ -1536,8 +1555,8 @@ void Generator::CallSpace } -template -pt::Space * Generator::CallSpaceTableForMiddleField(morm::SpaceWrapper & space_wrapper, std::wstring & root_space_name, std::vector & fields, + +pt::Space * Generator::CallSpaceTableForMiddleField(morm::SpaceWrapper & space_wrapper, std::wstring & root_space_name, std::vector & fields, size_t field_index, pt::Space * space, size_t model_wrapper_space_table_index) { pt::Space::TableType * table = space->get_table(); @@ -1560,8 +1579,8 @@ pt::Space * Generator::Ca } -template -void Generator::PrintLastSpaceField(pt::Space * space, std::vector & parameters, StreamType & out_stream) + +void Generator::PrintLastSpaceField(pt::Space * space, std::vector & parameters, pt::Stream & out_stream) { bool no_escape = HasParam(parameters, L"raw", L"noescape"); @@ -1579,8 +1598,8 @@ void Generator::PrintLast -template -bool Generator::CallSpace(FindHelper & find_helper, std::vector & fields, std::vector & parameters, StreamType & out_stream) + +bool Generator::CallSpace(FindHelper & find_helper, std::vector & fields, std::vector & parameters, pt::Stream & out_stream) { morm::SpaceWrapper * space_wrapper = find_helper.wrapper->space_wrapper; pt::Space * space = space_wrapper->get_space(); @@ -1681,9 +1700,9 @@ bool Generator::CallSpace #ifdef EZC_HAS_MORM_LIBRARY -template -bool Generator::CallModelField( - morm::Model & model, const std::wstring & field, std::vector & parameters, StreamType & out_stream, const StreamType & in_stream) + +bool Generator::CallModelField( + morm::Model & model, const std::wstring & field, std::vector & parameters, pt::Stream & out_stream, const pt::Stream & in_stream) { /* * if 'field' is a POD type then 'str' will be used in get_raw_value() @@ -1696,14 +1715,14 @@ bool Generator::CallModel if( parameters.empty() ) { - FunInfo info(out_stream, parameters, empty, in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); + FunInfo info(out_stream, parameters, empty, in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); PrepareEnvStruct(info); found = model.get_raw_value(nullptr, field.c_str(), nullptr, info, str, false); last_res = info.res; } else { - FunInfo info(out_stream, parameters, parameters[0].str, in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); + FunInfo info(out_stream, parameters, parameters[0].str, in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); PrepareEnvStruct(info); found = model.get_raw_value(nullptr, field.c_str(), nullptr, info, str, false); last_res = info.res; @@ -1719,9 +1738,9 @@ bool Generator::CallModel } -template -bool Generator::CallModel(morm::Model & model, FindHelper & find_helper, std::vector & fields, - std::vector & parameters, StreamType & out_stream, const StreamType & in_stream) + +bool Generator::CallModel(morm::Model & model, FindHelper & find_helper, std::vector & fields, + std::vector & parameters, pt::Stream & out_stream, const pt::Stream & in_stream) { bool found = true; @@ -1764,8 +1783,8 @@ bool Generator::CallModel -template -void Generator::FindLastModelWrapper(FindHelper & find_helper, std::vector & fields) + +void Generator::FindLastModelWrapper(FindHelper & find_helper, std::vector & fields) { for(find_helper.field_index = 0 ; find_helper.field_index < fields.size() && find_helper.wrapper->has_model_object() ; ++find_helper.field_index) { @@ -1806,9 +1825,9 @@ void Generator::FindLastM } -template -bool Generator::CallWrapper(FindHelper & find_helper, std::vector & fields, - std::vector & parameters, StreamType & out_stream, const StreamType & in_stream) + +bool Generator::CallWrapper(FindHelper & find_helper, std::vector & fields, + std::vector & parameters, pt::Stream & out_stream, const pt::Stream & in_stream) { bool found = true; last_res = false; @@ -1878,23 +1897,23 @@ bool Generator::CallWrapp -template -void Generator::CallObject(BaseObj & base_obj, + +void Generator::CallObject(BaseObj & base_obj, int method_index, std::vector & parameters, - StreamType & out_stream, - const StreamType & in_stream) + pt::Stream & out_stream, + const pt::Stream & in_stream) { if( !IsTestingFunctionExistence() ) { if( parameters.empty() ) { - FunInfo info(out_stream, parameters, empty, in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); + FunInfo info(out_stream, parameters, empty, in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); CallObject(base_obj, method_index, info); } else { - FunInfo info(out_stream, parameters, parameters[0].str, in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); + FunInfo info(out_stream, parameters, parameters[0].str, in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); CallObject(base_obj, method_index, info); } } @@ -1902,10 +1921,10 @@ void Generator::CallObjec -template -bool Generator::CallBlock(Item & item_block, + +bool Generator::CallBlock(Item & item_block, std::vector & parameters, - StreamType & out_stream) + pt::Stream & out_stream) { if( block_stack_index >= block_stack_tab.size() ) { @@ -1915,7 +1934,7 @@ bool Generator::CallBlock if( !IsTestingFunctionExistence() ) { - StreamType * old_stream = output_stream; + pt::Stream * old_stream = output_stream; BlockStack & block_stack = block_stack_tab[block_stack_index]; block_stack.was_return = false; @@ -1940,8 +1959,8 @@ return true; -template -bool Generator::CallVariable(Item::Function & item_fun, Var & variable, std::vector & parameters, StreamType & out_stream, const StreamType & in_stream) + +bool Generator::CallVariable(Item::Function & item_fun, Var & variable, std::vector & parameters, pt::Stream & out_stream, const pt::Stream & in_stream) { if( variable.is_function ) { @@ -1966,12 +1985,12 @@ bool Generator::CallVaria * * return: true if a function, variable or block was found and called (evaluated) */ -template -bool Generator::Call(Item::Function & item_fun, + +bool Generator::Call(Item::Function & item_fun, std::wstring * fun_name, - StreamType & out_stream, + pt::Stream & out_stream, bool clear_out_stream, - const StreamType & in_stream) + const pt::Stream & in_stream) { FindHelper find_helper; std::vector parameters; @@ -1979,8 +1998,9 @@ std::vector parameters; if( clear_out_stream ) ClearStream(out_stream); - if constexpr(is_autoescape_stream) - out_stream.Escape(true); + // if constexpr(is_autoescape_stream) + // out_stream.Escape(true); + out_stream.escape_input(true); find_helper.fun_name = fun_name; @@ -1998,10 +2018,11 @@ std::vector parameters; if( fun_child.is_function ) { - StreamType local_temp_stream; - Call(fun_child, nullptr, local_temp_stream, true, empty_stream); + //pt::Stream local_temp_stream; + std::unique_ptr local_temp_stream(main_stream->new_empty()); + Call(fun_child, nullptr, *local_temp_stream, true, *empty_stream); - CopyStreamToString(local_temp_stream, parameters[i].str); + CopyStreamToString(*local_temp_stream, parameters[i].str); parameters[i].res = last_res; } else @@ -2034,19 +2055,19 @@ std::vector parameters; // return: true if a function or variable was found and called -template -bool Generator::Call(Item::Function & item_fun) + +bool Generator::Call(Item::Function & item_fun) { - bool status = Call(item_fun, nullptr, stream_temp1, true, empty_stream); - ClearStream(stream_temp1); + bool status = Call(item_fun, nullptr, *stream_temp1, true, *empty_stream); + ClearStream(*stream_temp1); return status; } -template -wchar_t Generator::CreateSpecialChar(wchar_t c) + +wchar_t Generator::CreateSpecialChar(wchar_t c) { wchar_t res = 0; @@ -2074,8 +2095,8 @@ return res; // if the special character is unknown the first slash is printing // so "\t" gives one character of code 9 // and "\x" gives "\x" -template -const wchar_t * Generator::PrintSpecialChar(const wchar_t * start, const wchar_t * end) + +const wchar_t * Generator::PrintSpecialChar(const wchar_t * start, const wchar_t * end) { wchar_t special = 0; @@ -2099,8 +2120,8 @@ return start; } -template -void Generator::PrintSpecialText(const wchar_t * start, const wchar_t * end) + +void Generator::PrintSpecialText(const wchar_t * start, const wchar_t * end) { if( output_stream ) { @@ -2128,8 +2149,8 @@ void Generator::PrintSpec } -template -void Generator::PrintNormalText(const wchar_t * start, const wchar_t * end) + +void Generator::PrintNormalText(const wchar_t * start, const wchar_t * end) { if( output_stream ) { @@ -2163,8 +2184,8 @@ void Generator::PrintNorm } -template -bool Generator::IsWhite(wchar_t c) + +bool Generator::IsWhite(wchar_t c) { // 13 (\r) is from a dos file at the end of a line (\r\n) // 160 is a non-breaking space @@ -2176,8 +2197,8 @@ return false; } -template -void Generator::TrimWhite(const wchar_t *& start, const wchar_t *& end) + +void Generator::TrimWhite(const wchar_t *& start, const wchar_t *& end) { while( start != end && IsWhite(*start) ) ++start; @@ -2187,8 +2208,8 @@ void Generator::TrimWhite } -template -void Generator::SkipWhite(const wchar_t *& str) + +void Generator::SkipWhite(const wchar_t *& str) { while( IsWhite(*str) ) str += 1; @@ -2196,8 +2217,8 @@ void Generator::SkipWhite -template -size_t Generator::StrToSize(const wchar_t * str, const wchar_t ** str_end) + +size_t Generator::StrToSize(const wchar_t * str, const wchar_t ** str_end) { size_t res = 0; @@ -2222,10 +2243,10 @@ return res; } -template -StreamType * Generator::FindAddOutputStream(const std::wstring & name) + +pt::Stream * Generator::FindAddOutputStream(const std::wstring & name) { - StreamType * stream = nullptr; + pt::Stream * stream = nullptr; if( output_frames_streams ) { @@ -2255,8 +2276,8 @@ StreamType * Generator::F } -template -void Generator::CopyTmpStreamToOutputStreams(Item::Function & fun, StreamType & ezc_out_tmp_stream) + +void Generator::CopyTmpStreamToOutputStreams(Item::Function & fun, pt::Stream & ezc_out_tmp_stream) { if( output_frames_streams ) { @@ -2266,7 +2287,7 @@ void Generator::CopyTmpSt if( HasAskForSuchFrame(name) ) { - StreamType * stream = FindAddOutputStream(name); + pt::Stream * stream = FindAddOutputStream(name); if( stream ) { @@ -2278,8 +2299,8 @@ void Generator::CopyTmpSt } -template -void Generator::CreateMsg(const wchar_t * type, const wchar_t * arg) + +void Generator::CreateMsg(const wchar_t * type, const wchar_t * arg) { if( plog ) { @@ -2296,8 +2317,8 @@ void Generator::CreateMsg -template -void Generator::CreateMsg(const wchar_t * type, const std::wstring & model_name, std::vector & fields, size_t how_many_fields_print, + +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) { if( plog ) @@ -2329,8 +2350,8 @@ void Generator::CreateMsg } -template -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, std::vector & fields, const wchar_t * arg, const wchar_t * arg2, const wchar_t * arg3) { @@ -2339,24 +2360,24 @@ void Generator::CreateMsg -template -void Generator::CreateMsg(const std::wstring & type, const std::wstring & arg) + +void Generator::CreateMsg(const std::wstring & type, const std::wstring & arg) { CreateMsg(type.c_str(), arg.c_str()); } -template -void Generator::CreateMsg(const std::wstring & type) + +void Generator::CreateMsg(const std::wstring & type) { CreateMsg(type.c_str()); } -template -void Generator::EvaluateProgramNode(Item & item) + +void Generator::EvaluateProgramNode(Item & item) { if( output_stream ) { @@ -2399,8 +2420,8 @@ void Generator::EvaluateP } -template -bool Generator::HasAskForSuchFrame(Item::Function & fun) + +bool Generator::HasAskForSuchFrame(Item::Function & fun) { if( only_frames ) { @@ -2426,8 +2447,8 @@ bool Generator::HasAskFor } -template -bool Generator::HasAskForSuchFrame(const std::wstring & name) + +bool Generator::HasAskForSuchFrame(const std::wstring & name) { if( only_frames ) { @@ -2448,8 +2469,8 @@ bool Generator::HasAskFor } -template -void Generator::MakeItemText(Item & item) + +void Generator::MakeItemText(Item & item) { if( is_output_stream_allowed ) { @@ -2467,8 +2488,8 @@ void Generator::MakeItemT } -template -void Generator::MakeTextContainer(Item & item) + +void Generator::MakeTextContainer(Item & item) { std::vector::iterator i = item.item_tab.begin(); @@ -2478,8 +2499,8 @@ void Generator::MakeTextC -template -void Generator::MakeTextFunction(Item & item) + +void Generator::MakeTextFunction(Item & item) { is_generating_normal = true; @@ -2491,20 +2512,20 @@ void Generator::MakeTextF { if( output_stream && is_output_stream_allowed ) { - Call(item.function, nullptr, *output_stream, false, empty_stream); + Call(item.function, nullptr, *output_stream, false, *empty_stream); } else { - Call(item.function, nullptr, stream_temp1, false, empty_stream); - ClearStream(stream_temp1); + Call(item.function, nullptr, *stream_temp1, false, *empty_stream); + ClearStream(*stream_temp1); } } } -template -void Generator::MakeTextIf_go(Item & item, bool result) + +void Generator::MakeTextIf_go(Item & item, bool result) { if( result ) { @@ -2521,8 +2542,8 @@ void Generator::MakeTextI -template -void Generator::MakeTextIf(Item & item) + +void Generator::MakeTextIf(Item & item) { is_generating_if = true; @@ -2540,8 +2561,8 @@ void Generator::MakeTextI } -template -void Generator::MakeTextIfDef(Item & item) + +void Generator::MakeTextIfDef(Item & item) { is_generating_if_def = true; @@ -2559,8 +2580,8 @@ void Generator::MakeTextI } -template -void Generator::MakeTextIfNotDef(Item & item) + +void Generator::MakeTextIfNotDef(Item & item) { is_generating_if_not_def = true; @@ -2578,8 +2599,8 @@ void Generator::MakeTextI } -template -void Generator::MakeTextFor(Item & item) + +void Generator::MakeTextFor(Item & item) { stack_tab[stack_index-1].is_for = true; @@ -2602,7 +2623,7 @@ void Generator::MakeTextF } else { - if( !Call(item.function, nullptr, stream_temp1, true, empty_stream) ) + if( !Call(item.function, nullptr, *stream_temp1, true, *empty_stream) ) return; } @@ -2615,8 +2636,8 @@ void Generator::MakeTextF } -template -void Generator::MakeTextDefine(Item & item, Var & var) + +void Generator::MakeTextDefine(Item & item, Var & var) { var.str.clear(); var.res = ConvertToBool(var.str); @@ -2638,9 +2659,9 @@ void Generator::MakeTextD if( fun.is_function ) { // call function - if( Call(fun, nullptr, stream_temp_define, true, empty_stream) ) + if( Call(fun, nullptr, *stream_temp_define, true, *empty_stream) ) { - CopyStreamToString(stream_temp_define, var.str); + CopyStreamToString(*stream_temp_define, var.str); var.res = last_res; } else @@ -2656,8 +2677,8 @@ void Generator::MakeTextD } -template -void Generator::MakeTextDefine(Item & item) + +void Generator::MakeTextDefine(Item & item) { if( !can_use_vars || !pvars ) { @@ -2670,8 +2691,8 @@ void Generator::MakeTextD } -template -void Generator::MakeTextDefineIfNotSet(Item & item) + +void Generator::MakeTextDefineIfNotSet(Item & item) { if( !can_use_vars || !pvars ) { @@ -2691,8 +2712,8 @@ void Generator::MakeTextD -template -void Generator::MakeTextLet(Item & item, Var & var) + +void Generator::MakeTextLet(Item & item, Var & var) { var.str.clear(); var.res = ConvertToBool(var.str); @@ -2721,8 +2742,8 @@ void Generator::MakeTextL } -template -void Generator::MakeTextLet(Item & item) + +void Generator::MakeTextLet(Item & item) { if( !can_use_vars || !pvars ) { @@ -2735,8 +2756,8 @@ void Generator::MakeTextL } -template -void Generator::MakeTextLetIfNotSet(Item & item) + +void Generator::MakeTextLetIfNotSet(Item & item) { if( !can_use_vars || !pvars ) { @@ -2755,8 +2776,8 @@ void Generator::MakeTextL -template -void Generator::MakeTextFilter(Item & item) + +void Generator::MakeTextFilter(Item & item) { if( filter_index >= filter_tab.size() ) { @@ -2764,7 +2785,7 @@ void Generator::MakeTextF return; } - StreamType * old_stream = output_stream; + pt::Stream * old_stream = output_stream; output_stream = filter_tab[filter_index]; ClearStream(*output_stream); @@ -2781,8 +2802,8 @@ void Generator::MakeTextF } else { - Call(item.function, nullptr, stream_temp1, true, *output_stream); - ClearStream(stream_temp1); + Call(item.function, nullptr, *stream_temp1, true, *output_stream); + ClearStream(*stream_temp1); } ClearStream(*output_stream); @@ -2791,8 +2812,8 @@ void Generator::MakeTextF } -template -StreamType * Generator::GetNewStreamForFrame() + +pt::Stream * Generator::GetNewStreamForFrame() { if( ezc_frames_stack_index >= ezc_frame_stack_tab.size() ) { @@ -2800,7 +2821,7 @@ StreamType * Generator::G return nullptr; } - StreamType * stream = ezc_frame_stack_tab[ezc_frames_stack_index]; + pt::Stream * stream = ezc_frame_stack_tab[ezc_frames_stack_index]; ClearStream(*stream); ezc_frames_stack_index += 1; @@ -2808,8 +2829,8 @@ StreamType * Generator::G } -template -void Generator::DecrementFramesStackIndex() + +void Generator::DecrementFramesStackIndex() { if( ezc_frames_stack_index > 0 ) { @@ -2818,10 +2839,10 @@ void Generator::Decrement } -template -void Generator::MakeEzcFrame2(Item & item) + +void Generator::MakeEzcFrame2(Item & item) { - StreamType * old_stream = output_stream; + pt::Stream * old_stream = output_stream; bool old_is_output_stream_allowed = is_output_stream_allowed; bool put_directly = false; bool is_current_frame_allowed = HasAskForSuchFrame(item.function); @@ -2880,8 +2901,8 @@ void Generator::MakeEzcFr } -template -void Generator::MakeEzcFrame(Item & item) + +void Generator::MakeEzcFrame(Item & item) { /* if we encounter the first ezc frame statement without arguments e.g. [ezc frame] or just [frame] @@ -2899,8 +2920,8 @@ void Generator::MakeEzcFr } -template -void Generator::MakeTextEzc(Item & item) + +void Generator::MakeTextEzc(Item & item) { if( item.function.name == L"frame" ) { @@ -2917,8 +2938,8 @@ void Generator::MakeTextE } -template -void Generator::MakeTextReturn(Item & item) + +void Generator::MakeTextReturn(Item & item) { last_res = false; @@ -2936,16 +2957,16 @@ void Generator::MakeTextR { // output stream in [return] statement is ignored (we use only the stream produced by the whole block) // this Call() sets last_res which is used later when we return to CallBlock() - Call(item.function, nullptr, stream_temp1, false, empty_stream); - ClearStream(stream_temp1); + Call(item.function, nullptr, *stream_temp1, false, *empty_stream); + ClearStream(*stream_temp1); } } -template -bool Generator::LimitAchieved() + +bool Generator::LimitAchieved() { if( break_generating ) return true; @@ -2953,7 +2974,7 @@ bool Generator::LimitAchi if( current_item >= max_items ) { break_generating = true; - CreateMsg(L"Generator exceeded allowed number of elements"); + CreateMsg(L"Generator exceeded allowed number of elements"); return true; } @@ -2976,8 +2997,8 @@ return false; } -template -void Generator::MakeText(Item & item) + +void Generator::MakeText(Item & item) { if( LimitAchieved() ) return; diff --git a/src/objects.cpp b/src/objects.cpp new file mode 100644 index 0000000..f515fc7 --- /dev/null +++ b/src/objects.cpp @@ -0,0 +1,104 @@ +/* + * 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) 2024, 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 "objects.h" + + +namespace Ezc +{ + + +typename Objects::Iterator Objects::Find(const std::wstring & key, int & method_index) +{ + Iterator i = obj_tab.begin(); + method_index = -1; + + for( ; i != obj_tab.end() ; ++i) + { + int index = (*i)->FindFun(key); + + if( index != -1 ) + { + method_index = index; + return i; + } + } + + return obj_tab.end(); +} + + + + +void Objects::Insert(BaseObj * base_fun) +{ + obj_tab.push_back(base_fun); +} + + +void Objects::Insert(BaseObj & base_fun) +{ + obj_tab.push_back(&base_fun); +} + + + +typename Objects::Iterator Objects::Begin() +{ + return obj_tab.begin(); +} + + +typename Objects::Iterator Objects::End() +{ + return obj_tab.end(); +} + + +size_t Objects::Size() const +{ + return obj_tab.size(); +} + + +void Objects::Clear() +{ + obj_tab.clear(); +} + + + + +} // namespace Ezc + diff --git a/src/objects.h b/src/objects.h index 933297a..a71bc17 100644 --- a/src/objects.h +++ b/src/objects.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2015, Tomasz Sowa + * Copyright (c) 2015-2024, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -38,6 +38,7 @@ #include #include #include "utf8/utf8.h" +#include "funinfo.h" @@ -46,7 +47,6 @@ namespace Ezc { -template class BaseObj { public: @@ -69,7 +69,7 @@ public: /* * */ - virtual void CallFun(int fun_index, FunInfo &) = 0; + virtual void CallFun(int fun_index, FunInfo &) = 0; /* @@ -203,16 +203,15 @@ private: -template class Objects { public: - typedef std::vector * > ObjectsTable; + typedef std::vector ObjectsTable; typedef typename ObjectsTable::iterator Iterator; - void Insert(BaseObj * base_fun); - void Insert(BaseObj & base_fun); + void Insert(BaseObj * base_fun); + void Insert(BaseObj & base_fun); Iterator Begin(); Iterator End(); @@ -229,73 +228,6 @@ private: -template -typename Objects::Iterator Objects::Find(const std::wstring & key, int & method_index) -{ - Iterator i = obj_tab.begin(); - method_index = -1; - - for( ; i != obj_tab.end() ; ++i) - { - int index = (*i)->FindFun(key); - - if( index != -1 ) - { - method_index = index; - return i; - } - } - - return obj_tab.end(); -} - - - - -template -void Objects::Insert(BaseObj * base_fun) -{ - obj_tab.push_back(base_fun); -} - - -template -void Objects::Insert(BaseObj & base_fun) -{ - obj_tab.push_back(&base_fun); -} - - - -template -typename Objects::Iterator Objects::Begin() -{ - return obj_tab.begin(); -} - - -template -typename Objects::Iterator Objects::End() -{ - return obj_tab.end(); -} - - -template -size_t Objects::Size() const -{ - return obj_tab.size(); -} - - -template -void Objects::Clear() -{ - obj_tab.clear(); -} - - - } // namespace Ezc diff --git a/src/outstreams.h b/src/outstreams.h index df121c8..349da03 100644 --- a/src/outstreams.h +++ b/src/outstreams.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2015-2021, Tomasz Sowa + * Copyright (c) 2015-2024, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -35,6 +35,7 @@ #ifndef headerfile_ezc_outstreams #define headerfile_ezc_outstreams +#include "textstream/stream.h" #include #include #include @@ -44,18 +45,17 @@ namespace Ezc { -template class OutStreams { public: - typedef std::map StreamsMap; - typedef std::vector StreamsTab; + typedef std::map StreamsMap; + typedef std::vector StreamsTab; StreamsMap streams_map; StreamsTab streams_tab; - void ResizeTab(size_t len); + void ResizeTab(pt::Stream & stream_type, size_t len); void ClearMap(); void ClearTab(); @@ -68,40 +68,35 @@ public: }; -template -OutStreams::~OutStreams() +OutStreams::~OutStreams() { ClearTab(); } -template -OutStreams::OutStreams() +OutStreams::OutStreams() { } -template -OutStreams::OutStreams(const OutStreams & o) +OutStreams::OutStreams(const OutStreams & o) { // we do not copy streams but creating new ones - ResizeTab(o.streams_tab.size()); + //ResizeTab(o.streams_tab.size()); } -template -OutStreams & OutStreams::operator=(const OutStreams & o) +OutStreams & OutStreams::operator=(const OutStreams & o) { // we do not copy streams but creating new ones streams_map.clear(); - ResizeTab(o.streams_tab.size()); + //ResizeTab(o.streams_tab.size()); return *this; } -template -void OutStreams::ClearTab() +void OutStreams::ClearTab() { for(size_t i=0 ; i::ClearTab() } -template -void OutStreams::ResizeTab(size_t len) +void OutStreams::ResizeTab(pt::Stream & stream_type, size_t len) { if( streams_tab.size() != len ) { @@ -122,7 +116,7 @@ void OutStreams::ResizeTab(size_t len) streams_tab.resize(len); for( ; i::ResizeTab(size_t len) } -template -void OutStreams::ClearMap() +void OutStreams::ClearMap() { typename StreamsMap::iterator i; for(i=streams_map.begin() ; i != streams_map.end() ; ++i) { - StreamType & str = *(i->second); + pt::Stream & str = *(i->second); + str.clear(); - if constexpr(is_pikotools_stream) - { - str.clear(); - } - else - { - str.str(L""); - } + // if constexpr(is_pikotools_stream) + // { + // str.clear(); + // } + // else + // { + // str.str(L""); + // } } streams_map.clear(); diff --git a/src/pattern.h b/src/pattern.h index bb222f5..fe5e4cc 100644 --- a/src/pattern.h +++ b/src/pattern.h @@ -4,8 +4,8 @@ * Author: Tomasz Sowa */ -/* - * Copyright (c) 2007-2015, Tomasz Sowa +/* + * Copyright (c) 2007-2024, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -56,9 +56,9 @@ public: void Clear(); - template void CacheFunctions(Functions & fun); + void CacheFunctions(Functions & fun); void CacheBlocks(Blocks & blocks); - template void CacheObjects(Objects & obj); + void CacheObjects(Objects & obj); void ClearCache(); @@ -69,15 +69,13 @@ public: -template -void Pattern::CacheFunctions(Functions & fun) +void Pattern::CacheFunctions(Functions & fun) { Cache(fun, item_root); } -template -void Pattern::CacheObjects(Objects & obj) +void Pattern::CacheObjects(Objects & obj) { Cache(obj, item_root); }