ezc/src/var.cpp

302 lines
4.9 KiB
C++

/*
* 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.
*/
#include "var.h"
#include "utf8/utf8.h"
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;
}
}