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

This commit is contained in:
2024-12-09 16:36:53 +01:00
parent 5d5d5bcdde
commit 079a62c230
4 changed files with 1744 additions and 367 deletions

View File

@@ -39,6 +39,7 @@
#include <vector>
#include <map>
#include "item.h"
#include "val.h"
#include "textstream/stream.h"
@@ -49,27 +50,27 @@ namespace Ezc
/*
a variable
*/
struct Var
{
/*
* if true then means 'str' is a function name and should be called (res is ignored)
*
* if false then means 'str' is a string value and res is a boolean value
*/
bool is_function;
// struct Var
// {
// /*
// * if true then means 'str' is a function name and should be called (res is ignored)
// *
// * if false then means 'str' is a string value and res is a boolean value
// */
// bool is_function;
std::wstring str; // a string value
bool res; // a boolean value
// std::wstring str; // a string value
// bool res; // a boolean value
Var()
{
res = false;
is_function = false;
}
};
// Var()
// {
// res = false;
// is_function = false;
// }
// };
typedef std::map<std::wstring, Var> Vars;
//typedef std::map<std::wstring, Var> Vars;
@@ -141,25 +142,36 @@ struct Stack
};
// !! IMPROVE ME
// the name is bad
// may it should be called Env (environment) or FunEnv
struct Env
{
// a result consists of a string and a boolean value
// output stream
pt::Stream & out;
// return value from a user's function (default false if not set directly by the function)
bool res;
// table of parameters
// the table can be empty
std::vector<Var> & params;
std::vector<Val> & params;
// output
Val & res;
//
// old interface but still available
//
// a result consists of a string and a boolean value
// output stream
pt::Stream & out; // OLD INTERFACE a reference to res.stream
// return value from a user's function (default false if not set directly by the function)
//bool res;
// old interface DEPRECATED
// the first parameter
// you can always use it even if there are not any parameters (params is empty)
// in such a way the reference points to an empty string
const std::wstring & par;
//const std::wstring & par;
//
// an input stream used in [filter] statement
// if there is other statement than [filter] then this is an empty stream
@@ -208,11 +220,11 @@ struct Env
// arguments: output_stream, table_of_parameters, the_first_parameter
Env(pt::Stream & o,
std::vector<Var> & pars,
const std::wstring & first_par,
std::vector<Val> & pars,
Val & res,
const pt::Stream & input_stream,
Stack & s,
const Item & item_) : out(o), params(pars), par(first_par), in(input_stream), stack(s), item(item_)
const Item & item_) : out(o), params(pars), res(res), in(input_stream), stack(s), item(item_)
{
Clear();
}

File diff suppressed because it is too large Load Diff

1012
src/val.cpp Normal file

File diff suppressed because it is too large Load Diff

252
src/val.h Normal file
View File

@@ -0,0 +1,252 @@
/*
* 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) 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.
*
*/
#ifndef headerfile_ezc_val
#define headerfile_ezc_val
#include "spacewrapper.h"
#include "date/date.h"
#include "item.h"
#include "modelcontainerwrapper.h"
#include "textstream/textstream.h"
namespace morm
{
class Model;
}
namespace Ezc
{
struct Env;
/*
*/
class Val
{
public:
typedef void (*UserFunction)(Env &);
enum Type
{
TYPE_VOID,
TYPE_SPACE_LOCAL,
TYPE_STREAM,
TYPE_FUNCTION,
TYPE_DATE,
TYPE_MODEL,
TYPE_MODEL_CONTAINER_WRAPPER,
TYPE_SPACE_WRAPPER,
TYPE_SPACE,
TYPE_OUTPUT_STREAM,
TYPE_ITEM_BLOCK,
};
Val();
Val(pt::Stream * output_stream);
Val(const Val & val);
Val & operator=(const Val & val);
~Val();
// IMPROVEME add move cctor and operator=
void set_output_stream(pt::Stream * output_stream);
void set_output_stream(pt::Stream & output_stream);
Val * add_child(const std::wstring & child_name, const Val & val);
Val * find_child(const std::wstring & child_name);
bool has_object();
bool has_model_object();
void clear();
void clear_childs();
bool to_bool() const;
void set(const char * str);
void set(const wchar_t * str);
void set(const std::string & str);
void set(const std::wstring & str);
void set(bool val);
void set(short val);
void set(int val);
void set(long val);
void set(long long val);
void set(unsigned short val);
void set(unsigned int val);
void set(unsigned long val);
void set(unsigned long long val);
void set(float val);
void set(double val);
void set(long double val);
void set(pt::Stream & str);
void set(UserFunction user_function);
void set(pt::Date & date);
void set(morm::Model & model);
void set(morm::ModelContainerWrapper & model_container_wrapper);
template<typename ModelType>
void set(std::vector<ModelType> & model_container);
template<typename ModelType>
void set(std::list<ModelType> & model_container);
template<typename ModelType>
void set(std::vector<ModelType*> & model_container);
template<typename ModelType>
void set(std::list<ModelType*> & model_container);
void set(morm::SpaceWrapper & space_wrapper);
void set(pt::Space & space, bool create_wrapper = true);
void set(Val & val);
void set(Item & item_block);
// bool is_equal(const char * str) const;
// bool is_equal(const wchar_t * str) const;
// bool is_equal(const std::string & str) const;
// bool is_equal(const std::wstring & str) const;
void serialize_to(pt::Stream & str);
Val & operator<<(const char * str);
Val & operator<<(const wchar_t * str);
Val & operator<<(const std::string & str);
Val & operator<<(const std::wstring & str);
Val & operator<<(char val);
Val & operator<<(unsigned char val);
Val & operator<<(wchar_t val);
Val & operator<<(bool val);
Val & operator<<(short val);
Val & operator<<(int val);
Val & operator<<(long val);
Val & operator<<(long long val);
Val & operator<<(unsigned short val);
Val & operator<<(unsigned int val);
Val & operator<<(unsigned long val);
Val & operator<<(unsigned long long val);
Val & operator<<(float val);
Val & operator<<(double val);
Val & operator<<(long double val);
Val & operator<<(const pt::Stream & str);
Val & operator=(bool res);
Type type;
pt::Space space_local;
UserFunction user_function;
// Wrapper
morm::Model * model;
morm::ModelContainerWrapper * model_container_wrapper;
pt::Date * date;
morm::SpaceWrapper * space_wrapper;
//
pt::Space * space;
pt::Stream * stream;
Item * item_block;
// output stream
pt::Stream * output_stream;
size_t output_stream_original_size;
private:
std::map<std::wstring, Val> childs_map;
void initialize_empty();
void copy(const Val & val);
// bool is_equal_bool(const char * str) const;
// bool is_equal_string(const char * str) const;
// bool is_equal_bool(const wchar_t * str) const;
// bool is_equal_string(const wchar_t * str) const;
void assert_type_output_stream();
/*
* old
*/
/*
* if true then means 'str' is a function name and should be called (res is ignored)
*
* if false then means 'str' is a string value and res is a boolean value
*/
//bool is_function;
//std::wstring str; // a string value
//bool res; // a boolean value
};
typedef std::map<std::wstring, Val> Vals;
} // namespace Ezc
#endif