1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 # This software is governed by the CeCILL-B license under French law and
10 # abiding by the rules of distribution of free software. You can use,
11 # modify and/ or redistribute the software under the terms of the CeCILL-B
12 # license as circulated by CEA, CNRS and INRIA at the following URL
13 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 # or in the file LICENSE.txt.
16 # As a counterpart to the access to the source code and rights to copy,
17 # modify and redistribute granted by the license, users are provided only
18 # with a limited warranty and the software's author, the holder of the
19 # economic rights, and the successive licensors have only limited
22 # The fact that you are presently reading this means that you have had
23 # knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
27 //----------------------------------------------------------------------------------------------------------------
28 // Class definition include
29 //----------------------------------------------------------------------------------------------------------------
30 #include "PrefixMaxKeyGenerator.h"
32 //----------------------------------------------------------------------------------------------------------------
33 // Class implementation
34 //----------------------------------------------------------------------------------------------------------------
35 /** @file PrefixMaxKeyGenerator.cxx */
37 //------------------------------------------------------------------------------------------------------------
38 // Constructors & Destructors
39 //------------------------------------------------------------------------------------------------------------
42 * Creates the prefix+max key generator
44 PrefixMaxKeyGenerator :: PrefixMaxKeyGenerator()
46 numberOfKeyThings = 0;
50 * Destroys the outline manager
52 PrefixMaxKeyGenerator :: ~PrefixMaxKeyGenerator()
57 //------------------------------------------------------------------------------------------------------------
59 //------------------------------------------------------------------------------------------------------------
63 * Adds a key thing to the keyThings building the respective KeyThing (new keyThing).
64 * @param theName Is the name of the new keyThing to include. If the name is not unique, returns false.
65 * @param thePrefix Is the prefix of the new keyThing for the key generation correponding to the new keyThing
66 * @param theMax Is the maximum value for the key generation correponding to the new keyThing
67 * @return Returns true if the keyThing could be added of not.
69 bool PrefixMaxKeyGenerator :: addKeyThing( std::string theName, std::string thePrefix, int theMax )
71 KeyThing * kThing = new KeyThing(thePrefix, theMax);
72 int before = keyThings.size();
73 keyThings.insert(std::pair < std::string, KeyThing >( theName, *kThing ));
74 int after = keyThings.size();
75 return after > before;
80 * @param theName Is the name of the keyThing to remove.
82 void PrefixMaxKeyGenerator :: removeKeyThing( std::string theName )
84 keyThings.erase(theName);
89 * Indicates if a key thing existis in the generator
90 * @param theName Is the name of the keyThing to search.
91 * @return Returns true if the keyThing exists in the keyThings.
93 bool PrefixMaxKeyGenerator :: existsKeyThing( std::string theName )
95 return keyThings.find(theName) != keyThings.end();
99 * Updates the maximum value of a key thing if necesary (posibleMax>theMaxOfKeyThing). If the key thing doesn't exist nothing is done.
100 * @param theName Is the name of the keyThing to update.
101 * @param posibleMax Is the number that corresponds to a posible max value of the keyThing to update.
103 void PrefixMaxKeyGenerator :: updateMaxTo( std::string theName, int posibleMax )
105 std::map<std::string, KeyThing >::iterator iterP = keyThings.find(theName);
106 if ( iterP != keyThings.end() )
108 int max = (iterP->second).getValue();
109 if ( max < posibleMax )
110 (iterP->second).setValue(posibleMax);
115 * Generates a (std::string) key for a given keyThing. If the key thing doesn't exist nothing is done and returns false.
116 * @param theName Is the name of the keyThing to search.
117 * @param theInputString Is string to load the generated key formed like <prefixOfTheKeyThing> <maxOfTheKeyThing>
118 * @return theKey Returns true if the key was generated successfully. (If theName is an existent keyThing)
120 bool PrefixMaxKeyGenerator :: generateKeyOf( std::string theName, std::string &theInputString )
123 std::map<std::string, KeyThing >::iterator iterP = keyThings.find(theName);
124 if ( iterP != keyThings.end() )
126 KeyThing kthing = (iterP->second);
127 int max = kthing.getValue();
128 std::ostringstream genKey;
129 genKey<<kthing.getPrefix() << " " << max;
130 theInputString = genKey.str();
137 * Generates a (std::string) key for a given keyThing and updates the max value of it if necesary. If the key thing doesn't exist nothing is done.
138 * @param theName Is the name of the keyThing to search.
139 * @param posibleMax Is the number that corresponds to a posible max value of the keyThing to update.
140 * @param theInputString Is string to load the generated key formed like <prefixOfTheKeyThing> <maxOfTheKeyThing>
141 * @return Returns true if the key was generated successfully. (If theName is an existent keyThing)
143 bool PrefixMaxKeyGenerator :: generateKeyOf( std::string theName, int posibleMax, std::string &theInputString )
146 std::map<std::string, KeyThing >::iterator iterP = keyThings.find(theName);
147 if ( iterP != keyThings.end() )
149 KeyThing kthing = (iterP->second);
150 int max = kthing.getValue();
152 if ( max < posibleMax )
154 kthing.setValue(posibleMax);
158 std::ostringstream genKey;
159 genKey<<kthing.getPrefix() << " " << kthing.getValue();
160 theInputString = genKey.str();
167 * Gets the prefix of a specific keyThing identified with the name in the parameter, if the key thing doesn't exists return false.
168 * @param theName Is the name of the keyThing to search the prefix.
169 * @param theInputString Is string to load the prefix of the searched keyThing.
170 * @return isStringOutputReal Returns if the loaded string in theInputString is real (i.e if the -theName- keyThing exists)
172 bool PrefixMaxKeyGenerator :: getPrefixOf( std::string theName, std::string &theInputString )
175 std::map<std::string, KeyThing >::iterator iterP = keyThings.find(theName);
176 if ( iterP != keyThings.end() )
178 KeyThing kthing = (iterP->second);
179 theInputString = kthing.getPrefix();
186 * Gets the max value of a specific keyThing identified with the name in the parameter. If the key thing doesn't exists returns -1.
187 * @param theName Is the name of the keyThing to search the maximum.
188 * @return theMax Returns the maxumum value for key generation at the keyThing. Returns -1 if not finded keyThing.
190 int PrefixMaxKeyGenerator :: getCurrentMaxOf( std::string theName )
192 std::map<std::string, KeyThing >::iterator iterP = keyThings.find(theName);
193 if ( iterP != keyThings.end() )
195 KeyThing kthing = (iterP->second);
196 return kthing.getValue();
202 * Gets the total of keyThings managed
203 * @return Retuns the total of keyThing managed
205 int PrefixMaxKeyGenerator :: getTotalThingsNumber()
207 return keyThings.size();
211 * Clears the generator deleating the existring keyThings
213 void PrefixMaxKeyGenerator :: clear()
215 std::map<std::string, KeyThing >::iterator iter = keyThings.begin();
216 while( iter != keyThings.end() )
218 keyThings.erase(iter);
221 numberOfKeyThings = 0;