Remove dynamic exceptions
[anna.git] / include / anna / app / functions.hpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #ifndef anna_app_functions_hpp
10 #define anna_app_functions_hpp
11
12 #include <anna/core/functions.hpp>
13
14 #include <anna/app/Application.hpp>
15
16 namespace anna {
17
18 namespace app {
19
20 class Application;
21
22 /**
23    functions - Metodos y variables
24 */
25 struct functions : public anna::functions {
26   /**
27      Devuelve la referencia de la instancia de nuestra aplicacion
28   */
29   static Application& getApp() noexcept(false);
30
31
32   /**
33      Pattern to obtain a multi named application component instance easily.
34      Parameters are usually replaced by the macro C <b>FILE_LOCATION</b>.
35
36      \param className Application component class name
37      \param fromFile File which called the method
38      \param fromLine Line number within the file from where the method is called.
39
40      \return Application component instance for the class provided at the pattern
41      \see Component
42   */
43   template <typename T> static T* componentByName(const char *className, const char* fromFile, const int fromLine)
44   noexcept(false) {
45     T* result = static_cast <T*>(functions::getApp().find(className));
46
47     if(result == NULL) {
48       std::string msg(className);
49       msg += " | Componente no registrado";
50       throw RuntimeException(msg, fromFile, fromLine);
51     }
52
53     return result;
54   }
55 };
56
57   /**
58      Pattern to obtain a single named application component instance easily.
59      Parameters are usually replaced by the macro C <b>FILE_LOCATION</b>.
60
61      \param fromFile File which called the method
62      \param fromLine Line number within the file from where the method is called.
63
64      \return Application component instance for the class provided at the pattern
65      \warning T class must implement a method in the form:
66      \code
67          static const char* getClassName () ;
68      \endcode
69      \see Component
70   */
71   template <typename T> static T* component(const char* fromFile, const int fromLine)
72   noexcept(false) {
73     return functions::componentByName<T> (T::getClassName(), fromFile, fromLine);
74   }
75
76 }
77 }
78
79 #endif
80
81