2021-09-14 00:31:50 +02:00
|
|
|
/*
|
|
|
|
* 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) 2021, 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_vars
|
|
|
|
#define headerfile_ezc_vars
|
|
|
|
|
|
|
|
#include "var.h"
|
|
|
|
#include "utf8/utf8.h"
|
2021-10-20 08:23:45 +02:00
|
|
|
#include "env.h"
|
2021-09-14 00:31:50 +02:00
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
|
|
|
|
namespace Ezc
|
|
|
|
{
|
|
|
|
|
2021-10-20 08:23:45 +02:00
|
|
|
// rename to something like Object? or a better name?
|
2021-09-14 00:31:50 +02:00
|
|
|
template<typename StreamType>
|
|
|
|
class Vars
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2021-10-20 08:23:45 +02:00
|
|
|
typedef void (*UserFunction)(Env<StreamType> &);
|
|
|
|
|
2021-09-14 00:31:50 +02:00
|
|
|
|
|
|
|
Var<StreamType> & add(const char * name);
|
|
|
|
Var<StreamType> & add(const wchar_t * name);
|
|
|
|
Var<StreamType> & add(const std::string & name);
|
|
|
|
Var<StreamType> & add(const std::wstring & name);
|
|
|
|
|
|
|
|
void add(const char * name, const Var<StreamType> & var);
|
|
|
|
void add(const wchar_t * 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);
|
|
|
|
|
2021-10-20 08:23:45 +02:00
|
|
|
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);
|
|
|
|
|
2021-11-20 22:02:18 +01:00
|
|
|
template<typename ModelType>
|
|
|
|
Var<StreamType> & add(const char * name, std::vector<ModelType> & model_container);
|
|
|
|
|
|
|
|
template<typename ModelType>
|
|
|
|
Var<StreamType> & add(const wchar_t * name, std::vector<ModelType> & model_container);
|
|
|
|
|
|
|
|
template<typename ModelType>
|
|
|
|
Var<StreamType> & add(const std::string & name, std::vector<ModelType> & model_container);
|
|
|
|
|
|
|
|
template<typename ModelType>
|
|
|
|
Var<StreamType> & add(const std::wstring & name, std::vector<ModelType> & model_container);
|
|
|
|
|
|
|
|
template<typename ModelType>
|
|
|
|
Var<StreamType> & add(const char * name, std::list<ModelType> & model_container);
|
|
|
|
|
|
|
|
template<typename ModelType>
|
|
|
|
Var<StreamType> & add(const wchar_t * name, std::list<ModelType> & model_container);
|
|
|
|
|
|
|
|
template<typename ModelType>
|
|
|
|
Var<StreamType> & add(const std::string & name, std::list<ModelType> & model_container);
|
|
|
|
|
|
|
|
template<typename ModelType>
|
|
|
|
Var<StreamType> & add(const std::wstring & name, std::list<ModelType> & model_container);
|
|
|
|
|
2021-09-14 00:31:50 +02:00
|
|
|
Var<StreamType> * find(const char * name) const;
|
|
|
|
Var<StreamType> * find(const wchar_t * name) const;
|
|
|
|
Var<StreamType> * find(const std::string & name) const;
|
|
|
|
Var<StreamType> * find(const std::wstring & name) const;
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
typedef std::list<Var<StreamType>> VarList;
|
|
|
|
VarList var_list;
|
|
|
|
|
|
|
|
typedef std::map<std::wstring, Var<StreamType>*> VarMap;
|
2021-10-20 08:23:45 +02:00
|
|
|
VarMap var_map;
|
2021-09-14 00:31:50 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename StreamType>
|
|
|
|
Var<StreamType> & Vars<StreamType>::add(const char * name)
|
|
|
|
{
|
|
|
|
std::wstring name_wide;
|
|
|
|
pt::utf8_to_wide(name, name_wide);
|
|
|
|
add(name_wide);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename StreamType>
|
|
|
|
Var<StreamType> & Vars<StreamType>::add(const wchar_t * name)
|
|
|
|
{
|
2021-10-20 08:23:45 +02:00
|
|
|
std::wstring name_str(name);
|
|
|
|
return add(name_str);
|
2021-09-14 00:31:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename StreamType>
|
|
|
|
Var<StreamType> & Vars<StreamType>::add(const std::string & name)
|
|
|
|
{
|
|
|
|
std::wstring name_wide;
|
|
|
|
pt::utf8_to_wide(name, name_wide);
|
|
|
|
return add(name_wide);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename StreamType>
|
|
|
|
Var<StreamType> & Vars<StreamType>::add(const std::wstring & name)
|
|
|
|
{
|
2021-10-20 08:23:45 +02:00
|
|
|
auto i = var_map.insert(std::make_pair(name, nullptr));
|
|
|
|
|
|
|
|
if( i.second )
|
|
|
|
{
|
|
|
|
Var<StreamType> & var = var_list.emplace_back();
|
|
|
|
i.first->second = &var;
|
|
|
|
}
|
|
|
|
|
|
|
|
i.first->second->clear();
|
|
|
|
return *(i.first->second);
|
2021-09-14 00:31:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename StreamType>
|
|
|
|
void Vars<StreamType>::add(const char * name, const Var<StreamType> & var)
|
|
|
|
{
|
|
|
|
std::wstring name_wide;
|
|
|
|
pt::utf8_to_wide(name, name_wide);
|
|
|
|
add(name_wide, var);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename StreamType>
|
|
|
|
void Vars<StreamType>::add(const wchar_t * name, const Var<StreamType> & var)
|
|
|
|
{
|
2021-10-20 08:23:45 +02:00
|
|
|
std::wstring name_str(name);
|
|
|
|
add(name_str, var);
|
2021-09-14 00:31:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename StreamType>
|
|
|
|
void Vars<StreamType>::add(const std::string & name, const Var<StreamType> & var)
|
|
|
|
{
|
|
|
|
std::wstring name_wide;
|
|
|
|
pt::utf8_to_wide(name, name_wide);
|
|
|
|
add(name_wide, var);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename StreamType>
|
|
|
|
void Vars<StreamType>::add(const std::wstring & name, const Var<StreamType> & var)
|
|
|
|
{
|
|
|
|
var_list.push_back(var);
|
|
|
|
var_map[name] = &var_list.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-20 08:23:45 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-20 22:02:18 +01:00
|
|
|
template<typename StreamType>
|
|
|
|
template<typename ModelType>
|
|
|
|
Var<StreamType> & Vars<StreamType>::add(const char * name, std::vector<ModelType> & model_container)
|
|
|
|
{
|
|
|
|
Var<StreamType> & var = add(name);
|
|
|
|
var.set(model_container);
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename StreamType>
|
|
|
|
template<typename ModelType>
|
|
|
|
Var<StreamType> & Vars<StreamType>::add(const wchar_t * name, std::vector<ModelType> & model_container)
|
|
|
|
{
|
|
|
|
Var<StreamType> & var = add(name);
|
|
|
|
var.set(model_container);
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename StreamType>
|
|
|
|
template<typename ModelType>
|
|
|
|
Var<StreamType> & Vars<StreamType>::add(const std::string & name, std::vector<ModelType> & model_container)
|
|
|
|
{
|
|
|
|
Var<StreamType> & var = add(name);
|
|
|
|
var.set(model_container);
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename StreamType>
|
|
|
|
template<typename ModelType>
|
|
|
|
Var<StreamType> & Vars<StreamType>::add(const std::wstring & name, std::vector<ModelType> & model_container)
|
|
|
|
{
|
|
|
|
Var<StreamType> & var = add(name);
|
|
|
|
var.set(model_container);
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename StreamType>
|
|
|
|
template<typename ModelType>
|
|
|
|
Var<StreamType> & Vars<StreamType>::add(const char * name, std::list<ModelType> & model_container)
|
|
|
|
{
|
|
|
|
Var<StreamType> & var = add(name);
|
|
|
|
var.set(model_container);
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename StreamType>
|
|
|
|
template<typename ModelType>
|
|
|
|
Var<StreamType> & Vars<StreamType>::add(const wchar_t * name, std::list<ModelType> & model_container)
|
|
|
|
{
|
|
|
|
Var<StreamType> & var = add(name);
|
|
|
|
var.set(model_container);
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename StreamType>
|
|
|
|
template<typename ModelType>
|
|
|
|
Var<StreamType> & Vars<StreamType>::add(const std::string & name, std::list<ModelType> & model_container)
|
|
|
|
{
|
|
|
|
Var<StreamType> & var = add(name);
|
|
|
|
var.set(model_container);
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename StreamType>
|
|
|
|
template<typename ModelType>
|
|
|
|
Var<StreamType> & Vars<StreamType>::add(const std::wstring & name, std::list<ModelType> & model_container)
|
|
|
|
{
|
|
|
|
Var<StreamType> & var = add(name);
|
|
|
|
var.set(model_container);
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-10-20 08:23:45 +02:00
|
|
|
|
2021-09-14 00:31:50 +02:00
|
|
|
template<typename StreamType>
|
|
|
|
Var<StreamType> * Vars<StreamType>::find(const char * name) const
|
|
|
|
{
|
|
|
|
std::wstring name_wide;
|
|
|
|
pt::utf8_to_wide(name, name_wide);
|
|
|
|
return find(name_wide);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename StreamType>
|
|
|
|
Var<StreamType> * Vars<StreamType>::find(const wchar_t * name) const
|
|
|
|
{
|
2021-10-20 08:23:45 +02:00
|
|
|
typename VarMap::const_iterator i = var_map.find(name);
|
2021-09-14 00:31:50 +02:00
|
|
|
|
|
|
|
if( i != var_map.end() )
|
|
|
|
{
|
2021-10-20 08:23:45 +02:00
|
|
|
return i->second;
|
2021-09-14 00:31:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename StreamType>
|
|
|
|
Var<StreamType> * Vars<StreamType>::find(const std::string & name) const
|
|
|
|
{
|
|
|
|
std::wstring name_wide;
|
|
|
|
pt::utf8_to_wide(name, name_wide);
|
|
|
|
return find(name_wide);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename StreamType>
|
|
|
|
Var<StreamType> * Vars<StreamType>::find(const std::wstring & name) const
|
|
|
|
{
|
2021-10-20 08:23:45 +02:00
|
|
|
typename VarMap::const_iterator i = var_map.find(name);
|
2021-09-14 00:31:50 +02:00
|
|
|
|
|
|
|
if( i != var_map.end() )
|
|
|
|
{
|
2021-10-20 08:23:45 +02:00
|
|
|
return i->second;
|
2021-09-14 00:31:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
#endif
|