12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- /*=========================================================================
- Library: CTK
- Copyright (c) Kitware Inc.
- All rights reserved.
- Distributed under a BSD License. See LICENSE.txt file.
- This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
- the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- See the above copyright notice for more information.
- =========================================================================*/
- #include "ctkUtils.h"
- // STD includes
- #include <algorithm>
- //------------------------------------------------------------------------------
- void ctkUtils::qListToSTLVector(const QStringList& list,
- std::vector<char*>& vector)
- {
- // Resize if required
- if (list.count() != static_cast<int>(vector.size()))
- {
- vector.resize(list.count());
- }
- for (int i = 0; i < list.count(); ++i)
- {
- // Allocate memory
- char* str = new char[list[i].size()+1];
- strcpy(str, list[i].toLatin1());
- vector[i] = str;
- }
- }
- //------------------------------------------------------------------------------
- namespace
- {
- /// Convert QString to std::string
- static std::string qStringToSTLString(const QString& qstring)
- {
- return qstring.toStdString();
- }
- }
- //------------------------------------------------------------------------------
- void ctkUtils::qListToSTLVector(const QStringList& list,
- std::vector<std::string>& vector)
- {
- // To avoid unnessesary relocations, let's reserve the required amount of space
- vector.reserve(list.size());
- std::transform(list.begin(),list.end(),std::back_inserter(vector),&qStringToSTLString);
- }
- //------------------------------------------------------------------------------
- void ctkUtils::stlVectorToQList(const std::vector<std::string>& vector,
- QStringList& list)
- {
- std::transform(vector.begin(),vector.end(),std::back_inserter(list),&QString::fromStdString);
- }
|