Developer Documentation
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 #include "BaseBackup.hh"
43 #include <OpenFlipper/common/BackupData.hh>
44 
45 //-----------------------------------------------------------------------------
46 
47 static int maxBackupId_ = 0;
48 
49 //-----------------------------------------------------------------------------
50 
51 BaseBackup::BaseBackup(QString _name) : object_(0), name_(_name), id_(maxBackupId_++)
52 {
53 }
54 
55 //-----------------------------------------------------------------------------
56 
57 BaseBackup::BaseBackup(BaseObjectData* _object, QString _name, UpdateType _type) : object_(_object), name_(_name), id_(maxBackupId_++)
58 {
59 // std::cerr << "Create BaseBackup with name:" << name_.toStdString() << "(id : " << id_ << ")" << std::endl;
60 
61  // Get the per Object Datas
62  QMap<QString, PerObjectData*> dataMap = object_->getPerObjectDataMap();
63 
64  // Iterate over all per Object datas and try to copy them into our backup storage
65  QMap<QString, PerObjectData*>::const_iterator mapIter;
66 
67  for ( mapIter = dataMap.constBegin(); mapIter != dataMap.constEnd(); ++mapIter){
68  //don't copy myself
69  if ( mapIter.key() == OBJECT_BACKUPS ) continue;
70 
71  // Try to get a copy of the objectData
72  PerObjectData* copiedData = mapIter.value()->copyPerObjectData();
73 
74  if ( copiedData )
75  objectDatas_.push_back( std::make_pair( mapIter.key(),copiedData ) );
76  else
77  std::cerr << "Failed to copy per Object Data: " << mapIter.key().toStdString() << std::endl;
78  }
79 }
80 
81 //-----------------------------------------------------------------------------
82 
83 BaseBackup::~BaseBackup(){
84 
85 // std::cerr << "Delete BaseBackup with name:" << name_.toStdString() << "(id : " << id_ << ")" << std::endl;
86 
87  for (uint i=0; i < objectDatas_.size(); i++)
88  delete objectDatas_[i].second;
89 }
90 
91 //-----------------------------------------------------------------------------
92 
94 
95 // std::cerr << "Apply BaseBackup with name:" << name_.toStdString() << "(id : " << id_ << ")" << std::endl;
96 
97  if (object_ == 0)
98  return;
99 
100  PerObjectData* backupData = 0;
101 
102  // Get the per Object Data container in the object
103  QMap<QString, PerObjectData*>& dataMap = object_->getPerObjectDataMap();
104 
105  QMapIterator<QString, PerObjectData* > i(dataMap);
106  while (i.hasNext()) {
107  i.next();
108  if ( i.key() == OBJECT_BACKUPS )
109  backupData = i.value();
110  else
111  delete i.value();
112  }
113 
114  dataMap.clear();
115 
116  if (backupData == 0){
117  std::cerr << "Cannot apply backup. BackupData not found!!" << std::endl;
118  return;
119  }
120 
121  // insert backup Data
122  dataMap.insert( OBJECT_BACKUPS, backupData);
123 
124  // now insert copies of the perObjectData from the backup
125  for (unsigned int i=0; i < objectDatas_.size(); i++ ){
126  // Try to get a copy of the objectData
127  PerObjectData* copiedData = objectDatas_[i].second->copyPerObjectData();
128 
129  if ( copiedData )
130  dataMap.insert( objectDatas_[i].first, copiedData );
131  }
132 }
133 
134 //-----------------------------------------------------------------------------
135 
137  return name_;
138 }
139 
140 //-----------------------------------------------------------------------------
141 
143  return id_;
144 }
145 
146 //-----------------------------------------------------------------------------
147 
149  return !links_.empty();
150 }
151 
152 //-----------------------------------------------------------------------------
153 
154 void BaseBackup::setLinks(IdList _objectIDs){
155 
156  //remove myself from links
157  for(int i=_objectIDs.size()-1; i >= 0; --i )
158  if ( i == id_ )
159  _objectIDs.erase( _objectIDs.begin() + i );
160 
161  //store links
162  links_ = _objectIDs;
163 }
164 
165 //-----------------------------------------------------------------------------
Update type class.
Definition: UpdateType.hh:60
int id()
get id of this backup
Definition: BaseBackup.cc:142
QString name()
Get the backups name)
Definition: BaseBackup.cc:136
bool blocked()
Returns if this backup is blocked.
Definition: BaseBackup.cc:148
Object Payload.
std::vector< int > IdList
Standard Type for id Lists used for scripting.
Definition: DataTypes.hh:179
virtual void apply()
Revert this backup.
Definition: BaseBackup.cc:93
virtual PerObjectData * copyPerObjectData()
Copy Function.
QMap< QString, PerObjectData * > & getPerObjectDataMap()
get reference to map of all perObject Datas
Definition: BaseObject.cc:886
std::vector< std::pair< QString, PerObjectData * > > objectDatas_
Backup of the perObjectData objects.
Definition: BaseBackup.hh:111
void setLinks(IdList _objectIDs)
Set links to corresponding backups.
Definition: BaseBackup.cc:154