AceTime  0.8
Date and time classes for Arduino that support timezones from the TZ Database, and a system clock that can synchronize from an NTP server or an RTC chip.
ZoneProcessorCache.h
1 /*
2  * MIT License
3  * Copyright (c) 2019 Brian T. Park
4  */
5 
6 #ifndef ACE_TIME_ZONE_PROCESSOR_CACHE_H
7 #define ACE_TIME_ZONE_PROCESSOR_CACHE_H
8 
9 #include "common/common.h"
10 #include "TimeOffset.h"
11 #include "OffsetDateTime.h"
12 #include "BasicZoneProcessor.h"
13 #include "ExtendedZoneProcessor.h"
14 #include "ZoneRegistrar.h"
15 
16 namespace ace_time {
17 
25  public:
26  static const uint8_t kTypeBasicManaged = ZoneProcessor::kTypeBasic + 2;
27  static const uint8_t kTypeExtendedManaged =
29 
31  virtual uint8_t getType() = 0;
32 
39  virtual ZoneProcessor* getZoneProcessor(const void* zoneInfo) = 0;
40 };
41 
56 template<uint8_t SIZE, uint8_t TYPE, typename ZS, typename ZI, typename ZIB>
58  public:
60 
61  uint8_t getType() override { return TYPE; }
62 
64  ZoneProcessor* getZoneProcessor(const void* zoneInfo) override {
65  ZS* zoneProcessor = findUsingZoneInfo((const ZI*) zoneInfo);
66  if (zoneProcessor) return zoneProcessor;
67 
68  // Allocate the next ZoneProcessor in the cache using round-robin.
69  zoneProcessor = &mZoneProcessors[mCurrentIndex];
70  mCurrentIndex++;
71  if (mCurrentIndex >= SIZE) mCurrentIndex = 0;
72  zoneProcessor->setZoneInfo((const ZI*) zoneInfo);
73  return zoneProcessor;
74  }
75 
76  private:
77  // disable copy constructor and assignment operator
79  ZoneProcessorCacheImpl& operator=(const ZoneProcessorCacheImpl&) = delete;
80 
86  ZS* findUsingZoneInfo(const ZI* zoneInfoKey) {
87  for (uint8_t i = 0; i < SIZE; i++) {
88  const ZI* zoneInfo = (const ZI*) mZoneProcessors[i].getZoneInfo();
89  if (zoneInfo == zoneInfoKey) {
90  return &mZoneProcessors[i];
91  }
92  }
93  return nullptr;
94  }
95 
96  ZS mZoneProcessors[SIZE];
97  uint8_t mCurrentIndex = 0;
98 };
99 
100 #if 1
101 template<uint8_t SIZE>
103  SIZE, ZoneProcessorCache::kTypeBasicManaged,
104  BasicZoneProcessor, basic::ZoneInfo, basic::ZoneInfoBroker> {
105 };
106 
107 template<uint8_t SIZE>
109  SIZE, ZoneProcessorCache::kTypeExtendedManaged,
110  ExtendedZoneProcessor, extended::ZoneInfo, extended::ZoneInfoBroker> {
111 };
112 #else
113 
114 // NOTE: The following typedef seems shorter and easier to maintain. The
115 // problem is that it makes error messages basically impossible to decipher
116 // because the immensely long full template class name is printed out. There
117 // seems to be no difference in code size between the two. The compiler seems
118 // to optimize away the vtables of the parent and child classes.
119 
120 template<uint8_t SIZE>
122  SIZE, ZoneProcessorCache::kTypeBasicManaged,
124 
125 template<uint8_t SIZE>
127  SIZE, ZoneProcessorCache::kTypeExtendedManaged,
129 #endif
130 
131 }
132 
133 #endif
Base interface for ZoneProcessor classes.
Definition: ZoneProcessor.h:45
uint8_t getType() override
Return the type of this cache.
A cache of ZoneProcessors that provides a ZoneProcessor to the TimeZone upon request.
Representation of a given time zone, implemented as an array of ZoneEra records.
Definition: ZoneInfo.h:100
static const uint8_t kTypeExtended
Indicate ExtendedZoneProcessor.
Definition: ZoneProcessor.h:57
virtual ZoneProcessor * getZoneProcessor(const void *zoneInfo)=0
Get ZoneProcessor from either a basic::ZoneInfo or an extended::ZoneInfo.
ZoneProcessor * getZoneProcessor(const void *zoneInfo) override
Get the ZoneProcessor from the zoneInfo.
Representation of a given time zone, implemented as an array of ZoneEra records.
Definition: ZoneInfo.h:100
virtual uint8_t getType()=0
Return the type of this cache.
An implementation of ZoneProcessor that supports for all zones defined by the TZ Database.
An implementation of ZoneProcessor that supports a subset of the zones containing in the TZ Database...
Data broker for accessing ZoneInfo.
Definition: Brokers.h:682
Common interface to BasicZoneProcessorCache and ExtendedZoneProcessorCache.
static const uint8_t kTypeBasic
Indicate BasicZoneProcessor.
Definition: ZoneProcessor.h:51
Data broker for accessing ZoneInfo.
Definition: Brokers.h:312