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
This commit is contained in:
2014-10-19 05:42:25 +00:00
parent ad2fb11a5c
commit 2fca5f3492
12 changed files with 366 additions and 132 deletions

View File

@@ -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 } // namespace

View File

@@ -41,6 +41,7 @@
#include <string> #include <string>
#include <map> #include <map>
#include "item.h" #include "item.h"
#include "cache.h"
namespace Ezc namespace Ezc
@@ -63,12 +64,28 @@ public:
Iterator End(); Iterator End();
size_t Size(); size_t Size();
template<class StreamType> void CacheFunctions(Functions<StreamType> & fun);
void CacheBlocks(Blocks & blocks);
void ClearCache();
private: private:
BlocksTable blocks_tab; BlocksTable blocks_tab;
}; };
template<class StreamType>
void Blocks::CacheFunctions(Functions<StreamType> & fun)
{
BlocksTable::iterator i = blocks_tab.begin();
for( ; i != blocks_tab.end() ; ++i)
Cache(fun, i->second);
}
} // namespace } // namespace
#endif #endif

84
src/cache.cpp Normal file
View File

@@ -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 <t.sowa@ttmath.org>
*/
/*
* 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

93
src/cache.h Normal file
View File

@@ -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 <t.sowa@ttmath.org>
*/
/*
* 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<class StreamType>
void Cache(Functions<StreamType> & 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<StreamType>::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

View File

@@ -66,24 +66,24 @@ struct FunData
/* /*
a generator's stack item a generator's stack item
each statement ([if ...] [for ...] [normal_funcion]) have its own 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) iter - is used only in [for...] - it is the current iteration (start from zero)
for other statements it is always zero for other statements it is always zero
fun_data - by default this is null pointer, you can set it to a pointer fun_data - by default this is null pointer, you can set it to a pointer
to an object derived from FunData to an object derived from FunData
(this have sense only in [for...] statement because in other statements (this have sense only in [for...] statement because in other statements
this object would be immediately removed) this object would be immediately removed)
remove - when true it means that object pointing by fun_data should be automatically auto_remove - when true it means that object pointing by fun_data should be automatically
removed -- (by using delete fun_data) removed -- (by using delete fun_data)
is_for - true if the item is from [for] statement is_for - true if the item is from [for] statement
currently used only in [if-index] currently used only in [if-index]
(it has to look for the last [for] item) (it has to look for the last [for] item)
*/ */
struct Stack struct Stack
{ {
size_t iter; size_t iter;
FunData * fun_data; FunData * fun_data;
bool remove; bool auto_remove;
bool is_for; bool is_for; // !! CHECK ME it is needed here? we have something similar in FunInfo...
Stack() Stack()
{ {
@@ -92,7 +92,7 @@ struct Stack
~Stack() ~Stack()
{ {
if( fun_data && remove ) if( fun_data && auto_remove )
{ {
delete fun_data; delete fun_data;
fun_data = 0; fun_data = 0;
@@ -101,15 +101,17 @@ struct Stack
void Clear() void Clear()
{ {
iter = 0; iter = 0;
fun_data = 0; fun_data = 0;
remove = true; auto_remove = true;
is_for = false; is_for = false;
} }
}; };
// !! IMPROVE ME
// the name is bad
// may it should be called Env (environment)
template<class StreamType> template<class StreamType>
struct FunInfo struct FunInfo
{ {

View File

@@ -112,6 +112,10 @@ public:
void Generate(std::vector<StreamType> & o); void Generate(std::vector<StreamType> & o);
void Generate(std::vector<StreamType*> & o); void Generate(std::vector<StreamType*> & 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: private:
@@ -215,6 +219,8 @@ private:
// a stack for [for] statements // a stack for [for] statements
std::vector<Stack> stack_tab; std::vector<Stack> stack_tab;
std::wstring commentary_start, commentary_stop;
void ResizeFilterTab(); void ResizeFilterTab();
void ResizeStack(); void ResizeStack();
@@ -280,6 +286,7 @@ private:
void WriteTmpStreamToStreams(); 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 wchar_t * type, const wchar_t * arg = 0);
void CreateMsg(const std::wstring & type, const std::wstring & arg); void CreateMsg(const std::wstring & type, const std::wstring & arg);
void CreateMsg(const std::wstring & type); void CreateMsg(const std::wstring & type);
@@ -398,6 +405,42 @@ Generator<StreamType>::~Generator()
} }
template<class StreamType>
void Generator<StreamType>::SetCommentary(const char * com_start, const char * com_stop)
{
PT::UTF8ToWide(com_start, commentary_start);
PT::UTF8ToWide(com_stop, commentary_stop);
}
template<class StreamType>
void Generator<StreamType>::SetCommentary(const std::string & com_start, const std::string & com_stop)
{
PT::UTF8ToWide(com_start, commentary_start);
PT::UTF8ToWide(com_stop, commentary_stop);
}
template<class StreamType>
void Generator<StreamType>::SetCommentary(const wchar_t * com_start, const wchar_t * com_stop)
{
commentary_start = com_start;
commentary_stop = com_stop;
}
template<class StreamType>
void Generator<StreamType>::SetCommentary(const std::wstring & com_start, const std::wstring & com_stop)
{
commentary_start = com_start;
commentary_stop = com_stop;
}
template<class StreamType> template<class StreamType>
void Generator<StreamType>::SetPattern(Pattern & pattern) void Generator<StreamType>::SetPattern(Pattern & pattern)
{ {
@@ -520,7 +563,7 @@ void Generator<StreamType>::ClearStream(StreamType & str)
template<class StreamType> template<class StreamType>
void Generator<StreamType>::RemoveStackFunData(Stack & s) void Generator<StreamType>::RemoveStackFunData(Stack & s)
{ {
if( s.fun_data && s.remove ) if( s.fun_data && s.auto_remove )
{ {
delete s.fun_data; delete s.fun_data;
s.fun_data = 0; s.fun_data = 0;
@@ -963,6 +1006,7 @@ std::wstring * variable;
{ {
// in c++11 we can use std::move here // in c++11 we can use std::move here
parameters[i] = item_fun.parameters[i]->par; parameters[i] = item_fun.parameters[i]->par;
item_fun.parameters[i]->par.clear();
} }
} }
@@ -1200,13 +1244,30 @@ void Generator<StreamType>::WriteTmpStreamToStreams()
template<class StreamType>
void Generator<StreamType>::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<class StreamType> template<class StreamType>
void Generator<StreamType>::CreateMsg(const wchar_t * type, const wchar_t * arg) void Generator<StreamType>::CreateMsg(const wchar_t * type, const wchar_t * arg)
{ {
if( output_stream ) if( output_stream )
{ {
ppattern->CreateMsg(temp_msg, type, arg); CreateMsg(temp_msg, type, arg);
output_stream->write(temp_msg.c_str(), temp_msg.size()); output_stream->write(temp_msg.c_str(), temp_msg.size());
} }
} }

View File

@@ -86,10 +86,11 @@ void Item::ClearItems()
void Item::Clear() void Item::Clear()
{ {
ClearItems(); ClearItems();
type = item_none;
text.clear(); text.clear();
file_name.clear(); file_name.clear();
functions.clear(); functions.clear();
type = item_none;
} }

View File

@@ -144,49 +144,11 @@ struct Item
Type LastItemType(); Type LastItemType();
void DeleteLastItem(); void DeleteLastItem();
void Clear(); void Clear();
void ClearCache();
template<class StreamType>
void CacheFunctions(Functions<StreamType> & fun);
void ClearCache();
}; };
template<class StreamType>
void Item::CacheFunctions(Functions<StreamType> & 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<StreamType>::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 } // namespace Ezc

View File

@@ -45,9 +45,6 @@ namespace Ezc
Pattern::Pattern() Pattern::Pattern()
{ {
// !!!!! IMPROVE ME as default there can be empty strings
commentary_start = L"<!-- ";
commentary_stop = L" -->";
Clear(); Clear();
} }
@@ -61,57 +58,12 @@ void Pattern::Clear()
void Pattern::CacheBlocks(Blocks & blocks)
void Pattern::SetCommentary(const char * com_start, const char * com_stop)
{ {
PT::UTF8ToWide(com_start, commentary_start); Cache(blocks, item_root);
PT::UTF8ToWide(com_stop, commentary_stop);
} }
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() void Pattern::ClearCache()
{ {
item_root.ClearCache(); item_root.ClearCache();

View File

@@ -41,6 +41,8 @@
#include <string> #include <string>
#include "item.h" #include "item.h"
#include "cache.h"
#include "blocks.h"
#include "functions.h" #include "functions.h"
@@ -56,29 +58,13 @@ public:
void Clear(); void Clear();
void SetCommentary(const char * com_start, const char * com_stop); template<class StreamType> void CacheFunctions(Functions<StreamType> & fun);
void SetCommentary(const std::string & com_start, const std::string & com_stop); void CacheBlocks(Blocks & blocks);
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<class StreamType>
void CacheFunctions(Functions<StreamType> & fun);
void ClearCache(); void ClearCache();
Item item_root; Item item_root;
private:
/*
* IMRPOVE ME
* they should be moved into PatternParser and Generator
*/
std::wstring commentary_start, commentary_stop;
}; // class Pattern }; // class Pattern
@@ -87,7 +73,7 @@ private:
template<class StreamType> template<class StreamType>
void Pattern::CacheFunctions(Functions<StreamType> & fun) void Pattern::CacheFunctions(Functions<StreamType> & fun)
{ {
item_root.CacheFunctions(fun); Cache(fun, item_root);
} }

View File

@@ -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) void PatternParser::ParseFile(const std::string & file_name, Pattern & pattern)
{ {
ParseFile(file_name.c_str(), pattern); ParseFile(file_name.c_str(), pattern);
@@ -284,7 +333,7 @@ void PatternParser::ReadFile(const wchar_t * name, std::wstring & result)
{ {
if( !IsFileCorrect(name) ) if( !IsFileCorrect(name) )
{ {
pat->CreateMsg(result, L"incorrect file name: ", name); CreateMsg(result, L"incorrect file name: ", name);
} }
else else
{ {
@@ -292,7 +341,7 @@ void PatternParser::ReadFile(const wchar_t * name, std::wstring & result)
if( !ReadFileFromDir(directory, name, result) ) if( !ReadFileFromDir(directory, name, result) )
if( !ReadFileFromDir(directory2, name, result) ) if( !ReadFileFromDir(directory2, name, result) )
pat->CreateMsg(result, L"can't open: ", name); CreateMsg(result, L"can't open: ", name);
} }
} }

View File

@@ -85,6 +85,11 @@ public:
void SetBlocks(Blocks & blocks); 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: private:
// the output object // the output object
@@ -111,6 +116,7 @@ private:
std::wstring directory, directory2; std::wstring directory, directory2;
std::wstring commentary_start, commentary_stop;
int include_level, include_level_max; int include_level, include_level_max;
@@ -132,6 +138,7 @@ private:
Blocks * pblocks; 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 std::wstring & name, std::wstring & result);
void ReadFile(const wchar_t * name, std::wstring & result); void ReadFile(const wchar_t * name, std::wstring & result);