- Var is a template now: Var<StreamType>

- removed using cache for a while
- Generator: instead of blocks, functions, objects we have now only variables
  (work in progress)
This commit is contained in:
Tomasz Sowa 2021-10-20 08:23:45 +02:00
parent dfb8e5da78
commit 03a76d35ad
11 changed files with 1047 additions and 679 deletions

View File

@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2014, Tomasz Sowa * Copyright (c) 2014-2021, 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
@ -90,10 +90,12 @@ void Blocks::ClearCache()
void Blocks::CacheBlocks(Blocks & blocks) void Blocks::CacheBlocks(Blocks & blocks)
{ {
/*
BlocksTable::iterator i = blocks_tab.begin(); BlocksTable::iterator i = blocks_tab.begin();
for( ; i != blocks_tab.end() ; ++i) for( ; i != blocks_tab.end() ; ++i)
Cache(blocks, i->second); Cache(blocks, i->second);
*/
} }

View File

@ -43,7 +43,7 @@
namespace Ezc namespace Ezc
{ {
/*
void Cache(Blocks & blocks, Item::Function & function) void Cache(Blocks & blocks, Item::Function & function)
{ {
function.item_block = 0; function.item_block = 0;
@ -69,7 +69,7 @@ void Cache(Blocks & blocks, Item & item)
for(size_t i=0; i < item.item_tab.size() ; ++i) for(size_t i=0; i < item.item_tab.size() ; ++i)
Cache(blocks, *item.item_tab[i]); Cache(blocks, *item.item_tab[i]);
} }
*/

View File

@ -51,7 +51,7 @@ namespace Ezc
class Blocks; class Blocks;
/*
template<class StreamType> template<class StreamType>
void Cache(Functions<StreamType> & fun, Item::Function & function) void Cache(Functions<StreamType> & fun, Item::Function & function)
{ {
@ -114,7 +114,7 @@ void Cache(Objects<StreamType> & objects, Item & item)
for(size_t i=0; i < item.item_tab.size() ; ++i) for(size_t i=0; i < item.item_tab.size() ; ++i)
Cache(objects, *item.item_tab[i]); Cache(objects, *item.item_tab[i]);
} }
*/
} // namespace Ezc } // namespace Ezc

View File

@ -52,7 +52,7 @@ namespace Ezc
typedef std::map<std::wstring, Var> Vars; //typedef std::map<std::wstring, Var<StreamType>> Vars;
@ -130,16 +130,15 @@ struct Stack
template<class StreamType> template<class StreamType>
struct Env struct Env
{ {
// a result consists of a string and a boolean value // an alias to res.stream
// output stream
StreamType & out; StreamType & out;
// result // result
Var & res; Var<StreamType> & res;
// table of parameters // table of parameters
// the table can be empty // the table can be empty
std::vector<Var> & params; std::vector<Var<StreamType>> & params;
// the first parameter // the first parameter
// you can always use it even if there are not any parameters (params is empty) // you can always use it even if there are not any parameters (params is empty)
@ -191,8 +190,8 @@ struct Env
Env(Var & result, Env(Var<StreamType> & result,
std::vector<Var> & pars, std::vector<Var<StreamType>> & pars,
const StreamType & input_stream, const StreamType & input_stream,
Stack & s, Stack & s,
const Item & item_) : out(result.stream), res(result), params(pars), in(input_stream), stack(s), item(item_) const Item & item_) : out(result.stream), res(result), params(pars), in(input_stream), stack(s), item(item_)

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2007-2018, Tomasz Sowa * Copyright (c) 2007-2021, 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
@ -168,10 +168,10 @@ Item::~Item()
void Item::ClearCache(Item::Function & function) void Item::ClearCache(Item::Function & function)
{ {
function.base_obj = 0; // function.base_obj = 0;
function.method_index = -1; // function.method_index = -1;
function.fun_cache = 0; // function.fun_cache = 0;
function.item_block = 0; // function.item_block = 0;
for(size_t i=0 ; i<function.parameters.size() ; ++i) for(size_t i=0 ; i<function.parameters.size() ; ++i)
ClearCache(*function.parameters[i]); ClearCache(*function.parameters[i]);

View File

@ -69,20 +69,16 @@ struct Item
// e.g. [my_function:my_postfix] // e.g. [my_function:my_postfix]
std::vector<Function*> parameters; // if is_function is true then it is a function and can have 'parameters' std::vector<Function*> parameters; // if is_function is true then it is a function and can have 'parameters'
// if is_function is empty then 'parameters' is empty too // if is_function is empty then 'parameters' is empty too
void * fun_cache; // only valid if is_function is true //void * fun_cache; // only valid if is_function is true
Item * item_block; //Item * item_block;
void * base_obj;
int method_index;
int arg; // used if name is numeric (if no then is equal -1) int arg; // used if name is numeric (if no then is equal -1)
Function() Function()
{ {
is_function = false; is_function = false;
fun_cache = 0; //fun_cache = 0;
item_block = 0; //item_block = 0;
base_obj = 0;
method_index = -1;
arg = -1; arg = -1;
} }
@ -102,10 +98,8 @@ struct Item
name = f.name; name = f.name;
fields = f.fields; fields = f.fields;
postfix = f.postfix; postfix = f.postfix;
fun_cache = f.fun_cache; //fun_cache = f.fun_cache;
item_block = f.item_block; //item_block = f.item_block;
base_obj = f.base_obj;
method_index = f.method_index;
arg = f.arg; arg = f.arg;
for(size_t i=0 ; i<f.parameters.size() ; ++i) for(size_t i=0 ; i<f.parameters.size() ; ++i)
@ -130,10 +124,8 @@ struct Item
parameters.clear(); parameters.clear();
name.clear(); name.clear();
postfix.clear(); postfix.clear();
fun_cache = 0; //fun_cache = 0;
item_block = 0; //item_block = 0;
base_obj = 0;
method_index = -1;
arg = -1; arg = -1;
is_function = false; is_function = false;
} }

View File

@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2007-2014, Tomasz Sowa * Copyright (c) 2007-2021, 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
@ -60,7 +60,7 @@ void Pattern::Clear()
void Pattern::CacheBlocks(Blocks & blocks) void Pattern::CacheBlocks(Blocks & blocks)
{ {
Cache(blocks, item_root); //Cache(blocks, item_root);
} }

View File

@ -43,255 +43,6 @@ namespace Ezc
{ {
Var::Var()
{
clear();
}
void Var::clear()
{
//res = false;
//is_function = false;
type = TYPE_VOID;
model = nullptr;
model_container_wrapper = nullptr;
date = nullptr;
space_wrapper = nullptr;
space_local.clear();
stream.clear();
}
bool Var::to_bool() const
{
switch(type)
{
case TYPE_VOID:
return false;
case TYPE_BOOL:
return space_local.to_bool();
case TYPE_STRING:
return to_bool_str();
}
return false;
}
bool Var::to_bool_str() const
{
if( space_local.is_str() )
return !space_local.get_str()->empty();
else
if( space_local.is_wstr() )
return !space_local.get_wstr()->empty();
return false;
}
void Var::set(bool val)
{
type = TYPE_BOOL;
space_local.set(val);
}
void Var::set(const char * str)
{
type = TYPE_STRING;
space_local.set(str);
}
void Var::set(const wchar_t * str)
{
type = TYPE_STRING;
space_local.set(str);
}
void Var::set(const std::string & str)
{
type = TYPE_STRING;
space_local.set(str);
}
void Var::set(const std::wstring & str)
{
type = TYPE_STRING;
space_local.set(str);
}
void Var::set_function(const std::wstring & str)
{
type = TYPE_FUNCTION;
space_local.set(str);
}
bool Var::is_equal(const char * str) const
{
switch(type)
{
case TYPE_BOOL:
return is_equal_bool(str);
case TYPE_STRING:
return is_equal_string(str);
}
return false;
}
bool Var::is_equal(const wchar_t * str) const
{
switch(type)
{
case TYPE_BOOL:
return is_equal_bool(str);
case TYPE_STRING:
return is_equal_string(str);
}
return false;
}
bool Var::is_equal(const std::string & str) const
{
return is_equal(str.c_str());
}
bool Var::is_equal(const std::wstring & str) const
{
return is_equal(str.c_str());
}
bool Var::is_equal_bool(const char * str) const
{
if( space_local.to_bool() )
{
return str[0] != 0;
}
else
{
return str[0] == 0;
}
}
bool Var::is_equal_string(const char * str) const
{
if( space_local.is_str() )
{
return space_local.is_equal(str);
}
else
if( space_local.is_wstr() )
{
std::string space_str_utf8;
pt::wide_to_utf8(*space_local.get_wstr(), space_str_utf8);
return space_str_utf8 == str;
}
return false;
}
bool Var::is_equal_bool(const wchar_t * str) const
{
if( space_local.to_bool() )
{
return str[0] != 0;
}
else
{
return str[0] == 0;
}
}
bool Var::is_equal_string(const wchar_t * str) const
{
if( space_local.is_wstr() )
{
return space_local.is_equal(str);
}
else
if( space_local.is_str() )
{
std::string str_utf8;
pt::wide_to_utf8(str, str_utf8);
return space_local.is_equal(str_utf8);
}
return false;
}
void Var::serialize_to(pt::WTextStream & str)
{
switch(type)
{
case TYPE_BOOL:
case TYPE_LONG:
case TYPE_STRING:
space_local.serialize_to_string(str);
break;
case TYPE_STREAM:
str = stream;
break;
}
}
Var & Var::operator<<(const char * str)
{
type == TYPE_STREAM;
stream << str;
return *this;
}
Var & Var::operator<<(const wchar_t * str)
{
type == TYPE_STREAM;
stream << str;
return *this;
}
Var & Var::operator<<(const std::string & str)
{
type == TYPE_STREAM;
stream << str;
return *this;
}
Var & Var::operator<<(const std::wstring & str)
{
type == TYPE_STREAM;
stream << str;
return *this;
}

621
src/var.h
View File

@ -48,22 +48,31 @@
namespace Ezc namespace Ezc
{ {
template<typename StreamType>
struct Env;
/* /*
a variable a variable
*/ */
template<typename StreamType>
class Var class Var
{ {
public: public:
typedef void (*UserFunction)(Env<StreamType> &);
enum Type enum Type
{ {
// string or wstring from space_local // string or wstring from space_local
TYPE_VOID, TYPE_VOID,
// or change maybe to something like TYPE_LOCAL_SPACE?
TYPE_BOOL, TYPE_BOOL,
TYPE_LONG, TYPE_LONG,
TYPE_DOUBLE,
TYPE_STRING, TYPE_STRING,
TYPE_STREAM, TYPE_STREAM,
TYPE_FUNCTION, TYPE_FUNCTION,
@ -84,13 +93,39 @@ public:
bool to_bool() const; bool to_bool() const;
void set(bool val);
void set(const char * str); void set(const char * str);
void set(const wchar_t * str); void set(const wchar_t * str);
void set(const std::string & str); void set(const std::string & str);
void set(const std::wstring & str); void set(const std::wstring & str);
void set_function(const std::wstring & str); // void set(char val);
// void set(unsigned char val);
// void set(wchar_t val);
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);
// this str object is copied
void set(const pt::Stream & str);
void set(UserFunction user_function);
// this model is not copied, is it a correct interface?
void set(morm::Model & model);
bool is_equal(const char * str) const; bool is_equal(const char * str) const;
bool is_equal(const wchar_t * str) const; bool is_equal(const wchar_t * str) const;
@ -105,27 +140,35 @@ public:
Var & operator<<(const wchar_t * str); Var & operator<<(const wchar_t * str);
Var & operator<<(const std::string & str); Var & operator<<(const std::string & str);
Var & operator<<(const std::wstring & str); Var & operator<<(const std::wstring & str);
// add the rest of << operators... Var & operator<<(char val);
Var & operator<<(unsigned char val);
Var & operator<<(wchar_t val);
Var & operator<<(bool val);
Var & operator<<(short val);
Var & operator<<(int val);
Var & operator<<(long val);
Var & operator<<(long long val);
Var & operator<<(unsigned short val);
Var & operator<<(unsigned int val);
Var & operator<<(unsigned long val);
Var & operator<<(unsigned long long val);
Var & operator<<(float val);
Var & operator<<(double val);
Var & operator<<(long double val);
Var & operator<<(const pt::Stream & str);
UserFunction user_function;
morm::Model * model; morm::Model * model;
morm::ModelContainerWrapper * model_container_wrapper; morm::ModelContainerWrapper * model_container_wrapper;
pt::Date * date; pt::Date * date;
morm::SpaceWrapper * space_wrapper; morm::SpaceWrapper * space_wrapper;
//pt::Space * space;
pt::Space space_local; pt::Space space_local;
pt::WTextStream stream; StreamType stream;
private: private:
bool to_bool_str() const;
bool is_equal_bool(const char * str) const; bool is_equal_bool(const char * str) const;
bool is_equal_string(const char * str) const; bool is_equal_string(const char * str) const;
@ -134,6 +177,7 @@ private:
/* /*
* old * old
*/ */
@ -153,6 +197,559 @@ private:
template<typename StreamType>
Var<StreamType>::Var()
{
clear();
}
template<typename StreamType>
void Var<StreamType>::clear()
{
//res = false;
//is_function = false;
type = TYPE_VOID;
user_function = nullptr;
model = nullptr;
model_container_wrapper = nullptr;
date = nullptr;
space_wrapper = nullptr;
space_local.clear();
stream.clear();
}
template<typename StreamType>
bool Var<StreamType>::to_bool() const
{
switch(type)
{
case TYPE_VOID:
return false;
case TYPE_BOOL:
case TYPE_LONG:
case TYPE_DOUBLE:
case TYPE_STRING:
return space_local.to_bool();
case TYPE_STREAM:
case TYPE_FUNCTION:
case TYPE_MODEL:
case TYPE_MODEL_CONTAINER_WRAPPER:
case TYPE_SPACE_WRAPPER:
break;
}
return false;
}
template<typename StreamType>
void Var<StreamType>::set(const char * str)
{
type = TYPE_STRING;
space_local.set(str);
}
template<typename StreamType>
void Var<StreamType>::set(const wchar_t * str)
{
type = TYPE_STRING;
space_local.set(str);
}
template<typename StreamType>
void Var<StreamType>::set(const std::string & str)
{
type = TYPE_STRING;
space_local.set(str);
}
template<typename StreamType>
void Var<StreamType>::set(const std::wstring & str)
{
type = TYPE_STRING;
space_local.set(str);
}
/*
template<typename StreamType>
void Var<StreamType>::set(char val)
{
type = TYPE_STRING;
space_local.set(str);
}
template<typename StreamType>
void Var<StreamType>::set(unsigned char val)
{
type = TYPE_STRING;
space_local.set(str);
}
template<typename StreamType>
void Var<StreamType>::set(wchar_t val)
{
type = TYPE_STRING;
space_local.set(str);
}
*/
template<typename StreamType>
void Var<StreamType>::set(bool val)
{
type = TYPE_BOOL;
space_local.set(val);
}
template<typename StreamType>
void Var<StreamType>::set(short val)
{
type = TYPE_LONG;
space_local.set(val);
}
template<typename StreamType>
void Var<StreamType>::set(int val)
{
type = TYPE_LONG;
space_local.set(val);
}
template<typename StreamType>
void Var<StreamType>::set(long val)
{
type = TYPE_LONG;
space_local.set(val);
}
template<typename StreamType>
void Var<StreamType>::set(long long val)
{
type = TYPE_LONG;
space_local.set(val);
}
template<typename StreamType>
void Var<StreamType>::set(unsigned short val)
{
type = TYPE_LONG;
space_local.set(val);
}
template<typename StreamType>
void Var<StreamType>::set(unsigned int val)
{
type = TYPE_LONG;
space_local.set(val);
}
template<typename StreamType>
void Var<StreamType>::set(unsigned long val)
{
type = TYPE_LONG;
space_local.set(val);
}
template<typename StreamType>
void Var<StreamType>::set(unsigned long long val)
{
type = TYPE_LONG;
space_local.set(val);
}
template<typename StreamType>
void Var<StreamType>::set(float val)
{
type = TYPE_DOUBLE;
space_local.set(val);
}
template<typename StreamType>
void Var<StreamType>::set(double val)
{
type = TYPE_DOUBLE;
space_local.set(val);
}
template<typename StreamType>
void Var<StreamType>::set(long double val)
{
type = TYPE_DOUBLE;
space_local.set(val);
}
template<typename StreamType>
void Var<StreamType>::set(const pt::Stream & str)
{
type = TYPE_STREAM;
stream.clear();
stream << str;
}
template<typename StreamType>
void Var<StreamType>::set(UserFunction user_function)
{
type = TYPE_FUNCTION;
this->user_function = user_function;
}
template<typename StreamType>
void Var<StreamType>::set(morm::Model & model)
{
type = TYPE_MODEL;
this->model = &model;
}
template<typename StreamType>
bool Var<StreamType>::is_equal(const char * str) const
{
switch(type)
{
case TYPE_BOOL:
return is_equal_bool(str);
case TYPE_STRING:
return is_equal_string(str);
case TYPE_VOID:
case TYPE_LONG:
case TYPE_DOUBLE:
case TYPE_STREAM:
case TYPE_FUNCTION:
case TYPE_MODEL:
case TYPE_MODEL_CONTAINER_WRAPPER:
case TYPE_SPACE_WRAPPER:
break;
}
return false;
}
template<typename StreamType>
bool Var<StreamType>::is_equal(const wchar_t * str) const
{
switch(type)
{
case TYPE_BOOL:
return is_equal_bool(str);
case TYPE_STRING:
return is_equal_string(str);
case TYPE_VOID:
case TYPE_LONG:
case TYPE_DOUBLE:
case TYPE_STREAM:
case TYPE_FUNCTION:
case TYPE_MODEL:
case TYPE_MODEL_CONTAINER_WRAPPER:
case TYPE_SPACE_WRAPPER:
break;
}
return false;
}
template<typename StreamType>
bool Var<StreamType>::is_equal(const std::string & str) const
{
return is_equal(str.c_str());
}
template<typename StreamType>
bool Var<StreamType>::is_equal(const std::wstring & str) const
{
return is_equal(str.c_str());
}
template<typename StreamType>
bool Var<StreamType>::is_equal_bool(const char * str) const
{
if( space_local.to_bool() )
{
return str[0] != 0;
}
else
{
return str[0] == 0;
}
}
template<typename StreamType>
bool Var<StreamType>::is_equal_string(const char * str) const
{
if( space_local.is_str() )
{
return space_local.is_equal(str);
}
else
if( space_local.is_wstr() )
{
std::string space_str_utf8;
pt::wide_to_utf8(*space_local.get_wstr(), space_str_utf8);
return space_str_utf8 == str;
}
return false;
}
template<typename StreamType>
bool Var<StreamType>::is_equal_bool(const wchar_t * str) const
{
if( space_local.to_bool() )
{
return str[0] != 0;
}
else
{
return str[0] == 0;
}
}
template<typename StreamType>
bool Var<StreamType>::is_equal_string(const wchar_t * str) const
{
if( space_local.is_wstr() )
{
return space_local.is_equal(str);
}
else
if( space_local.is_str() )
{
std::string str_utf8;
pt::wide_to_utf8(str, str_utf8);
return space_local.is_equal(str_utf8);
}
return false;
}
template<typename StreamType>
void Var<StreamType>::serialize_to(pt::WTextStream & str)
{
switch(type)
{
case TYPE_BOOL:
case TYPE_LONG:
case TYPE_DOUBLE:
case TYPE_STRING:
space_local.serialize_to_string(str);
break;
case TYPE_STREAM:
str = stream;
break;
case TYPE_VOID:
case TYPE_FUNCTION:
case TYPE_MODEL:
case TYPE_MODEL_CONTAINER_WRAPPER:
case TYPE_SPACE_WRAPPER:
break;
}
}
template<typename StreamType>
Var<StreamType> & Var<StreamType>::operator<<(const char * str)
{
type = TYPE_STREAM;
stream << str;
return *this;
}
template<typename StreamType>
Var<StreamType> & Var<StreamType>::operator<<(const wchar_t * str)
{
type = TYPE_STREAM;
stream << str;
return *this;
}
template<typename StreamType>
Var<StreamType> & Var<StreamType>::operator<<(const std::string & str)
{
type = TYPE_STREAM;
stream << str;
return *this;
}
template<typename StreamType>
Var<StreamType> & Var<StreamType>::operator<<(const std::wstring & str)
{
type = TYPE_STREAM;
stream << str;
return *this;
}
template<typename StreamType>
Var<StreamType> & Var<StreamType>::operator<<(char val)
{
type = TYPE_STREAM;
stream << val;
return *this;
}
template<typename StreamType>
Var<StreamType> & Var<StreamType>::operator<<(unsigned char val)
{
type = TYPE_STREAM;
stream << val;
return *this;
}
template<typename StreamType>
Var<StreamType> & Var<StreamType>::operator<<(wchar_t val)
{
type = TYPE_STREAM;
stream << val;
return *this;
}
template<typename StreamType>
Var<StreamType> & Var<StreamType>::operator<<(bool val)
{
type = TYPE_STREAM;
stream << val;
return *this;
}
template<typename StreamType>
Var<StreamType> & Var<StreamType>::operator<<(short val)
{
type = TYPE_STREAM;
stream << val;
return *this;
}
template<typename StreamType>
Var<StreamType> & Var<StreamType>::operator<<(int val)
{
type = TYPE_STREAM;
stream << val;
return *this;
}
template<typename StreamType>
Var<StreamType> & Var<StreamType>::operator<<(long val)
{
type = TYPE_STREAM;
stream << val;
return *this;
}
template<typename StreamType>
Var<StreamType> & Var<StreamType>::operator<<(long long val)
{
type = TYPE_STREAM;
stream << val;
return *this;
}
template<typename StreamType>
Var<StreamType> & Var<StreamType>::operator<<(unsigned short val)
{
type = TYPE_STREAM;
stream << val;
return *this;
}
template<typename StreamType>
Var<StreamType> & Var<StreamType>::operator<<(unsigned int val)
{
type = TYPE_STREAM;
stream << val;
return *this;
}
template<typename StreamType>
Var<StreamType> & Var<StreamType>::operator<<(unsigned long val)
{
type = TYPE_STREAM;
stream << val;
return *this;
}
template<typename StreamType>
Var<StreamType> & Var<StreamType>::operator<<(unsigned long long val)
{
type = TYPE_STREAM;
stream << val;
return *this;
}
template<typename StreamType>
Var<StreamType> & Var<StreamType>::operator<<(float val)
{
type = TYPE_STREAM;
stream << val;
return *this;
}
template<typename StreamType>
Var<StreamType> & Var<StreamType>::operator<<(double val)
{
type = TYPE_STREAM;
stream << val;
return *this;
}
template<typename StreamType>
Var<StreamType> & Var<StreamType>::operator<<(long double val)
{
type = TYPE_STREAM;
stream << val;
return *this;
}
template<typename StreamType>
Var<StreamType> & Var<StreamType>::operator<<(const pt::Stream & str)
{
type = TYPE_STREAM;
stream << str;
return *this;
}
} }
#endif #endif

View File

@ -40,20 +40,23 @@
#include "var.h" #include "var.h"
#include "utf8/utf8.h" #include "utf8/utf8.h"
#include "env.h"
#include <string> #include <string>
#include <map> #include <map>
#include <list> #include <list>
namespace Ezc namespace Ezc
{ {
// rename to something like Object? or a better name?
template<typename StreamType> template<typename StreamType>
class Vars class Vars
{ {
public: public:
typedef void (*UserFunction)(Env<StreamType> &);
Var<StreamType> & add(const char * name); Var<StreamType> & add(const char * name);
Var<StreamType> & add(const wchar_t * name); Var<StreamType> & add(const wchar_t * name);
@ -65,6 +68,16 @@ public:
void add(const std::string & name, const Var<StreamType> & var); void add(const std::string & name, const Var<StreamType> & var);
void add(const std::wstring & name, const Var<StreamType> & var); void add(const std::wstring & name, const Var<StreamType> & var);
Var<StreamType> & add(const char * name, UserFunction user_function);
Var<StreamType> & add(const wchar_t * name, UserFunction user_function);
Var<StreamType> & add(const std::string & name, UserFunction user_function);
Var<StreamType> & add(const std::wstring & name, UserFunction user_function);
Var<StreamType> & add(const char * name, morm::Model & model);
Var<StreamType> & add(const wchar_t * name, morm::Model & model);
Var<StreamType> & add(const std::string & name, morm::Model & model);
Var<StreamType> & add(const std::wstring & name, morm::Model & model);
Var<StreamType> * find(const char * name) const; Var<StreamType> * find(const char * name) const;
Var<StreamType> * find(const wchar_t * name) const; Var<StreamType> * find(const wchar_t * name) const;
Var<StreamType> * find(const std::string & name) const; Var<StreamType> * find(const std::string & name) const;
@ -77,7 +90,7 @@ private:
VarList var_list; VarList var_list;
typedef std::map<std::wstring, Var<StreamType>*> VarMap; typedef std::map<std::wstring, Var<StreamType>*> VarMap;
VarMap var_map VarMap var_map;
}; };
@ -95,9 +108,8 @@ Var<StreamType> & Vars<StreamType>::add(const char * name)
template<typename StreamType> template<typename StreamType>
Var<StreamType> & Vars<StreamType>::add(const wchar_t * name) Var<StreamType> & Vars<StreamType>::add(const wchar_t * name)
{ {
Var<StreamType> & var = var_map[name]; std::wstring name_str(name);
var.clear(); return add(name_str);
return var;
} }
@ -113,9 +125,16 @@ Var<StreamType> & Vars<StreamType>::add(const std::string & name)
template<typename StreamType> template<typename StreamType>
Var<StreamType> & Vars<StreamType>::add(const std::wstring & name) Var<StreamType> & Vars<StreamType>::add(const std::wstring & name)
{ {
Var<StreamType> & var = var_map[name]; auto i = var_map.insert(std::make_pair(name, nullptr));
var.clear();
return var; if( i.second )
{
Var<StreamType> & var = var_list.emplace_back();
i.first->second = &var;
}
i.first->second->clear();
return *(i.first->second);
} }
@ -131,8 +150,8 @@ void Vars<StreamType>::add(const char * name, const Var<StreamType> & var)
template<typename StreamType> template<typename StreamType>
void Vars<StreamType>::add(const wchar_t * name, const Var<StreamType> & var) void Vars<StreamType>::add(const wchar_t * name, const Var<StreamType> & var)
{ {
var_list.push_back(var); std::wstring name_str(name);
var_map[name] = &var_list.back(); add(name_str, var);
} }
@ -153,6 +172,73 @@ void Vars<StreamType>::add(const std::wstring & name, const Var<StreamType> & va
} }
template<typename StreamType>
Var<StreamType> & Vars<StreamType>::add(const char * name, UserFunction user_function)
{
Var<StreamType> & var = add(name);
var.set(user_function);
return var;
}
template<typename StreamType>
Var<StreamType> & Vars<StreamType>::add(const wchar_t * name, UserFunction user_function)
{
Var<StreamType> & var = add(name);
var.set(user_function);
return var;
}
template<typename StreamType>
Var<StreamType> & Vars<StreamType>::add(const std::string & name, UserFunction user_function)
{
Var<StreamType> & var = add(name);
var.set(user_function);
return var;
}
template<typename StreamType>
Var<StreamType> & Vars<StreamType>::add(const std::wstring & name, UserFunction user_function)
{
Var<StreamType> & var = add(name);
var.set(user_function);
return var;
}
template<typename StreamType>
Var<StreamType> & Vars<StreamType>::add(const char * name, morm::Model & model)
{
Var<StreamType> & var = add(name);
var.set(model);
return var;
}
template<typename StreamType>
Var<StreamType> & Vars<StreamType>::add(const wchar_t * name, morm::Model & model)
{
Var<StreamType> & var = add(name);
var.set(model);
return var;
}
template<typename StreamType>
Var<StreamType> & Vars<StreamType>::add(const std::string & name, morm::Model & model)
{
Var<StreamType> & var = add(name);
var.set(model);
return var;
}
template<typename StreamType>
Var<StreamType> & Vars<StreamType>::add(const std::wstring & name, morm::Model & model)
{
Var<StreamType> & var = add(name);
var.set(model);
return var;
}
template<typename StreamType> template<typename StreamType>
Var<StreamType> * Vars<StreamType>::find(const char * name) const Var<StreamType> * Vars<StreamType>::find(const char * name) const
{ {
@ -165,11 +251,11 @@ Var<StreamType> * Vars<StreamType>::find(const char * name) const
template<typename StreamType> template<typename StreamType>
Var<StreamType> * Vars<StreamType>::find(const wchar_t * name) const Var<StreamType> * Vars<StreamType>::find(const wchar_t * name) const
{ {
VarMap::const_iterator i = var_map.find(name); typename VarMap::const_iterator i = var_map.find(name);
if( i != var_map.end() ) if( i != var_map.end() )
{ {
return &(*i); return i->second;
} }
return nullptr; return nullptr;
@ -188,11 +274,11 @@ Var<StreamType> * Vars<StreamType>::find(const std::string & name) const
template<typename StreamType> template<typename StreamType>
Var<StreamType> * Vars<StreamType>::find(const std::wstring & name) const Var<StreamType> * Vars<StreamType>::find(const std::wstring & name) const
{ {
VarMap::const_iterator i = var_map.find(name); typename VarMap::const_iterator i = var_map.find(name);
if( i != var_map.end() ) if( i != var_map.end() )
{ {
return &(*i); return i->second;
} }
return nullptr; return nullptr;