2012-11-26 11:29:22 +00:00
|
|
|
#ifndef CSM_WOLRD_RECORD_H
|
|
|
|
#define CSM_WOLRD_RECORD_H
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
namespace CSMWorld
|
|
|
|
{
|
|
|
|
template <typename ESXRecordT>
|
|
|
|
struct Record
|
|
|
|
{
|
2012-11-29 13:45:34 +00:00
|
|
|
enum State
|
2012-11-26 11:29:22 +00:00
|
|
|
{
|
|
|
|
State_BaseOnly, // defined in base only
|
|
|
|
State_Modified, // exists in base, but has been modified
|
|
|
|
State_ModifiedOnly, // newly created in modified
|
|
|
|
State_Deleted // exists in base, but has been deleted
|
|
|
|
};
|
|
|
|
|
|
|
|
ESXRecordT mBase;
|
|
|
|
ESXRecordT mModified;
|
2012-11-29 13:45:34 +00:00
|
|
|
State mState;
|
2012-11-26 11:29:22 +00:00
|
|
|
|
|
|
|
bool isDeleted() const;
|
|
|
|
|
|
|
|
bool isModified() const;
|
|
|
|
|
|
|
|
const ESXRecordT& get() const;
|
|
|
|
///< Throws an exception, if the record is deleted.
|
|
|
|
|
2012-11-29 13:45:34 +00:00
|
|
|
const ESXRecordT& getBase() const;
|
|
|
|
///< Throws an exception, if the record is deleted. Returns modified, if there is no base.
|
2012-11-26 11:29:22 +00:00
|
|
|
|
|
|
|
void setModified (const ESXRecordT& modified);
|
2012-11-29 13:45:34 +00:00
|
|
|
///< Throws an exception, if the record is deleted.
|
2012-11-26 11:29:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename ESXRecordT>
|
|
|
|
bool Record<ESXRecordT>::isDeleted() const
|
|
|
|
{
|
|
|
|
return mState==State_Deleted;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ESXRecordT>
|
|
|
|
bool Record<ESXRecordT>::isModified() const
|
|
|
|
{
|
|
|
|
return mState==State_Modified || mState==State_ModifiedOnly;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ESXRecordT>
|
|
|
|
const ESXRecordT& Record<ESXRecordT>::get() const
|
|
|
|
{
|
|
|
|
if (isDeleted())
|
|
|
|
throw std::logic_error ("attempt to access a deleted record");
|
|
|
|
|
|
|
|
return mState==State_BaseOnly ? mBase : mModified;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ESXRecordT>
|
2012-11-29 13:45:34 +00:00
|
|
|
const ESXRecordT& Record<ESXRecordT>::getBase() const
|
2012-11-26 11:29:22 +00:00
|
|
|
{
|
|
|
|
if (isDeleted())
|
|
|
|
throw std::logic_error ("attempt to access a deleted record");
|
|
|
|
|
2012-11-29 13:45:34 +00:00
|
|
|
return mState==State_ModifiedOnly ? mModified : mBase;
|
2012-11-26 11:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ESXRecordT>
|
|
|
|
void Record<ESXRecordT>::setModified (const ESXRecordT& modified)
|
|
|
|
{
|
2012-11-29 13:45:34 +00:00
|
|
|
if (isDeleted())
|
|
|
|
throw std::logic_error ("attempt to modify a deleted record");
|
|
|
|
|
2012-11-26 11:29:22 +00:00
|
|
|
mModified = modified;
|
|
|
|
|
|
|
|
if (mState!=State_ModifiedOnly)
|
2012-11-29 13:45:34 +00:00
|
|
|
mState = mBase==mModified ? State_BaseOnly : State_Modified;
|
2012-11-26 11:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|