Back to index

texmacs  1.0.7.15
resource.hpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : resource.hpp
00004 * COPYRIGHT  : (C) 1999  Joris van der Hoeven
00005 *******************************************************************************
00006 * This software falls under the GNU general public license version 3 or later.
00007 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00008 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00009 ******************************************************************************/
00010 
00011 #ifndef RESOURCE_H
00012 #define RESOURCE_H
00013 #include "string.hpp"
00014 #include "hashmap.hpp"
00015 
00016 template<class T> struct rep {
00017   string res_name;
00018   inline rep<T> (string res_name2):res_name (res_name2) {
00019     T::instances (res_name)= static_cast<pointer>(this); }
00020   inline virtual ~rep<T> () {
00021     T::instances -> reset (res_name); }
00022 };
00023 
00024 template<class R> class resource_ptr {
00025 protected:
00026   ~resource_ptr() {};
00027 public:
00028   R* rep;
00029   static hashmap<string,pointer> instances;
00030   inline R* operator ->()  { return rep; }
00031 };
00032 
00033 #ifdef OS_WIN32
00034 #define RESOURCE(PTR)                               \
00035 struct PTR##_rep;                                    \
00036 struct PTR : public resource_ptr<PTR##_rep> {       \
00037   inline PTR (PTR##_rep* rep2= NULL) { rep=rep2; }  \
00038   inline PTR (string s) { rep=(PTR##_rep*) instances [s]; } \
00039   inline ~PTR() {}                                  \
00040 }
00041 #else
00042 #define RESOURCE(PTR)                               \
00043 struct PTR##_rep;                                    \
00044 template<> hashmap<string,pointer> resource_ptr<PTR##_rep>::instances; \
00045 struct PTR : public resource_ptr<PTR##_rep> {       \
00046   inline PTR (PTR##_rep* rep2= NULL) { rep=rep2; }  \
00047   inline PTR (string s) { rep=(PTR##_rep*) instances [s]; } \
00048   inline ~PTR() {}                                  \
00049 }
00050 #endif
00051 
00052 #ifdef OS_WIN32
00053 #define RESOURCE_CODE(PTR) \
00054 hashmap<string,pointer> resource_ptr<PTR##_rep>::instances (NULL); 
00055 #else
00056 #define RESOURCE_CODE(PTR) \
00057 template<> hashmap<string,pointer> resource_ptr<PTR##_rep>::instances (NULL);
00058 #endif
00059 
00060 template<class R>
00061 inline bool is_nil (const resource_ptr<R>& res) { return res.rep == NULL; }
00062 
00063 template<class R>
00064 tm_ostream& operator << (tm_ostream& out, const resource_ptr<R>& t);
00065 
00066 #define make(T,s,im) ((T::instances -> contains (s))? T(s): T(im))
00067 
00068 template<class T>
00069 tm_ostream& operator << (tm_ostream& out, const resource_ptr<T>& t) {
00070   return out << t->res_name;
00071 }
00072 
00073 #endif // RESOURCE_H