added Vars<> container
This commit is contained in:
parent
09beafeab7
commit
d9804f05aa
|
@ -0,0 +1,206 @@
|
|||
/*
|
||||
* 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"
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <list>
|
||||
|
||||
|
||||
|
||||
namespace Ezc
|
||||
{
|
||||
|
||||
template<typename StreamType>
|
||||
class Vars
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
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);
|
||||
|
||||
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;
|
||||
VarMap var_map
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
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)
|
||||
{
|
||||
Var<StreamType> & var = var_map[name];
|
||||
var.clear();
|
||||
return var;
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
{
|
||||
Var<StreamType> & var = var_map[name];
|
||||
var.clear();
|
||||
return var;
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
{
|
||||
var_list.push_back(var);
|
||||
var_map[name] = &var_list.back();
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
{
|
||||
VarMap::const_iterator i = var_map.find(name);
|
||||
|
||||
if( i != var_map.end() )
|
||||
{
|
||||
return &(*i);
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
VarMap::const_iterator i = var_map.find(name);
|
||||
|
||||
if( i != var_map.end() )
|
||||
{
|
||||
return &(*i);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue