|
| 1 | +/** |
| 2 | + * custom.h - Test creation of custom objects |
| 3 | + * |
| 4 | + * Copyright 2019 mikee47 <[email protected]> |
| 5 | + * |
| 6 | + * This file is part of the FlashString Library |
| 7 | + * |
| 8 | + * This library is free software: you can redistribute it and/or modify it under the terms of the |
| 9 | + * GNU General Public License as published by the Free Software Foundation, version 3 or later. |
| 10 | + * |
| 11 | + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 12 | + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | + * See the GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License along with FlashString. |
| 16 | + * If not, see <https://www.gnu.org/licenses/>. |
| 17 | + * |
| 18 | + * @author: Nov 2019 - mikee47 <[email protected]> |
| 19 | + * |
| 20 | + ****/ |
| 21 | + |
| 22 | +#include <WString.h> |
| 23 | +#include <FlashString/Array.hpp> |
| 24 | + |
| 25 | +/* |
| 26 | + * This is the structure for our custom object, the contents of which will be |
| 27 | + * loaded from an external file. |
| 28 | + */ |
| 29 | +struct MyCustomStruct { |
| 30 | + FSTR::ObjectBase object; |
| 31 | + char name[12]; |
| 32 | + char description[20]; |
| 33 | + /* |
| 34 | + * A contained Object. This could be: |
| 35 | + * |
| 36 | + * - A String |
| 37 | + * - An Array |
| 38 | + * - Another MyCustomObject |
| 39 | + * - Another custom object |
| 40 | + * |
| 41 | + * Structures cannot contain pointers when loaded from a file, |
| 42 | + * so Vectors and Maps aren't possible. |
| 43 | + */ |
| 44 | + FSTR::ObjectBase dataArray; |
| 45 | +}; |
| 46 | + |
| 47 | +/* |
| 48 | + * Define a base object type using char[] |
| 49 | + */ |
| 50 | +class CustomObject : public FSTR::Object<CustomObject, char> |
| 51 | +{ |
| 52 | +protected: |
| 53 | + // Helper method so we can pull out text strings more easily |
| 54 | + String readString(size_t offset, size_t len) const |
| 55 | + { |
| 56 | + char buf[len + 1]; |
| 57 | + len = read(offset, buf, len); |
| 58 | + buf[len] = '\0'; |
| 59 | + return buf; |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +/* |
| 64 | + * Now derive our custom object with appropriate accessor methods |
| 65 | + */ |
| 66 | +class MyCustomObject : public CustomObject |
| 67 | +{ |
| 68 | +public: |
| 69 | + String name() const |
| 70 | + { |
| 71 | + return readString(offsetof(MyCustomStruct, name), sizeof(MyCustomStruct::name)); |
| 72 | + } |
| 73 | + |
| 74 | + String description() const |
| 75 | + { |
| 76 | + return readString(offsetof(MyCustomStruct, description), sizeof(MyCustomStruct::description)); |
| 77 | + } |
| 78 | + |
| 79 | + /* |
| 80 | + * Access the contained object as uint8_t[] |
| 81 | + */ |
| 82 | + const FSTR::Array<uint8_t>& content() const |
| 83 | + { |
| 84 | + return data()->dataArray.as<FSTR::Array<uint8_t>>(); |
| 85 | + } |
| 86 | + |
| 87 | + /* |
| 88 | + * Provide a pointer to the raw data as defined by our structure |
| 89 | + */ |
| 90 | + const MyCustomStruct* data() const |
| 91 | + { |
| 92 | + return reinterpret_cast<const MyCustomStruct*>(CustomObject::data()); |
| 93 | + } |
| 94 | +}; |
| 95 | + |
| 96 | +/* |
| 97 | + * Declare the global object (imported and defined in custom.cpp). |
| 98 | + */ |
| 99 | +DECLARE_FSTR_OBJECT(customObject, MyCustomObject); |
0 commit comments