00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef FXCODETOPYTHONCODE_H
00023 #define FXCODETOPYTHONCODE_H
00024 #include "FXPython.h"
00025 #include "../include/qmemarray.h"
00026 #include "../include/FXException.h"
00027
00028 namespace FX {
00029
00030 #ifdef _MSC_VER
00031
00032 #pragma warning(disable: 4355)
00033 #endif
00034
00079 template<template<typename, int> class vector, int length> class FXCodeToPythonCode
00080 {
00081 struct vectorBase
00082 {
00083 static FXCodeToPythonCode *&getParent()
00084 {
00085 static FXCodeToPythonCode *parent;
00086 return parent;
00087 }
00088 };
00089 typedef typename Generic::FnFromList<typename vector<vectorBase, 0>::functionSpec>::value vectorSpec;
00090 typedef typename Generic::TL::numberRange<length>::value noRange;
00091 struct CodeItem
00092 {
00093 vectorSpec ptr;
00094 boost::python::api::object *code;
00095 CodeItem(vectorSpec _ptr=0) : ptr(_ptr), code(0) { }
00096 };
00097 QMemArray<CodeItem> list;
00098 public:
00099 boost::python::object &lookupVector(int idx)
00100 {
00101 boost::python::object *code=list[idx].code;
00102 assert(code);
00103 return *code;
00104 }
00105 private:
00106 template<typename type> struct CodeAddr;
00107 template<int no> struct CodeAddr<Generic::IntToType<no> > : public vector<vectorBase, no>
00108 {
00109 CodeAddr();
00110 CodeAddr(FXCodeToPythonCode *_parent) { vector<vectorBase, no>::getParent()=_parent; _parent->list[no]=&vector<vectorBase, no>::function; }
00111 };
00112 Generic::TL::instantiateH<noRange, CodeAddr> codevectors;
00113 public:
00115 FXCodeToPythonCode() : list(length), codevectors(this) { }
00117 vectorSpec allocate(boost::python::api::object *code)
00118 {
00119 int n, empty=-1;
00120 for(n=0; n<length; n++)
00121 {
00122 CodeItem &ci=list[n];
00123 if(empty<0 && !ci.code) empty=n;
00124 if(code==ci.code) return ci.ptr;
00125 }
00126 FXERRH(empty>=0, "No more space in list", 0, FXERRH_ISDEBUG);
00127 list[empty].code=code;
00128 return list[empty].ptr;
00129 }
00131 bool deallocate(boost::python::api::object *code)
00132 {
00133 for(int n=0; n<length; n++)
00134 {
00135 CodeItem &ci=list[n];
00136 if(code==ci.code)
00137 {
00138 ci.code=0;
00139 return true;
00140 }
00141 }
00142 return false;
00143 }
00145 bool deallocate(vectorSpec spec)
00146 {
00147 for(int n=0; n<length; n++)
00148 {
00149 CodeItem &ci=list[n];
00150 if(spec==ci.ptr)
00151 {
00152 ci.code=0;
00153 return true;
00154 }
00155 }
00156 return false;
00157 }
00158 };
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 #ifdef _MSC_VER
00171 #pragma warning(default: 4355)
00172 #endif
00173
00174 }
00175
00176 #endif