MySQL++  3.3.0
noexceptions.h
Go to the documentation of this file.
1 
15 /***********************************************************************
16  Copyright (c) 2005-2007 by Educational Technology Resources, Inc.
17  Others may also hold copyrights on code in this file. See the
18  CREDITS.txt file in the top directory of the distribution for details.
19 
20  This file is part of MySQL++.
21 
22  MySQL++ is free software; you can redistribute it and/or modify it
23  under the terms of the GNU Lesser General Public License as published
24  by the Free Software Foundation; either version 2.1 of the License, or
25  (at your option) any later version.
26 
27  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
28  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
29  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
30  License for more details.
31 
32  You should have received a copy of the GNU Lesser General Public
33  License along with MySQL++; if not, write to the Free Software
34  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
35  USA
36 ***********************************************************************/
37 
38 #ifndef MYSQLPP_NOEXCEPTIONS_H
39 #define MYSQLPP_NOEXCEPTIONS_H
40 
41 #include "common.h"
42 
43 namespace mysqlpp {
44 
45 #if !defined(DOXYGEN_IGNORE)
46 // Make Doxygen ignore this
47 class MYSQLPP_EXPORT NoExceptions;
48 #endif
49 
70 
71 class MYSQLPP_EXPORT OptionalExceptions
72 {
73 public:
77  OptionalExceptions(bool e = true) :
78  exceptions_(e)
79  {
80  }
81 
83  virtual ~OptionalExceptions() { }
84 
86  void enable_exceptions() const { exceptions_ = true; }
87 
89  void disable_exceptions() const { exceptions_ = false; }
90 
92  bool throw_exceptions() const { return exceptions_; }
93 
94 protected:
99  void set_exceptions(bool e) const { exceptions_ = e; }
100 
103  friend class NoExceptions;
104 
105 private:
106  mutable bool exceptions_;
107 };
108 
109 
118 
119 class MYSQLPP_EXPORT NoExceptions
120 {
121 public:
128  assoc_(a),
129  exceptions_were_enabled_(a.throw_exceptions())
130  {
131  assoc_.disable_exceptions();
132  }
133 
134 #if __cplusplus >= 201103L
135  // C++11+ alternative to the hidden copy ctor and operator= below.
136  NoExceptions(const NoExceptions&) = delete;
137  NoExceptions& operator=(const NoExceptions&) = delete;
138 #endif
139 
144  {
145  assoc_.set_exceptions(exceptions_were_enabled_);
146  }
147 
148 private:
149  const OptionalExceptions& assoc_;
150  bool exceptions_were_enabled_;
151 
152 #if __cplusplus < 201103L
153  // Hidden assignment operator and copy ctor, because we should not
154  // be copied.
155  NoExceptions(const NoExceptions&);
156  NoExceptions& operator=(const NoExceptions&);
157 #endif
158 };
159 
160 } // end namespace mysqlpp
161 
162 #endif // MYSQLPP_NOEXCEPTIONS_H
163 
Disable exceptions in an object derived from OptionalExceptions.
Definition: noexceptions.h:120
NoExceptions(const OptionalExceptions &a)
Constructor.
Definition: noexceptions.h:127
~NoExceptions()
Destructor.
Definition: noexceptions.h:143
Interface allowing a class to have optional exceptions.
Definition: noexceptions.h:72
OptionalExceptions(bool e=true)
Default constructor.
Definition: noexceptions.h:77
void enable_exceptions() const
Enable exceptions from the object.
Definition: noexceptions.h:86
void disable_exceptions() const
Disable exceptions from the object.
Definition: noexceptions.h:89
void set_exceptions(bool e) const
Sets the exception state to a particular value.
Definition: noexceptions.h:99
bool throw_exceptions() const
Returns true if exceptions are enabled.
Definition: noexceptions.h:92
virtual ~OptionalExceptions()
Destroy object.
Definition: noexceptions.h:83
This file includes top-level definitions for use both internal to the library, and outside it....