/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
*/
/*
* This file is based on WME Lite.
* http://dead-code.org/redir.php?target=wmelite
* Copyright (c) 2011 Jan Nedoma
*/
#ifndef WINTERMUTE_COLL_TEMPL_H
#define WINTERMUTE_COLL_TEMPL_H
#include "common/array.h"
#include "engines/wintermute/base/base_persistence_manager.h"
namespace Wintermute {
// Basically Common::Array with peristence-support.
template
class BaseArrayBase : public Common::Array {
public:
// TODO: Might want to make sure that destructors are called when replacing/deleting/getting destructed
int add(TYPE newElement) {
Common::Array::push_back(newElement);
return Common::Array::size() - 1;
}
void remove_at(uint32 idx) {
Common::Array::remove_at(idx);
}
void remove_at(uint32 idx, uint32 num) {
while (num) {
if (idx >= Common::Array::size()) {
break;
}
Common::Array::remove_at(idx);
}
}
template
void copy(const BaseArrayBase &src) {
Common::Array::insert_at(0, src);
}
};
template
class BaseArray : public BaseArrayBase {
public:
bool persist(BasePersistenceManager *persistMgr) {
int32 j;
if (persistMgr->getIsSaving()) {
j = Common::Array::size();
persistMgr->transferSint32("ArraySize", &j);
typename Common::Array::const_iterator it = Common::Array::begin();
for (; it != Common::Array::end(); ++it) {
TYPE obj = *it;
persistMgr->transferPtr("", &obj);
}
} else {
Common::Array::clear();
persistMgr->transferSint32("ArraySize", &j);
for (int i = 0; i < j; i++) {
TYPE obj = nullptr;
persistMgr->transferPtr("", &obj);
this->add(obj);
}
}
return true;
}
};
template <>
class BaseArray : public BaseArrayBase {
public:
bool persist(BasePersistenceManager *persistMgr) {
int32 j;
if (persistMgr->getIsSaving()) {
j = Common::Array::size();
persistMgr->transferSint32("ArraySize", &j);
Common::Array::const_iterator it = Common::Array::begin();
for (; it != Common::Array::end(); ++it) {
char * obj = *it;
persistMgr->transferCharPtr("", &obj);
}
} else {
Common::Array::clear();
persistMgr->transferSint32("ArraySize", &j);
for (int i = 0; i < j; i++) {
char * obj = nullptr;
persistMgr->transferCharPtr("", &obj);
add(obj);
}
}
return true;
}
};
template <>
class BaseArray : public BaseArrayBase {
public:
bool persist(BasePersistenceManager *persistMgr) {
int32 j;
if (persistMgr->getIsSaving()) {
j = Common::Array::size();
persistMgr->transferSint32("ArraySize", &j);
Common::Array::const_iterator it = Common::Array::begin();
for (; it != Common::Array::end(); ++it) {
const char * obj = *it;
persistMgr->transferConstChar("", &obj);
}
} else {
Common::Array::clear();
persistMgr->transferSint32("ArraySize", &j);
for (int i = 0; i < j; i++) {
const char * obj = nullptr;
persistMgr->transferConstChar("", &obj);
add(obj);
}
}
return true;
}
};
} // End of namespace Wintermute
#endif