$darkmode
resource.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
22 #pragma once
23 
24 #include <cstddef>
25 #include <string>
26 
27 #ifdef INCBIN_HDR
28 #error "The incbin.h header should only be included by this file."
29 #endif
30 #define INCBIN_STYLE INCBIN_STYLE_SNAKE
31 #define INCBIN_PREFIX _blob_
32 #include <incbin.h>
33 
34 // These definitions are necessary because order of ## evaluation is undefined.
35 #define _BLOB_DATA(xName) _CONCAT(_BLOB_X(xName), _data)
36 #define _BLOB_PATH(xName) _CONCAT(_BLOB_X(xName), _path)
37 #define _BLOB_END(xName) _CONCAT(_BLOB_X(xName), _end)
38 #define _BLOB_SIZE(xName) _CONCAT(_BLOB_X(xName), _size)
39 #define _BLOB_X(xName) _CONCAT(_blob_, xName)
40 #define _CONCAT(a, b) _CONCAT2(a, b)
41 #define _CONCAT2(a, b) a##b
42 
46 #define INCLUDE_RESOURCE(xName, xFilePath) \
47  const char* _BLOB_PATH(xName) = xFilePath; \
48  INCBIN(xName, xFilePath)
49 
50 #define RESOURCE(xName) \
51  ::Resource(_BLOB_DATA(xName), _BLOB_END(xName), _BLOB_SIZE(xName), _BLOB_PATH(xName))
52 
53 class Resource {
54  public:
55  Resource(const unsigned char* data, const unsigned char* end, const unsigned int len,
56  const char* filepath)
57  : data_(reinterpret_cast<const char*>(data))
58  , end_(reinterpret_cast<const char*>(end))
59  , size_(len)
60  , filepath_(filepath) {}
61 
62  std::string to_string() const { return std::string(data_, size_); }
63 
64  size_t size() const { return size_; }
65  const char* begin() const { return data_; }
66  const char* end() const { return data_ + size_; }
67  const char* filepath() const { return filepath_; }
68 
69  private:
70  const char* data_;
71  const char* end_;
72  const size_t size_;
73  const char* filepath_;
74 };
Definition: resource.hpp:53