From 2fca5f3492f00c9bfd32526b6b76aaa19b9e0592 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Sun, 19 Oct 2014 05:42:25 +0000 Subject: [PATCH] changed: SetCommentary() methods from Pattern were moved to PatternParser and Generator added: caching functions and blocks caching is added into Pattern and Blocks methods: CacheFunctions() and CacheBlocks() git-svn-id: svn://ttmath.org/publicrep/ezc/trunk@975 e52654a7-88a9-db11-a3e9-0013d4bc506e --- src/blocks.cpp | 20 ++++++++++ src/blocks.h | 17 ++++++++ src/cache.cpp | 84 ++++++++++++++++++++++++++++++++++++++ src/cache.h | 93 +++++++++++++++++++++++++++++++++++++++++++ src/funinfo.h | 40 ++++++++++--------- src/generator.h | 65 +++++++++++++++++++++++++++++- src/item.cpp | 3 +- src/item.h | 40 +------------------ src/pattern.cpp | 52 +----------------------- src/pattern.h | 24 +++-------- src/patternparser.cpp | 53 +++++++++++++++++++++++- src/patternparser.h | 7 ++++ 12 files changed, 366 insertions(+), 132 deletions(-) create mode 100644 src/cache.cpp create mode 100644 src/cache.h diff --git a/src/blocks.cpp b/src/blocks.cpp index a109470..306c11e 100644 --- a/src/blocks.cpp +++ b/src/blocks.cpp @@ -72,4 +72,24 @@ size_t Blocks::Size() } +void Blocks::ClearCache() +{ + BlocksTable::iterator i = blocks_tab.begin(); + + for( ; i != blocks_tab.end() ; ++i) + i->second.ClearCache(); +} + + + +void Blocks::CacheBlocks(Blocks & blocks) +{ + BlocksTable::iterator i = blocks_tab.begin(); + + for( ; i != blocks_tab.end() ; ++i) + Cache(blocks, i->second); +} + + + } // namespace diff --git a/src/blocks.h b/src/blocks.h index e72dea1..39a88a9 100644 --- a/src/blocks.h +++ b/src/blocks.h @@ -41,6 +41,7 @@ #include #include #include "item.h" +#include "cache.h" namespace Ezc @@ -63,12 +64,28 @@ public: Iterator End(); size_t Size(); + template void CacheFunctions(Functions & fun); + void CacheBlocks(Blocks & blocks); + void ClearCache(); + private: BlocksTable blocks_tab; }; + +template +void Blocks::CacheFunctions(Functions & fun) +{ + BlocksTable::iterator i = blocks_tab.begin(); + + for( ; i != blocks_tab.end() ; ++i) + Cache(fun, i->second); +} + + + } // namespace #endif diff --git a/src/cache.cpp b/src/cache.cpp new file mode 100644 index 0000000..0e95154 --- /dev/null +++ b/src/cache.cpp @@ -0,0 +1,84 @@ +/* + * This file is a part of EZC -- Easy templating in C++ library + * and is distributed under the BSD 3-Clause licence. + * Author: Tomasz Sowa + */ + +/* + * Copyright (c) 2014, 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: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * * 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. + * + * * Neither the name Tomasz Sowa nor the names of contributors to this + * project may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * 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 OWNER 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. + */ + + +#ifndef headerfile_ezc_cache +#define headerfile_ezc_cache + +#include "cache.h" +#include "blocks.h" + + + +namespace Ezc +{ + + +void Cache(Blocks & blocks, Item & item) +{ + // one exception (if_index is putting its argument on the functions stack) + /* + !! IMPROVE ME we need to change the parser and the index take as a string + */ + if( item.type != Item::item_ifindex ) + { + for(size_t f=0; f < item.functions.size() ; ++f) + { + Item::Function & function = item.functions[f]; + function.item_block = 0; + + if( function.arg < 0 ) + { + Blocks::Iterator b = blocks.Find(function.name); + + if( b != blocks.End() ) + function.item_block = &b->second; + } + } + } + + for(size_t i=0; i < item.item_tab.size() ; ++i) + Cache(blocks, *item.item_tab[i]); +} + + + +} // namespace Ezc + + + +#endif diff --git a/src/cache.h b/src/cache.h new file mode 100644 index 0000000..9bea2c2 --- /dev/null +++ b/src/cache.h @@ -0,0 +1,93 @@ +/* + * This file is a part of EZC -- Easy templating in C++ library + * and is distributed under the BSD 3-Clause licence. + * Author: Tomasz Sowa + */ + +/* + * Copyright (c) 2014, 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: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * * 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. + * + * * Neither the name Tomasz Sowa nor the names of contributors to this + * project may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * 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 OWNER 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. + */ + + +#ifndef headerfile_ezc_cache +#define headerfile_ezc_cache + +#include "item.h" +#include "functions.h" + + + +namespace Ezc +{ + +class Blocks; + + + +template +void Cache(Functions & fun, Item & item) +{ + // one exception (if_index is putting its argument on the functions stack) + /* + !! IMPROVE ME we need to change the parser and the index take as a string + */ + if( item.type != Item::item_ifindex ) + { + for(size_t f=0; f < item.functions.size() ; ++f) + { + Item::Function & function = item.functions[f]; + + function.fun_cache = 0; + + if( 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 < item.item_tab.size() ; ++i) + Cache(fun, *item.item_tab[i]); +} + + + +void Cache(Blocks & blocks, Item & item); + + + +} // namespace Ezc + + + +#endif diff --git a/src/funinfo.h b/src/funinfo.h index d13bff0..a2f3206 100755 --- a/src/funinfo.h +++ b/src/funinfo.h @@ -66,24 +66,24 @@ struct FunData /* a generator's stack item each statement ([if ...] [for ...] [normal_funcion]) have its own stack item - iter - is used only in [for...] - it is the current iteration (start from zero) - for other statements it is always zero - fun_data - by default this is null pointer, you can set it to a pointer - to an object derived from FunData - (this have sense only in [for...] statement because in other statements - this object would be immediately removed) - remove - when true it means that object pointing by fun_data should be automatically - removed -- (by using delete fun_data) - is_for - true if the item is from [for] statement - currently used only in [if-index] - (it has to look for the last [for] item) + iter - is used only in [for...] - it is the current iteration (start from zero) + for other statements it is always zero + fun_data - by default this is null pointer, you can set it to a pointer + to an object derived from FunData + (this have sense only in [for...] statement because in other statements + this object would be immediately removed) + auto_remove - when true it means that object pointing by fun_data should be automatically + removed -- (by using delete fun_data) + is_for - true if the item is from [for] statement + currently used only in [if-index] + (it has to look for the last [for] item) */ struct Stack { size_t iter; FunData * fun_data; - bool remove; - bool is_for; + bool auto_remove; + bool is_for; // !! CHECK ME it is needed here? we have something similar in FunInfo... Stack() { @@ -92,7 +92,7 @@ struct Stack ~Stack() { - if( fun_data && remove ) + if( fun_data && auto_remove ) { delete fun_data; fun_data = 0; @@ -101,15 +101,17 @@ struct Stack void Clear() { - iter = 0; - fun_data = 0; - remove = true; - is_for = false; + iter = 0; + fun_data = 0; + auto_remove = true; + is_for = false; } }; - +// !! IMPROVE ME +// the name is bad +// may it should be called Env (environment) template struct FunInfo { diff --git a/src/generator.h b/src/generator.h index bb781d7..cb867ab 100755 --- a/src/generator.h +++ b/src/generator.h @@ -112,6 +112,10 @@ public: void Generate(std::vector & o); void Generate(std::vector & o); + void SetCommentary(const char * com_start, const char * com_stop); + void SetCommentary(const std::string & com_start, const std::string & com_stop); + void SetCommentary(const wchar_t * com_start, const wchar_t * com_stop); + void SetCommentary(const std::wstring & com_start, const std::wstring & com_stop); private: @@ -215,6 +219,8 @@ private: // a stack for [for] statements std::vector stack_tab; + std::wstring commentary_start, commentary_stop; + void ResizeFilterTab(); void ResizeStack(); @@ -280,6 +286,7 @@ private: void WriteTmpStreamToStreams(); + void CreateMsg(std::wstring & out, const wchar_t * type, const wchar_t * arg = 0); void CreateMsg(const wchar_t * type, const wchar_t * arg = 0); void CreateMsg(const std::wstring & type, const std::wstring & arg); void CreateMsg(const std::wstring & type); @@ -398,6 +405,42 @@ Generator::~Generator() } +template +void Generator::SetCommentary(const char * com_start, const char * com_stop) +{ + PT::UTF8ToWide(com_start, commentary_start); + PT::UTF8ToWide(com_stop, commentary_stop); +} + + + +template +void Generator::SetCommentary(const std::string & com_start, const std::string & com_stop) +{ + PT::UTF8ToWide(com_start, commentary_start); + PT::UTF8ToWide(com_stop, commentary_stop); +} + + + +template +void Generator::SetCommentary(const wchar_t * com_start, const wchar_t * com_stop) +{ + commentary_start = com_start; + commentary_stop = com_stop; +} + + + +template +void Generator::SetCommentary(const std::wstring & com_start, const std::wstring & com_stop) +{ + commentary_start = com_start; + commentary_stop = com_stop; +} + + + template void Generator::SetPattern(Pattern & pattern) { @@ -520,7 +563,7 @@ void Generator::ClearStream(StreamType & str) template void Generator::RemoveStackFunData(Stack & s) { - if( s.fun_data && s.remove ) + if( s.fun_data && s.auto_remove ) { delete s.fun_data; s.fun_data = 0; @@ -963,6 +1006,7 @@ std::wstring * variable; { // in c++11 we can use std::move here parameters[i] = item_fun.parameters[i]->par; + item_fun.parameters[i]->par.clear(); } } @@ -1200,13 +1244,30 @@ void Generator::WriteTmpStreamToStreams() +template +void Generator::CreateMsg(std::wstring & out, const wchar_t * type, const wchar_t * arg) +{ + out = commentary_start; + out += L"Ezc runtime error: "; + out += type; + + if( arg ) + { + out += ' '; + out += arg; + } + + out += commentary_stop; +} + + template void Generator::CreateMsg(const wchar_t * type, const wchar_t * arg) { if( output_stream ) { - ppattern->CreateMsg(temp_msg, type, arg); + CreateMsg(temp_msg, type, arg); output_stream->write(temp_msg.c_str(), temp_msg.size()); } } diff --git a/src/item.cpp b/src/item.cpp index 4c4ffc6..710b22e 100755 --- a/src/item.cpp +++ b/src/item.cpp @@ -86,10 +86,11 @@ void Item::ClearItems() void Item::Clear() { ClearItems(); - type = item_none; + text.clear(); file_name.clear(); functions.clear(); + type = item_none; } diff --git a/src/item.h b/src/item.h index ebca37f..01edda4 100755 --- a/src/item.h +++ b/src/item.h @@ -144,49 +144,11 @@ struct Item Type LastItemType(); void DeleteLastItem(); void Clear(); - - template - void CacheFunctions(Functions & fun); - - void ClearCache(); + void ClearCache(); }; -template -void Item::CacheFunctions(Functions & fun) -{ - // one exception (if_index is putting its argument on the functions stack) - /* - * IMPROVE ME - * and now we have [0] [1] too... - */ - if( type != Item::item_ifindex ) - { - for(size_t f=0; f < functions.size() ; ++f) - { - typename Functions::Iterator i = fun.Find(functions[f].name); - - if( i != fun.End() ) - { - functions[f].fun_cache = &i->second; - } - else - { - functions[f].fun_cache = 0; -// !! CHECK ME -// now probably we don't have to log these -// because we've got variables and an identifier can be found at runtime -// #ifdef EZC_USE_WINIX_LOGGER -// Winix::log << Winix::log1 << "Ezc: unknown function: " << item.functions[f].name << Winix::logend; -// #endif - } - } - } - - for(size_t i=0; i < item_tab.size() ; ++i) - item_tab[i]->CacheFunctions(fun); -} } // namespace Ezc diff --git a/src/pattern.cpp b/src/pattern.cpp index 5d320d0..7ec8313 100755 --- a/src/pattern.cpp +++ b/src/pattern.cpp @@ -45,9 +45,6 @@ namespace Ezc Pattern::Pattern() { - // !!!!! IMPROVE ME as default there can be empty strings - commentary_start = L""; Clear(); } @@ -61,57 +58,12 @@ void Pattern::Clear() - - -void Pattern::SetCommentary(const char * com_start, const char * com_stop) +void Pattern::CacheBlocks(Blocks & blocks) { - PT::UTF8ToWide(com_start, commentary_start); - PT::UTF8ToWide(com_stop, commentary_stop); + Cache(blocks, item_root); } - -void Pattern::SetCommentary(const std::string & com_start, const std::string & com_stop) -{ - PT::UTF8ToWide(com_start, commentary_start); - PT::UTF8ToWide(com_stop, commentary_stop); -} - - - -void Pattern::SetCommentary(const wchar_t * com_start, const wchar_t * com_stop) -{ - commentary_start = com_start; - commentary_stop = com_stop; -} - - - -void Pattern::SetCommentary(const std::wstring & com_start, const std::wstring & com_stop) -{ - commentary_start = com_start; - commentary_stop = com_stop; -} - - - -void Pattern::CreateMsg(std::wstring & out, const wchar_t * type, const wchar_t * arg) -{ - out = commentary_start; - out += L"Ezc: "; - out += type; - - if( arg ) - { - out += ' '; - out += arg; - } - - out += commentary_stop; -} - - - void Pattern::ClearCache() { item_root.ClearCache(); diff --git a/src/pattern.h b/src/pattern.h index 4dd63b4..8cc8d73 100755 --- a/src/pattern.h +++ b/src/pattern.h @@ -41,6 +41,8 @@ #include #include "item.h" +#include "cache.h" +#include "blocks.h" #include "functions.h" @@ -56,29 +58,13 @@ public: void Clear(); - void SetCommentary(const char * com_start, const char * com_stop); - void SetCommentary(const std::string & com_start, const std::string & com_stop); - void SetCommentary(const wchar_t * com_start, const wchar_t * com_stop); - void SetCommentary(const std::wstring & com_start, const std::wstring & com_stop); - - void CreateMsg(std::wstring & out, const wchar_t * type, const wchar_t * arg = 0); - - template - void CacheFunctions(Functions & fun); - + template void CacheFunctions(Functions & fun); + void CacheBlocks(Blocks & blocks); void ClearCache(); Item item_root; - -private: - - /* - * IMRPOVE ME - * they should be moved into PatternParser and Generator - */ - std::wstring commentary_start, commentary_stop; }; // class Pattern @@ -87,7 +73,7 @@ private: template void Pattern::CacheFunctions(Functions & fun) { - item_root.CacheFunctions(fun); + Cache(fun, item_root); } diff --git a/src/patternparser.cpp b/src/patternparser.cpp index 05e8f6f..0a35a16 100755 --- a/src/patternparser.cpp +++ b/src/patternparser.cpp @@ -121,6 +121,55 @@ void PatternParser::SetBlocks(Blocks & blocks) } +void PatternParser::SetCommentary(const char * com_start, const char * com_stop) +{ + PT::UTF8ToWide(com_start, commentary_start); + PT::UTF8ToWide(com_stop, commentary_stop); +} + + + +void PatternParser::SetCommentary(const std::string & com_start, const std::string & com_stop) +{ + PT::UTF8ToWide(com_start, commentary_start); + PT::UTF8ToWide(com_stop, commentary_stop); +} + + + +void PatternParser::SetCommentary(const wchar_t * com_start, const wchar_t * com_stop) +{ + commentary_start = com_start; + commentary_stop = com_stop; +} + + + +void PatternParser::SetCommentary(const std::wstring & com_start, const std::wstring & com_stop) +{ + commentary_start = com_start; + commentary_stop = com_stop; +} + + + +void PatternParser::CreateMsg(std::wstring & out, const wchar_t * type, const wchar_t * arg) +{ + out = commentary_start; + out += L"Ezc: "; + out += type; + + if( arg ) + { + out += ' '; + out += arg; + } + + out += commentary_stop; +} + + + void PatternParser::ParseFile(const std::string & file_name, Pattern & pattern) { ParseFile(file_name.c_str(), pattern); @@ -284,7 +333,7 @@ void PatternParser::ReadFile(const wchar_t * name, std::wstring & result) { if( !IsFileCorrect(name) ) { - pat->CreateMsg(result, L"incorrect file name: ", name); + CreateMsg(result, L"incorrect file name: ", name); } else { @@ -292,7 +341,7 @@ void PatternParser::ReadFile(const wchar_t * name, std::wstring & result) if( !ReadFileFromDir(directory, name, result) ) if( !ReadFileFromDir(directory2, name, result) ) - pat->CreateMsg(result, L"can't open: ", name); + CreateMsg(result, L"can't open: ", name); } } diff --git a/src/patternparser.h b/src/patternparser.h index 881a2c1..7fa6258 100755 --- a/src/patternparser.h +++ b/src/patternparser.h @@ -85,6 +85,11 @@ public: void SetBlocks(Blocks & blocks); + void SetCommentary(const char * com_start, const char * com_stop); + void SetCommentary(const std::string & com_start, const std::string & com_stop); + void SetCommentary(const wchar_t * com_start, const wchar_t * com_stop); + void SetCommentary(const std::wstring & com_start, const std::wstring & com_stop); + private: // the output object @@ -111,6 +116,7 @@ private: std::wstring directory, directory2; + std::wstring commentary_start, commentary_stop; int include_level, include_level_max; @@ -132,6 +138,7 @@ private: Blocks * pblocks; + void CreateMsg(std::wstring & out, const wchar_t * type, const wchar_t * arg = 0); void ReadFile(const std::wstring & name, std::wstring & result); void ReadFile(const wchar_t * name, std::wstring & result);