45 #ifndef CGU_EXTENSION_H
46 #define CGU_EXTENSION_H
397 #include <type_traits>
414 #include <libguile.h>
417 #ifndef DOXYGEN_PARSING
420 namespace Extension {
427 enum VectorDeleteType {Long, Double, String};
429 struct VectorDeleteArgs {
430 VectorDeleteType type;
436 extern
bool init_mutex() noexcept;
444 inline SCM cgu_format_try_handler(
void* data) {
445 using Cgu::Extension::FormatArgs;
446 FormatArgs* format_args =
static_cast<FormatArgs*
>(data);
447 return scm_simple_format(SCM_BOOL_F, format_args->text, format_args->rest);
449 inline SCM cgu_format_catch_handler(
void*, SCM, SCM) {
452 inline void* cgu_guile_wrapper(
void* data) {
467 inline void cgu_delete_vector(
void* data) {
468 using Cgu::Extension::VectorDeleteArgs;
469 VectorDeleteArgs* args =
static_cast<VectorDeleteArgs*
>(data);
470 switch (args->type) {
471 case Cgu::Extension::Long:
472 delete static_cast<std::vector<long>*
>(args->vec);
474 case Cgu::Extension::Double:
475 delete static_cast<std::vector<double>*
>(args->vec);
477 case Cgu::Extension::String:
478 delete static_cast<std::vector<std::string>*
>(args->vec);
481 g_critical(
"Incorrect argument passed to cgu_delete_vector");
485 inline void cgu_unlock_module_mutex(
void*) {
488 Cgu::Extension::get_user_module_mutex()->unlock();
492 #endif // DOXYGEN_PARSING
496 namespace Extension {
502 virtual const char*
what()
const throw() {
return (
const char*)message.
get();}
503 const char*
guile_text()
const throw() {
return (
const char*)guile_message.
get();}
505 message(g_strdup_printf(u8
"Cgu::Extension::GuileException: %s", msg)),
506 guile_message(g_strdup(msg)) {}
514 virtual const char*
what()
const throw() {
return (
const char*)message.
get();}
515 const char*
err_text()
const throw() {
return (
const char*)err_message.
get();}
517 message(g_strdup_printf(u8
"Cgu::Extension::ReturnValueError: %s", msg)),
518 err_message(g_strdup(msg)) {}
525 virtual const char*
what()
const throw() {
return (
const char*)message.
get();}
527 message(g_strdup_printf(u8
"Cgu::Extension::WrapperError: %s", msg)) {}
531 #ifndef DOXYGEN_PARSING
538 template <
class Ret,
class Translator>
539 Ret exec_impl(
const std::string& preamble,
540 const std::string& file,
541 Translator&& translator,
550 loader += u8
"((lambda ()";
551 loader += u8
"(catch "
556 loader += u8
"primitive-load \"";
558 loader += u8
"load \"";
561 "(lambda (key . details)"
562 "(cons \"***cgu-guile-exception***\" (cons key details))))";
569 std::string guile_except;
570 std::string guile_ret_val_err;
593 std::unique_ptr<Cgu::Callback::Callback> cb(Cgu::Callback::lambda<>([&] () ->
void {
596 scm = scm_eval_string_in_module(scm_from_utf8_string(loader.c_str()),
597 scm_c_resolve_module(
"guile-user"));
601 throw std::bad_alloc();
603 scm_dynwind_begin(scm_t_dynwind_flags(0));
604 scm_dynwind_unwind_handler(&cgu_unlock_module_mutex, 0, SCM_F_WIND_EXPLICITLY);
605 get_user_module_mutex()->lock();
606 SCM new_mod = scm_call_0(scm_c_public_ref(
"guile",
"make-fresh-user-module"));
609 scm = scm_eval_string_in_module(scm_from_utf8_string(loader.c_str()),
633 #ifndef CGU_GUILE_HAS_BROKEN_LINKING
634 scm_dynwind_begin(scm_t_dynwind_flags(0));
635 scm_dynwind_block_asyncs();
642 bool badalloc =
false;
644 retval = translator(scm);
660 catch (GuileException& e) {
662 guile_except = e.guile_text();
668 catch (ReturnValueError& e) {
670 guile_ret_val_err = e.err_text();
676 catch (std::exception& e) {
686 gen_err = u8
"C++ exception thrown in cgu_guile_wrapper()";
692 #ifndef CGU_GUILE_HAS_BROKEN_LINKING
695 if (badalloc)
throw std::bad_alloc();
700 if (scm_with_guile(&cgu_guile_wrapper, cb.get()))
701 throw WrapperError(u8
"cgu_guile_wrapper() has trapped std::bad_alloc");
702 if (!guile_except.empty())
703 throw GuileException(guile_except.c_str());
704 if (!guile_ret_val_err.empty())
705 throw ReturnValueError(guile_ret_val_err.c_str());
706 if (!gen_err.empty())
707 throw WrapperError(gen_err.c_str());
709 throw WrapperError(u8
"the preamble or translator threw a native guile exception");
713 #endif // DOXYGEN_PARSING
749 SCM ret = SCM_BOOL_F;
750 int length = scm_to_int(scm_length(args));
752 SCM first = scm_car(args);
753 if (scm_is_true(scm_string_p(first))) {
756 ret = scm_string_append(scm_list_4(scm_from_utf8_string(u8
"Exception "),
757 scm_symbol_to_string(key),
758 scm_from_utf8_string(u8
": "),
762 SCM second = scm_cadr(args);
763 if (scm_is_true(scm_string_p(second))) {
765 SCM text = scm_string_append(scm_list_n(scm_from_utf8_string(u8
"Exception "),
766 scm_symbol_to_string(key),
767 scm_from_utf8_string(u8
" in procedure "),
769 scm_from_utf8_string(u8
": "),
775 SCM third = scm_caddr(args);
776 if (scm_is_false(third))
778 else if (scm_is_true(scm_list_p(third))) {
779 FormatArgs format_args = {text, third};
780 ret = scm_internal_catch(SCM_BOOL_T,
781 &cgu_format_try_handler,
783 &cgu_format_catch_handler,
793 if (scm_is_false(ret)) {
796 ret = scm_simple_format(SCM_BOOL_F,
797 scm_from_utf8_string(u8
"Exception ~S: ~S"),
798 scm_list_2(key, args));
831 if (scm_is_false(scm_list_p(scm))
832 || scm_is_true(scm_null_p(scm)))
return;
833 SCM first = scm_car(scm);
834 if (scm_is_true(scm_string_p(first))) {
836 const char* text = 0;
840 scm_dynwind_begin(scm_t_dynwind_flags(0));
841 char* car = scm_to_utf8_stringn(first, &len);
851 scm_dynwind_unwind_handler(&free, car, scm_t_wind_flags(0));
852 if (len == strlen(u8
"***cgu-guile-exception***")
853 && !strncmp(car, u8
"***cgu-guile-exception***", len)) {
858 text = scm_to_utf8_stringn(str, &len);
864 std::unique_ptr<char, Cgu::CFree> up_car(car);
865 std::unique_ptr<const char, Cgu::CFree> up_text(text);
913 if (scm_is_false(scm_list_p(scm)))
919 scm_dynwind_begin(scm_t_dynwind_flags(0));
927 bool badalloc =
false;
928 const char* rv_error = 0;
929 std::vector<long>* res = 0;
930 VectorDeleteArgs* args = 0;
936 res =
new std::vector<long>;
939 args =
new VectorDeleteArgs{Long, res};
954 scm_dynwind_unwind_handler(&cgu_delete_vector, args, scm_t_wind_flags(0));
960 SCM guile_vec = scm_vector(scm);
983 size_t length = scm_to_size_t(scm_vector_length(guile_vec));
985 res->reserve(length);
990 for (
size_t count = 0;
991 count < length && !rv_error && !badalloc;
993 SCM item = scm_vector_ref(guile_vec, scm_from_size_t(count));
994 if (scm_is_false(scm_integer_p(item)))
995 rv_error = u8
"scheme code did not evaluate to a homogeneous list of integer\n";
997 SCM min = scm_from_long(std::numeric_limits<long>::min());
998 SCM max = scm_from_long(std::numeric_limits<long>::max());
999 if (scm_is_false(scm_leq_p(item, max)) || scm_is_false(scm_geq_p(item, min)))
1000 rv_error = u8
"scheme code evaluated out of range for long\n";
1003 res->push_back(scm_to_long(item));
1016 std::unique_ptr<std::vector<long>> up_res(res);
1017 std::unique_ptr<VectorDeleteArgs> up_args(args);
1018 if (badalloc)
throw std::bad_alloc();
1022 return std::move(*res);
1072 if (scm_is_false(scm_list_p(scm)))
1078 scm_dynwind_begin(scm_t_dynwind_flags(0));
1086 bool badalloc =
false;
1087 const char* rv_error = 0;
1088 std::vector<double>* res = 0;
1089 VectorDeleteArgs* args = 0;
1095 res =
new std::vector<double>;
1098 args =
new VectorDeleteArgs{Double, res};
1113 scm_dynwind_unwind_handler(&cgu_delete_vector, args, scm_t_wind_flags(0));
1119 SCM guile_vec = scm_vector(scm);
1142 size_t length = scm_to_size_t(scm_vector_length(guile_vec));
1144 res->reserve(length);
1149 for (
size_t count = 0;
1150 count < length && !rv_error && !badalloc;
1152 SCM item = scm_vector_ref(guile_vec, scm_from_size_t(count));
1153 if (scm_is_false(scm_real_p(item)))
1154 rv_error = u8
"scheme code did not evaluate to a homogeneous list of real numbers\n";
1156 SCM min = scm_from_double(std::numeric_limits<double>::lowest());
1157 SCM max = scm_from_double(std::numeric_limits<double>::max());
1158 if (scm_is_false(scm_leq_p(item, max)) || scm_is_false(scm_geq_p(item, min)))
1159 rv_error = u8
"scheme code evaluated out of range for double\n";
1162 res->push_back(scm_to_double(item));
1175 std::unique_ptr<std::vector<double>> up_res(res);
1176 std::unique_ptr<VectorDeleteArgs> up_args(args);
1177 if (badalloc)
throw std::bad_alloc();
1181 return std::move(*res);
1232 if (scm_is_false(scm_list_p(scm)))
1238 scm_dynwind_begin(scm_t_dynwind_flags(0));
1246 bool badalloc =
false;
1247 const char* rv_error = 0;
1248 std::vector<std::string>* res = 0;
1249 VectorDeleteArgs* args = 0;
1255 res =
new std::vector<std::string>;
1258 args =
new VectorDeleteArgs{String, res};
1273 scm_dynwind_unwind_handler(&cgu_delete_vector, args, scm_t_wind_flags(0));
1279 SCM guile_vec = scm_vector(scm);
1302 size_t length = scm_to_size_t(scm_vector_length(guile_vec));
1304 res->reserve(length);
1309 for (
size_t count = 0;
1310 count < length && !rv_error && !badalloc;
1312 SCM item = scm_vector_ref(guile_vec, scm_from_size_t(count));
1313 if (scm_is_false(scm_string_p(item)))
1314 rv_error = u8
"scheme code did not evaluate to a homogeneous list of string\n";
1320 char* str = scm_to_utf8_stringn(item, &len);
1322 res->emplace_back(str, len);
1335 std::unique_ptr<std::vector<std::string>> up_res(res);
1336 std::unique_ptr<VectorDeleteArgs> up_args(args);
1337 if (badalloc)
throw std::bad_alloc();
1341 return std::move(*res);
1381 if (scm_is_false(scm_integer_p(scm)))
1383 SCM min = scm_from_long(std::numeric_limits<long>::min());
1384 SCM max = scm_from_long(std::numeric_limits<long>::max());
1385 if (scm_is_false(scm_leq_p(scm, max)) || scm_is_false(scm_geq_p(scm, min)))
1387 return scm_to_long(scm);
1433 if (scm_is_false(scm_real_p(scm)))
1434 throw ReturnValueError(u8
"scheme code did not evaluate to a real number\n");
1435 SCM min = scm_from_double(std::numeric_limits<double>::lowest());
1436 SCM max = scm_from_double(std::numeric_limits<double>::max());
1437 if (scm_is_false(scm_leq_p(scm, max)) || scm_is_false(scm_geq_p(scm, min)))
1438 throw ReturnValueError(u8
"scheme code evaluated out of range for double\n");
1439 return scm_to_double(scm);
1481 if (scm_is_false(scm_string_p(scm)))
1487 std::unique_ptr<const char, Cgu::CFree> s(scm_to_utf8_stringn(scm, &len));
1488 return std::string(s.get(), len);
1610 template <
class Translator>
1611 auto exec(
const std::string& preamble,
1612 const std::string& file,
1613 Translator&& translator) ->
typename std::result_of<Translator(SCM)>::type {
1617 typedef typename std::result_of<Translator(SCM)>::type Ret;
1618 return exec_impl<Ret>(preamble, file, std::forward<Translator>(translator),
false);
1698 template <
class Translator>
1700 const std::string& file,
1701 Translator&& translator) ->
typename std::result_of<Translator(SCM)>::type {
1705 typedef typename std::result_of<Translator(SCM)>::type Ret;
1706 return exec_impl<Ret>(preamble, file, std::forward<Translator>(translator),
true);
1713 #endif // CGU_EXTENSION_H
std::vector< long > list_to_vector_long(SCM scm)
Definition: extension.h:911
GuileException(const char *msg)
Definition: extension.h:504
long integer_to_long(SCM scm)
Definition: extension.h:1379
~ReturnValueError()
Definition: extension.h:519
T get() const noexcept
Definition: shared_handle.h:762
~GuileException()
Definition: extension.h:507
const char * err_text() const
Definition: extension.h:515
void * any_to_void(SCM scm)
Definition: extension.h:1526
WrapperError(const char *msg)
Definition: extension.h:526
This file provides classes for type erasure.
Definition: extension.h:510
SCM exception_to_string(SCM key, SCM args) noexcept
Definition: extension.h:742
A class enabling the cancellation state of a thread to be controlled.
Definition: thread.h:718
double real_to_double(SCM scm)
Definition: extension.h:1431
Definition: extension.h:498
std::string string_to_string(SCM scm)
Definition: extension.h:1479
std::vector< double > list_to_vector_double(SCM scm)
Definition: extension.h:1070
virtual const char * what() const
Definition: extension.h:525
auto exec(const std::string &preamble, const std::string &file, Translator &&translator) -> typename std::result_of< Translator(SCM)>::type
Definition: extension.h:1611
A wrapper class for pthread mutexes.
Definition: mutex.h:117
Provides wrapper classes for pthread mutexes and condition variables, and scoped locking classes for ...
Definition: application.h:44
std::vector< std::string > list_to_vector_string(SCM scm)
Definition: extension.h:1230
Definition: extension.h:522
virtual const char * what() const
Definition: extension.h:514
~WrapperError()
Definition: extension.h:528
virtual const char * what() const
Definition: extension.h:502
auto exec_shared(const std::string &preamble, const std::string &file, Translator &&translator) -> typename std::result_of< Translator(SCM)>::type
Definition: extension.h:1699
void rethrow_guile_exception(SCM scm)
Definition: extension.h:828
ReturnValueError(const char *msg)
Definition: extension.h:516
The callback interface class.
Definition: callback.h:567
const char * guile_text() const
Definition: extension.h:503