AceTime  2.3.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>
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
61  = delete;
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>
122  public:
124  BasicZoneProcessorCacheBase(mZoneProcessors, SIZE)
125  {}
126 
127  private:
128  // disable copy constructor and assignment operator
130  BasicZoneProcessorCache& operator=(const BasicZoneProcessorCache&) = delete;
131 
132  private:
133  BasicZoneProcessor mZoneProcessors[SIZE];
134 };
135 
146 template <uint8_t SIZE>
148  public:
150  ExtendedZoneProcessorCacheBase(mZoneProcessors, SIZE)
151  {}
152 
153  private:
154  // disable copy constructor and assignment operator
157  = delete;
158 
159  private:
160  ExtendedZoneProcessor mZoneProcessors[SIZE];
161 };
162 
173 template <uint8_t SIZE>
175  public:
177  CompleteZoneProcessorCacheBase(mZoneProcessors, SIZE)
178  {}
179 
180  private:
181  // disable copy constructor and assignment operator
184  = delete;
185 
186  private:
187  CompleteZoneProcessor mZoneProcessors[SIZE];
188 };
189 
190 }
191 
192 #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 CompleteZoneProcessorCacheBase where the cache of size SIZE is embedded into ...
A specific implementation of ExtendedZoneProcessorTemplate that uses the complete::ZoneXxxBrokers cla...
An implementation of an ExtendedZoneProcessorCacheBase where the cache of size SIZE is embedded into ...
A specific implementation of ExtendedZoneProcessorTemplate that uses the extended::ZoneXxxBrokers cla...
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.
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.