git repos / blockattack-game

blame: source/code/Libs/include/cereal/details/static_object.hpp

normal view · raw

7a956470 sago007 2016-02-14 17:09 1
/*! \file static_object.hpp
7a956470 sago007 2016-02-14 17:09 2
    \brief Internal polymorphism static object support
7a956470 sago007 2016-02-14 17:09 3
    \ingroup Internal */
7a956470 sago007 2016-02-14 17:09 4
/*
7a956470 sago007 2016-02-14 17:09 5
  Copyright (c) 2014, Randolph Voorhies, Shane Grant
7a956470 sago007 2016-02-14 17:09 6
  All rights reserved.
7a956470 sago007 2016-02-14 17:09 7
  Redistribution and use in source and binary forms, with or without
7a956470 sago007 2016-02-14 17:09 8
  modification, are permitted provided that the following conditions are met:
7a956470 sago007 2016-02-14 17:09 9
      * Redistributions of source code must retain the above copyright
7a956470 sago007 2016-02-14 17:09 10
        notice, this list of conditions and the following disclaimer.
7a956470 sago007 2016-02-14 17:09 11
      * Redistributions in binary form must reproduce the above copyright
7a956470 sago007 2016-02-14 17:09 12
        notice, this list of conditions and the following disclaimer in the
7a956470 sago007 2016-02-14 17:09 13
        documentation and/or other materials provided with the distribution.
7a956470 sago007 2016-02-14 17:09 14
      * Neither the name of cereal nor the
7a956470 sago007 2016-02-14 17:09 15
        names of its contributors may be used to endorse or promote products
7a956470 sago007 2016-02-14 17:09 16
        derived from this software without specific prior written permission.
7a956470 sago007 2016-02-14 17:09 17
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
7a956470 sago007 2016-02-14 17:09 18
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
7a956470 sago007 2016-02-14 17:09 19
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
7a956470 sago007 2016-02-14 17:09 20
  DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES OR SHANE GRANT BE LIABLE FOR ANY
7a956470 sago007 2016-02-14 17:09 21
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
7a956470 sago007 2016-02-14 17:09 22
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
7a956470 sago007 2016-02-14 17:09 23
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
7a956470 sago007 2016-02-14 17:09 24
  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
7a956470 sago007 2016-02-14 17:09 25
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
7a956470 sago007 2016-02-14 17:09 26
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7a956470 sago007 2016-02-14 17:09 27
*/
7a956470 sago007 2016-02-14 17:09 28
#ifndef CEREAL_DETAILS_STATIC_OBJECT_HPP_
7a956470 sago007 2016-02-14 17:09 29
#define CEREAL_DETAILS_STATIC_OBJECT_HPP_
7a956470 sago007 2016-02-14 17:09 30
7a956470 sago007 2016-02-14 17:09 31
//! Prevent link optimization from removing non-referenced static objects
7a956470 sago007 2016-02-14 17:09 32
/*! Especially for polymorphic support, we create static objects which
7a956470 sago007 2016-02-14 17:09 33
    may not ever be explicitly referenced.  Most linkers will detect this
7a956470 sago007 2016-02-14 17:09 34
    and remove the code causing various unpleasant runtime errors.  These
7a956470 sago007 2016-02-14 17:09 35
    macros, adopted from Boost (see force_include.hpp) prevent this
7a956470 sago007 2016-02-14 17:09 36
    (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
7a956470 sago007 2016-02-14 17:09 37
    Use, modification and distribution is subject to the Boost Software
7a956470 sago007 2016-02-14 17:09 38
    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7a956470 sago007 2016-02-14 17:09 39
    http://www.boost.org/LICENSE_1_0.txt) */
7a956470 sago007 2016-02-14 17:09 40
7a956470 sago007 2016-02-14 17:09 41
#ifdef _MSC_VER
7a956470 sago007 2016-02-14 17:09 42
#   define CEREAL_DLL_EXPORT __declspec(dllexport)
7a956470 sago007 2016-02-14 17:09 43
#   define CEREAL_USED
7a956470 sago007 2016-02-14 17:09 44
#else // clang or gcc
7a956470 sago007 2016-02-14 17:09 45
#   define CEREAL_DLL_EXPORT
7a956470 sago007 2016-02-14 17:09 46
#   define CEREAL_USED __attribute__ ((__used__))
7a956470 sago007 2016-02-14 17:09 47
#endif
7a956470 sago007 2016-02-14 17:09 48
7a956470 sago007 2016-02-14 17:09 49
namespace cereal
7a956470 sago007 2016-02-14 17:09 50
{
7a956470 sago007 2016-02-14 17:09 51
  namespace detail
7a956470 sago007 2016-02-14 17:09 52
  {
7a956470 sago007 2016-02-14 17:09 53
    //! A static, pre-execution object
7a956470 sago007 2016-02-14 17:09 54
    /*! This class will create a single copy (singleton) of some
7a956470 sago007 2016-02-14 17:09 55
        type and ensures that merely referencing this type will
7a956470 sago007 2016-02-14 17:09 56
        cause it to be instantiated and initialized pre-execution.
7a956470 sago007 2016-02-14 17:09 57
        For example, this is used heavily in the polymorphic pointer
7a956470 sago007 2016-02-14 17:09 58
        serialization mechanisms to bind various archive types with
7a956470 sago007 2016-02-14 17:09 59
        different polymorphic classes */
7a956470 sago007 2016-02-14 17:09 60
    template <class T>
7a956470 sago007 2016-02-14 17:09 61
    class CEREAL_DLL_EXPORT StaticObject
7a956470 sago007 2016-02-14 17:09 62
    {
7a956470 sago007 2016-02-14 17:09 63
      private:
7a956470 sago007 2016-02-14 17:09 64
        //! Forces instantiation at pre-execution time
7a956470 sago007 2016-02-14 17:09 65
        static void instantiate( T const & ) {}
7a956470 sago007 2016-02-14 17:09 66
7a956470 sago007 2016-02-14 17:09 67
        static T & create()
7a956470 sago007 2016-02-14 17:09 68
        {
7a956470 sago007 2016-02-14 17:09 69
          static T t;
7a956470 sago007 2016-02-14 17:09 70
          instantiate(instance);
7a956470 sago007 2016-02-14 17:09 71
          return t;
7a956470 sago007 2016-02-14 17:09 72
        }
7a956470 sago007 2016-02-14 17:09 73
7a956470 sago007 2016-02-14 17:09 74
        StaticObject( StaticObject const & /*other*/ ) {}
7a956470 sago007 2016-02-14 17:09 75
7a956470 sago007 2016-02-14 17:09 76
      public:
7a956470 sago007 2016-02-14 17:09 77
        static T & getInstance()
7a956470 sago007 2016-02-14 17:09 78
        {
7a956470 sago007 2016-02-14 17:09 79
          return create();
7a956470 sago007 2016-02-14 17:09 80
        }
7a956470 sago007 2016-02-14 17:09 81
7a956470 sago007 2016-02-14 17:09 82
      private:
7a956470 sago007 2016-02-14 17:09 83
        static T & instance;
7a956470 sago007 2016-02-14 17:09 84
    };
7a956470 sago007 2016-02-14 17:09 85
7a956470 sago007 2016-02-14 17:09 86
    template <class T> T & StaticObject<T>::instance = StaticObject<T>::create();
7a956470 sago007 2016-02-14 17:09 87
  } // namespace detail
7a956470 sago007 2016-02-14 17:09 88
} // namespace cereal
7a956470 sago007 2016-02-14 17:09 89
7a956470 sago007 2016-02-14 17:09 90
#endif // CEREAL_DETAILS_STATIC_OBJECT_HPP_