CustusX  16.5.0-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxRepManager.h
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #ifndef CXREPMANAGER_H_
34 #define CXREPMANAGER_H_
35 
36 #include "org_custusx_core_view_Export.h"
37 
38 #include <QObject>
39 #include <map>
40 #include <vector>
41 #include "cxForwardDeclarations.h"
42 
43 namespace cx
44 {
45 typedef boost::shared_ptr<class Rep> RepPtr;
46 }
47 
48 namespace cx
49 {
50 
51 typedef std::map<QString, RepPtr> RepMap;
52 typedef std::map<QString, VolumetricBaseRepPtr> VolumetricRepMap;
53 
54 class Reporter;
55 
72 class org_custusx_core_view_EXPORT RepManager: public QObject
73 {
74 Q_OBJECT
75 
76 public:
77  static RepManager* getInstance();
78  static void destroyInstance();
79 
92  template<class REP>
93  boost::shared_ptr<REP> getCachedRep(QString uid = "")
94  {
95  // look for existing value:
96  for (RepMultiMap::iterator iter = mRepCache.begin(); iter != mRepCache.end(); ++iter)
97  {
98  if (iter->first != uid)
99  continue;
100  int uc = iter->second.use_count();
101  if (uc >1)
102  {
103 // std::cout << "cached rep in use: " << uid.toStdString() << ", use count: " << uc << std::endl;
104  continue;
105  }
106 
107  boost::shared_ptr<REP> retval = boost::dynamic_pointer_cast<REP>(iter->second);
108  if (retval)
109  {
110 // std::cout << "reusing cached rep: " << uid.toStdString() << ", use count: " << uc << std::endl;
111  return retval;
112  }
113  }
114 
115  // create new value, store and return:
116  boost::shared_ptr<REP> retval = REP::New(uid);
117  mRepCache.insert(std::make_pair(uid, retval));
118 
119  return retval;
120  }
121 
122 protected:
124 
125  typedef std::multimap<QString, RepPtr> RepMultiMap;
127 
128 private:
129  RepManager();
130  virtual ~RepManager();
131  RepManager(RepManager const&);
132  RepManager& operator=(RepManager const&);
133 };
134 
135 
139 } //namespace cx
140 
141 #endif /* CXREPMANAGER_H_ */
std::map< QString, RepPtr > RepMap
Definition: cxRepManager.h:51
Rep caching and utilities.
Definition: cxRepManager.h:72
static RepManager * mTheInstance
the only instance of this class
Definition: cxRepManager.h:123
boost::shared_ptr< REP > getCachedRep(QString uid="")
Definition: cxRepManager.h:93
std::multimap< QString, RepPtr > RepMultiMap
Definition: cxRepManager.h:125
std::map< QString, VolumetricBaseRepPtr > VolumetricRepMap
Definition: cxRepManager.h:52
RepMultiMap mRepCache
Definition: cxRepManager.h:126
boost::shared_ptr< class Rep > RepPtr
Definition: cxRepManager.h:45