1 #ifndef VTESTBED_GUILE_BASE_HH 2 #define VTESTBED_GUILE_BASE_HH 4 #include <blitz/array.h> 16 template <
class Po
inter>
18 define_procedure(
const char *name,
int req,
int opt,
int rest, Pointer ptr,
19 const char* documentation=
nullptr) {
20 SCM proc = scm_c_define_gsubr(name, req, opt, rest,
21 reinterpret_cast<scm_t_subr>(ptr));
23 scm_set_procedure_property_x(proc, scm_from_utf8_symbol(
"documentation"),
24 scm_from_utf8_string(documentation));
29 throw_error(
const char* message) {
30 scm_throw(scm_from_utf8_symbol(
"vtb-error"),
31 scm_list_1(scm_from_utf8_string(message)));
35 symbol_equal(SCM a, SCM b) {
36 return scm_is_true(scm_eq_p(a, b));
48 return SCM_UNSPECIFIED;
51 template <
class T>
inline T*
52 allocate(
const char* name) {
53 return reinterpret_cast<T*>(scm_gc_malloc(
sizeof(T), name));
56 template <
class T>
inline T*
57 allocate_pointerless(
const char* name) {
58 return reinterpret_cast<T*>(scm_gc_malloc_pointerless(
sizeof(T), name));
61 template <
class T,
class ... Args>
inline T*
62 construct_pointerless(
const char* name, Args ... args) {
63 auto* ptr = allocate_pointerless<T>(name);
64 new (ptr) T(std::forward<Args>(args)...);
68 inline bool is_bound(SCM s) {
return !SCM_UNBNDP(s); }
69 inline int length(SCM s) {
return scm_to_int(scm_length(s)); }
70 inline bool is_list(SCM s) {
return scm_is_true(scm_list_p(s)); }
73 inline SCM to_scm(
const blitz::TinyVector<float,N>& x) {
74 auto *ptr = reinterpret_cast<float*>(std::malloc(
sizeof(
float)*N));
75 for (
int i=0; i<N; ++i) { ptr[i] = x[i]; }
76 return scm_take_f32vector(ptr, N);
80 inline SCM to_scm(
const blitz::TinyVector<double,N>& x) {
81 auto *ptr = reinterpret_cast<double*>(std::malloc(
sizeof(
double)*N));
82 for (
int i=0; i<N; ++i) { ptr[i] = x[i]; }
83 return scm_take_f64vector(ptr, N);
89 using scalar_type = int;
90 static constexpr
const auto elements = scm_s32vector_elements;
94 using scalar_type = float;
95 static constexpr
const auto elements = scm_f32vector_elements;
99 using scalar_type = double;
100 static constexpr
const auto elements = scm_f64vector_elements;
103 template <
class T,
int N>
104 inline blitz::TinyVector<T,N> to_vector(SCM x) {
105 scm_t_array_handle handle;
109 blitz::TinyVector<T,N> y;
110 for (
size_t i=0; i<n; ++i, ptr += dn) { y[i] = *ptr; }
115 inline void operator()(
void* ptr) { std::free(ptr); }
127 inline SCM to_scm(int32_t x) {
return scm_from_int32(x); }
128 inline SCM to_scm(uint32_t x) {
return scm_from_uint32(x); }
129 inline SCM to_scm(int64_t x) {
return scm_from_int64(x); }
130 inline SCM to_scm(uint64_t x) {
return scm_from_uint64(x); }
131 inline SCM to_scm(
float x) {
return scm_from_double(x); }
132 inline SCM to_scm(
double x) {
return scm_from_double(x); }
133 inline SCM to_scm(
const char* x) {
return scm_from_utf8_string(x); }
134 inline SCM to_scm(
const std::string& x) {
return to_scm(x.data()); }
136 template <
class T> T from_scm(SCM x);
137 template <>
inline bool from_scm<bool>(SCM x) {
return scm_is_true(x); }
138 template <>
inline int from_scm<int>(SCM x) {
return scm_to_int(x); }
139 template <>
inline float from_scm<float>(SCM x) {
return scm_to_double(x); }
140 template <>
inline double from_scm<double>(SCM x) {
return scm_to_double(x); }
146 #endif // vim:filetype=cpp
SCM vtestbed_gc()
Run garbage collection and all object destructors.
T length(const blitz::TinyVector< T, N > &x)
Computes vector length without overflow.