FXCodeToPythonCode.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                    Converts a code location into a number                     *
00004 *                                                                               *
00005 *********************************************************************************
00006 *        Copyright (C) 2003 by Niall Douglas.   All Rights Reserved.            *
00007 *       NOTE THAT I DO NOT PERMIT ANY OF MY CODE TO BE PROMOTED TO THE GPL      *
00008 *********************************************************************************
00009 * This code is free software; you can redistribute it and/or modify it under    *
00010 * the terms of the GNU Library General Public License v2.1 as published by the  *
00011 * Free Software Foundation EXCEPT that clause 3 does not apply ie; you may not  *
00012 * "upgrade" this code to the GPL without my prior written permission.           *
00013 * Please consult the file "License_Addendum2.txt" accompanying this file.       *
00014 *                                                                               *
00015 * This code is distributed in the hope that it will be useful,                  *
00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                          *
00018 *********************************************************************************
00019 * $Id:                                                                          *
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 // Disable warning about initialising bases off this
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 /*template<typename base, int no> struct sortFuncVector : public base
00161 {
00162     typedef Generic::TL::create<FXint, const FXListItem *, const FXListItem *>::value functionSpec;
00163     static FXint function(const FXListItem *a, const FXListItem *b)
00164     {
00165         return extract<FXint>(getParent()->lookupVector(no)(a, b));
00166     }
00167 };
00168 */
00169 
00170 #ifdef _MSC_VER
00171 #pragma warning(default: 4355)
00172 #endif
00173 
00174 }
00175 
00176 #endif

(C) 2002-2009 Niall Douglas. Some parts (C) to assorted authors.
Generated on Fri Nov 20 18:31:19 2009 for TnFOX by doxygen v1.4.7