libfoedus-core
FOEDUS Core Library
initializable.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
18 #ifndef FOEDUS_INITIALIZABLE_HPP_
19 #define FOEDUS_INITIALIZABLE_HPP_
20 
21 #include "foedus/cxx11.hpp"
22 #include "foedus/error_stack.hpp"
23 namespace foedus {
100  public:
101  virtual ~Initializable() {}
102 
113  virtual ErrorStack initialize() = 0;
114 
116  virtual bool is_initialized() const = 0;
117 
134  virtual ErrorStack uninitialize() = 0;
135 };
136 
157 class DefaultInitializable : public virtual Initializable {
158  public:
159  DefaultInitializable() : initialized_(false) {}
161 
163  DefaultInitializable& operator=(const DefaultInitializable&) CXX11_FUNC_DELETE;
164 
170  if (is_initialized()) {
172  }
173  ErrorStack init_error = initialize_once();
174  if (init_error.is_error()) {
175  // if error happes in the middle of initialization, we release resources we acquired.
177  return init_error;
178  }
179  initialized_ = true;
180  return kRetOk;
181  }
182 
188  if (!is_initialized()) {
189  return kRetOk;
190  }
192  initialized_ = false;
193  return kRetOk;
194  }
195 
197  return initialized_;
198  }
199 
200  virtual ErrorStack initialize_once() = 0;
201  virtual ErrorStack uninitialize_once() = 0;
202 
203  private:
204  bool initialized_;
205 };
206 
221  public:
225  enum Policy {
250  };
252  : target_(target), policy_(policy) {}
254 
255  private:
256  Initializable* target_;
257  Policy policy_;
258 };
259 
260 } // namespace foedus
261 #endif // FOEDUS_INITIALIZABLE_HPP_
Policy
Defines the behavior of this scope guard.
The pure-virtual interface to initialize/uninitialize non-trivial resources.
Automatically calls if uninitialize() wasn't called when it gets out of scope, and just complains whe...
#define ERROR_STACK(e)
Instantiates ErrorStack with the given foedus::error_code, creating an error stack with the current f...
UninitializeGuard(Initializable *target, Policy policy=kAbortIfUninitializeError)
Automatically calls if uninitialize() wasn't called when it gets out of scope, and does nothing even ...
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
virtual ErrorStack initialize()=0
Acquires resources in this object, usually called right after constructor.
ErrorStack uninitialize() override final
Typical implementation of Initializable::uninitialize() that provides uninitialize-once semantics...
Brings error stacktrace information as return value of functions.
Definition: error_stack.hpp:81
Terminates the entire program if uninitialize() wasn't called when it gets out of scope...
Typical implementation of Initializable as a skeleton base class.
Automatically calls if uninitialize() wasn't called when it gets out of scope, and terminates the ent...
#define CXX11_FINAL
Used in public headers in place of "final" of C++11.
Definition: cxx11.hpp:131
virtual ErrorStack uninitialize_once()=0
Calls Initializable::uninitialize() automatically when it gets out of scope.
ErrorStack initialize() override final
Typical implementation of Initializable::initialize() that provides initialize-once semantics...
0x0003 : "GENERAL: Already initialized" .
Definition: error_code.hpp:107
#define CXX11_OVERRIDE
Used in public headers in place of "override" of C++11.
Definition: cxx11.hpp:134
#define CXX11_FUNC_DELETE
Used in public headers in place of " = delete" of C++11.
Definition: cxx11.hpp:128
virtual ErrorStack uninitialize()=0
An idempotent method to release all resources of this object, if any.
#define CHECK_ERROR(x)
This macro calls x and checks its returned value.
const ErrorStack kRetOk
Normal return value for no-error case.
virtual ErrorStack initialize_once()=0
virtual bool is_initialized() const =0
Returns whether the object has been already initialized or not.
bool is_initialized() const override final
Returns whether the object has been already initialized or not.
bool is_error() const
Returns if this return code is not kErrorCodeOk.