ClientMarker.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
4 #include <deque>
5 #include <typeindex>
6 #include <unordered_set>
7 #include <vector>
8 
9 #include "../Basics/StringIO.hpp"
10 #include "../Basics/multiline_ostream.hpp"
11 #include "../Basics/LogId.hpp"
12 #include "../DebugView/TreeDebugView.hpp"
13 #include "../DebugView/TableDebugView.hpp"
14 
15 namespace iv
16 {
17 
18 class Instance;
19 
25 class ClientMarker : public Context
26 {
27 public:
28  struct Status{};
29 
30 public:
31 //========================= used by client ========================
32  template< class ClientType >
33  ClientMarker( Instance * inst, ClientType * client, const char * name );
34 
35  template< class ClientType >
36  ClientMarker( Instance * inst, ClientType * client, const char * name, Status );
37 
38  ~ClientMarker();
39 
40  ClientMarker( ClientMarker const & ) = delete;
41  ClientMarker & operator=( ClientMarker const & ) = delete;
42 
47  template< class ... Rest >
48  void inherits( ClientMarker & parent, Rest & ... rest );
49 
54  template< class ... Rest >
55  void owns( ClientMarker & ownee, Rest & ... rest );
56 
57 //========================= used from the outside ====================================
58  //--------- client access ------------
62  std::type_index client_type() const;
63  std::type_index type() const;
64 
67  template< class TypedClient >
68  TypedClient * client() const;
69  Instance * instance() const;
70 
71  //----------------
72  std::string const & name() const;
73  unsigned marker_id() const;
74  std::string name_id() const;
75  std::string root_name_id() const;
76 
77  //------ status -------
78  void print_status( TableDebugView * view ) const;
79  void print_status( TextDebugView * view ) const;
80 
81  void print_status_with_inherited( TextDebugView * view, ClientMarker * emphasize = nullptr ) const;
82  void print_status_with_inherited( TreeDebugView * view, ClientMarker * emphasize = nullptr ) const;
83 
84  void print_status_with_related( TextDebugView * view ) const;
86 
87  //--- CM relations ------
88  ClientMarker const * inheritance_root() const;
89  ClientMarker const * inheritance_child() const;
90  std::unordered_set< ClientMarker const * > inheritance_parents() const;
91 
92  ClientMarker const * relation_root() const;
93  ClientMarker const * ownership_parent() const;
94  std::unordered_set< ClientMarker const * > ownership_children() const;
95 
96 protected:
97  virtual bool log_process_enabled( LogId id ) const override final;
98  virtual void log_process( SrcInfo const & info, LogId id, std::string const & message ) const override final;
99 
100 private:
101  void inherits(){}
102  void owns(){}
103 
104  using StatusPrinterW = void (*)( void * client_ptr, TableDebugView * );
105 
106  template< class ClientType >
107  static void StatusPrinterWImpl( void * client_ptr, TableDebugView * );
108 
109 private:
110  //--- set by ClientMarker::mark
111  Instance * inst;
112  std::string _name;
113  void * _client_ptr;
114  std::type_index _client_type;
115  unsigned _marker_id;
116 
117  StatusPrinterW status_printer_w;
118 
119  //---- relations
120  std::unordered_set< ClientMarker const * > inh_parents;
121  ClientMarker const * inh_child;
122 
123  std::unordered_set< ClientMarker const * > own_children;
124  ClientMarker const * own_parent;
125 };
126 
131 {
132 public:
133  static unsigned UniqueId( std::string name );
134 
135 private:
136  static std::unordered_map< std::string, unsigned > * Index;
137 };
138 
139 //--------------- StringIO -------------------------
140 template<>
142 {
143  /*
144  std::nullptr_t Read( const char * source, Context const * context ) const
145  {
146  return nullptr;
147  }*/
148 
149  std::string Write( ClientMarker const & cm, Context const * context ) const
150  {
151  return cm.root_name_id();
152  }
153 };
154 
155 template<>
156 struct StringIO< ClientMarker const * >
157 {
158  ClientMarker const * Read( const char * source, Context const * context ) const
159  {
160  return nullptr;
161  }
162 
163  std::string Write( ClientMarker const * const & cm, Context const * context ) const
164  {
165  return cm->root_name_id();
166  }
167 };
168 
169 }
170 
171 #include "../Basics/StringIO_defs.hpp"
172 #include "Instance.hpp"
173 #include "ClientMarker.inl"