libsesstype  2.0.0
Library for Session Types programming.
session.h
Go to the documentation of this file.
00001 
00006 #ifndef SESSTYPE__SESSION_H__
00007 #define SESSTYPE__SESSION_H__
00008 
00009 #ifdef __cplusplus
00010 #include <string>
00011 #include <iterator>
00012 #include <unordered_map>
00013 #endif
00014 
00015 #include <sesstype/import.h>
00016 #include <sesstype/node.h>
00017 #include <sesstype/role.h>
00018 
00019 #ifdef __cplusplus
00020 namespace sesstype {
00021 #endif
00022 
00023 #define ST_TYPE_GLOBAL 1
00024 #define ST_TYPE_LOCAL  2
00025 
00026 #ifdef __cplusplus
00027 
00034 template <class BaseNode, class RoleType>
00035 class SessionTmpl {
00036     std::string name_;
00037     int type_;
00038     RoleType *me_; // Localised role (only used in endpoint session)
00039     BaseNode *root_;
00040     std::unordered_map<std::string, RoleType *> roles_;
00041 
00042   public:
00043     using RoleContainer = std::unordered_map<std::string, RoleType *>;
00044 
00046     SessionTmpl()
00047         : name_("default"), type_(ST_TYPE_GLOBAL), me_(), root_(0), roles_() { }
00048 
00051     SessionTmpl(std::string name)
00052         : name_(name), type_(ST_TYPE_GLOBAL), me_(), root_(0), roles_() { }
00053 
00055     virtual ~SessionTmpl()
00056     {
00057         {
00058             for (auto role_pair : roles_) {
00059                 delete role_pair.second;
00060             }
00061             if (root_ != NULL) {
00062                 delete root_;
00063             }
00064         }
00065     }
00066 
00068     std::string name() const
00069     {
00070         return name_;
00071     }
00072 
00074     void set_root(BaseNode *root)
00075     {
00076         if (root_ != NULL) {
00077             delete root_;
00078         }
00079         root_ = root;
00080     }
00081 
00083     BaseNode *root() const
00084     {
00085         return root_;
00086     }
00087 
00089     RoleType *endpoint() const
00090     {
00091         return me_;
00092     }
00093 
00095     void set_endpoint(RoleType *endpoint)
00096     {
00097         me_ = endpoint;
00098         type_ = ST_TYPE_LOCAL;
00099     }
00100 
00103     void project(Role *role)
00104     {
00105 //        util::Projection projection_visitor(role);
00106 //        root_->accept(projection_visitor);
00107     }
00108 
00110     int type() const
00111     {
00112         return type_;
00113     }
00114 
00117     void add_role(RoleType *role)
00118     {
00119         roles_.insert(std::pair<std::string, RoleType *>(role->name(), role));
00120     }
00121 
00125     bool has_role(RoleType *role) const
00126     {
00127         return (roles_.find(role->name()) != roles_.end());
00128     }
00129 
00133     bool has_role(std::string name) const
00134     {
00135         return (roles_.find(name) != roles_.end());
00136     }
00137 
00142     RoleType *role(std::string name) const
00143     {
00144         return roles_.at(name);
00145     }
00146 
00147     unsigned int num_roles() const
00148     {
00149         return roles_.size();
00150     }
00151 
00152     typename RoleContainer::const_iterator role_begin() const
00153     {
00154         return roles_.begin();
00155     }
00156 
00157     typename RoleContainer::const_iterator role_end() const
00158     {
00159         return roles_.end();
00160     }
00161 };
00162 
00163 using Session = SessionTmpl<Node, Role>;
00164 #endif
00165 
00166 #ifdef __cplusplus
00167 extern "C" {
00168 #endif
00169 
00170 #ifdef __cplusplus
00171 typedef Session st_tree;
00172 #else
00173 typedef struct Session st_tree;
00174 #endif
00175 
00178 st_tree *st_tree_mk_init(const char *name);
00179 
00183 st_tree *st_tree_add_role(st_tree *tree, st_role *role);
00184 
00188 st_tree *st_tree_set_root(st_tree *tree, st_node *root);
00189 
00192 st_node *st_tree_get_root(st_tree *tree);
00193 
00195 void st_tree_free(st_tree *tree);
00196 
00197 #ifdef __cplusplus
00198 } // extern "C"
00199 #endif
00200 
00201 #ifdef __cplusplus
00202 } // namespace sesstype
00203 #endif
00204 
00205 #endif//SESSTYPE__SESSION_H__
 All Classes Namespaces Files Functions