00001 #ifndef _READONLY_H
00002 #define _READONLY_H
00003
00004 #if CMK_HAS_TYPEINFO
00005 # include <typeinfo>
00006 #endif
00007
00008 #include <stdlib.h>
00009 #include "charm.h"
00010
00011 extern bool _mainDone;
00012
00013 class readonlybase {
00014 private:
00015
00016 void *operator new(size_t sz) { return malloc(sz);}
00017
00018
00019 readonlybase(const readonlybase &);
00020 readonlybase &operator=(const readonlybase &);
00021 public:
00022 readonlybase() {}
00023 ~readonlybase() { }
00024 };
00025
00026
00027 template <class dtype> class readonly : public readonlybase
00028 {
00029 private:
00030 dtype data;
00031
00032 public:
00033 readonly()
00034 {
00035 CkRegisterReadonly(
00036 #if 0
00037 typeid(*this).name(),typeid(dtype).name()
00038 #else
00039 "readonly<>","unknown"
00040 #endif
00041 ,sizeof(dtype), (void*)&data,NULL);
00042 }
00043 readonly<dtype>& operator=(dtype d)
00044 {
00045 if(_mainDone)
00046 CkAbort("Cannot set readonly variables after main::main\n");
00047 data = d;
00048 return *this;
00049 }
00050 operator dtype() { return data; }
00051 };
00052
00053 template <class dtype> class _roatom : public readonlybase
00054 {
00055 private:
00056 dtype data;
00057 public:
00058 _roatom<dtype>& operator=(dtype d)
00059 {
00060 if(_mainDone)
00061 CkAbort("Cannot set readonly variables after main::main\n");
00062 data = d;
00063 return *this;
00064 }
00065 operator dtype() { return data; }
00066 };
00067
00068 template <class dtype, int len> class roarray : public readonlybase
00069 {
00070 private:
00071 _roatom<dtype> data[len];
00072 public:
00073 roarray()
00074 {
00075 CkRegisterReadonly(
00076 #if 0
00077 typeid(*this).name(),typeid(data).name()
00078 #else
00079 "roarray<>","unknown"
00080 #endif
00081 ,len*sizeof(_roatom<dtype>), (void*)&data[0],NULL);
00082 }
00083 _roatom<dtype> &operator[](int idx) {
00084 if(idx <0 || idx>=len)
00085 CkAbort("Readonly array access bounds violation.\n");
00086 return data[idx];
00087 }
00088 };
00089
00090 template <class dtype> class romsg : public readonlybase
00091 {
00092 private:
00093 dtype *msg;
00094 public:
00095 romsg()
00096 {
00097 CkRegisterReadonlyMsg(
00098 #if 0
00099 typeid(*this).name(),typeid(dtype).name()
00100 #else
00101 "romsg<>","unknown"
00102 #endif
00103 ,(void**) &msg);
00104 }
00105 romsg<dtype>& operator=(dtype *d)
00106 {
00107 if(_mainDone)
00108 CkAbort("Cannot set readonly variables after main::main\n");
00109 msg = d;
00110 return *this;
00111 }
00112 operator dtype*() { return msg; }
00113 dtype& operator*() { return *msg; }
00114 dtype* operator->() { return msg; }
00115 };
00116
00117 #endif