sarray.cxx

Go to the documentation of this file.
00001 //    sampalib.org ESL library and tools
00002 //    Copyright (C) 2007  Thierry Grellier
00003 //
00004 //    This program is free software; you can redistribute it and/or modify
00005 //    it under the terms of the GNU General Public License version 2 as
00006 //    published by the Free Software Foundation.
00007 //
00008 //    This program is distributed in the hope that it will be useful,
00009 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //    GNU General Public License for more details.
00012 //
00013 //    You should have received a copy of the GNU General Public License along
00014 //    with this program; if not, write to the Free Software Foundation, Inc.,
00015 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016 //
00017 //    contact: www.sampalib.org
00018 
00019 #include <cassert>
00020 #include "sampa/core/sarray.h"
00021 #include "sampa/core/persistence.cxx"
00022 #include <boost/type_traits/is_base_of.hpp>
00023 #include <boost/type_traits/is_fundamental.hpp>
00024 
00025 namespace Sampa {
00026 
00027 enum InitMode {
00028   INIT_OBJECT,
00029   INIT_DEFAULT_CTR,
00030   INIT_FUNDAMENTAL
00031 };
00032 
00033 template<InitMode, typename Element>
00034 struct InitElements;
00035 
00036 template<typename Element>
00037 struct InitElements<INIT_OBJECT, Element> {
00038   void operator()(Element* elements, size_t capacity, const char* name) {
00039     char idxname[std::strlen(name)+sizeof("[012345]")+1];
00040     for (size_t i = 0; i != capacity; i++) { 
00041       sprintf(idxname, "%s[%ld]", name, i+1);
00042       Element* initialized = new(&elements[i]) Element(StringParameter(idxname, idxname).get_value()); 
00043       assert(initialized == &elements[i]);
00044     }
00045   }
00046 };
00047 
00048 template<typename Element>
00049 struct InitElements<INIT_DEFAULT_CTR, Element> {
00050   void operator()(Element* elements, size_t capacity, const char*) {
00051     for (size_t i = 0; i != capacity; i++) { 
00052       Element* initialized = new(&elements[i]) Element(DEFAULT_CTR); 
00053       assert(initialized == &elements[i]);
00054     }
00055   }
00056 };
00057 
00058 template<typename Element>
00059 struct InitElements<INIT_FUNDAMENTAL, Element> {
00060   void operator()(Element*, size_t, const char*) {}
00061 };
00062 
00063 template<typename Element>
00064 Array<Element>::Array(size_t capacity, const char* name)
00065 {
00066 #if SAMPA_CONFIG_COLD_RESTART == SAMPA_ENABLED
00067   m_elements = array_of_scalar<Element>::create(*get_storage(), capacity);
00068 #else
00069   m_elements = new Element[capacity];
00070 #endif
00071   Element* p = begin();
00072   InitElements<   boost::is_base_of<Object, Element>::value ? INIT_OBJECT
00073                : (boost::is_fundamental<Element>::value     ? INIT_FUNDAMENTAL
00074                                                             : INIT_DEFAULT_CTR), Element> init;
00075   init(p, capacity, name);
00076 }
00077 
00078 template<typename Element>
00079 Array<Element>::~Array() 
00080 {
00081 #if SAMPA_CONFIG_COLD_RESTART == SAMPA_ENABLED
00082   delete   m_elements;
00083 #else
00084   delete[] m_elements;
00085 #endif
00086 }
00087  
00088 }
00089 
00090 #if SAMPA_CONFIG_COLD_RESTART == SAMPA_ENABLED
00091 #  define SAMPA_MAKE_ARRAY(class_name) \
00092     REGISTER_TEMPLATE(array_of_scalar<class_name >); \
00093     namespace Sampa { \
00094       SAMPA_MAKE_TMPL_PERSISTENT(Array<class_name>, Array, REF(m_elements)); \
00095       template class Array<class_name>; \
00096     }
00097 #else
00098 #  define SAMPA_MAKE_ARRAY(class_name) \
00099     namespace Sampa { \
00100       SAMPA_MAKE_TMPL_PERSISTENT(Array<class_name>, Array, REF(m_elements)); \
00101       template class Array<class_name>; \
00102     }
00103 #endif
00104 
00105 #define SAMPA_MAKE_PTRQUEUE(class_name) \
00106   namespace Sampa { \
00107     SAMPA_MAKE_TMPL_PERSISTENT(PtrQueue<class_name >, PtrQueue, NO_REFS); \
00108     template class PtrQueue<class_name >; \
00109   }
00110 

Generated on Sat Feb 16 16:23:15 2008 for Sampa by  doxygen 1.5.3