AceTime  1.11.2
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 
43  ZP* getZoneProcessor(uintptr_t zoneKey) {
44  ZP* zoneProcessor = findUsingZoneKey(zoneKey);
45  if (zoneProcessor) return zoneProcessor;
46 
47  // Allocate the next ZoneProcessor in the cache using round-robin.
48  zoneProcessor = &mZoneProcessors[mCurrentIndex];
49  mCurrentIndex++;
50  if (mCurrentIndex >= mSize) mCurrentIndex = 0;
51  zoneProcessor->setZoneKey(zoneKey);
52  return zoneProcessor;
53  }
54 
55  private:
56  // disable copy constructor and assignment operator
58  = delete;
60  const ZoneProcessorCacheBaseTemplate&) = delete;
61 
67  ZP* findUsingZoneKey(uintptr_t zoneKey) {
68  for (uint8_t i = 0; i < mSize; i++) {
69  ZP* zoneProcessor = &mZoneProcessors[i];
70  if (zoneProcessor->equalsZoneKey(zoneKey)) {
71  return zoneProcessor;
72  }
73  }
74  return nullptr;
75  }
76 
77  private:
78  uint8_t const mSize;
79  uint8_t mCurrentIndex = 0;
80  ZP* const mZoneProcessors;
81 };
82 
87 using BasicZoneProcessorCacheBase =
88  ZoneProcessorCacheBaseTemplate<BasicZoneProcessor>;
89 
94 using ExtendedZoneProcessorCacheBase =
95  ZoneProcessorCacheBaseTemplate<ExtendedZoneProcessor>;
96 
97 #if 1
98 
108 template <uint8_t SIZE>
110  public:
112  BasicZoneProcessorCacheBase(mZoneProcessors, SIZE)
113  {}
114 
115  private:
116  // disable copy constructor and assignment operator
118  BasicZoneProcessorCache& operator=(const BasicZoneProcessorCache&) = delete;
119 
120  private:
121  BasicZoneProcessor mZoneProcessors[SIZE];
122 };
123 
134 template <uint8_t SIZE>
136  public:
138  ExtendedZoneProcessorCacheBase(mZoneProcessors, SIZE)
139  {}
140 
141  private:
142  // disable copy constructor and assignment operator
145  = delete;
146 
147  private:
148  ExtendedZoneProcessor mZoneProcessors[SIZE];
149 };
150 #else
151 
152 // NOTE: The following typedef seems shorter and easier to maintain. The
153 // problem is that it makes error messages basically impossible to decipher
154 // because the immensely long full template class name is printed out. There
155 // seems to be no difference in code size between the two. The compiler seems
156 // to optimize away the vtables of the parent and child classes.
157 
158 template <uint8_t SIZE>
159 using BasicZoneProcessorCache = ZoneProcessorCacheTemplate<
160  SIZE, BasicZoneProcessor>;
161 
162 template <uint8_t SIZE>
163 using ExtendedZoneProcessorCache = ZoneProcessorCacheTemplate<
164  SIZE, ExtendedZoneProcessor>;
165 #endif
166 
167 }
168 
169 #endif
ace_time::BasicZoneProcessorCache
An implementation of a BasicZoneProcessorCacheBase where the cache of size SIZE is embedded into the ...
Definition: ZoneProcessorCache.h:109
ace_time::ZoneProcessorCacheBaseTemplate::size
uint8_t size() const
Return the size of the cache.
Definition: ZoneProcessorCache.h:34
ace_time::ZoneProcessorCacheBaseTemplate::getZoneProcessorAtIndex
ZP * getZoneProcessorAtIndex(uint8_t i)
Get the ZoneProcessor at index i.
Definition: ZoneProcessorCache.h:37
ace_time::ExtendedZoneProcessorCache
An implementation of an ExtendedZoneProcessorCacheBase where the cache of size SIZE is embedded into ...
Definition: ZoneProcessorCache.h:135
ace_time::ExtendedZoneProcessor
A specific implementation of ExtendedZoneProcessorTemplate that uses ZoneXxxBrokers which read from z...
Definition: ExtendedZoneProcessor.h:2156
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:43
ace_time::BasicZoneProcessor
A specific implementation of BasicZoneProcessorTemplate that uses ZoneXxxBrokers which read from zone...
Definition: BasicZoneProcessor.h:1022