user-gcc/src/env_fake.cpp
King_DuckZ ac6ea57675 Add new EnvFake class.
It's an unordered_map wrapped into an EnvBase interface.
2020-04-13 16:29:15 +02:00

129 lines
3.8 KiB
C++

/* Copyright 2020, Michele Santullo
* This file is part of user-gcc.
*
* User-gcc is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* User-gcc is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with user-gcc. If not, see <http://www.gnu.org/licenses/>.
*/
#include "env_fake.hpp"
#include "to_var_map.hpp"
#include "safe_print.hpp"
#include "string_view_cat.hpp"
#include <unistd.h>
#include <utility>
#include <optional>
#include <type_traits>
#include <cassert>
#include <algorithm>
namespace duck {
namespace {
template <
typename M,
typename I,
typename F,
typename R=decltype(std::declval<F>()(std::declval<decltype(std::declval<M>().find(std::declval<I>()))&>())),
//not supposed to be altered by the caller
bool FIsNoex=noexcept(std::declval<F>()(std::declval<decltype(std::declval<M>().find(std::declval<I>()))&>())) and noexcept(R())
>
R run_if_found (M& map, const I& item, F func) noexcept(FIsNoex) {
typedef decltype(map.find(item)) iterator_type;
iterator_type it_found;
if constexpr (FIsNoex) {
try {
it_found = map.find(item);
}
catch (...) {
safe_print_error("Unexpected exception in run_if_found() will be ignored");
}
}
else {
it_found = map.find(item);
}
if constexpr (std::is_same<R, void>::value) {
if (map.end() != it_found) {
func(it_found);
}
}
else {
if (map.end() != it_found) {
return func(it_found);
}
else {
return R{};
}
}
}
} //unnamed namespace
EnvFake::EnvFake (bool starts_empty) :
m_map_strong_cpy(),
m_map(starts_empty ? map_type() : to_var_map_ignore_ownership(environ))
{
}
std::optional<std::string_view> EnvFake::get (std::string_view name) const noexcept {
return run_if_found(m_map, name, [](auto& it)noexcept{return std::make_optional(it->second);});
}
std::string_view EnvFake::get (std::string_view name, std::string_view def) const noexcept {
bool ret_def{true};
std::string_view maybe_ret(run_if_found(m_map, name, [&ret_def](auto& it)noexcept{ret_def=false; return it->second;}));
if (ret_def)
return def;
else
return maybe_ret;
}
void EnvFake::set (std::string_view name, std::string_view value) {
assert(not name.empty());
if (name.empty())
return;
const std::size_t size = name.size() + value.size();
std::unique_ptr<char[]> buff = std::make_unique<char[]>(size);
std::copy(name.begin(), name.end(), buff.get());
std::copy(value.begin(), value.end(), buff.get() + name.size());
const std::string_view new_name(buff.get(), name.size());
assert(new_name == name);
const std::string_view new_value(buff.get() + name.size(), value.size());
assert(new_value == value);
m_map_strong_cpy.insert_or_assign(std::string_view(new_name), std::move(buff));
m_map.insert_or_assign(std::string_view(new_name), std::string_view(new_value));
assert(m_map.at(name) == value);
assert(m_map.find(name)->first.data() == new_name.data());
}
void EnvFake::unset (std::string_view name) noexcept {
run_if_found(m_map, name, [&](auto& it)noexcept{m_map.erase(it);});
run_if_found(m_map_strong_cpy, name, [&](auto& it)noexcept{m_map_strong_cpy.erase(it);});
}
bool EnvFake::is_set (std::string_view name) const noexcept {
return run_if_found(m_map, name, [](auto&)noexcept{return true;});
}
auto EnvFake::size() const noexcept -> size_type {
return m_map.size();
}
void EnvFake::clear() noexcept {
m_map.clear();
m_map_strong_cpy.clear();
}
} //namespace duck