wrenpp/src/def_configuration.cpp

68 lines
1.9 KiB
C++

/* Copyright 2020-2022, Michele Santullo
* This file is part of wrenpp.
*
* Wrenpp 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.
*
* Wrenpp 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 wrenpp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "wrenpp/def_configuration.hpp"
#include <iostream>
#include <algorithm>
namespace wren {
namespace {
template <typename T>
bool empty(T* message) {
return not (message and *message);
}
} //unnamed namespace
void DefConfiguration::write_fn (VM*, wren_string_t text) {
std::cout << text;
}
void DefConfiguration::error_fn (VM*, ErrorType type, wren_string_t module, int line, wren_string_t message) {
using std::empty;
std::cerr << "Wren error: " << message;
if (!empty(module)) {
std::cerr << " in " << module;
if (line > -1) {
std::cerr << ':' << line;
}
}
std::cerr << '\n';
}
void* DefConfiguration::reallocate_fn (void* ptr, std::size_t size) {
if (ptr and size) {
//reallocate
char* const old_mem = static_cast<char*>(ptr);
char* const new_mem = new char[size];
std::copy(old_mem, old_mem + size, new_mem);
delete[] old_mem;
return new_mem;
}
else if (ptr and not size) {
//free
char* const old_mem = static_cast<char*>(ptr);
delete[] old_mem;
}
else if (not ptr and size) {
//allocate
char* const new_mem = new char[size];
return new_mem;
}
return nullptr;
}
} //namespace wren