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>
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
62  = delete;
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>
123  public:
125  BasicZoneProcessorCacheBase(mZoneProcessors, SIZE)
126  {}
127 
128  private:
129  // disable copy constructor and assignment operator
131  BasicZoneProcessorCache& operator=(const BasicZoneProcessorCache&) = delete;
132 
133  private:
134  BasicZoneProcessor mZoneProcessors[SIZE];
135 };
136 
147 template <uint8_t SIZE>
149  public:
151  ExtendedZoneProcessorCacheBase(mZoneProcessors, SIZE)
152  {}
153 
154  private:
155  // disable copy constructor and assignment operator
158  = delete;
159 
160  private:
161  ExtendedZoneProcessor mZoneProcessors[SIZE];
162 };
163 
174 template <uint8_t SIZE>
176  public:
178  CompleteZoneProcessorCacheBase(mZoneProcessors, SIZE)
179  {}
180 
181  private:
182  // disable copy constructor and assignment operator
185  = delete;
186 
187  private:
188  CompleteZoneProcessor mZoneProcessors[SIZE];
189 };
190 
191 }
192 
193 #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::Info::ZoneXxxBroke...
The template class of BasicZoneProcessorCacheBase or ExtendedZoneProcessorCacheBase.
ZP * getZoneProcessor(uintptr_t zoneKey)
Get ZoneProcessor from either a ZoneKey, basic::Info::ZoneInfo, an extended::Info::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.