ctkUtils.cpp

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Library:   CTK
00004  
00005   Copyright (c) 2010  Kitware Inc.
00006 
00007   Licensed under the Apache License, Version 2.0 (the "License");
00008   you may not use this file except in compliance with the License.
00009   You may obtain a copy of the License at
00010 
00011       http://www.commontk.org/LICENSE
00012 
00013   Unless required by applicable law or agreed to in writing, software
00014   distributed under the License is distributed on an "AS IS" BASIS,
00015   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016   See the License for the specific language governing permissions and
00017   limitations under the License.
00018  
00019 =========================================================================*/
00020 
00021 #include "ctkUtils.h"
00022 
00023 // STD includes
00024 #include <algorithm>
00025 
00026 //------------------------------------------------------------------------------
00027 void ctk::qListToSTLVector(const QStringList& list,
00028                                  std::vector<char*>& vector)
00029 {
00030   // Resize if required
00031   if (list.count() != static_cast<int>(vector.size()))
00032     {
00033     vector.resize(list.count());
00034     }
00035   for (int i = 0; i < list.count(); ++i)
00036     {
00037     // Allocate memory
00038     char* str = new char[list[i].size()+1];
00039     strcpy(str, list[i].toLatin1());
00040     vector[i] = str;
00041     }
00042 }
00043 
00044 //------------------------------------------------------------------------------
00045 namespace
00046 {
00048 static std::string qStringToSTLString(const QString& qstring)
00049 {
00050   return qstring.toStdString();
00051 }
00052 }
00053 
00054 //------------------------------------------------------------------------------
00055 void ctk::qListToSTLVector(const QStringList& list,
00056                                  std::vector<std::string>& vector)
00057 {
00058   // To avoid unnessesary relocations, let's reserve the required amount of space
00059   vector.reserve(list.size());
00060   std::transform(list.begin(),list.end(),std::back_inserter(vector),&qStringToSTLString);
00061 }
00062 
00063 //------------------------------------------------------------------------------
00064 void ctk::stlVectorToQList(const std::vector<std::string>& vector,
00065                                  QStringList& list)
00066 {
00067   std::transform(vector.begin(),vector.end(),std::back_inserter(list),&QString::fromStdString);
00068 }
00069 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated on 21 May 2010 for CTK by  doxygen 1.6.1