/src/script/C4ValueMap.cpp
C++ | 472 lines | 306 code | 77 blank | 89 comment | 66 complexity | 33780e39ae884641657872df58f91fed MD5 | raw file
Possible License(s): WTFPL, 0BSD, LGPL-2.1, CC-BY-3.0
- /*
- * OpenClonk, http://www.openclonk.org
- *
- * Copyright (c) 2001-2002, 2004-2006 Peter Wortmann
- * Copyright (c) 2004-2006 Sven Eberhardt
- * Copyright (c) 2005, 2008 G?nther Brammer
- * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de
- *
- * Portions might be copyrighted by other authors who have contributed
- * to OpenClonk.
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- * See isc_license.txt for full license and disclaimer.
- *
- * "Clonk" is a registered trademark of Matthes Bender.
- * See clonk_trademark_license.txt for full license.
- */
- #include "C4Include.h"
- #include "C4ValueMap.h"
- // *** C4ValueMapData ***
- C4ValueMapData::C4ValueMapData()
- : pData(0), pNames(0), bTempNameList(false), pNext(0)
- {
- }
- C4ValueMapData::C4ValueMapData(const C4ValueMapData &DataToCopy)
- : pData(0), pNames(0), bTempNameList(false), pNext(0)
- {
- SetNameList(DataToCopy.pNames);
- if (pNames) for(int32_t i = 0; i < pNames->iSize; i++)
- pData[i].Set(DataToCopy.pData[i]);
- }
- C4ValueMapData &C4ValueMapData::operator = (const C4ValueMapData &DataToCopy)
- {
- SetNameList(DataToCopy.pNames);
- if (pNames) for(int32_t i = 0; i < pNames->iSize; i++)
- pData[i].Set(DataToCopy.pData[i]);
- return *this;
- }
- bool C4ValueMapData::operator == (const C4ValueMapData &Data) const
- {
- if(pNames != Data.pNames) return false;
- if(pNames)
- for(int i = 0; i < pNames->iSize; i++)
- if(pData[i] != Data.pData[i])
- return false;
- return true;
- }
- C4ValueMapData::~C4ValueMapData()
- {
- Reset();
- }
- void C4ValueMapData::Reset()
- {
- // unreg from name list (if using one)
- if(pNames) UnRegister();
- pNames = 0;
- // free data
- delete[] pData;
- pData = 0;
- }
- void C4ValueMapData::ResetContent()
- {
- if(pNames)
- // Realloc list (will destroy all data)
- ReAllocList();
- else
- {
- delete[] pData;
- pData = 0;
- }
- }
- void C4ValueMapData::SetNameList(C4ValueMapNames *pnNames)
- {
- if(pNames == pnNames) return;
- if(pNames)
- {
- // save name array from old name list
- char **pOldNames = pNames->pNames;
- int32_t iOldSize = pNames->iSize;
- // unreg from old name list
- // (manually, because Data::UnRegister() would destroy content)
- C4ValueMapNames *pNames = this->pNames;
- pNames->UnRegister(this);
- // register at new name list
- pnNames->Register(this);
- // call OnNameListChanged to copy data and realloc data array
- OnNameListChanged(const_cast<const char **>(pOldNames), iOldSize);
- // delete old names list, if it is temporary
- if(bTempNameList)
- delete pNames;
- bTempNameList = false;
- // ok
- }
- else
- {
- // simply register...
- Register(pnNames);
- }
- }
- void C4ValueMapData::Register(C4ValueMapNames *pnNames)
- {
- // UnReg from old?
- if(pNames) UnRegister();
- if (pnNames) pnNames->Register(this);
- pNames = pnNames;
- // alloc data array
- ReAllocList();
- }
- void C4ValueMapData::UnRegister()
- {
- if(!pNames) return;
- // safe pointer
- C4ValueMapNames *pNames = this->pNames;
- // unreg
- pNames->UnRegister(this);
- // delete name list (if it is temporary)
- if(bTempNameList)
- delete pNames;
- bTempNameList = false;
- // delete data array
- delete[] pData;
- pData = 0;
- }
- C4ValueMapNames *C4ValueMapData::CreateTempNameList()
- {
- // create new list
- C4ValueMapNames *pTempNames = new C4ValueMapNames();
- // register (this func will unreg if necessary, too)
- Register(pTempNames);
- // error?
- if(pNames != pTempNames)
- {
- delete pTempNames;
- return 0;
- }
- // set flag
- bTempNameList = true;
- return pTempNames;
- }
- void C4ValueMapData::ReAllocList()
- {
- if(!pNames)
- {
- Reset();
- return;
- }
- // delete old array
- delete[] pData;
- // create new one
- pData = new C4Value [pNames->iSize] ();
- // ok...
- }
- void C4ValueMapData::OnNameListChanged(const char **pOldNames, int32_t iOldSize)
- {
- if(!pNames)
- {
- Reset();
- return;
- }
- // this function does not use ReAllocList because the old values
- // have to be hold.
- // save pointer on old data
- C4Value *pOldData = pData;
- // create new data list
- pData = new C4Value [pNames->iSize] ();
- // (try to) copy data
- int32_t i, j;
- for(i = 0; i < iOldSize; i++)
- {
- //FIXME: This optimization is ugly.
- if(i < pNames->iSize && SEqual(pNames->pNames[i], pOldNames[i]))
- {
- pOldData[i].Move(&pData[i]);
- }
- else for(j = 0; j < pNames->iSize; j++)
- {
- if(SEqual(pNames->pNames[j], pOldNames[i]))
- {
- pOldData[i].Move(&pData[j]);
- break; // in hope this will only break the last for...
- }
- }
- }
- // delete old data array
- delete[] pOldData;
- }
- C4Value *C4ValueMapData::GetItem(int32_t iNr)
- {
- // the list is nothing without name list...
- if(!pNames) return 0;
- if(iNr >= pNames->iSize) return 0;
- return &pData[iNr];
- }
- C4Value *C4ValueMapData::GetItem(const char *strName)
- {
- if(!pNames) return 0;
- int32_t iNr = pNames->GetItemNr(strName);
- if(iNr == -1) return 0;
- return &pData[iNr];