AceTime 3.0.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
47 ZP* getZoneProcessor(uintptr_t zoneKey) {
48 ZP* zoneProcessor = findUsingZoneKey(zoneKey);
49 if (zoneProcessor) return zoneProcessor;
50
51 // Allocate the next ZoneProcessor in the cache using round-robin.
52 zoneProcessor = &mZoneProcessors[mCurrentIndex];
53 mCurrentIndex++;
54 if (mCurrentIndex >= mSize) mCurrentIndex = 0;
55 zoneProcessor->setZoneKey(zoneKey);
56 return zoneProcessor;
57 }
58
59 private:
60 // disable copy constructor and assignment operator
61 ZoneProcessorCacheBaseTemplate(const ZoneProcessorCacheBaseTemplate&)
62 = delete;
63 ZoneProcessorCacheBaseTemplate& operator=(
64 const ZoneProcessorCacheBaseTemplate&) = delete;
65
74 ZP* findUsingZoneKey(uintptr_t zoneKey) {
75 for (uint8_t i = 0; i < mSize; i++) {
76 ZP* zoneProcessor = &mZoneProcessors[i];
77 if (zoneProcessor->equalsZoneKey(zoneKey)) {
78 return zoneProcessor;
79 }
80 }
81 return nullptr;
82 }
83
84 private:
85 uint8_t const mSize;
86 uint8_t mCurrentIndex = 0;
87 ZP* const mZoneProcessors;
88 };
89
94 using BasicZoneProcessorCacheBase =
95 ZoneProcessorCacheBaseTemplate<BasicZoneProcessor>;
96
101 using ExtendedZoneProcessorCacheBase =
102 ZoneProcessorCacheBaseTemplate<ExtendedZoneProcessor>;
103
108 using CompleteZoneProcessorCacheBase =
109 ZoneProcessorCacheBaseTemplate<CompleteZoneProcessor>;
110
121 template <uint8_t SIZE>
122 class BasicZoneProcessorCache : public BasicZoneProcessorCacheBase {
123 public:
124 BasicZoneProcessorCache() :
125 BasicZoneProcessorCacheBase(mZoneProcessors, SIZE)
126 {}
127
128 private:
129 // disable copy constructor and assignment operator
130 BasicZoneProcessorCache(const BasicZoneProcessorCache&) = delete;
131 BasicZoneProcessorCache& operator=(const BasicZoneProcessorCache&) = delete;
132
133 private:
134 BasicZoneProcessor mZoneProcessors[SIZE];
135 };
136
147 template <uint8_t SIZE>
148 class ExtendedZoneProcessorCache: public ExtendedZoneProcessorCacheBase {
149 public:
150 ExtendedZoneProcessorCache() :
151 ExtendedZoneProcessorCacheBase(mZoneProcessors, SIZE)
152 {}
153
154 private:
155 // disable copy constructor and assignment operator
156 ExtendedZoneProcessorCache(const ExtendedZoneProcessorCache&) = delete;
157 ExtendedZoneProcessorCache& operator=(const ExtendedZoneProcessorCache&)
158 = delete;
159
160 private:
161 ExtendedZoneProcessor mZoneProcessors[SIZE];
162 };
163
174 template <uint8_t SIZE>
175 class CompleteZoneProcessorCache: public CompleteZoneProcessorCacheBase {
176 public:
177 CompleteZoneProcessorCache() :
178 CompleteZoneProcessorCacheBase(mZoneProcessors, SIZE)
179 {}
180
181 private:
182 // disable copy constructor and assignment operator
183 CompleteZoneProcessorCache(const CompleteZoneProcessorCache&) = delete;
184 CompleteZoneProcessorCache& operator=(const CompleteZoneProcessorCache&)
185 = delete;
186
187 private:
188 CompleteZoneProcessor mZoneProcessors[SIZE];
189 };
190
191 }
192
193 #endif
ace_time::BasicZoneProcessorCache
An implementation of a BasicZoneProcessorCacheBase where the cache of size SIZE is embedded into the ...
Definition: ZoneProcessorCache.h:122
ace_time::BasicZoneProcessor
A specific implementation of BasicZoneProcessorTemplate that uses ZoneXxxBrokers which read from zone...
Definition: BasicZoneProcessor.h:932
ace_time::CompleteZoneProcessorCache
An implementation of an CompleteZoneProcessorCacheBase where the cache of size SIZE is embedded into ...
Definition: ZoneProcessorCache.h:175
ace_time::CompleteZoneProcessor
A specific implementation of ExtendedZoneProcessorTemplate that uses the complete::ZoneXxxBrokers cla...
Definition: CompleteZoneProcessor.h:20
ace_time::ExtendedZoneProcessorCache
An implementation of an ExtendedZoneProcessorCacheBase where the cache of size SIZE is embedded into ...
Definition: ZoneProcessorCache.h:148
ace_time::ExtendedZoneProcessor
A specific implementation of ExtendedZoneProcessorTemplate that uses the extended::Info::ZoneXxxBroke...
Definition: ExtendedZoneProcessor.h:1170
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, basic::Info::ZoneInfo, an extended::Info::ZoneInfo,...
Definition: ZoneProcessorCache.h:47
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.