WIP: add a Val struct as an input/output when calling a function

This commit is contained in:
2025-01-24 19:40:52 +01:00
parent 37f78ad8e0
commit 1f2b5c8d12
6 changed files with 357 additions and 71 deletions

View File

@@ -1,5 +1,13 @@
./blocks.o: ../../pikotools/src/convert/inttostr.h
./blocks.o: ../../pikotools/src/convert/misc.h
./blocks.o: ../../pikotools/src/date/date.h
./blocks.o: ../../pikotools/src/membuffer/membuffer.h
./blocks.o: ../../pikotools/src/space/space.h
./blocks.o: ../../pikotools/src/textstream/stream_private.h
./blocks.o: ../../pikotools/src/textstream/stream.h ./blocks.o: ../../pikotools/src/textstream/stream.h
./blocks.o: ../../pikotools/src/textstream/textstream.h
./blocks.o: ../../pikotools/src/textstream/types.h
./blocks.o: ../../pikotools/src/utf8/utf8.h ./blocks.o: ../../pikotools/src/utf8/utf8.h
./blocks.o: blocks.h ./blocks.o: blocks.h
./blocks.o: cache.h ./blocks.o: cache.h
@@ -7,7 +15,16 @@
./blocks.o: functions.h ./blocks.o: functions.h
./blocks.o: item.h ./blocks.o: item.h
./blocks.o: objects.h ./blocks.o: objects.h
./blocks.o: val.h
./cache.o: ../../pikotools/src/convert/inttostr.h
./cache.o: ../../pikotools/src/convert/misc.h
./cache.o: ../../pikotools/src/date/date.h
./cache.o: ../../pikotools/src/membuffer/membuffer.h
./cache.o: ../../pikotools/src/space/space.h
./cache.o: ../../pikotools/src/textstream/stream_private.h
./cache.o: ../../pikotools/src/textstream/stream.h ./cache.o: ../../pikotools/src/textstream/stream.h
./cache.o: ../../pikotools/src/textstream/textstream.h
./cache.o: ../../pikotools/src/textstream/types.h
./cache.o: ../../pikotools/src/utf8/utf8.h ./cache.o: ../../pikotools/src/utf8/utf8.h
./cache.o: blocks.h ./cache.o: blocks.h
./cache.o: cache.h ./cache.o: cache.h
@@ -15,17 +32,46 @@
./cache.o: functions.h ./cache.o: functions.h
./cache.o: item.h ./cache.o: item.h
./cache.o: objects.h ./cache.o: objects.h
./cache.o: val.h
./functions.o: ../../pikotools/src/convert/inttostr.h
./functions.o: ../../pikotools/src/convert/misc.h
./functions.o: ../../pikotools/src/date/date.h
./functions.o: ../../pikotools/src/membuffer/membuffer.h
./functions.o: ../../pikotools/src/space/space.h
./functions.o: ../../pikotools/src/textstream/stream_private.h
./functions.o: ../../pikotools/src/textstream/stream.h ./functions.o: ../../pikotools/src/textstream/stream.h
./functions.o: ../../pikotools/src/textstream/textstream.h
./functions.o: ../../pikotools/src/textstream/types.h
./functions.o: ../../pikotools/src/utf8/utf8.h ./functions.o: ../../pikotools/src/utf8/utf8.h
./functions.o: env.h ./functions.o: env.h
./functions.o: functions.h ./functions.o: functions.h
./functions.o: item.h ./functions.o: item.h
./functions.o: val.h
./item.o: item.h ./item.o: item.h
./models.o: models.h ./models.o: models.h
./objects.o: ../../pikotools/src/convert/inttostr.h
./objects.o: ../../pikotools/src/convert/misc.h
./objects.o: ../../pikotools/src/date/date.h
./objects.o: ../../pikotools/src/membuffer/membuffer.h
./objects.o: ../../pikotools/src/space/space.h
./objects.o: ../../pikotools/src/textstream/stream_private.h
./objects.o: ../../pikotools/src/textstream/stream.h ./objects.o: ../../pikotools/src/textstream/stream.h
./objects.o: ../../pikotools/src/textstream/textstream.h
./objects.o: ../../pikotools/src/textstream/types.h
./objects.o: ../../pikotools/src/utf8/utf8.h ./objects.o: ../../pikotools/src/utf8/utf8.h
./objects.o: env.h
./objects.o: item.h
./objects.o: objects.h ./objects.o: objects.h
./objects.o: val.h
./pattern.o: ../../pikotools/src/convert/inttostr.h
./pattern.o: ../../pikotools/src/convert/misc.h
./pattern.o: ../../pikotools/src/date/date.h
./pattern.o: ../../pikotools/src/membuffer/membuffer.h
./pattern.o: ../../pikotools/src/space/space.h
./pattern.o: ../../pikotools/src/textstream/stream_private.h
./pattern.o: ../../pikotools/src/textstream/stream.h ./pattern.o: ../../pikotools/src/textstream/stream.h
./pattern.o: ../../pikotools/src/textstream/textstream.h
./pattern.o: ../../pikotools/src/textstream/types.h
./pattern.o: ../../pikotools/src/utf8/utf8.h ./pattern.o: ../../pikotools/src/utf8/utf8.h
./pattern.o: blocks.h ./pattern.o: blocks.h
./pattern.o: cache.h ./pattern.o: cache.h
@@ -34,6 +80,7 @@
./pattern.o: item.h ./pattern.o: item.h
./pattern.o: objects.h ./pattern.o: objects.h
./pattern.o: pattern.h ./pattern.o: pattern.h
./pattern.o: val.h
./patternparser.o: ../../pikotools/src/convert/convert.h ./patternparser.o: ../../pikotools/src/convert/convert.h
./patternparser.o: ../../pikotools/src/convert/double.h ./patternparser.o: ../../pikotools/src/convert/double.h
./patternparser.o: ../../pikotools/src/convert/inttostr.h ./patternparser.o: ../../pikotools/src/convert/inttostr.h
@@ -59,4 +106,17 @@
./patternparser.o: objects.h ./patternparser.o: objects.h
./patternparser.o: pattern.h ./patternparser.o: pattern.h
./patternparser.o: patternparser.h ./patternparser.o: patternparser.h
./patternparser.o: val.h
./val.o: ../../pikotools/src/convert/inttostr.h
./val.o: ../../pikotools/src/convert/misc.h
./val.o: ../../pikotools/src/date/date.h
./val.o: ../../pikotools/src/membuffer/membuffer.h
./val.o: ../../pikotools/src/space/space.h
./val.o: ../../pikotools/src/textstream/stream_private.h
./val.o: ../../pikotools/src/textstream/stream.h
./val.o: ../../pikotools/src/textstream/textstream.h
./val.o: ../../pikotools/src/textstream/types.h
./val.o: ../../pikotools/src/utf8/utf8.h
./val.o: item.h
./val.o: val.h
# DO NOT DELETE # DO NOT DELETE

View File

@@ -149,6 +149,7 @@ private:
item_fun(item_fun), fun_name(fun_name), fields(fields), item_fun(item_fun), fun_name(fun_name), fields(fields),
parameters(parameters), result(result), out_stream(out_stream), in_stream(in_stream) parameters(parameters), result(result), out_stream(out_stream), in_stream(in_stream)
{ {
valcache = nullptr;
currentval = nullptr; currentval = nullptr;
#ifdef EZC_HAS_MORM_LIBRARY #ifdef EZC_HAS_MORM_LIBRARY
@@ -158,11 +159,16 @@ private:
FindHelper(const FindHelper &) = delete; FindHelper(const FindHelper &) = delete;
void set_valcache(ValCache * valcache)
{
this->valcache = valcache;
}
// RENAMEME // RENAMEME
void set_current_val(Val * val) void set_current_val(Val * val)
{ {
//baseval = val; //baseval = val;
currentval = val; this->currentval = val;
} }
void set_current_val(Val & val) void set_current_val(Val & val)
@@ -174,6 +180,7 @@ private:
const std::wstring & fun_name; const std::wstring & fun_name;
std::vector<std::wstring> & fields; std::vector<std::wstring> & fields;
//Val * baseval; //Val * baseval;
ValCache * valcache;
Val * currentval; Val * currentval;
std::vector<Val> & parameters; std::vector<Val> & parameters;
Val & result; Val & result;
@@ -1158,11 +1165,21 @@ bool Generator::FindInModels(FindHelper & find_helper)
if( pmodels ) if( pmodels )
{ {
Val * val = pmodels->Find(find_helper.fun_name); ValCache * valcache = pmodels->Find(find_helper.fun_name);
if( val ) if( valcache )
{ {
find_helper.set_current_val(val); find_helper.set_valcache(valcache);
if( valcache->has_evaluated_val )
{
find_helper.set_current_val(valcache->evaluated_val);
}
else
{
find_helper.set_current_val(valcache->val);
}
found = true; found = true;
} }
} }
@@ -2020,21 +2037,35 @@ bool Generator::EvaluateFunctionOrMethod(pt::Stream & out_stream, Val & val, Val
bool Generator::XXXRenameMe(FindHelper & find_helper, bool is_for_alias) bool Generator::XXXRenameMe(FindHelper & find_helper, bool is_for_alias)
{ {
if( find_helper.currentval->has_function() || find_helper.currentval->has_method() ) if( !find_helper.valcache->has_evaluated_val && (!is_for_alias || find_helper.fields.size() > 0) )
{ {
//Val res(&find_helper.out_stream); if( find_helper.valcache->val.has_function() || find_helper.valcache->val.has_method() )
{
pt::Stream * str = find_helper.out_stream.new_empty();
if( EvaluateFunctionOrMethod(find_helper.out_stream, *find_helper.currentval, find_helper.result) ) if( EvaluateFunctionOrMethod(*str, find_helper.valcache->val, find_helper.valcache->evaluated_val) )
{ {
find_helper.currentval = &find_helper.result; find_helper.valcache->has_evaluated_val = true;
//child_val = find_helper.currentval->add_child(field, find_helper.result); find_helper.valcache->evaluated_str = str;
} find_helper.currentval = &find_helper.valcache->evaluated_val;
else
{ if( !str->empty() )
// put some error? {
return false; find_helper.out_stream << *str;
}
}
else
{
find_helper.valcache->evaluated_val.clear();
delete str;
// put some error log?
return false;
}
} }
} }
return true;
} }
@@ -2426,6 +2457,9 @@ bool Generator::Call(Item::Function & item_fun,
out_stream.escape_input(true); out_stream.escape_input(true);
if( !XXXRenameMe(find_helper, false) )
return false;
if( !FindLastModelWrapper(find_helper, false) ) if( !FindLastModelWrapper(find_helper, false) )
return false; return false;

View File

@@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2021-2024, Tomasz Sowa * Copyright (c) 2021-2025, Tomasz Sowa
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@@ -38,7 +38,6 @@
#ifdef EZC_HAS_MORM_LIBRARY #ifdef EZC_HAS_MORM_LIBRARY
namespace Ezc namespace Ezc
{ {
@@ -60,72 +59,71 @@ void Models::Clear()
} }
void Models::Add(const std::wstring & name, morm::Model & model) void Models::Add(const std::wstring & name, morm::Model & model)
{ {
Val val; ValCache valcache;
val.set_pointer_to(&model); valcache.val.set_pointer_to(&model);
models_map[name] = val; models_map[name] = valcache;
} }
void Models::Add(const std::wstring & name, morm::Model * model) void Models::Add(const std::wstring & name, morm::Model * model)
{ {
Val val; ValCache valcache;
val.set_pointer_to(model); valcache.val.set_pointer_to(model);
models_map[name] = val; models_map[name] = valcache;
} }
void Models::Add(const std::wstring & name, pt::Space & space) void Models::Add(const std::wstring & name, pt::Space & space)
{ {
Val val; ValCache valcache;
val.set_pointer_to(new morm::SpaceWrapper(&space)); valcache.val.set_pointer_to(new morm::SpaceWrapper(&space));
models_map[name] = val; models_map[name] = valcache;
} }
void Models::Add(const std::wstring & name, pt::Space * space) void Models::Add(const std::wstring & name, pt::Space * space)
{ {
Val val; ValCache valcache;
val.set_pointer_to(new morm::SpaceWrapper(space)); valcache.val.set_pointer_to(new morm::SpaceWrapper(space));
models_map[name] = val; models_map[name] = valcache;
} }
void Models::Add(const std::wstring & name, pt::Date & date) void Models::Add(const std::wstring & name, pt::Date & date)
{ {
Val val; ValCache valcache;
val.set_pointer_to(&date); valcache.val.set_pointer_to(&date);
models_map[name] = val; models_map[name] = valcache;
} }
void Models::Add(const std::wstring & name, pt::Date * date) void Models::Add(const std::wstring & name, pt::Date * date)
{ {
Val val; ValCache valcache;
val.set_pointer_to(date); valcache.val.set_pointer_to(date);
models_map[name] = val; models_map[name] = valcache;
} }
void Models::Add(const std::wstring & name, const std::wstring & value) void Models::Add(const std::wstring & name, const std::wstring & value)
{ {
Val val; ValCache valcache;
val.set(value); valcache.val.set(value);
models_map[name] = val; models_map[name] = valcache;
} }
void Models::Add(const std::wstring & name, Val::UserFunction ufunction) void Models::Add(const std::wstring & name, Val::UserFunction ufunction)
{ {
Val val; ValCache valcache;
val.set_pointer_to(ufunction); valcache.val.set_pointer_to(ufunction);
models_map[name] = val; models_map[name] = valcache;
} }
Val * Models::Find(const std::wstring & name) ValCache * Models::Find(const std::wstring & name)
{ {
auto iterator = models_map.find(name); auto iterator = models_map.find(name);
@@ -138,6 +136,22 @@ Val * Models::Find(const std::wstring & name)
} }
Val * Models::FindVal(const std::wstring & name)
{
auto iterator = models_map.find(name);
if( iterator != models_map.end() )
{
if( iterator->second.has_evaluated_val )
return &iterator->second.evaluated_val;
else
return &iterator->second.val;
}
return nullptr;
}
Models::ModelsMap & Models::GetMap() Models::ModelsMap & Models::GetMap()
{ {
return models_map; return models_map;

View File

@@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2021-2024, Tomasz Sowa * Copyright (c) 2021-2025, Tomasz Sowa
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@@ -37,7 +37,7 @@
#ifdef EZC_HAS_MORM_LIBRARY #ifdef EZC_HAS_MORM_LIBRARY
#include "val.h" #include "valcache.h"
#include "space/space.h" #include "space/space.h"
@@ -45,11 +45,16 @@ namespace Ezc
{ {
/*
* RENAMEME to a better name
* e.g.
* Vals, Vars, Variables, Values...
*/
class Models class Models
{ {
public: public:
typedef std::map<std::wstring, Val> ModelsMap; typedef std::map<std::wstring, ValCache> ModelsMap;
Models(); Models();
~Models(); ~Models();
@@ -70,68 +75,71 @@ public:
template<typename VectorType> template<typename VectorType>
void Add(const std::wstring & name, std::vector<VectorType> & container) void Add(const std::wstring & name, std::vector<VectorType> & container)
{ {
Val val; ValCache valcache;
val.set_pointer_to(new morm::ModelWrapperVector<VectorType>(&container)); valcache.val.set_pointer_to(new morm::ModelWrapperVector<VectorType>(&container));
models_map[name] = val; models_map[name] = valcache;
} }
template<typename VectorType> template<typename VectorType>
void Add(const std::wstring & name, std::vector<VectorType> * container) void Add(const std::wstring & name, std::vector<VectorType> * container)
{ {
Val val; ValCache valcache;
val.set_pointer_to(new morm::ModelWrapperVector<VectorType>(container)); valcache.val.set_pointer_to(new morm::ModelWrapperVector<VectorType>(container));
models_map[name] = val; models_map[name] = valcache;
} }
template<typename VectorType> template<typename VectorType>
void Add(const std::wstring & name, std::vector<VectorType*> & container) void Add(const std::wstring & name, std::vector<VectorType*> & container)
{ {
Val val; ValCache valcache;
val.set_pointer_to(new morm::ModelWrapperVectorPointer<VectorType>(&container)); valcache.val.set_pointer_to(new morm::ModelWrapperVectorPointer<VectorType>(&container));
models_map[name] = val; models_map[name] = valcache;
} }
template<typename VectorType> template<typename VectorType>
void Add(const std::wstring & name, std::vector<VectorType*> * container) void Add(const std::wstring & name, std::vector<VectorType*> * container)
{ {
Val val; ValCache valcache;
val.set_pointer_to(new morm::ModelWrapperVectorPointer<VectorType>(container)); valcache.val.set_pointer_to(new morm::ModelWrapperVectorPointer<VectorType>(container));
models_map[name] = val; models_map[name] = valcache;
} }
template<typename ListType> template<typename ListType>
void Add(const std::wstring & name, std::list<ListType> & container) void Add(const std::wstring & name, std::list<ListType> & container)
{ {
Val val; ValCache valcache;
val.set_pointer_to(new morm::ModelWrapperList<ListType>(&container)); valcache.val.set_pointer_to(new morm::ModelWrapperList<ListType>(&container));
models_map[name] = val; models_map[name] = valcache;
} }
template<typename ListType> template<typename ListType>
void Add(const std::wstring & name, std::list<ListType> * container) void Add(const std::wstring & name, std::list<ListType> * container)
{ {
Val val; ValCache valcache;
val.set_pointer_to(new morm::ModelWrapperList<ListType>(container)); valcache.val.set_pointer_to(new morm::ModelWrapperList<ListType>(container));
models_map[name] = val; models_map[name] = valcache;
} }
template<typename ListType> template<typename ListType>
void Add(const std::wstring & name, std::list<ListType*> & container) void Add(const std::wstring & name, std::list<ListType*> & container)
{ {
Val val; ValCache valcache;
val.set_pointer_to(new morm::ModelWrapperListPointer<ListType>(&container)); valcache.val.set_pointer_to(new morm::ModelWrapperListPointer<ListType>(&container));
models_map[name] = val; models_map[name] = valcache;
} }
template<typename ListType> template<typename ListType>
void Add(const std::wstring & name, std::list<ListType*> * container) void Add(const std::wstring & name, std::list<ListType*> * container)
{ {
Val val; ValCache valcache;
val.set_pointer_to(new morm::ModelWrapperListPointer<ListType>(container)); valcache.val.set_pointer_to(new morm::ModelWrapperListPointer<ListType>(container));
models_map[name] = val; models_map[name] = valcache;
} }
Val * Find(const std::wstring & name); ValCache * Find(const std::wstring & name);
Val * FindVal(const std::wstring & name);
ModelsMap & GetMap(); ModelsMap & GetMap();
void Clear(); void Clear();

103
src/valcache.cpp Normal file
View File

@@ -0,0 +1,103 @@
/*
* This file is a part of EZC -- Easy templating in C++ library
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2025, 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 "valcache.h"
namespace Ezc
{
ValCache::ValCache()
{
has_evaluated_val = false;
evaluated_str = nullptr;
}
ValCache::ValCache(const ValCache & valcache)
{
operator=(valcache);
}
ValCache & ValCache::operator=(const ValCache & valcache)
{
val = valcache.val;
has_evaluated_val = valcache.has_evaluated_val;
evaluated_val = valcache.evaluated_val;
if( valcache.evaluated_str )
{
evaluated_str = valcache.evaluated_str->new_empty();
if( !valcache.evaluated_str->empty() )
{
evaluated_str->escape_input(false);
*evaluated_str << *valcache.evaluated_str;
}
}
else
{
evaluated_str = nullptr;
}
return *this;
}
void ValCache::clear()
{
val.clear();
clear_cache();
}
void ValCache::clear_cache()
{
has_evaluated_val = false;
if( evaluated_str )
{
delete evaluated_str;
}
evaluated_str = nullptr;
evaluated_val.clear();
}
} // namespace Ezc

67
src/valcache.h Normal file
View File

@@ -0,0 +1,67 @@
/*
* This file is a part of EZC -- Easy templating in C++ library
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2025, 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.
*
*/
#ifndef headerfile_ezc_valcache
#define headerfile_ezc_valcache
#include "val.h"
namespace Ezc
{
class ValCache
{
public:
Val val;
bool has_evaluated_val;
Val evaluated_val;
pt::Stream * evaluated_str;
ValCache();
ValCache(const ValCache &);
ValCache & operator=(const ValCache &);
void clear();
void clear_cache();
};
} // namespace Ezc
#endif