ROOT_Application  2.0
C++ Core modules and GUIStock
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Controller.h
Go to the documentation of this file.
1 
26 #ifndef _CONTROLLER_BASE_H_
27 #define _CONTROLLER_BASE_H_
28 
29 #include <iostream>
30 #include <list>
31 #include <vector>
32 
41 {
42  private :
46  std::string name;
47 
48  public :
50 #ifdef DEBUG_CONT_FRAME
51  std::cout << "ControllerBase constructor" << std::endl;
52 #endif
53  }
54  ControllerBase( ControllerBase *parent, std::string cont_name ) : fparent_controller( parent ), name ( cont_name ) {
55 #ifdef DEBUG_CONT_FRAME
56  std::cout << "ControllerBase constructor with parent " << parent << std::endl;
57 #endif
58  }
59  virtual ~ControllerBase() {
60 #ifdef DEBUG_CONT_FRAME
61  std::cout << "controller base destructor " << std::endl;
62 #endif
64  }
65 
66  std::string GetName() const {return name;}
67  // problem accessing from MainControllerIndicator
68  // - can save specific fparent_controller, but will be hardcoded dependent ??
69  // - can try virtual function, overwritten when needed, if base class enought ok
71  //virtual ControllerBase * GetParentController() {return fparent_controller;}
72  // may work, force all to implement, can be in List
73  //virtual ControllerBase * GetParentController() =0;
74 
75  // needed for GUIMainController, has only base class
76  // a very general function to overload
78  virtual void Updated( std::vector<std::string> & ) =0;
79 
80  //common function, here or in ControllerFrame ??
81  //virtual void attach();
82  //virtual void detach();
83 
84  // needed if object do not derive from List, or force to implement in final object ??
85  // but ambiguous ! try overwrite for class without sub-controller
86  // no more ambiguous, but called by all
87  // try pure virtual, ok working,
88  // needed if List calls DeleteList for only BaseClass derived
89  // not best, but working, FORCE to overwrite in final class
90  // Coupling but normal because we want different data types in List
91  //virtual void DeleteList() =0;
92  /* {std::cout << "DeleteList in ControllerBase name " << GetName() << std::endl;} */
93 
94  // not logic DeleteList here and in ControllerFrame. Only because MainControllerIndicatorTHStock ??
95 };
96 
97 // independent of controller in fact, could be ListComposite ( ListIterative )
103 template <class T>
105 {
106  protected :
108  std::list<T *> list_controller;
110  typedef typename std::list<T *>::iterator it_list;
111 
112  // a current controller useful here, may point to a specific one
113  //ControllerBase * fcurrent_controller;
114 
115  // could have a template for the parent controller
116  // strange coupling between both, or a template class ParentController<T> to derive from...
117  // ControllerBase would become a ControllerView with pure virtual implemented in ParentController ??
118  // still coupling, would need definition of GUIMainController when instantiating the template.
119  // to test
120 
121  public :
123 #ifdef DEBUG_CONT_FRAME
124  std::cout << "list controller constructor" << std::endl;
125 #endif
126  }
127  // proper deletion
128  virtual ~ListController() {
129 #ifdef DEBUG_CONT_FRAME
130  std::cout << "list controller destructor " << std::endl;
131 #endif
132  }
134  void AddController( T * control ) {
135  //std::cout << "ListController::AddController name: " << control->GetName() << std::endl;
136  list_controller.push_back( control );
137  }
138  // could use list.remove instead
139  void RemoveController( T * control ) {
140 #ifdef DEBUG_CONT_FRAME
141  std::cout << "Entry ListController<T>::RemoveController" << std::endl;
142  std::cout << "size Controller " << list_controller.size() << std::endl;
143 #endif
144  for ( it_list it = list_controller.begin(); it != list_controller.end(); it++ ) {
145  if ( (*it) == control ) {
146 #ifdef DEBUG_CONT_FRAME
147  std::cout << "found controller to remove " << std::endl;
148 #endif
149  list_controller.erase( it );
150  return;
151  }
152  }
153  std::cout << "\n\t Cannot remove from the controller \n" << std::endl;
154  }
155  // first test, stupid only the first one should improve, certainly not needed
156  T * GetController( std::string name) { return list_controller.front(); }
158  T * GetFirstController() { return list_controller.front(); }
159 
163  virtual void DeleteList( bool call_delete = true ) {
164 #ifdef DEBUG_CONT_FRAME
165  std::cout << "ListController<T>::DeleteList " << list_controller.size() << std::endl;
166 #endif
167  // delete explicitly all objects before deleting the full list
168  if ( call_delete ) {
169  it_list end = list_controller.end();
170 
171  for ( it_list it = list_controller.begin(); it != end; ++it ) {
172  // works, compile in test, but very dependent of controller !
173  std::cout << "Will DeleteList in ListController " << (*it)->GetName() << std::endl;
175  (*it)->DeleteList();
176  delete (*it);
177  }
178  }
179  // delete the list
180  list_controller.clear();
181  std::cout << "End Controller::DeleteList " << std::endl;
182  }
183  //RemoveController ( name and *T ) // could be recursive functions as well
184  //GetController ( name and *T ?? )
185 
186  virtual unsigned int GetSizeListController() const {return list_controller.size();}
187 };
188 
189 
206 template<class T> // =TypeNull, no needed, can derive from BaseController only
210 {
211  public :
213 #ifdef DEBUG_CONT_FRAME
214  std::cout << "ControllerFrame constructor default" << std::endl;
215 #endif
216  }
217  ControllerFrame( ControllerBase *parent, std::string name = "") :
218  ControllerBase( parent, name ), ListController<T>() {
219 #ifdef DEBUG_CONT_FRAME
220  std::cout << "ControllerFrame constructor" << std::endl;
221 #endif
222  }
223  virtual ~ControllerFrame() {
224 #ifdef DEBUG_CONT_FRAME
225  std::cout << "ControllerFrame destructor" << std::endl;
226 #endif
227  }
228 
229  // can overload GetParentController here ?? we do not have the controller parent...
230  // if store a U parent_controller, yes ControllerFrame<parent_type_controller,child_type_controler>
231  // practical MyController : public ControllerFrame<GUIMainController,IndicatorTHStockFrameController>
232  // with NullType implemented. List maybe valid for a view, ListVieW<THStockFrame>, View has a parent controller by default...
233 
234  // can decide of the association with Observer-Subject for message if it limits coupling ( need of Specific controller *.h file in view )
235 
236  // if pure virtual in base class seems necessary
237  // but not
238  /* virtual void DeleteList() {
239  //ListController<T>::DeleteList();
240  DeleteList();}
241  */
242 };
243 
244 class NullType
245 {
246 };
247 
248 // re-copy all stuffs ?? yes but will save overloading from all final class
249 template<>
251 {
252  public :
254 #ifdef DEBUG_CONT_FRAME
255  std::cout << "ControllerFrame<NullType> constructor default" << std::endl;
256 #endif
257  }
258  ControllerFrame( ControllerBase *parent, std::string name = "") :
259  ControllerBase( parent, name ) {
260 #ifdef DEBUG_CONT_FRAME
261  std::cout << "ControllerFrame<NullType> constructor" << std::endl;
262 #endif
263  }
264  virtual ~ControllerFrame() {
265 #ifdef DEBUG_CONT_FRAME
266  std::cout << "ControllerFrame<NullType> destructor" << std::endl;
267 #endif
268  }
269 
270  // argument or not, seems working, //( bool call_delete = true )
271  virtual void DeleteList( bool call_delete = true ) {
272 #ifdef DEBUG_CONT_FRAME
273  std::cout <<"ControllerFrame<NullType>::DeleteList" << std::endl;
274 #endif
275  }
276 };
277 
278 #endif
279 
T * GetController(std::string name)
Definition: Controller.h:156
std::list< T * > list_controller
list more convenient, remove if not shown ?, or insert
Definition: Controller.h:108
virtual void DeleteList(bool call_delete=true)
Definition: Controller.h:271
ControllerFrame(ControllerBase *parent, std::string name="")
Definition: Controller.h:258
ControllerBase * fparent_controller
parent controller 0 by default
Definition: Controller.h:44
void AddController(T *control)
Common Function for dealing with the list, need a name, may call other controllers...
Definition: Controller.h:134
virtual void DeleteList(bool call_delete=true)
Iterative call for deleting child controller.
Definition: Controller.h:163
virtual unsigned int GetSizeListController() const
Definition: Controller.h:186
std::list< T * >::iterator it_list
useful typedef for the class ( and derivatives !)
Definition: Controller.h:110
virtual void Updated(std::vector< std::string > &)=0
virtual ~ListController()
Definition: Controller.h:128
T * GetFirstController()
get the first controller in list, useful after a delete
Definition: Controller.h:158
std::string GetName() const
Definition: Controller.h:66
ControllerBase * GetParentController()
Definition: Controller.h:70
virtual ~ControllerFrame()
Definition: Controller.h:223
void RemoveController(T *control)
Definition: Controller.h:139
ControllerFrame(ControllerBase *parent, std::string name="")
Definition: Controller.h:217
ControllerBase(ControllerBase *parent, std::string cont_name)
Definition: Controller.h:54
see remark below, much easier if composition than derivation in the case of ListController, same for parent_controller as member <U> easily extends to View as well ( ListView possible ), but strong coupling again
Definition: Controller.h:209
virtual ~ControllerBase()
Definition: Controller.h:59
std::string name
name Controller_X, initialized to "" by default
Definition: Controller.h:46