Updated license
[anna.git] / include / anna / core / util / TextManager.hpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #ifndef anna_core_util_TextManager_hpp
38 #define anna_core_util_TextManager_hpp
39
40 #include <vector>
41
42 #include <anna/core/functions.hpp>
43 #include <anna/core/mt/Mutex.hpp>
44
45 namespace anna {
46
47 class TextComposer;
48
49 /**
50    Gestor de textos con formato.
51 */
52 class TextManager : public Mutex {
53 public:
54   /**
55      Constructor.
56   */
57   TextManager(const char* name);
58
59   /**
60      Destructor.
61   */
62   virtual ~TextManager() { clear(); }
63
64   /**
65      Operador de acceso. El compositor solicitado deberia estar creado mediate #create.
66      \param composer Indica el numero de compositor al que deseamos acceder.
67      \return El compositor de textos.
68   */
69   TextComposer& operator [](const int composer) throw(RuntimeException) { return find(composer); }
70
71   /**
72      Operador de acceso. El compositor solicitado deberia estar creado mediate #create.
73      \param composer Indica el numero de compositor al que deseamos acceder.
74      \return El compositor de textos.
75   */
76   const TextComposer& operator [](const int composer) const throw(RuntimeException) { return find(composer); }
77
78   /**
79      Crea un nuevo compositor de textos.
80      \param composer Índice del compositor.
81      \param expression Expresion asociada al compositor que vamos a crear. El numero de variables que hay que
82      establecer antes de invocar a TextComposer::apply dependera de este texto.
83   */
84   void create(const int composer, const char* expression) throw(RuntimeException);
85
86   /**
87      Devuelve el compositor de textos asociado al numero recibido como parametro.
88      El compositor solicitado deberia estar creado mediate #create.
89      \param composer Indica el numero de compositor al que deseamos acceder.
90      \return El compositor de textos.
91   */
92   TextComposer& find(const int composer) throw(RuntimeException);
93
94   /**
95      Devuelve el compositor de textos asociado al numero recibido como parametro.
96      El compositor solicitado deberia estar creado mediate #create.
97      \param composer Indica el numero de compositor al que deseamos acceder.
98      \return El compositor de textos.
99   */
100   const TextComposer& find(const int composer) const throw(RuntimeException);
101
102   /**
103      Elimina todos los compositores de texto creados hasta el momento.
104   */
105   void clear() throw();
106
107   /**
108      Metodo que podemos reescribir para crear cualquier clase heredada de TextComposer.
109      Por defecto la impletacion sera:
110      \code
111         return new util::TextComposer (composer, expression);
112      \endcode
113      \warning La reimplementacion de este metodo nunca deberia devolver NULL.
114   */
115   virtual TextComposer* createTextComposer(const int composer, const char* expression) throw();
116
117 private:
118   typedef std::vector <TextComposer*> TextComposerVector;
119
120   const char* a_name;
121   TextComposerVector a_composers;
122
123   TextManager(const TextManager&);
124
125   TextComposer* xfind(const int composer) throw();
126 };
127
128 }
129
130 #endif
131