Skip to content

Commit

Permalink
converting types to int with templates and without dummy in globals.h
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderRothkegel committed Jun 4, 2012
1 parent 7cbc653 commit 8dd4064
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
11 changes: 0 additions & 11 deletions globals.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
#include "globals.h"

int typeInteger(int) { return 0;}
int typeInteger(double) { return 1;}
int typeInteger(string) { return 2;}
#ifndef PYTHON
int typeInteger(bool) { return 3;}
#else
int typeInteger(bool) { return 0;}
#endif
int typeInteger(float) { return 4;}
int typeInteger(long double) { return 5;}

map <string, void*> globals::value;
map <string, int> globals::type;
38 changes: 27 additions & 11 deletions globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,28 @@

using namespace std;

int typeInteger(int);
int typeInteger(double);
int typeInteger(string);
int typeInteger(bool);
int typeInteger(float);
int typeInteger(long double);

template <typename T>
struct typeInteger
{
// enum { result = 666; }
};



template<> struct typeInteger<int> { enum { result = 0 }; };
template<> struct typeInteger<double> { enum { result = 1 }; };
template<> struct typeInteger<string> { enum { result = 2 }; };
#ifndef PYTHON
template<> struct typeInteger<bool> { enum { result = 3 }; };
#else
template<> struct typeInteger<bool> { enum { result = 0 }; };
#endif
template<> struct typeInteger<float> { enum { result = 4 }; };
template<> struct typeInteger<long double> { enum { result = 5 }; };





// Maps of Types to ints for identification and to strings for error texts.
Expand Down Expand Up @@ -61,7 +77,7 @@ class globals

template <typename T> static void registerGlobal(string name, T v)
{
type[name] = typeInteger(v);
type[name] = typeInteger<T>::result;
value[name] = new T (v);
#ifndef PYTHON
command::declare(name, (T*)value[name]);
Expand All @@ -70,7 +86,7 @@ class globals

template <typename T> static T getGlobal(string name)
{
T dummy; int typeInt = typeInteger(dummy);
int typeInt = typeInteger <T>::result;

if (type.count(name) > 0)
{
Expand All @@ -90,7 +106,7 @@ class globals

template <typename T> static T* getPointerToGlobal(string name)
{
T dummy; int typeInt = typeInteger(dummy);
int typeInt = typeInteger <T>::result;

if (type.count(name) > 0)
{
Expand All @@ -108,12 +124,12 @@ class globals
{
if (type.count(name) > 0)
{
if (type [name] == typeInteger(d))
if (type [name] == typeInteger <T>::result)
* ((T*)value[name]) = d;
else
{
stringstream fehler;
fehler << "Type mismatch: " << name << " is of type " << typeString(type[name]) << ". But you try to set it as a " << typeString(typeInteger(d)) << endl;;
fehler << "Type mismatch: " << name << " is of type " << typeString(type[name]) << ". But you try to set it as a " << typeString(typeInteger<T>::result) << endl;;
throw fehler.str().c_str();
}
}
Expand Down

0 comments on commit 8dd4064

Please sign in to comment.