AceCommon  1.1.2
Arduino library for low-level common functions and features with no external dependencies
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 FSTRINGS_FCSTRING_H
26 #define FSTRINGS_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_common {
36 
47 class FCString {
48  public:
50  static const uint8_t kCStringType = 0;
51 
53  static const uint8_t kFStringType = 1;
54 
56  FCString() {}
57 
59  explicit FCString(const char* s):
60  mStringType(kCStringType) {
61  mString.cstring = s;
62  }
63 
65  explicit FCString(const __FlashStringHelper* s):
66  mStringType(kFStringType) {
67  mString.fstring = s;
68  }
69 
71  bool isNull() const { return mString.cstring == nullptr; }
72 
74  uint8_t getType() const { return mStringType; }
75 
77  const char* getCString() const { return mString.cstring; }
78 
80  const __FlashStringHelper* getFString() const { return mString.fstring; }
81 
88  size_t printTo(Print& printer) const;
89 
91  int compareTo(const FCString& that) const;
92 
93  private:
94  // NOTE: It might be possible just use a (void *) instead of a union.
95  union {
96  const char* cstring;
97  const __FlashStringHelper* fstring;
98  } mString = { nullptr };
99 
100  uint8_t mStringType = kCStringType;
101 };
102 
103 }
104 
105 #endif
ace_common::FCString::isNull
bool isNull() const
Return if this is a null string.
Definition: FCString.h:71
ace_common::FCString::getCString
const char * getCString() const
Get the c-string pointer.
Definition: FCString.h:77
ace_common::FCString::FCString
FCString(const __FlashStringHelper *s)
Construct with a flash string.
Definition: FCString.h:65
ace_common::FCString::printTo
size_t printTo(Print &printer) const
Convenience method for printing an FCString to printer.
Definition: FCString.cpp:35
ace_common::FCString::FCString
FCString(const char *s)
Construct with a c-string.
Definition: FCString.h:59
ace_common::FCString::FCString
FCString()
Default constructor initializes to a nullptr of kCStringType.
Definition: FCString.h:56
ace_common::FCString
A union of (const char*) and (const __FlashStringHelper*) with a discriminator.
Definition: FCString.h:47
ace_common::FCString::compareTo
int compareTo(const FCString &that) const
Compare to another FCString.
Definition: FCString.cpp:86
ace_common::FCString::getFString
const __FlashStringHelper * getFString() const
Get the flash string pointer.
Definition: FCString.h:80
ace_common::FCString::kFStringType
static const uint8_t kFStringType
Identifies the object as holding an f-string.
Definition: FCString.h:53
ace_common::FCString::getType
uint8_t getType() const
Get the internal type of string.
Definition: FCString.h:74
ace_common::FCString::kCStringType
static const uint8_t kCStringType
Identifies the object as holding a c-string.
Definition: FCString.h:50