AceTime  1.7.2
Date and time classes for Arduino that support timezones from the TZ Database, and a system clock that can synchronize from an NTP server or an RTC chip.
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 
28 template<uint8_t SIZE, typename ZP>
30  public:
31  ZoneProcessorCacheTemplate() = default;
32 
37  ZP* getZoneProcessor(uintptr_t zoneKey) {
38  ZP* zoneProcessor = findUsingZoneKey(zoneKey);
39  if (zoneProcessor) return zoneProcessor;
40 
41  // Allocate the next ZoneProcessor in the cache using round-robin.
42  zoneProcessor = &mZoneProcessors[mCurrentIndex];
43  mCurrentIndex++;
44  if (mCurrentIndex >= SIZE) mCurrentIndex = 0;
45  zoneProcessor->setZoneKey(zoneKey);
46  return zoneProcessor;
47  }
48 
50  ZP* getZoneProcessor(uint8_t i) {
51  return &mZoneProcessors[i];
52  }
53 
54  private:
55  // disable copy constructor and assignment operator
58  = delete;
59 
65  ZP* findUsingZoneKey(uintptr_t zoneKey) {
66  for (uint8_t i = 0; i < SIZE; i++) {
67  ZP* zoneProcessor = &mZoneProcessors[i];
68  if (zoneProcessor->equalsZoneKey(zoneKey)) {
69  return zoneProcessor;
70  }
71  }
72  return nullptr;
73  }
74 
75  uint8_t mCurrentIndex = 0;
76  ZP mZoneProcessors[SIZE];
77 };
78 
79 #if 1
80 template<uint8_t SIZE>
82  SIZE, BasicZoneProcessor> {
83 };
84 
85 template<uint8_t SIZE>
87  SIZE, ExtendedZoneProcessor> {
88 };
89 #else
90 
91 // NOTE: The following typedef seems shorter and easier to maintain. The
92 // problem is that it makes error messages basically impossible to decipher
93 // because the immensely long full template class name is printed out. There
94 // seems to be no difference in code size between the two. The compiler seems
95 // to optimize away the vtables of the parent and child classes.
96 
97 template<uint8_t SIZE>
99  SIZE, BasicZoneProcessor>;
100 
101 template<uint8_t SIZE>
103  SIZE, ExtendedZoneProcessor>;
104 #endif
105 
106 }
107 
108 #endif
ace_time::BasicZoneProcessorCache
Definition: ZoneProcessorCache.h:81
ace_time::ExtendedZoneProcessorCache
Definition: ZoneProcessorCache.h:86
ace_time::ZoneProcessorCacheTemplate::getZoneProcessor
ZP * getZoneProcessor(uint8_t i)
Return the ZoneProcessor at position i.
Definition: ZoneProcessorCache.h:50
ace_time::ExtendedZoneProcessor
A specific implementation of ExtendedZoneProcessorTemplate that uses ZoneXxxBrokers which read from z...
Definition: ExtendedZoneProcessor.h:1712
ace_time::ZoneProcessorCacheTemplate::getZoneProcessor
ZP * getZoneProcessor(uintptr_t zoneKey)
Get ZoneProcessor from either a ZoneKey, either a basic::ZoneInfo or an extended::ZoneInfo.
Definition: ZoneProcessorCache.h:37
ace_time::ZoneProcessorCacheTemplate
A cache of ZoneProcessors that provides a ZoneProcessor to the TimeZone upon request by the ZoneManag...
Definition: ZoneProcessorCache.h:29
ace_time::BasicZoneProcessor
A specific implementation of BasicZoneProcessorTemplate that uses ZoneXxxBrokers which read from zone...
Definition: BasicZoneProcessor.h:991