Scopira  20080306
model.h
1 
2 /*
3  * Copyright (c) 2005 National Research Council
4  *
5  * All rights reserved.
6  *
7  * This material is confidential and proprietary information of
8  * National Research Council Canada ("Confidential Information").
9  * This Confidential Information may only be used and reproduced
10  * in accordance with the terms of the license agreement.
11  *
12  */
13 
14 #ifndef __INCLUDED_SCOPIRA_CORE_MODEL_H__
15 #define __INCLUDED_SCOPIRA_CORE_MODEL_H__
16 
17 #include <assert.h>
18 
19 #include <vector>
20 #include <map>
21 
22 #include <scopira/tool/object.h>
23 #include <scopira/tool/reactor.h>
24 #include <scopira/tool/iterator.h>
25 #include <scopira/tool/export.h>
26 
27 namespace scopira
28 {
29  namespace core
30  {
31  class model_ptr_base;
32  template <class T> class model_ptr;
33 
34  class model_i;
35 
36  class project_i; //fwd
37 
38  class view_i; // fwd
39 
42  }
43 }
44 
51 {
52  protected:
55  public:
58  : dm_model(0) { }
59 
61  model_i * const & get_model_ptr(void) const { return dm_model; }
62 };
63 
69 template <class T> class scopira::core::model_ptr : public scopira::core::model_ptr_base
70 {
71  public:
73  typedef T data_type;
74 
75  protected:
79  T *dm_ptr;
80 
81  public:
88  : dm_ins(ins), dm_ptr(0) { assert(ins); }
94  { set(0); }
95 
102  void set(T* o) {
103  if (o) {
104  o->add_ref();
105  o->add_view(dm_ins);
106  }
107  if (dm_ptr) {
108  dm_ptr->remove_view(dm_ins);
109  dm_ptr->sub_ref();
110  }
111  dm_ptr = o;
112  dm_model = o; // double concrete magic [NOTE]
113  }
119  model_ptr & operator = (T *o)
120  { set(o); return *this; }
121 
127  model_ptr & operator = (const model_ptr<T> &o)
128  { set(o.dm_ptr); return *this; }
134  bool operator == (const model_ptr<T> &rhs) const
135  { return dm_ptr == rhs.dm_ptr; }
141  bool operator != (const model_ptr<T> &rhs) const
142  { return dm_ptr != rhs.dm_ptr; }
148  bool operator < (const model_ptr<T> &rhs) const
149  { return dm_ptr < rhs.dm_ptr; }
150 
156  //oflow_i & print(oflow_i &o) const
157  //{ if (dm_ptr) dm_ptr->print(o); /*else o<<"(null)";*/ return o; }
158 
159  // do save and load like the above?
160 
166  T* get(void) const
167  { return dm_ptr; }
168  //T* get(void)
169  //{ return dm_ptr; }
170 
176  T& ref(void) const
177  { assert(dm_ptr); return *dm_ptr; }
178  //T& ref(void)
179  //{ assert(dm_ptr); return *dm_ptr; }
180 
186  T* operator ->(void) const
187  { assert(dm_ptr); return dm_ptr; }
188  //T* operator ->(void)
189  //{ assert(dm_ptr); return dm_ptr; }
190 
196  T & operator *(void) const
197  { assert(dm_ptr); return *dm_ptr; }
198 
204  bool is_null(void) const { return dm_ptr == 0; }
205 };
206 
217 {
218  private:
219  typedef std::vector<view_i*> view_list;
220  typedef std::vector<scopira::tool::rename_reactor_i*> rename_list;
221  typedef std::map<std::string, scopira::tool::count_ptr<model_i> > archive_cache_t;
222 
223  mutable view_list dm_views;
224  mutable rename_list dm_rename_reactors;
225  std::string dm_title;
226  project_i *dm_owner;
227 
228  // mutable? mutex locked?
229  archive_cache_t dm_archive_cache;
230 
231  public:
233  SCOPIRA_EXPORT virtual bool load(scopira::tool::iobjflow_i& in);
235  SCOPIRA_EXPORT virtual void save(scopira::tool::oobjflow_i& out) const;
236 
244  SCOPIRA_EXPORT virtual void set_title(const std::string &newtitle);
245 
251  virtual bool is_project(void) const { return false; }
252 
257  const std::string & get_title(void) const { return dm_title; }
258 
265  SCOPIRA_EXPORT void add_view(view_i *ins) const;
271  SCOPIRA_EXPORT void remove_view(view_i *ins) const;
280  SCOPIRA_EXPORT void notify_views(view_i *src);
281 
286  SCOPIRA_EXPORT void add_rename_reactor(tool::rename_reactor_i *r) const;
291  SCOPIRA_EXPORT void remove_rename_reactor(tool::rename_reactor_i *r) const;
292 
301  SCOPIRA_EXPORT virtual void set_project(project_i *newowner);
302 
308  project_i * get_project(void) const { return dm_owner; }
309 
315  SCOPIRA_EXPORT void set_tagged_model(const std::string &name, model_i *what);
316 
325  template <class W>
326  bool get_tagged_model(const std::string &name, W &out) {
327  typedef typename W::data_type data_type; // just to make sure they dont pass us a simple *T
329  if (!get_tagged_model_impl(name, mm))
330  return false;
331  out = dynamic_cast<data_type*>(mm.get());
332  return mm.is_null() || out.get(); // success is if the data was null OR the cast worked
333  }
334 
335  protected:
337  SCOPIRA_EXPORT model_i(void);
339  SCOPIRA_EXPORT model_i(const std::string &title);
341  SCOPIRA_EXPORT model_i(const model_i &src);
343  SCOPIRA_EXPORT virtual ~model_i();
344 
345  private:
347  SCOPIRA_EXPORT bool get_tagged_model_impl(const std::string &name,
349 };
350 
351 #endif
352 
Definition: model.h:50
model_ptr(view_i *ins)
Definition: model.h:87
Definition: archiveflow.h:20
scopira::tool::iterator_g< model_i * > model_iterator
the model iterator type
Definition: model.h:38
Definition: flow.h:352
T * get(void) const
Definition: object.h:378
Definition: object.h:71
project_i * get_project(void) const
Definition: model.h:308
model_i * dm_model
internal: the actual pointer
Definition: model.h:54
~model_ptr()
Definition: model.h:93
T & ref(void) const
Definition: model.h:176
view_i * dm_ins
the view
Definition: model.h:77
model_ptr_base(void)
ctor
Definition: model.h:57
Definition: model.h:216
Definition: iterator.h:26
Definition: object.h:42
T data_type
the type of the data this model_ptr points too
Definition: model.h:73
model_i *const & get_model_ptr(void) const
gets the raw base pointer (really specialized use here)
Definition: model.h:61
T * dm_ptr
the actual pointer
Definition: model.h:79
bool is_null(void) const
Definition: object.h:414
Definition: reactor.h:67
virtual bool is_project(void) const
Definition: model.h:251
Definition: flow.h:421
bool is_null(void) const
Definition: model.h:204
Definition: model.h:32
Definition: project.h:38
bool get_tagged_model(const std::string &name, W &out)
Definition: model.h:326
Definition: view.h:80
const std::string & get_title(void) const
Definition: model.h:257