Skip to content

Commit e472bbe

Browse files
authored
Merge pull request #1572 from paulapatience/numbers-translators
Simplify Integer_O::create and add const reference translators
2 parents daa9fe2 + 4edf1b6 commit e472bbe

File tree

4 files changed

+128
-220
lines changed

4 files changed

+128
-220
lines changed

include/clasp/core/numbers.h

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -250,55 +250,31 @@ class Integer_O : public Rational_O {
250250
public:
251251
/*! Return a Cons (integer low high) */
252252
static T_sp makeIntegerType(gc::Fixnum low, gc::Fixnum high);
253-
static Integer_sp create(const mpz_class& v);
254-
static Integer_sp create(gctools::Fixnum v);
255-
static Integer_sp create(const string& v, int base = 0) { return create(v.c_str(), base); };
256-
static Integer_sp create(const char* v, int base = 0) {
257-
if (v[0] == '+')
258-
v = &v[1]; // skip leading +
259-
mpz_class zv(v, base);
260-
return create(zv);
261-
};
253+
254+
static Integer_sp create(std::signed_integral auto v);
255+
static Integer_sp create(std::unsigned_integral auto v);
256+
static Integer_sp create(std::floating_point auto v);
262257

263258
static Integer_sp create(int8_t v);
264259
static Integer_sp create(uint8_t v);
265-
266260
static Integer_sp create(int16_t v);
267261
static Integer_sp create(uint16_t v);
268-
262+
#ifdef CLASP_FIXNUM_IS_INT64
269263
static Integer_sp create(int32_t v);
270264
static Integer_sp create(uint32_t v);
271-
272-
#if !defined(_TARGET_OS_LINUX) && !defined(_TARGET_OS_FREEBSD)
273-
static Integer_sp create(uintptr_t v);
274265
#endif
275-
276-
#if !defined(CLASP_FIXNUM_IS_INT64)
277-
static Integer_sp create(int64_t v);
278-
#endif
279-
static Integer_sp create(uint64_t v);
280-
281-
// THOSE ARE ALREADY DEFINED ABOVE
282-
// static Integer_sp create( short v );
283-
// static Integer_sp create( unsigned short v );
284-
//
285-
// static Integer_sp create( int v );
286-
// static Integer_sp create( unsigned int v );
287-
//
288266
#if !defined(_TARGET_OS_LINUX) && !defined(_TARGET_OS_FREEBSD)
289-
static Integer_sp create(long v) { return Integer_O::create((Fixnum)v); }
290-
#endif
291-
// static Integer_sp create( unsigned long v );
292-
//
293-
#if !defined(CLASP_LONG_LONG_IS_INT64)
294-
static Integer_sp create(long long v);
267+
static Integer_sp create(long v) { return Integer_O::create(static_cast<Fixnum>(v)); };
268+
static Integer_sp create(unsigned long v);
295269
#endif
296-
#if !defined(CLASP_UNSIGNED_LONG_LONG_IS_UINT64)
297-
static Integer_sp create(unsigned long long v);
298-
#endif
299-
static Integer_sp create(float f);
300-
static Integer_sp create(double f);
301-
static Integer_sp createLongFloat(LongFloat f);
270+
271+
static Integer_sp create(const mpz_class& v);
272+
static Integer_sp create(const string& v, int base = 0) { return create(v.c_str(), base); };
273+
static Integer_sp create(const char* v, int base = 0) {
274+
if (v[0] == '+')
275+
v = &v[1];
276+
return create(mpz_class{v, base});
277+
};
302278

303279
public:
304280
virtual mpz_class mpz() const { SUBIMP(); };

include/clasp/core/translators.h

Lines changed: 67 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -54,59 +54,84 @@ template <std::integral I> struct from_object<I> {
5454
from_object(core::T_sp o) : _v(core::clasp_to_integral<I>(o)) {};
5555
};
5656

57+
template <std::integral I> struct from_object<const I&> {
58+
typedef I DeclareType;
59+
DeclareType _v;
60+
from_object(core::T_sp o) : _v(core::clasp_to_integral<I>(o)) {};
61+
};
62+
5763
template <> struct from_object<float> {
5864
typedef float DeclareType;
65+
DeclareType _v;
66+
from_object(core::T_sp o) : _v(core::clasp_to_float(gc::As<core::Number_sp>(o))) {};
67+
};
5968

69+
template <> struct from_object<const float&> {
70+
typedef float DeclareType;
6071
DeclareType _v;
61-
from_object(core::T_sp o) : _v(core::clasp_to_float(gc::As<core::Number_sp>(o))){};
72+
from_object(core::T_sp o) : _v(core::clasp_to_float(gc::As<core::Number_sp>(o))) {};
6273
};
6374

6475
template <> struct from_object<double> {
6576
typedef double DeclareType;
77+
DeclareType _v;
78+
from_object(core::T_sp o) : _v(core::clasp_to_double(gc::As<core::Number_sp>(o))) {};
79+
};
6680

81+
template <> struct from_object<const double&> {
82+
typedef double DeclareType;
6783
DeclareType _v;
68-
from_object(core::T_sp o) : _v(core::clasp_to_double(gc::As<core::Number_sp>(o))){};
84+
from_object(core::T_sp o) : _v(core::clasp_to_double(gc::As<core::Number_sp>(o))) {};
6985
};
7086

7187
template <> struct from_object<long double> {
7288
typedef long double DeclareType;
89+
DeclareType _v;
90+
from_object(core::T_sp o) : _v(core::clasp_to_long_double(gc::As<core::Number_sp>(o))) {};
91+
};
7392

93+
template <> struct from_object<const long double&> {
94+
typedef long double DeclareType;
7495
DeclareType _v;
75-
from_object(core::T_sp o) : _v(core::clasp_to_long_double(gc::As<core::Number_sp>(o))){};
96+
from_object(core::T_sp o) : _v(core::clasp_to_long_double(gc::As<core::Number_sp>(o))) {};
7697
};
7798

7899
template <> struct from_object<bool> {
79100
typedef bool DeclareType;
80101
DeclareType _v;
81-
from_object(core::T_sp o) : _v(!o.nilp()){};
102+
from_object(core::T_sp o) : _v(!o.nilp()) {};
103+
};
104+
105+
template <> struct from_object<const bool&> {
106+
typedef bool DeclareType;
107+
DeclareType _v;
108+
from_object(core::T_sp o) : _v(!o.nilp()) {};
82109
};
83110

84111
template <> struct from_object<bool&> {
85112
typedef bool DeclareType;
86113
DeclareType _v;
87-
from_object(core::T_sp o) : _v(!o.nilp()){};
114+
from_object(core::T_sp o) : _v(!o.nilp()) {};
88115
~from_object(){/*non trivial*/};
89116
};
90117

91118
template <> struct from_object<core::T_O*> {
92119
typedef core::T_O* DeclareType;
93-
94120
DeclareType _v;
95-
from_object(core::T_sp o) : _v(o.raw_()){};
121+
from_object(core::T_sp o) : _v(o.raw_()) {};
96122
};
97123

98124
template <> struct from_object<void*> {
99125
typedef void* DeclareType;
100126
DeclareType _v;
101-
from_object(core::T_sp o) : _v(core::lisp_to_void_ptr(o)){};
127+
from_object(core::T_sp o) : _v(core::lisp_to_void_ptr(o)) {};
102128
};
103129

104130
template <> struct from_object<bool*> {
105131
typedef bool* DeclareType;
106-
107132
DeclareType _v;
108133
bool _val;
109-
from_object(T_P o) : _v(&_val), _val(true){};
134+
from_object(T_P o) : _v(&_val), _val(true) {};
110135
};
111136

112137
// TO_OBJECT TRANSLATORS
@@ -138,11 +163,12 @@ template <> struct to_object<void*> {
138163

139164
template <> struct to_object<bool> {
140165
typedef bool DeclareType;
141-
static core::T_sp convert(DeclareType v) {
142-
if (v)
143-
return core::lisp_true();
144-
return nil<core::T_O>();
145-
}
166+
static core::T_sp convert(DeclareType v) { return v ? core::lisp_true() : nil<core::T_O>(); }
167+
};
168+
169+
template <> struct to_object<const bool&> {
170+
typedef const bool& DeclareType;
171+
static core::T_sp convert(DeclareType v) { return v ? core::lisp_true() : nil<core::T_O>(); }
146172
};
147173

148174
// THIS FN IS ALREADY DEFINED IN CHARACTER.H
@@ -156,125 +182,58 @@ template <> struct to_object<bool> {
156182
// }
157183
// };
158184

159-
template <> struct to_object<unsigned char> {
160-
typedef unsigned char DeclareType;
161-
static core::T_sp convert(DeclareType v) { return (core::clasp_make_fixnum(v)); }
162-
};
163-
164-
template <> struct to_object<short> {
165-
typedef short DeclareType;
166-
static core::T_sp convert(DeclareType v) {
167-
core::Fixnum_sp oi = core::make_fixnum(v);
168-
return (oi);
169-
}
170-
};
171-
172-
template <> struct to_object<unsigned short> {
173-
typedef unsigned short DeclareType;
174-
static core::T_sp convert(DeclareType v) {
175-
core::Fixnum_sp oi = core::make_fixnum(v);
176-
return (oi);
177-
}
178-
};
179-
180-
template <> struct to_object<int> {
181-
typedef int DeclareType;
182-
static core::T_sp convert(DeclareType v) {
183-
core::Fixnum_sp oi = core::make_fixnum(v);
184-
return (oi);
185-
}
186-
};
187-
188-
template <> struct to_object<unsigned int> {
189-
typedef unsigned int DeclareType;
190-
static core::T_sp convert(DeclareType v) {
191-
core::Integer_sp oi = core::Integer_O::create(v);
192-
return (oi);
193-
}
194-
};
195-
196-
template <> struct to_object<const unsigned int> {
197-
typedef const unsigned int DeclareType;
198-
static core::T_sp convert(DeclareType v) {
199-
core::Integer_sp oi = core::Integer_O::create(v);
200-
return (oi);
201-
}
185+
template <std::integral I> struct to_object<I> {
186+
typedef I DeclareType;
187+
static core::T_sp convert(DeclareType v) { return core::Integer_O::create(v); }
202188
};
203189

204-
template <> struct to_object<long> {
205-
typedef long DeclareType;
206-
static core::T_sp convert(DeclareType v) {
207-
core::Integer_sp oi = core::Integer_O::create(static_cast<gctools::Fixnum>(v));
208-
return (oi);
209-
}
190+
template <std::integral I> struct to_object<const I&> {
191+
typedef const I& DeclareType;
192+
static core::T_sp convert(DeclareType v) { return core::Integer_O::create(v); }
210193
};
211194

212-
template <> struct to_object<unsigned long> {
213-
typedef unsigned long DeclareType;
214-
static core::T_sp convert(DeclareType v) {
215-
core::Integer_sp oi = core::Integer_O::create(static_cast<gctools::Fixnum>(v));
216-
return (oi);
217-
}
195+
template <> struct to_object<float> {
196+
typedef float DeclareType;
197+
static core::T_sp convert(DeclareType v) { return core::clasp_make_single_float(v); }
218198
};
219199

220-
#if __SIZEOF_LONG_LONG__ <= 8
221-
222-
template <> struct to_object<long long> {
223-
typedef long long DeclareType;
224-
static core::T_sp convert(DeclareType v) {
225-
core::Integer_sp oi = core::Integer_O::create((int64_t)v);
226-
return (oi);
227-
}
200+
template <> struct to_object<const float&> {
201+
typedef const float& DeclareType;
202+
static core::T_sp convert(DeclareType v) { return core::clasp_make_single_float(v); }
228203
};
229204

230-
template <> struct to_object<unsigned long long> {
231-
typedef unsigned long long DeclareType;
232-
static core::T_sp convert(DeclareType v) {
233-
core::Integer_sp oi = core::Integer_O::create((uint64_t)v);
234-
return (oi);
235-
}
205+
template <> struct to_object<double> {
206+
typedef double DeclareType;
207+
static core::T_sp convert(DeclareType v) { return core::clasp_make_double_float(v); }
236208
};
237209

238-
#endif
239-
240-
template <> struct to_object<float> {
241-
typedef double DeclareType;
242-
static core::T_sp convert(DeclareType v) {
243-
core::SingleFloat_sp oi = core::clasp_make_single_float(v);
244-
return (oi);
245-
}
210+
template <> struct to_object<const double&> {
211+
typedef const double& DeclareType;
212+
static core::T_sp convert(DeclareType v) { return core::clasp_make_double_float(v); }
246213
};
247214

248-
template <> struct to_object<double> {
249-
typedef double DeclareType;
215+
template <> struct to_object<long double> {
216+
typedef long double DeclareType;
250217
static core::T_sp convert(DeclareType v) {
251-
core::DoubleFloat_sp oi = core::clasp_make_double_float(v);
252-
return (oi);
218+
return core::clasp_make_double_float(static_cast<double>(v));
253219
}
254220
};
255221

256-
template <> struct to_object<long double> {
257-
typedef double DeclareType;
222+
template <> struct to_object<const long double&> {
223+
typedef const long double& DeclareType;
258224
static core::T_sp convert(DeclareType v) {
259-
core::DoubleFloat_sp oi = core::DoubleFloat_O::create(v);
260-
return (oi);
225+
return core::clasp_make_double_float(static_cast<double>(v));
261226
}
262227
};
263228

264229
template <> struct to_object<mpz_class> {
265230
typedef mpz_class DeclareType;
266-
static core::T_sp convert(const DeclareType& v) {
267-
core::Integer_sp oi = core::Integer_O::create(v);
268-
return (oi);
269-
}
231+
static core::T_sp convert(const DeclareType& v) { return core::Integer_O::create(v); }
270232
};
271233

272234
template <> struct to_object<const mpz_class&> {
273235
typedef const mpz_class& DeclareType;
274-
static core::T_sp convert(DeclareType v) {
275-
core::Integer_sp oi = core::Integer_O::create(v);
276-
return (oi);
277-
}
236+
static core::T_sp convert(DeclareType v) { return core::Integer_O::create(v); }
278237
};
279238

280239
// String translators

0 commit comments

Comments
 (0)