Updated license
[anna.git] / include / anna / dbos / Object.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_dbos_Object_hpp
38 #define anna_dbos_Object_hpp
39
40 #include <anna/core/RuntimeException.hpp>
41
42 #include <anna/dbms/DatabaseException.hpp>
43
44 #include <anna/dbos/Loader.hpp>
45
46 namespace anna {
47
48 namespace dbos {
49
50 class Creator;
51
52 /**
53    Interfaz que deben cumplir los objetos persistentes.
54
55    Ejemplo de definicion de una clase usando esta interfaz:
56
57    \include dbos_demo.p/oodb.d/hdrs/dbos_demo.oodb.Table01.h
58
59    Ejemplo de implementacion de la clase correspondiente a la definicion:
60
61    \include dbos_demo.p/oodb.d/oodb.Table01.cc
62 */
63 class Object {
64 public:
65   /**
66      Devuelve el indice asociado a este objeto
67      \return el indice asociado a este objeto
68   */
69   Index getIndex() const throw() { return a_index; }
70
71   /**
72    * Devuelve \em true si este objeto ya existe en el medio físico (fué cargado desde allí o fue creado y grabado posteriormente) o
73    * \em false si este objeto sólo existe en la memoria intermedia.
74    * \return \em true si este objeto ya existe en el medio físico (fué cargado desde allí o fue creado y grabado posteriormente) o
75    * \em false si este objeto sólo existe en la memoria intermedia.
76    */
77   bool isStored() const throw() { return a_isStored; }
78
79 protected:
80   /**
81      Constructor.
82   */
83   Object() : a_index(0), a_isStored(false) {;}
84
85   /**
86      Inicializa este objeto con la informacion obtenida desde el medio fisico donde
87      esta grabado el objeto. Normalmente este medio fisico correspondera con una base de datos.
88
89      \param loader Cargador que contiene la informacion con la que debemos inicializar este objeto.
90
91   */
92   virtual void initialize(Loader& loader) throw(RuntimeException, dbms::DatabaseException) = 0;
93
94   /**
95      Actualiza la informacion de este objeto con la nueva informacion obtenida del medio
96      fisico.
97      \param creator Creador que contiene la informacion con la que debemos inicializar este objeto.
98   */
99   virtual void create(Creator& creator) throw(RuntimeException, dbms::DatabaseException) {;}
100
101   /**
102      Libera los recursos reservados por este objeto. Este metodo solo se invocara cuando el objeto
103      vaya a ser sacado definitivamente del area de almacenamiento.
104   */
105   virtual void destroy() throw() {;}
106
107   /**
108      Devuelve \em true si el registro del medio fisico ha cambiado respecto al registro
109      cargado en memoria o \em false en otro caso.
110
111      \param loader Cargador que contiene la informacion con la que deberiamos re-inicializar
112      este objeto.
113
114      \return \em true si el registro del medio fisico ha cambiado respecto al registro
115      cargado en memoria o \em false en otro caso.
116   */
117   virtual bool hasChanges(Loader& loader) throw(RuntimeException, dbms::DatabaseException) {
118     return true;
119   }
120
121   /**
122      Devuelve \em true si el objeto requiere comenzar el proceso de comprobacion de recarga
123      de datos \em false en otro caso.
124   */
125   virtual bool enableUpdate() const throw() { return true; }
126
127 private:
128   Index a_index;
129   bool a_isStored;
130
131   void setIndex(const Index index) throw() { a_index = index; }
132
133   friend class StorageArea;
134 };
135
136
137 }
138 }
139
140 #endif