Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
BaseBackup.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 #include "BaseBackup.hh"
51 #include <OpenFlipper/common/BackupData.hh>
52 
53 //-----------------------------------------------------------------------------
54 
55 static int maxBackupId_ = 0;
56 
57 //-----------------------------------------------------------------------------
58 
59 BaseBackup::BaseBackup(QString _name) : object_(0), name_(_name), id_(maxBackupId_++)
60 {
61 }
62 
63 //-----------------------------------------------------------------------------
64 
65 BaseBackup::BaseBackup(BaseObjectData* _object, QString _name, UpdateType _type) : object_(_object), name_(_name), id_(maxBackupId_++)
66 {
67 // std::cerr << "Create BaseBackup with name:" << name_.toStdString() << "(id : " << id_ << ")" << std::endl;
68 
69  // Get the per Object Datas
70  QMap<QString, PerObjectData*> dataMap = object_->getPerObjectDataMap();
71 
72  // Iterate over all per Object datas and try to copy them into our backup storage
73  QMap<QString, PerObjectData*>::const_iterator mapIter;
74 
75  for ( mapIter = dataMap.constBegin(); mapIter != dataMap.constEnd(); ++mapIter){
76  //don't copy myself
77  if ( mapIter.key() == OBJECT_BACKUPS ) continue;
78 
79  // Try to get a copy of the objectData
80  PerObjectData* copiedData = mapIter.value()->copyPerObjectData();
81 
82  if ( copiedData )
83  objectDatas_.push_back( std::make_pair( mapIter.key(),copiedData ) );
84  else
85  std::cerr << "Failed to copy per Object Data: " << mapIter.key().toStdString() << std::endl;
86  }
87 }
88 
89 //-----------------------------------------------------------------------------
90 
91 BaseBackup::~BaseBackup(){
92 
93 // std::cerr << "Delete BaseBackup with name:" << name_.toStdString() << "(id : " << id_ << ")" << std::endl;
94 
95  for (uint i=0; i < objectDatas_.size(); i++)
96  delete objectDatas_[i].second;
97 }
98 
99 //-----------------------------------------------------------------------------
100 
102 
103 // std::cerr << "Apply BaseBackup with name:" << name_.toStdString() << "(id : " << id_ << ")" << std::endl;
104 
105  if (object_ == 0)
106  return;
107 
108  PerObjectData* backupData = 0;
109 
110  // Get the per Object Data container in the object
111  QMap<QString, PerObjectData*>& dataMap = object_->getPerObjectDataMap();
112 
113  QMapIterator<QString, PerObjectData* > i(dataMap);
114  while (i.hasNext()) {
115  i.next();
116  if ( i.key() == OBJECT_BACKUPS )
117  backupData = i.value();
118  else
119  delete i.value();
120  }
121 
122  dataMap.clear();
123 
124  if (backupData == 0){
125  std::cerr << "Cannot apply backup. BackupData not found!!" << std::endl;
126  return;
127  }
128 
129  // insert backup Data
130  dataMap.insert( OBJECT_BACKUPS, backupData);
131 
132  // now insert copies of the perObjectData from the backup
133  for (unsigned int i=0; i < objectDatas_.size(); i++ ){
134  // Try to get a copy of the objectData
135  PerObjectData* copiedData = objectDatas_[i].second->copyPerObjectData();
136 
137  if ( copiedData )
138  dataMap.insert( objectDatas_[i].first, copiedData );
139  }
140 }
141 
142 //-----------------------------------------------------------------------------
143 
145  return name_;
146 }
147 
148 //-----------------------------------------------------------------------------
149 
151  return id_;
152 }
153 
154 //-----------------------------------------------------------------------------
155 
157  return !links_.empty();
158 }
159 
160 //-----------------------------------------------------------------------------
161 
162 void BaseBackup::setLinks(IdList _objectIDs){
163 
164  //remove myself from links
165  for(int i=_objectIDs.size()-1; i >= 0; --i )
166  if ( i == id_ )
167  _objectIDs.erase( _objectIDs.begin() + i );
168 
169  //store links
170  links_ = _objectIDs;
171 }
172 
173 //-----------------------------------------------------------------------------
QMap< QString, PerObjectData * > & getPerObjectDataMap()
get reference to map of all perObject Datas
Definition: BaseObject.cc:835
std::vector< std::pair< QString, PerObjectData * > > objectDatas_
Backup of the perObjectData objects.
Definition: BaseBackup.hh:120
Update type class.
Definition: UpdateType.hh:70
void setLinks(IdList _objectIDs)
Set links to corresponding backups.
Definition: BaseBackup.cc:162
virtual PerObjectData * copyPerObjectData()
Copy Function.
std::vector< int > IdList
Standard Type for id Lists used for scripting.
Definition: DataTypes.hh:192
QString name()
Get the backups name)
Definition: BaseBackup.cc:144
int id()
get id of this backup
Definition: BaseBackup.cc:150
bool blocked()
Returns if this backup is blocked.
Definition: BaseBackup.cc:156
virtual void apply()
Revert this backup.
Definition: BaseBackup.cc:101
Object Payload.