00001
00002
00003 #ifndef NAMESPACE_CONTEXT_H_
00004 #define NAMESPACE_CONTEXT_H_
00005
00006 #include <unordered_set>
00007
00008 #include "hash.h"
00009 #include <set>
00010
00011 using namespace std;
00012
00013 #include <boost/serialization/vector.hpp>
00014 #include <boost/serialization/map.hpp>
00015 #include <boost/serialization/list.hpp>
00016 #include <boost/serialization/string.hpp>
00017 #include <boost/serialization/version.hpp>
00018 #include <boost/serialization/split_member.hpp>
00019 #include <boost/serialization/shared_ptr.hpp>
00020 #include <boost/serialization/base_object.hpp>
00021 #include <boost/serialization/export.hpp>
00022 #include <boost/serialization/set.hpp>
00023
00024 namespace il {
00025
00026
00027
00028
00029 class _namespace_context;
00030
00035 class _namespace_context {
00036 friend class namespace_context;
00037 friend class boost::serialization::access;
00038 template<class Archive>
00039 void save(Archive & ar, const unsigned int version) const
00040 {
00041 ar & name;
00042 ar & context;
00043 }
00044
00045 template<class Archive>
00046 void load(Archive & ar, const unsigned int version)
00047 {
00048 ar & name;
00049 ar & context;
00050 context = _namespace_context::get_instance(context);
00051
00052 if(name == "") assert(context == NULL);
00053 if(context != NULL) assert(context->name != "");
00054
00055 compute_hash();
00056 }
00057
00058 BOOST_SERIALIZATION_SPLIT_MEMBER()
00059
00060 bool is_empty();
00061 public:
00062 static void delete_unused_instances();
00063
00064 private:
00065 string name;
00066 _namespace_context *context;
00067 size_t hash_c;
00068 struct nc_hash {
00069 size_t operator()(_namespace_context *ns) const;
00070 };
00071
00072 struct nc_eq {
00073 bool operator()(_namespace_context *a, _namespace_context *b) const;
00074 };
00075 static unordered_set<_namespace_context *, nc_hash, nc_eq> instances;
00076
00077 static _namespace_context* get_instance(_namespace_context* ns);
00078 static _namespace_context *make(const string &name, _namespace_context *context);
00079
00080 string to_string() const;
00081 bool operator==(const _namespace_context &rhs);
00082 inline size_t get_hash_code() {return hash_c;}
00083 _namespace_context(const string &aname, _namespace_context *acontext);
00084 _namespace_context();
00085 ~_namespace_context();
00086 void compute_hash();
00087
00088
00089
00090 static unordered_set<_namespace_context*> unused_instances;
00091
00092
00093 };
00094
00095
00096
00097 class namespace_context
00098 {
00099 static bool _init;
00100 static bool init();
00101 friend class boost::serialization::access;
00102 template<class Archive>
00103 void save(Archive & ar, const unsigned int version) const
00104 {
00105 ar & ns;
00106 }
00107
00108 template<class Archive>
00109 void load(Archive & ar, const unsigned int version)
00110 {
00111 ar & ns;
00112 ns = _namespace_context::get_instance(ns);
00113 }
00114
00115
00116
00117 BOOST_SERIALIZATION_SPLIT_MEMBER()
00118 private:
00119 _namespace_context* ns;
00120
00121
00122
00123
00124
00125 public:
00126 namespace_context(_namespace_context* ns);
00127 namespace_context();
00128 namespace_context(const namespace_context & ns);
00129 namespace_context(const string &name, const namespace_context &context);
00130 namespace_context(const string &name);
00131 string to_string() const;
00132 bool operator==(const namespace_context &rhs) const;
00133 bool operator!=(const namespace_context& other) const;
00134 bool operator<(const namespace_context& other) const;
00135 bool operator>(const namespace_context& other) const;
00136 const string& get_name();
00137 namespace_context get_context();
00138 size_t get_hash_code();
00139 void* get_ns() const;
00140
00145 namespace_context get_toplevel_namespace();
00146
00147
00148 };
00149
00150
00151
00152 }
00153
00154 #endif