@ -39,8 +39,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# define STFU_HPP
namespace stfu {
using namespace std ;
const static string not_found ( " search->No such child/value " ) ;
const static std : : st ring not_found ( " search->No such child/value " ) ;
/*! \class node
* \ brief A node in an STF tree .
@ -54,16 +54,16 @@ namespace stfu {
class node {
/** Overloaded ostream's operator<< */
friend ostream & operator < < ( ostream & out , const node & root ) ;
friend std : : ostream & operator < < ( std : : ostream & out , const node & root ) ;
/** Returns whether it was succesful or not*/
friend bool operator < < ( ofstream & out , const node & root ) ;
friend bool operator < < ( std : : ofstream & out , const node & root ) ;
/** Acts like write(), but looks like this: "filename" << node */
friend bool operator < < ( const char * filename , const node & root ) ;
/** Overloaded istream's operator>> */
friend bool operator > > ( istream & in , node & root ) ;
friend bool operator > > ( std : : istream & in , node & root ) ;
/** Acts like read(), but looks like this: "filename" >> node */
friend bool operator > > ( const char * filename , node & root ) ;
@ -71,8 +71,8 @@ namespace stfu {
public :
//@{
/** The values and children belonging to this node. To add, remove, do whatever: you CAN use these variables directly. To use indexing, use the member functions below*/
multimap < string , string > values ;
multimap < string , node > children ;
std : : multimap < std : : st ring , std : : string > values ;
std : : multimap < std : : string , node > children ;
//@}
/**
@ -83,19 +83,20 @@ namespace stfu {
children . clear ( ) ;
}
/**
Gets the string value from a variable
Gets the std : : st ring value from a variable
\ param name The name of the value you wish to retrieve
\ param index If there are more values with the same name , they are indexed . Here you can supply its indexnumber .
\ return The retrieved value
*/
string & value ( const string & name , size_t index = 0 ) ;
std : : st ring & value ( const std : : string & name , size_t index = 0 ) ;
/**
Same as value ( ) , but for unnamed values
\ note same as value ( " " , index )
\ param index If there are more unnamed values , they are indexed . Here you can supply its indexnumber .
\ return Same as value
\ param index If there are more unnamed values , they are indexed . Here you can supply its indexnumber .
\ return Same as value
*/
string & value ( size_t index ) {
std : : st ring & value ( size_t index ) {
return value ( " " , index ) ;
}
@ -104,41 +105,41 @@ namespace stfu {
\ param name The name of the value to be created
\ return A reference to the value of the created value .
*/
string & addValue ( const string & name = " " ) ;
std : : st ring & addValue ( const std : : string & name = " " ) ;
/**
Const function for const objects to get a value . It ' s NOT possible to call value ( ) on constant objects for value ( ) isn ' t const .
\ param name Name of the value to be retrieved
\ param index If there are > 1 values with the same name , they are indexed . Here you can supply an indexnumber .
\ return Returns a const string & to the value of value with the name and index specified
\ return Returns a const std : : st ring & to the value of value with the name and index specified
*/
const string & getValue ( const string & name , size_t index ) const throw ( out_of_range ) ;
const std : : st ring & getValue ( const std : : st ring & name , size_t index ) const throw ( std : : out_of_range ) ;
/**
Same as getValue ( ) const , but for unnamed values
\ note same as getValue ( " " , index ) const
\ param index If there are > 1 unnamed values , they are indexed . Here you can supply an indexnumber .
\ return Returns a const string & to the value of value with the name and index specified
\ return Returns a const std : : st ring & to the value of value with the name and index specified
*/
const string & getValue ( size_t index ) const throw ( out_of_range ) {
const std : : st ring & getValue ( size_t index ) const throw ( std : : out_of_range ) {
return getValue ( " " , index ) ;
}
/**
Same as getValue ( ) const , but for non - const objects . The returned string & can safely be modified .
Same as getValue ( ) const , but for non - const objects . The returned std : : st ring & can safely be modified .
\ param name Name of the value to be retrieved
\ param index If there are > 1 values with the same name , they are indexed . Here you can supply an indexnumber .
\ return Returns a string & to the value of value with the name and index specified
\ return Returns a std : : st ring & to the value of value with the name and index specified
*/
string & getValue ( const string & name , size_t index = 0 ) throw ( out_of_range ) ;
std : : st ring & getValue ( const std : : st ring & name , size_t index = 0 ) throw ( std : : out_of_range ) ;
/**
Same as getValue ( ) , but for unnamed values
\ note same as getValue ( " " , index )
\ param index If there are > 1 unnamed values , they are indexed . Here you can supply an indexnumber .
\ return Returns a string & to the value of value with the name and index specified
\ return Returns a std : : st ring & to the value of value with the name and index specified
*/
string & getValue ( size_t index ) throw ( out_of_range ) {
std : : st ring & getValue ( size_t index ) throw ( std : : out_of_range ) {
return getValue ( " " , index ) ;
}
@ -147,14 +148,14 @@ namespace stfu {
\ param name Name of the value to be removed
\ param index If there are > 1 values with the same name , they are indexed . Here you can supply an indexnumber .
*/
void removeValue ( const string & name , size_t index = 0 ) throw ( out_of_range ) ;
void removeValue ( const std : : st ring & name , size_t index = 0 ) throw ( std : : out_of_range ) ;
/**
Removes an unnamed value .
\ note same as removeValue ( " " , index ) ;
\ param index If there are > 1 unnamed values , they are indexed . Here you can supply an indexnumber .
*/
void removeValue ( size_t index ) throw ( out_of_range ) {
void removeValue ( size_t index ) throw ( std : : out_of_range ) {
removeValue ( " " , index ) ;
}
@ -164,7 +165,7 @@ namespace stfu {
\ param newName The name that the oldName - value should have
\ param index If there are > 1 values with the same name , they are indexed . Here you can supply an indexnumber .
*/
void renameValue ( const string & oldName , const string & newName , size_t index = 0 ) ;
void renameValue ( const std : : st ring & oldName , const std : : string & newName , size_t index = 0 ) ;
/**
Changes , adds or retrieves node
@ -175,7 +176,7 @@ namespace stfu {
If this index number is > the number of variables with that name , a new variable with that name is created with index = current number of same name variables + 1.
So say you have 4 variables named " tree " , you call child ( " tree " , 10 ) , another tree gets made with index 5 , NOT 10.
*/
node & child ( const string & name , size_t index = 0 ) ;
node & child ( const std : : st ring & name , size_t index = 0 ) ;
/**
Same as child ( ) , but for unnamed children .
@ -192,7 +193,7 @@ namespace stfu {
\ param name Name for the child
\ return A reference to the created node
*/
node & addChild ( const string & name = " " ) ;
node & addChild ( const std : : st ring & name = " " ) ;
/**
As addChild ( name ) , but copies data from newChild as well .
@ -200,7 +201,7 @@ namespace stfu {
\ param newChild Data to copy from the child .
\ return A reference to the created node
*/
node & addChild ( const string & name , node & newChild ) ;
node & addChild ( const std : : st ring & name , node & newChild ) ;
/**
As addChild ( name , newChild ) , but without name , for unnamed children
@ -218,7 +219,7 @@ namespace stfu {
\ param index If there are > 1 children with the same name , they are indexed . Here you can supply an indexnumber .
\ return Returns a const node & to the node with the name and index specified
*/
const node & getChild ( const string & name , size_t index = 0 ) const throw ( out_of_range ) ;
const node & getChild ( const std : : st ring & name , size_t index = 0 ) const throw ( std : : out_of_range ) ;
/**
Const function for const objects to get an unnamed const node & .
@ -226,7 +227,7 @@ namespace stfu {
\ param index If there are > 1 unnamed children , they are indexed . Here you can supply an indexnumber .
\ return Returns a const node & to the node with the name and index specified
*/
const node & getChild ( size_t index = 0 ) const throw ( out_of_range ) {
const node & getChild ( size_t index = 0 ) const throw ( std : : out_of_range ) {
return getChild ( " " , index ) ;
}
@ -236,7 +237,7 @@ namespace stfu {
\ param index If there are > 1 children with the same name , they are indexed . Here you can supply an indexnumber .
\ return Returns a node & to the node with the name and index specified
*/
node & getChild ( const string & name , size_t index = 0 ) throw ( out_of_range ) ;
node & getChild ( const std : : st ring & name , size_t index = 0 ) throw ( std : : out_of_range ) ;
/**
Same as getChild ( ) const , but for non - const objects .
@ -244,7 +245,7 @@ namespace stfu {
\ param index If there are > 1 unnamed children , they are indexed . Here you can supply an indexnumber .
\ return Returns a node & to the node with the name and index specified
*/
node & getChild ( size_t index = 0 ) throw ( out_of_range ) {
node & getChild ( size_t index = 0 ) throw ( std : : out_of_range ) {
return getChild ( " " , index ) ;
}
@ -253,13 +254,13 @@ namespace stfu {
\ param name Name of the child to be removed
\ param index If there are > 1 children with the same name , they are indexed . Here you can supply an indexnumber .
*/
void removeChild ( const string & name , size_t index = 0 ) throw ( out_of_range ) ;
void removeChild ( const std : : st ring & name , size_t index = 0 ) throw ( std : : out_of_range ) ;
/**
As removeChild ( ) for unnamed children .
\ param index If there are > 1 unnamed children , they are indexed . Here you can supply an indexnumber .
*/
void removeChild ( size_t index = 0 ) throw ( out_of_range ) {
void removeChild ( size_t index = 0 ) throw ( std : : out_of_range ) {
removeChild ( " " , index ) ;
}
@ -269,7 +270,7 @@ namespace stfu {
\ param newName The name that the oldName - child should have
\ param index If there are > 1 children with the same name , they are indexed . Here you can supply an indexnumber .
*/
void renameChild ( const string & oldName , const string & newName , size_t index = 0 ) ;
void renameChild ( const std : : st ring & oldName , const std : : string & newName , size_t index = 0 ) ;
/**
@ -277,7 +278,7 @@ namespace stfu {
\ return Returns whether it was succesful
*/
bool read ( istream & in ) ;
bool read ( std : : istream & in ) ;
/**
Reads the STF from a file
@ -290,10 +291,10 @@ namespace stfu {
Writes the STF to an ostream with optional indentation
\ param out ostream to write to
\ param depth how much indentation to start with
\ param indent What string to use as indenation
\ param indent What std : : st ring to use as indenation
\ return Returns whether it was succesful
*/
bool write ( ostream & out , size_t depth = 0 , string indent = " \t " ) const ;
bool write ( std : : ostream & out , size_t depth = 0 , std : : string indent = " \t " ) const ;
/**
Writes to a file . Simply first opens a file and passes that ostream to itself .
@ -303,9 +304,9 @@ namespace stfu {
bool write ( const char * filename ) const ;
private :
char streamRead ( istream & in , string & out , const string & delim ) ;
char streamRead ( istream & in , string & out , const char delim ) ;
char streamSkip ( istream & in , const string & delim ) ;
char streamRead ( std : : istream & in , std : : st ring & out , const std : : string & delim ) ;
char streamRead ( std : : istream & in , std : : string & out , const char delim ) ;
char streamSkip ( std : : istream & in , const std : : string & delim ) ;
/*
returns a T2 & , not a const T2 & . The functions getValue / getChild make sure this will be done correctly for const objects
@ -313,15 +314,15 @@ namespace stfu {
const_iterators can not be transformed into iterators , and the iterator is needed for erase ( ) , which wants an iterator .
*/
template < typename T1 , typename T2 >
T2 & get_indexed ( const multimap < T1 , T2 > & container , const T1 & key , size_t index = 0 ) const throw ( out_of_range ) {
T2 & get_indexed ( const std : : multimap < T1 , T2 > & container , const T1 & key , size_t index = 0 ) const throw ( std : : out_of_range ) {
size_t count = container . count ( key ) ;
if ( count ! = 0 & & count - 1 > = index ) {
typename multimap < T1 , T2 > : : const_iterator it = container . find ( key ) ;
typename std : : multimap < T1 , T2 > : : const_iterator it = container . find ( key ) ;
while ( index - - ) it + + ;
return const_cast < T2 & > ( it - > second ) ;
} else {
throw out_of_range ( ( string ) " get_indexed-> " + " Element " + key + " doesn't exist! " ) ;
throw std : : out_of_range ( ( std : : string ) " get_indexed-> " + " Element " + key + " doesn't exist! " ) ;
}
}
@ -334,25 +335,25 @@ namespace stfu {
// while (index--) it++;
// return it;
// } else {
// throw out_of_range((string)"get_indexed_it->"+"Element " + key + "doesn't exist!");
// throw out_of_range((std::st ring)"get_indexed_it->"+"Element " + key + "doesn't exist!");
// }
// }
template < typename Container , typename T >
typename Container : : iterator get_indexed_it ( Container & container , const T & key , size_t index = 0 )
throw ( out_of_range ) {
throw ( std : : out_of_range ) {
typename Container : : iterator it = container . find ( key ) ;
while ( index - - & & it ! = container . end ( ) & & it - > first = = key ) it + + ;
if ( it = = container . end ( ) ) throw out_of_range ( " get_indexed_it(Container&, const T&, size_t) " ) ;
if ( it = = container . end ( ) ) throw std : : out_of_range ( " get_indexed_it(Container&, const T&, size_t) " ) ;
return it ;
}
} ;
typedef pair < string , string > value ;
typedef pair < string , node > child ;
typedef std : : pair < std : : st ring , std : : string > value ;
typedef std : : pair < std : : string , node > child ;
typedef multimap < string , string > : : iterator valueiterator ;
typedef multimap < string , node > : : iterator childiterator ;
typedef std : : multimap < std : : st ring , std : : string > : : iterator valueiterator ;
typedef std : : multimap < std : : string , node > : : iterator childiterator ;
}
# endif // STFU_HPP