AceTime 2.4.0 Date and time classes for Arduino that support timezones from the TZ Database.

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 "OffsetDateTime.h"
11 #include "BasicZoneProcessor.h"
12 #include "ExtendedZoneProcessor.h"
13 #include "CompleteZoneProcessor.h"
14
15 namespace ace_time {
16
25 template <typename ZP>
26 class ZoneProcessorCacheBaseTemplate {
27 public:
28 ZoneProcessorCacheBaseTemplate(ZP* zoneProcessors, uint8_t size) :
29 mSize(size),
30 mZoneProcessors(zoneProcessors)
31 {}
32
34 uint8_t size() const { return mSize; }
35
37 ZP* getZoneProcessorAtIndex(uint8_t i) { return &mZoneProcessors[i]; }
38
46 ZP* getZoneProcessor(uintptr_t zoneKey) {
47 ZP* zoneProcessor = findUsingZoneKey(zoneKey);
48 if (zoneProcessor) return zoneProcessor;
49
50 // Allocate the next ZoneProcessor in the cache using round-robin.
51 zoneProcessor = &mZoneProcessors[mCurrentIndex];
52 mCurrentIndex++;
53 if (mCurrentIndex >= mSize) mCurrentIndex = 0;
54 zoneProcessor->setZoneKey(zoneKey);
55 return zoneProcessor;
56 }
57
58 private:
59 // disable copy constructor and assignment operator
60 ZoneProcessorCacheBaseTemplate(const ZoneProcessorCacheBaseTemplate&)
61 = delete;
62 ZoneProcessorCacheBaseTemplate& operator=(
63 const ZoneProcessorCacheBaseTemplate&) = delete;
64
73 ZP* findUsingZoneKey(uintptr_t zoneKey) {
74 for (uint8_t i = 0; i < mSize; i++) {
75 ZP* zoneProcessor = &mZoneProcessors[i];
76 if (zoneProcessor->equalsZoneKey(zoneKey)) {
77 return zoneProcessor;
78 }
79 }
80 return nullptr;
81 }
82
83 private:
84 uint8_t const mSize;
85 uint8_t mCurrentIndex = 0;
86 ZP* const mZoneProcessors;
87 };
88
93 using BasicZoneProcessorCacheBase =
94 ZoneProcessorCacheBaseTemplate<BasicZoneProcessor>;
95
100 using ExtendedZoneProcessorCacheBase =
101 ZoneProcessorCacheBaseTemplate<ExtendedZoneProcessor>;
102
107 using CompleteZoneProcessorCacheBase =
108 ZoneProcessorCacheBaseTemplate<CompleteZoneProcessor>;
109
120 template <uint8_t SIZE>
121 class BasicZoneProcessorCache : public BasicZoneProcessorCacheBase {
122 public:
123 BasicZoneProcessorCache() :
124 BasicZoneProcessorCacheBase(mZoneProcessors, SIZE)
125 {}
126
127 private:
128 // disable copy constructor and assignment operator
129 BasicZoneProcessorCache(const BasicZoneProcessorCache&) = delete;
130 BasicZoneProcessorCache& operator=(const BasicZoneProcessorCache&) = delete;
131
132 private:
133 BasicZoneProcessor mZoneProcessors[SIZE];
134 };
135
146 template <uint8_t SIZE>
147 class ExtendedZoneProcessorCache: public ExtendedZoneProcessorCacheBase {
148 public:
149 ExtendedZoneProcessorCache() :
150 ExtendedZoneProcessorCacheBase(mZoneProcessors, SIZE)
151 {}
152
153 private:
154 // disable copy constructor and assignment operator
155 ExtendedZoneProcessorCache(const ExtendedZoneProcessorCache&) = delete;
156 ExtendedZoneProcessorCache& operator=(const ExtendedZoneProcessorCache&)
157 = delete;
158
159 private:
160 ExtendedZoneProcessor mZoneProcessors[SIZE];
161 };
162
173 template <uint8_t SIZE>
174 class CompleteZoneProcessorCache: public CompleteZoneProcessorCacheBase {
175 public:
176 CompleteZoneProcessorCache() :
177 CompleteZoneProcessorCacheBase(mZoneProcessors, SIZE)
178 {}
179
180 private:
181 // disable copy constructor and assignment operator
182 CompleteZoneProcessorCache(const CompleteZoneProcessorCache&) = delete;
183 CompleteZoneProcessorCache& operator=(const CompleteZoneProcessorCache&)
184 = delete;
185
186 private:
187 CompleteZoneProcessor mZoneProcessors[SIZE];
188 };
189
190 }
191
192 #endif
ace_time::BasicZoneProcessorCache
An implementation of a BasicZoneProcessorCacheBase where the cache of size SIZE is embedded into the ...
Definition: ZoneProcessorCache.h:121
ace_time::BasicZoneProcessor
A specific implementation of BasicZoneProcessorTemplate that uses ZoneXxxBrokers which read from zone...
Definition: BasicZoneProcessor.h:928
ace_time::CompleteZoneProcessorCache
An implementation of an CompleteZoneProcessorCacheBase where the cache of size SIZE is embedded into ...
Definition: ZoneProcessorCache.h:174
ace_time::CompleteZoneProcessor
A specific implementation of ExtendedZoneProcessorTemplate that uses the complete::ZoneXxxBrokers cla...
Definition: CompleteZoneProcessor.h:25
ace_time::ExtendedZoneProcessorCache
An implementation of an ExtendedZoneProcessorCacheBase where the cache of size SIZE is embedded into ...
Definition: ZoneProcessorCache.h:147
ace_time::ExtendedZoneProcessor
A specific implementation of ExtendedZoneProcessorTemplate that uses the extended::ZoneXxxBrokers cla...
Definition: ExtendedZoneProcessor.h:1180
ace_time::ZoneProcessorCacheBaseTemplate
The template class of BasicZoneProcessorCacheBase or ExtendedZoneProcessorCacheBase.
Definition: ZoneProcessorCache.h:26
ace_time::ZoneProcessorCacheBaseTemplate::getZoneProcessor
ZP * getZoneProcessor(uintptr_t zoneKey)
Get ZoneProcessor from either a ZoneKey, either a basic::ZoneInfo or an extended::ZoneInfo.
Definition: ZoneProcessorCache.h:46
ace_time::ZoneProcessorCacheBaseTemplate::getZoneProcessorAtIndex
ZP * getZoneProcessorAtIndex(uint8_t i)
Get the ZoneProcessor at index i.
Definition: ZoneProcessorCache.h:37
ace_time::ZoneProcessorCacheBaseTemplate::size
uint8_t size() const
Return the size of the cache.
Definition: ZoneProcessorCache.h:34
common.h
Identifiers used by implementation code which need to be publically exported.