// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- //----------------------------------------------------------------------------- // eoString.h // (c) GeNeura Team, 1998 /* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Contact: todos@geneura.ugr.es, http://geneura.ugr.es */ //----------------------------------------------------------------------------- #ifndef _eoString_H #define _eoString_H // STL libraries #include #include using namespace std; // EO headers #include //----------------------------------------------------------------------------- // eoString //----------------------------------------------------------------------------- /** Adaptor that turns an STL string into an EO */ template class eoString: public eo1d, public string { public: /// Canonical part of the objects: several ctors, copy ctor, dtor and assignment operator //@{ /// ctor eoString( const string& _str ="" ) : eo1d(), string( _str ) {}; /** Ctor using a random number generator @param _size Lineal length of the object @param _rnd a random number generator, which returns a random value each time it´s called */ eoString( unsigned _size, eoRnd& _rnd ) : eo1d(), string(){ for ( unsigned i = 0; i < _size; i ++ ) { *this += _rnd(); } }; /// copy ctor eoString( const eoString& _eoStr ) :eo1d( static_cast & > ( _eoStr ) ), string( _eoStr ){}; /// Assignment operator const eoString& operator =( const eoString& _eoStr ) { if ( this != & _eoStr ) { eo1d::operator = ( _eoStr ); string::operator = ( _eoStr ); } return *this; } /// dtor virtual ~eoString() {}; //@} /** methods that implement the eo1d protocol @exception out_of_range if _i is larger than EO´s size */ virtual char gene( unsigned _i ) const { if ( _i >= length() ) throw out_of_range( "out_of_range when reading gene"); return (*this)[_i]; }; /** methods that implement the eo1d protocol @exception out_of_range if _i is larger than EO´s size */ virtual char& gene( unsigned _i ) { if ( _i >= size() ) throw out_of_range( "out_of_range when writing a gene"); return (*this)[_i]; }; /** Inserts a value after _i, displacing anything to the right @exception out_of_range if _i is larger than EO´s size */ virtual void insertGene( unsigned _i, char _val ) { if (_i <= this->size() ) { string::iterator i = this->begin()+_i; this->insert( i, _val ); } else throw out_of_range( "out_of_range when inserting gene"); }; /** Eliminates the gene at position _i @exception out_of_range if _i is larger than EO´s size */ virtual void deleteGene( unsigned _i ) { if (_i < this->size() ) { string::iterator i = this->begin()+_i; this->erase( i ); } else throw out_of_range( "out_of_range when deleting gene"); }; /// methods that implement the EO protocol virtual unsigned length() const { return this->size(); }; /** @name Methods from eoObject readFrom and printOn are directly inherited from eo1d */ //@{ /** Inherited from eoObject @see eoObject */ string className() const {return "eoString";}; //@} }; #endif