AceTime  2.2.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 "TimeOffset.h"
11 #include "OffsetDateTime.h"
12 #include "BasicZoneProcessor.h"
13 #include "ExtendedZoneProcessor.h"
14 
15 namespace ace_time {
16 
25 template <typename ZP>
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 
63  for (uint8_t i = 0; i < mSize; i++) {
64  ZP* zoneProcessor = &mZoneProcessors[i];
65  zoneProcessor->resetTransitionCache();
66  }
67  }
68 
69  private:
70  // disable copy constructor and assignment operator
72  = delete;
74  const ZoneProcessorCacheBaseTemplate&) = delete;
75 
84  ZP* findUsingZoneKey(uintptr_t zoneKey) {
85  for (uint8_t i = 0; i < mSize; i++) {
86  ZP* zoneProcessor = &mZoneProcessors[i];
87  if (zoneProcessor->equalsZoneKey(zoneKey)) {
88  return zoneProcessor;
89  }
90  }
91  return nullptr;
92  }
93 
94  private:
95  uint8_t const mSize;
96  uint8_t mCurrentIndex = 0;
97  ZP* const mZoneProcessors;
98 };
99 
104 using BasicZoneProcessorCacheBase =
105  ZoneProcessorCacheBaseTemplate<BasicZoneProcessor>;
106 
111 using ExtendedZoneProcessorCacheBase =
112  ZoneProcessorCacheBaseTemplate<ExtendedZoneProcessor>;
113 
114 #if 1
125 template <uint8_t SIZE>
127  public:
129  BasicZoneProcessorCacheBase(mZoneProcessors, SIZE)
130  {}
131 
132  private:
133  // disable copy constructor and assignment operator
135  BasicZoneProcessorCache& operator=(const BasicZoneProcessorCache&) = delete;
136 
137  private:
138  BasicZoneProcessor mZoneProcessors[SIZE];
139 };
140 
151 template <uint8_t SIZE>
153  public:
155  ExtendedZoneProcessorCacheBase(mZoneProcessors, SIZE)
156  {}
157 
158  private:
159  // disable copy constructor and assignment operator
162  = delete;
163 
164  private:
165  ExtendedZoneProcessor mZoneProcessors[SIZE];
166 };
167 #else
168 
169 // NOTE: The following typedef seems shorter and easier to maintain. The
170 // problem is that it makes error messages basically impossible to decipher
171 // because the immensely long full template class name is printed out. There
172 // seems to be no difference in code size between the two. The compiler seems
173 // to optimize away the vtables of the parent and child classes.
174 
175 template <uint8_t SIZE>
176 using BasicZoneProcessorCache = ZoneProcessorCacheTemplate<
177  SIZE, BasicZoneProcessor>;
178 
179 template <uint8_t SIZE>
180 using ExtendedZoneProcessorCache = ZoneProcessorCacheTemplate<
181  SIZE, ExtendedZoneProcessor>;
182 #endif
183 
184 }
185 
186 #endif
An implementation of a BasicZoneProcessorCacheBase where the cache of size SIZE is embedded into the ...
A specific implementation of BasicZoneProcessorTemplate that uses ZoneXxxBrokers which read from zone...
An implementation of an ExtendedZoneProcessorCacheBase where the cache of size SIZE is embedded into ...
A specific implementation of ExtendedZoneProcessorTemplate that uses ZoneXxxBrokers which read from z...
The template class of BasicZoneProcessorCacheBase or ExtendedZoneProcessorCacheBase.
ZP * getZoneProcessor(uintptr_t zoneKey)
Get ZoneProcessor from either a ZoneKey, either a basic::ZoneInfo or an extended::ZoneInfo.
void resetZoneProcessors()
Reset the transition cache of all zone processors in the cache.
ZP * getZoneProcessorAtIndex(uint8_t i)
Get the ZoneProcessor at index i.
uint8_t size() const
Return the size of the cache.
Identifiers used by implementation code which need to be publically exported.