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
47static int maxBackupId_ = 0;
48
49//-----------------------------------------------------------------------------
50
51BaseBackup::BaseBackup(const QString& _name) : object_(0), name_(_name), id_(maxBackupId_++)
52{
53}
54
55//-----------------------------------------------------------------------------
56
57BaseBackup::BaseBackup(BaseObjectData* _object, const 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
83BaseBackup::~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 for (auto i = dataMap.begin(); i != dataMap.end(); ++i) {
106 if ( i.key() == OBJECT_BACKUPS )
107 backupData = i.value();
108 else
109 delete i.value();
110 }
111
112 dataMap.clear();
113
114 if (backupData == 0){
115 std::cerr << "Cannot apply backup. BackupData not found!!" << std::endl;
116 return;
117 }
118
119 // insert backup Data
120 dataMap.insert( OBJECT_BACKUPS, backupData);
121
122 // now insert copies of the perObjectData from the backup
123 for (unsigned int j=0; j < objectDatas_.size(); j++ ){
124 // Try to get a copy of the objectData
125 PerObjectData* copiedData = objectDatas_[j].second->copyPerObjectData();
126
127 if ( copiedData )
128 dataMap.insert( objectDatas_[j].first, copiedData );
129 }
130}
131
132//-----------------------------------------------------------------------------
133
135 return name_;
136}
137
138//-----------------------------------------------------------------------------
139
141 return id_;
142}
143
144//-----------------------------------------------------------------------------
145
147 return !links_.empty();
148}
149
150//-----------------------------------------------------------------------------
151
153
154 //remove myself from links
155 for(int i=_objectIDs.size()-1; i >= 0; --i )
156 if ( i == id_ )
157 _objectIDs.erase( _objectIDs.begin() + i );
158
159 //store links
160 links_ = _objectIDs;
161}
162
163//-----------------------------------------------------------------------------
std::vector< int > IdList
Standard Type for id Lists used for scripting.
Definition: DataTypes.hh:181
bool blocked()
Returns if this backup is blocked.
Definition: BaseBackup.cc:146
virtual void apply()
Revert this backup.
Definition: BaseBackup.cc:93
std::vector< std::pair< QString, PerObjectData * > > objectDatas_
Backup of the perObjectData objects.
Definition: BaseBackup.hh:111
int id()
get id of this backup
Definition: BaseBackup.cc:140
void setLinks(IdList _objectIDs)
Set links to corresponding backups.
Definition: BaseBackup.cc:152
QString name()
Get the backups name)
Definition: BaseBackup.cc:134
QMap< QString, PerObjectData * > & getPerObjectDataMap()
get reference to map of all perObject Datas
Definition: BaseObject.cc:880
Object Payload.
virtual PerObjectData * copyPerObjectData()
Copy Function.
Update type class.
Definition: UpdateType.hh:59