The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.
Dependents: hello SerialTestv11 SerialTestv12 Sierpinski ... more
mbed 2
This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.
Diff: platform/SingletonPtr.h
- Revision:
- 172:65be27845400
- Parent:
- 171:3a7713b1edbc
--- a/platform/SingletonPtr.h Thu Nov 08 11:45:42 2018 +0000 +++ b/platform/SingletonPtr.h Wed Feb 20 20:53:29 2019 +0000 @@ -7,6 +7,7 @@ */ /* mbed Microcontroller Library * Copyright (c) 2006-2013 ARM Limited + * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +24,7 @@ #ifndef SINGLETONPTR_H #define SINGLETONPTR_H +#include <stdlib.h> #include <stdint.h> #include <new> #include "platform/mbed_assert.h" @@ -88,7 +90,7 @@ * @returns * A pointer to the singleton */ - T *get() + T *get() const { if (NULL == _ptr) { singleton_lock(); @@ -108,15 +110,30 @@ * @returns * A pointer to the singleton */ - T *operator->() + T *operator->() const { return get(); } + /** Get a reference to the underlying singleton + * + * @returns + * A reference to the singleton + */ + T &operator*() const + { + return *get(); + } + // This is zero initialized when in global scope - T *_ptr; - // Force data to be 4 byte aligned - uint32_t _data[(sizeof(T) + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; + mutable T *_ptr; +#if __cplusplus >= 201103L + // Align data appropriately + alignas(T) mutable char _data[sizeof(T)]; +#else + // Force data to be 8 byte aligned + mutable uint64_t _data[(sizeof(T) + sizeof(uint64_t) - 1) / sizeof(uint64_t)]; +#endif }; #endif