$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 #ifndef CLOE_UTILITY_RESOURCE_HPP_
24 #define CLOE_UTILITY_RESOURCE_HPP_
25 
26 #include <cstddef>
27 #include <string>
28 
29 #ifdef INCBIN_HDR
30 #error "The incbin.h header should only be included by this file."
31 #endif
32 #define INCBIN_STYLE INCBIN_STYLE_SNAKE
33 #define INCBIN_PREFIX _blob_
34 #include <incbin.h>
35 
36 // These definitions are necessary because order of ## evaluation is undefined.
37 #define _BLOB_DATA(xName) _CONCAT(_BLOB_X(xName), _data)
38 #define _BLOB_PATH(xName) _CONCAT(_BLOB_X(xName), _path)
39 #define _BLOB_END(xName) _CONCAT(_BLOB_X(xName), _end)
40 #define _BLOB_SIZE(xName) _CONCAT(_BLOB_X(xName), _size)
41 #define _BLOB_X(xName) _CONCAT(_blob_, xName)
42 #define _CONCAT(a, b) _CONCAT2(a, b)
43 #define _CONCAT2(a, b) a##b
44 
48 #define INCLUDE_RESOURCE(xName, xFilePath) \
49  const char* _BLOB_PATH(xName) = xFilePath; \
50  INCBIN(xName, xFilePath)
51 
52 #define RESOURCE(xName) \
53  ::Resource(_BLOB_DATA(xName), _BLOB_END(xName), _BLOB_SIZE(xName), _BLOB_PATH(xName))
54 
55 class Resource {
56  public:
57  Resource(const unsigned char* data, const unsigned char* end, const unsigned int len,
58  const char* filepath)
59  : data_(reinterpret_cast<const char*>(data))
60  , end_(reinterpret_cast<const char*>(end))
61  , size_(len)
62  , filepath_(filepath) {}
63 
64  std::string to_string() const { return std::string(data_, size_); }
65 
66  size_t size() const { return size_; }
67  const char* begin() const { return data_; }
68  const char* end() const { return data_ + size_; }
69  const char* filepath() const { return filepath_; }
70 
71  private:
72  const char* data_;
73  const char* end_;
74  const size_t size_;
75  const char* filepath_;
76 };
77 
78 #endif // CLOE_UTILITY_RESOURCE_HPP_
Definition: resource.hpp:55