00001 /* SimData: Data Infrastructure for Simulations 00002 * Copyright (C) 2002 Mark Rose <tm2@stm.lbl.gov> 00003 * 00004 * This file is part of SimData. 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00019 */ 00020 00026 #ifndef __SIMDATA_LIST_H__ 00027 #define __SIMDATA_LIST_H__ 00028 00029 #include <string> 00030 #include <vector> 00031 #include <SimData/BaseType.h> 00032 #include <SimData/Archive.h> 00033 00034 00035 NAMESPACE_SIMDATA 00036 00037 00043 class ListBase: public BaseType { 00044 public: 00045 virtual ~ListBase() {} 00046 }; 00047 00048 00055 template <class T> class List: public ListBase, public std::vector<T> { 00056 00057 public: 00058 00059 ~List() {} 00060 00061 // can be used by python code to extend a list of unknown type, 00062 // assigning or operating on the added element as necessary. 00063 T& extend(); 00064 00067 virtual void serialize(Archive&); 00068 00069 std::string asString() const; 00070 00071 }; 00072 00073 00074 template<typename T> 00075 T& List<T>::extend() { 00076 T x; 00077 this->push_back(x); 00078 return this->operator[](this->size()-1); 00079 } 00080 00081 template<typename T> 00082 void List<T>::serialize(Archive &archive) { 00083 if (archive.isLoading()) { 00084 T a; 00085 int size; 00086 archive(size); 00087 assert(size >= 0); 00088 this->clear(); 00089 this->reserve(size); 00090 for (int i = 0; i < size; ++i) { 00091 archive(a); 00092 this->push_back(a); 00093 } 00094 } else { 00095 typename std::vector<T>::iterator a; 00096 int s = static_cast<int>(size()); 00097 archive(s); 00098 for (a=begin(); a!=end(); ++a) { 00099 archive(*a); 00100 } 00101 } 00102 } 00103 00104 template<typename T> 00105 std::string List<T>::asString() const { 00106 std::stringstream ss; 00107 ss << "<simdata::List[" << size() << ">"; 00108 return ss.str(); 00109 } 00110 00111 00112 NAMESPACE_SIMDATA_END 00113 00114 00115 #endif //__SIMDATA_LIST_H__ 00116
|
SimData version pre-0.4.0. For more information on SimData, visit the SimData Homepage. Generated on Tue Oct 14 12:06:38 2003, using Doxygen 1.2.18. |