• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 #ifndef BOOST_CONTRACT_DETAIL_STATIC_LOCAL_VAR_HPP_
3 #define BOOST_CONTRACT_DETAIL_STATIC_LOCAL_VAR_HPP_
4 
5 // Copyright (C) 2008-2018 Lorenzo Caminiti
6 // Distributed under the Boost Software License, Version 1.0 (see accompanying
7 // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
8 // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
9 
10 namespace boost { namespace contract { namespace detail {
11 
12 // This is used to hold the state of this library (already checking assertions,
13 // failure handers, mutexes, etc.). Local static variables are used instead of
14 // global or class-level static variables to avoid ODR errors when this library
15 // is used as header-only.
16 
17 // Use T's default constructor to init the local var.
18 template<typename Tag, typename T>
19 struct static_local_var {
refboost::contract::detail::static_local_var20     static T& ref() {
21         static T data;
22         return data;
23     }
24 };
25 
26 // Use `init` param to init local var (Init same as or convertible to T).
27 // NOTE: Template specializations could be used to program both this and the
28 // template above together but some pre-C++11 compilers give errors (e.g., Clang
29 // without -std=c++11), plus the `_init` postfix is more readable at call site.
30 template<typename Tag, typename T, typename Init, Init init>
31 struct static_local_var_init {
refboost::contract::detail::static_local_var_init32     static T& ref() {
33         static T data = init;
34         return data;
35     }
36 };
37 
38 } } } // namespace
39 
40 #endif // #include guard
41 
42