1fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Protocol Buffers - Google's data interchange format 2b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// Copyright 2014 Google Inc. All rights reserved. 3b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// https://developers.google.com/protocol-buffers/ 4fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 5fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Redistribution and use in source and binary forms, with or without 6fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// modification, are permitted provided that the following conditions are 7fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// met: 8fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 9fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// * Redistributions of source code must retain the above copyright 10fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// notice, this list of conditions and the following disclaimer. 11fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// * Redistributions in binary form must reproduce the above 12fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// copyright notice, this list of conditions and the following disclaimer 13fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// in the documentation and/or other materials provided with the 14fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// distribution. 15fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// * Neither the name of Google Inc. nor the names of its 16fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// contributors may be used to endorse or promote products derived from 17fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// this software without specific prior written permission. 18fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 19fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer#ifndef GOOGLE_PROTOBUF_STUBS_SINGLETON_H__ 31b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer#define GOOGLE_PROTOBUF_STUBS_SINGLETON_H__ 32fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 33b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer#include <google/protobuf/stubs/atomicops.h> 34b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer#include <google/protobuf/stubs/common.h> 35b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer#include <google/protobuf/stubs/once.h> 36fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 37fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace google { 38fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace protobuf { 39b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammernamespace internal { 40b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammertemplate<typename T> 41b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammerclass Singleton { 42b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer public: 43b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer static T* get() { 44b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer GoogleOnceInit(&once_, &Singleton<T>::Init); 45b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer return instance_; 46b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 47b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer static void ShutDown() { 48b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer delete instance_; 49b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer instance_ = NULL; 50b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 51b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer private: 52b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer static void Init() { 53b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer instance_ = new T(); 54b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 55b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer static ProtobufOnceType once_; 56b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer static T* instance_; 57b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer}; 58fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 59b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammertemplate<typename T> 60b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas BerghammerProtobufOnceType Singleton<T>::once_; 61fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 62b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammertemplate<typename T> 63b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas BerghammerT* Singleton<T>::instance_ = NULL; 64b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer} // namespace internal 65fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} // namespace protobuf 66fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} // namespace google 67b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 68b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer#endif // GOOGLE_PROTOBUF_STUBS_SINGLETON_H__ 69