libsesstype  2.0.0
Library for Session Types programming.
print.h
00001 #ifndef SESSTYPE__UTIL__PRINT_H__
00002 #define SESSTYPE__UTIL__PRINT_H__
00003 
00004 #ifdef __cplusplus
00005 #include <iostream>
00006 #include <string>
00007 #endif
00008 
00009 #include "sesstype/node.h"
00010 #include "sesstype/node/block.h"
00011 #include "sesstype/node/interaction.h"
00012 #include "sesstype/node/choice.h"
00013 #include "sesstype/node/recur.h"
00014 #include "sesstype/node/continue.h"
00015 #include "sesstype/node/par.h"
00016 #include "sesstype/node/nested.h"
00017 #include "sesstype/node/interruptible.h"
00018 
00019 #include "sesstype/util/role_visitor.h"
00020 #include "sesstype/util/node_visitor.h"
00021 
00022 #ifdef __cplusplus
00023 namespace sesstype {
00024 namespace util {
00025 #endif
00026 
00027 #ifdef __cplusplus
00028 
00031 class Print : public NodeVisitor, public RoleVisitor {
00032     std::ostream &os_;
00033     unsigned int indent_lvl_;
00034     std::string indent_str_;
00035     unsigned int line_count_;
00036 
00037   public:
00039     Print()
00040         : os_(std::cout), indent_lvl_(0), indent_str_("  "), line_count_(1) { }
00041 
00044     Print(std::ostream &os)
00045         : os_(os), indent_lvl_(0), indent_str_("  "), line_count_(1) { }
00046 
00048     void prefix()
00049     {
00050         if (line_count_ == 1) {
00051             os_ << "Line\t |\tTree\n====================\n";
00052         }
00053         os_ << line_count_++ << "\t ";
00054         if (indent_lvl_ > 1) {
00055             os_ << '|';
00056             for (unsigned int i=1; i<indent_lvl_; i++) {
00057                 os_ << indent_str_;
00058             }
00059         }
00060     }
00061 
00062     void reset_line_num()
00063     {
00064         indent_lvl_ = 0;
00065         line_count_ = 1;
00066     }
00067 
00068     void visit(Node *node)
00069     {
00070         prefix();
00071         os_ << "generic {} @ " << node << "\n";
00072     }
00073 
00074     void visit(InteractionNode *node)
00075     {
00076         prefix();
00077         os_ << "interaction { from: ";
00078         if (node->sndr()) {
00079             node->sndr()->accept(*this);
00080         } else {
00081             os_ << "(empty)";
00082         }
00083         os_ << ", to(" << node->num_rcvrs() << "): [";
00084         if (node->num_rcvrs() > 0) {
00085             node->rcvr()->accept(*this);
00086         } else {
00087             os_ << "(empty)";
00088         }
00089         os_ << (node->num_rcvrs()>1 ? ".. ]" : "]")
00090             << ", msg: " << node->msg()->label()
00091             << "(" << node->msg()->num_payloads() << ") } @" << node << "\n";
00092     }
00093 
00094     void visit(BlockNode *node)
00095     {
00096         indent_lvl_++;
00097         for (auto it=node->child_begin(); it!=node->child_end(); it++) {
00098             (*it)->accept(*this);
00099         }
00100         indent_lvl_--;
00101     }
00102 
00103     void visit(RecurNode *node)
00104     {
00105         prefix();
00106         os_ << "recur " << "{ label: " << node->label() << " }";
00107         os_ << " children: " << node->num_children() << " @" << node << "\n";
00108 
00109         node->BlockNodeTmpl<Node, Role, MsgSig, util::NodeVisitor>::accept(*this);
00110     }
00111 
00112     void visit(ContinueNode *node)
00113     {
00114         prefix();
00115         os_ << "cont { label: " << node->label() << " } @" << node << "\n";
00116     }
00117 
00118     void visit(ChoiceNode *node)
00119     {
00120         prefix();
00121         os_ << "choice { at: " << node->at()->name() << " }";
00122         os_ << " children: " << node->num_children() << " @" << node << "\n";
00123 
00124         node->BlockNodeTmpl<Node, Role, MsgSig, util::NodeVisitor>::accept(*this);
00125     }
00126 
00127     void visit(ParNode *node)
00128     {
00129         prefix();
00130         os_ << "par {}";
00131         os_ << " parblocks:children: " << node->num_children() << " @" << node << "\n";
00132 
00133         node->BlockNodeTmpl<Node, Role, MsgSig, util::NodeVisitor>::accept(*this);
00134     }
00135 
00136     void visit(NestedNode *node)
00137     {
00138         prefix();
00139         os_ << "nested { name: " << node->name();
00140         os_ << ", scope_name: " << node->scope();
00141         os_ << ", arg("<< node->num_roleargs() <<"): <";
00142         for (auto it=node->arg_begin(); it!=node->arg_end(); it++) {
00143             os_ << (*it)->label() << "(" << (*it)->num_payloads() << ")" << ", ";
00144         }
00145         os_ << ">";
00146         os_ << ", rolearg("<< node->num_args() <<"): [";
00147         for (auto it=node->rolearg_begin(); it!=node->rolearg_end(); it++) {
00148             (*it)->accept(*this);
00149             os_ << ", ";
00150         }
00151         os_ << "]} @ " << node << "\n";
00152     }
00153 
00154     void visit(InterruptibleNode *node)
00155     {
00156         prefix();
00157         os_ << "interruptible { scope: " << node->scope();
00158         os_ << " interrupts(" << node->num_interrupts() << "): ";
00159         for (auto it=node->interrupt_begin(); it!=node->interrupt_end(); it++) {
00160             (*it).first->accept(*this);
00161             os_ << "/" << (*it).second->label() << ", ";
00162         }
00163         os_ << ", catches(" << node->num_catches() << "): ";
00164         for (auto it=node->catch_begin(); it!=node->catch_end(); it++) {
00165             (*it).first->accept(*this);
00166             os_ << "/" << (*it).second->label() << ", ";
00167         }
00168         os_ << ", throws(" << node->num_throws() << "): ";
00169         for (auto it=node->throw_begin(); it!=node->throw_end(); it++) {
00170             (*it).first->accept(*this);
00171             os_ << "/" << (*it).second->label() << ", ";
00172         }
00173         os_ <<"} @ " << node << "\n";
00174     }
00175 
00176     void visit(Role *role)
00177     {
00178         os_ << role->name();
00179         os_ << "@" << role;
00180     }
00181 };
00182 #endif // __cplusplus
00183 
00184 #ifdef __cplusplus
00185 } // namespace util
00186 } // namespace sesstype
00187 #endif
00188 
00189 #endif//SESSTYPE__UTIL__PRINT_H__
 All Classes Namespaces Files Functions