2015-09-29 19:21:02 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-08-17 22:53:46 +00:00
|
|
|
#include "DateLUTImpl.h"
|
2017-07-10 04:34:14 +00:00
|
|
|
#include <ext/singleton.h>
|
2015-09-29 19:21:02 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <atomic>
|
|
|
|
#include <mutex>
|
|
|
|
#include <memory>
|
|
|
|
|
2017-06-23 20:22:35 +00:00
|
|
|
// Also defined in Core/Defines.h
|
|
|
|
#if !defined(ALWAYS_INLINE)
|
2018-03-23 16:05:14 +00:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#define ALWAYS_INLINE __forceinline
|
|
|
|
#else
|
2017-06-23 20:22:35 +00:00
|
|
|
#define ALWAYS_INLINE __attribute__((__always_inline__))
|
|
|
|
#endif
|
2018-03-23 16:05:14 +00:00
|
|
|
#endif
|
2017-06-23 20:22:35 +00:00
|
|
|
|
2017-01-22 09:27:51 +00:00
|
|
|
|
2016-11-13 19:34:31 +00:00
|
|
|
/// This class provides lazy initialization and lookup of singleton DateLUTImpl objects for a given timezone.
|
2017-07-10 04:34:14 +00:00
|
|
|
class DateLUT : public ext::singleton<DateLUT>
|
2015-09-29 19:21:02 +00:00
|
|
|
{
|
2017-07-10 04:34:14 +00:00
|
|
|
friend class ext::singleton<DateLUT>;
|
2015-09-29 19:21:02 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
DateLUT(const DateLUT &) = delete;
|
|
|
|
DateLUT & operator=(const DateLUT &) = delete;
|
|
|
|
|
|
|
|
/// Return singleton DateLUTImpl instance for the default time zone.
|
|
|
|
static ALWAYS_INLINE const DateLUTImpl & instance()
|
|
|
|
{
|
2017-11-05 05:32:22 +00:00
|
|
|
const auto & date_lut = ext::singleton<DateLUT>::instance();
|
2017-04-01 07:20:54 +00:00
|
|
|
return *date_lut.default_impl.load(std::memory_order_acquire);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return singleton DateLUTImpl instance for a given time zone.
|
|
|
|
static ALWAYS_INLINE const DateLUTImpl & instance(const std::string & time_zone)
|
|
|
|
{
|
2017-11-05 05:32:22 +00:00
|
|
|
const auto & date_lut = ext::singleton<DateLUT>::instance();
|
2017-04-01 07:20:54 +00:00
|
|
|
if (time_zone.empty())
|
|
|
|
return *date_lut.default_impl.load(std::memory_order_acquire);
|
|
|
|
|
|
|
|
return date_lut.getImplementation(time_zone);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setDefaultTimezone(const std::string & time_zone)
|
|
|
|
{
|
2017-11-05 05:32:22 +00:00
|
|
|
auto & date_lut = ext::singleton<DateLUT>::instance();
|
2017-04-01 07:20:54 +00:00
|
|
|
const auto & impl = date_lut.getImplementation(time_zone);
|
|
|
|
date_lut.default_impl.store(&impl, std::memory_order_release);
|
|
|
|
}
|
2015-09-29 19:21:02 +00:00
|
|
|
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
DateLUT();
|
2015-09-29 19:21:02 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
const DateLUTImpl & getImplementation(const std::string & time_zone) const;
|
2016-11-13 19:34:31 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
using DateLUTImplPtr = std::unique_ptr<DateLUTImpl>;
|
2016-11-13 19:34:31 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Time zone name -> implementation.
|
|
|
|
mutable std::unordered_map<std::string, DateLUTImplPtr> impls;
|
|
|
|
mutable std::mutex mutex;
|
2016-11-13 19:34:31 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::atomic<const DateLUTImpl *> default_impl;
|
2015-09-29 19:21:02 +00:00
|
|
|
};
|