[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Toon-members] TooN TooN.h internal/operators.hh test/SXX_test.cc
From: |
Edward Rosten |
Subject: |
[Toon-members] TooN TooN.h internal/operators.hh test/SXX_test.cc |
Date: |
Wed, 25 Mar 2009 19:46:11 +0000 |
CVSROOT: /cvsroot/toon
Module name: TooN
Changes by: Edward Rosten <edrosten> 09/03/25 19:46:11
Modified files:
. : TooN.h
internal : operators.hh
test : SXX_test.cc
Log message:
Add in traits class do determine if a type is a field.
Generic vector<A> * thing (designed for vector * scalar) uses type
deduction to
make the return type vector<decltype(A*thing)>. If A*thing does not
exist,
compilation will fail in the type deduction stage.
The type deduction part will now return void unless both A and thing
are fields.
This allows SFINAE to kick in and reject functions if A*thing does not
exist,
allowing an overloaded function (eg Vector<A> * SO2<A>) can be used
instead.
If you define some field F, such that F * double doesn't exist, but
wish to have
a specialization vector<F> * double which does exist, then your code
will not
compile.
CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/TooN/TooN.h?cvsroot=toon&r1=1.24&r2=1.25
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/operators.hh?cvsroot=toon&r1=1.21&r2=1.22
http://cvs.savannah.gnu.org/viewcvs/TooN/test/SXX_test.cc?cvsroot=toon&r1=1.1&r2=1.2
Patches:
Index: TooN.h
===================================================================
RCS file: /cvsroot/toon/TooN/TooN.h,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -b -r1.24 -r1.25
--- TooN.h 6 Mar 2009 15:34:33 -0000 1.24
+++ TooN.h 25 Mar 2009 19:46:08 -0000 1.25
@@ -3,6 +3,7 @@
#define TOON_INCLUDE_TOON_H
#include <iostream>
#include <cstdlib>
+#include <limits>
#include <TooN/internal/config.hh>
#include <TooN/internal/typeof.hh>
@@ -21,6 +22,14 @@
}
#endif
+ //Is the number a field? ie, *, -, *, / defined.
+ //Specialize this to make TooN work properly with new types
+ using std::numeric_limits;
+ template<class C> struct IsField
+ {
+ static const int value = numeric_limits<C>::is_specialized;
+ };
+
namespace Internal
{
static const unsigned int max_bytes_on_stack=1000;
Index: internal/operators.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/operators.hh,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -b -r1.21 -r1.22
--- internal/operators.hh 25 Mar 2009 17:51:34 -0000 1.21
+++ internal/operators.hh 25 Mar 2009 19:46:10 -0000 1.22
@@ -117,11 +117,26 @@
template<class C> C gettype();
+ template<class L, class R> struct Field
+ {
+ static const int is = IsField<L>::value && IsField<R>::value;
+ };
+
//Automatic type deduction of return types
- template<class L, class R> struct AddType { typedef
TOON_TYPEOF(gettype<L>()+gettype<R>()) type;};
- template<class L, class R> struct SubtractType { typedef
TOON_TYPEOF(gettype<L>()-gettype<R>()) type;};
- template<class L, class R> struct MultiplyType { typedef
TOON_TYPEOF(gettype<L>()*gettype<R>()) type;};
- template<class L, class R> struct DivideType { typedef
TOON_TYPEOF(gettype<L>()/gettype<R>()) type;};
+
+ //We have to use the traits here because it is not possible to
+ //check for the existence of a valid operator *, especially
+ //in the presence of builtin operators. Therefore, the type is
+ //only deduced if both of the input types are fields.
+ template<class L, class R, int F = Field<L,R>::is> struct AddType
{ typedef TOON_TYPEOF(gettype<L>()+gettype<R>()) type;};
+ template<class L, class R, int F = Field<L,R>::is> struct SubtractType
{ typedef TOON_TYPEOF(gettype<L>()-gettype<R>()) type;};
+ template<class L, class R, int F = Field<L,R>::is> struct MultiplyType
{ typedef TOON_TYPEOF(gettype<L>()*gettype<R>()) type;};
+ template<class L, class R, int F = Field<L,R>::is> struct DivideType
{ typedef TOON_TYPEOF(gettype<L>()/gettype<R>()) type;};
+
+ template<class L, class R> struct AddType<L, R, 0> { typedef
void type;};
+ template<class L, class R> struct SubtractType<L, R, 0> { typedef
void type;};
+ template<class L, class R> struct MultiplyType<L, R, 0> { typedef
void type;};
+ template<class L, class R> struct DivideType<L, R, 0> { typedef
void type;};
//Output size, given input size. Be static if possible.
template<int i, int j> struct Sizer{static const int size=i;};
Index: test/SXX_test.cc
===================================================================
RCS file: /cvsroot/toon/TooN/test/SXX_test.cc,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- test/SXX_test.cc 25 Mar 2009 17:41:09 -0000 1.1
+++ test/SXX_test.cc 25 Mar 2009 19:46:11 -0000 1.2
@@ -4,7 +4,7 @@
#include <TooN/so2.h>
-int main(int, char *){
+int main(int, char* *){
TooN::SO2<> r(M_PI_2);
cout << r << endl;
cout << r.generator() << endl;
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Toon-members] TooN TooN.h internal/operators.hh test/SXX_test.cc,
Edward Rosten <=