AceRoutine  0.1
A low-memory, fast-switching, cooperative multitasking library using stackless coroutines on Arduino platforms.
FCString.h
1 /*
2 MIT License
3 
4 Copyright (c) 2018 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #ifndef ACE_ROUTINE_FCSTRING_H
26 #define ACE_ROUTINE_FCSTRING_H
27 
28 // Much of this copied from AUnit/src/aunit/FCString.h and Compare.h.
29 
30 #include <stdint.h>
31 
32 class Print;
33 class __FlashStringHelper;
34 
35 namespace ace_routine {
36 
56 class FCString {
57  public:
58  static const uint8_t kCStringType = 0;
59  static const uint8_t kFStringType = 1;
60 
62  FCString() {}
63 
65  explicit FCString(const char* s):
66  mStringType(kCStringType) {
67  mString.cstring = s;
68  }
69 
71  explicit FCString(const __FlashStringHelper* s):
72  mStringType(kFStringType) {
73  mString.fstring = s;
74  }
75 
77  uint8_t getType() const { return mStringType; }
78 
80  const char* getCString() const { return mString.cstring; }
81 
83  const __FlashStringHelper* getFString() const { return mString.fstring; }
84 
86  void print(Print& printer) const;
87 
89  void println(Print& printer) const;
90 
92  int compareTo(const FCString& that) const;
93 
94  private:
95  // NOTE: It might be possible just use a (void *) instead of a union.
96  union {
97  const char* cstring;
98  const __FlashStringHelper* fstring;
99  } mString = { nullptr };
100 
101  uint8_t mStringType = kCStringType;
102 };
103 
104 }
105 
106 #endif
const __FlashStringHelper * getFString() const
Get the flash string pointer.
Definition: FCString.h:83
uint8_t getType() const
Get the internal type of string.
Definition: FCString.h:77
void print(Print &printer) const
Convenience method for printing an FCString.
Definition: FCString.cpp:35
A union of (const char*) and (const __FlashStringHelper*) with a discriminator.
Definition: FCString.h:56
void println(Print &printer) const
Convenience method for printing an FCString.
Definition: FCString.cpp:48
const char * getCString() const
Get the c-string pointer.
Definition: FCString.h:80
FCString(const __FlashStringHelper *s)
Construct with a flash string.
Definition: FCString.h:71
int compareTo(const FCString &that) const
Compare to another FCString.
Definition: FCString.cpp:100
FCString(const char *s)
Construct with a c-string.
Definition: FCString.h:65
FCString()
Default constructor initializes to a nullptr of kCStringType.
Definition: FCString.h:62