![]() |
libsesstype
2.0.0
Library for Session Types programming.
|
00001 00005 #ifndef SESSTYPE__MODULE_H__ 00006 #define SESSTYPE__MODULE_H__ 00007 00008 #ifdef __cplusplus 00009 #include <string> 00010 #include <iterator> 00011 #include <unordered_map> 00012 #endif 00013 00014 #include "sesstype/import.h" 00015 #include "sesstype/session.h" 00016 00017 #ifdef __cplusplus 00018 00021 namespace sesstype { 00022 #endif 00023 00024 #ifdef __cplusplus 00025 00028 template <class ImportType, class SessionType> 00029 class ModuleTmpl { 00030 std::string name_; 00031 // Pair<Import * theimport, bool is_alias> 00032 std::unordered_map<std::string, std::pair<ImportType *, bool>> imports_; 00033 std::unordered_map<std::string, SessionType *> sessions_; 00034 00035 public: 00036 using ImportContainer = std::unordered_map<std::string, std::pair<ImportType *, bool>>; 00037 using SessionContainer = std::unordered_map<std::string, SessionType *>; 00038 00040 ModuleTmpl() : name_("default"), imports_(), sessions_() { } 00041 00043 ModuleTmpl(std::string name) : name_(name), imports_(), sessions_() { } 00044 00046 virtual ~ModuleTmpl() 00047 { 00048 for (auto import_pair : imports_) { 00049 if (!import_pair.second.second /*alias?*/) { 00050 delete import_pair.second.first; 00051 } 00052 } 00053 } 00054 00056 std::string name() const 00057 { 00058 return name_; 00059 } 00060 00063 void set_name(std::string name) 00064 { 00065 name_ = name; 00066 } 00067 00069 void add_session(SessionType *session) 00070 { 00071 sessions_.insert({ session->name(), session }); 00072 } 00073 00075 unsigned int num_sessions() const 00076 { 00077 return sessions_.size(); 00078 } 00079 00082 bool has_session(std::string name) const 00083 { 00084 return (sessions_.find(name) != sessions_.end()); 00085 } 00086 00089 SessionType *session(std::string name) const 00090 { 00091 return sessions_.at(name); 00092 } 00093 00094 typename SessionContainer::const_iterator session_begin() const 00095 { 00096 return sessions_.begin(); 00097 } 00098 00099 typename SessionContainer::const_iterator session_end() const 00100 { 00101 return sessions_.end(); 00102 } 00103 00105 void add_import(ImportType *import) 00106 { 00107 imports_.insert({ import->name(), std::make_pair(import, false)}); 00108 if (import->as() != "") { 00109 imports_.insert({ import->as(), std::make_pair(import, true/*alias*/)}); 00110 } 00111 } 00112 00113 unsigned int num_imports() const 00114 { 00115 return imports_.size(); 00116 } 00117 00120 bool has_import(std::string name) const 00121 { 00122 return (imports_.find(name) != imports_.end()); 00123 } 00124 00128 ImportType *import(std::string name) const 00129 { 00130 return imports_.at(name).first; 00131 } 00132 00133 typename ImportContainer::const_iterator import_begin() const 00134 { 00135 return imports_.begin(); 00136 } 00137 00138 typename ImportContainer::const_iterator import_end() const 00139 { 00140 return imports_.end(); 00141 } 00142 }; 00143 00144 using Module = ModuleTmpl<Import, Session>; 00145 #endif // __cplusplus 00146 00147 #ifdef __cplusplus 00148 extern "C" { 00149 #endif 00150 00151 #ifdef __cplusplus 00152 typedef Module st_module; 00153 #else 00154 typedef struct Module st_module; 00155 #endif 00156 00159 st_module *st_module_mk_init(const char *name); 00160 00162 const char *st_module_get_name(st_module *const module); 00163 00167 st_module *st_module_add_tree(st_module *const module, st_tree *tree); 00168 00169 st_tree *st_module_get_tree(st_module *const module, const char *name); 00170 00171 st_tree *st_module_get_tree_at_idx(st_module *const module, unsigned int index); 00172 00176 st_module *st_module_import(st_module *const module, st_import *import); 00177 00179 void st_module_free(st_module *module); 00180 00181 #ifdef __cplusplus 00182 } // extern "C" 00183 #endif 00184 00185 #ifdef __cplusplus 00186 } // namespace sesstype 00187 #endif 00188 00189 #endif//SESSTYPE__MODULE_H__