Skip to content

Commit 8313678

Browse files
committed
added constructors with modern buffer wrappers to TokenList make it possible to hide "unsafe" ones [skip ci]
1 parent 1678b7d commit 8313678

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

simplecpp.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ simplecpp::TokenList::TokenList(std::istream &istr, std::vector<std::string> &fi
466466
readfile(stream,filename,outputList);
467467
}
468468

469+
#ifdef SIMPLECPP_UNSAFE_API
469470
simplecpp::TokenList::TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
470471
: frontToken(nullptr), backToken(nullptr), files(filenames)
471472
{
@@ -479,6 +480,7 @@ simplecpp::TokenList::TokenList(const char* data, std::size_t size, std::vector<
479480
StdCharBufStream stream(reinterpret_cast<const unsigned char*>(data), size);
480481
readfile(stream,filename,outputList);
481482
}
483+
#endif
482484

483485
simplecpp::TokenList::TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList)
484486
: frontToken(nullptr), backToken(nullptr), files(filenames)

simplecpp.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
#include <unordered_map>
2222
#include <vector>
2323

24+
#if __cplusplus >= 201703L
25+
#include <string_view>
26+
#endif
27+
#if __cplusplus >= 202002L
28+
#include <span>
29+
#endif
30+
2431
#ifdef _WIN32
2532
# ifdef SIMPLECPP_EXPORT
2633
# define SIMPLECPP_LIB __declspec(dllexport)
@@ -46,6 +53,15 @@
4653
# pragma warning(disable : 4244)
4754
#endif
4855

56+
// provide unsafe (i.e. raw pointer) API for TokenList
57+
// note: std::istream has an overhead compared to raw pointers
58+
#ifndef SIMPLECPP_UNSAFE_API
59+
// still provide the unsafe API for standards which lack the performant wrappers
60+
# if __cplusplus < 201703L
61+
# define SIMPLECPP_UNSAFE_API
62+
# endif
63+
#endif
64+
4965
namespace simplecpp {
5066
/** C code standard */
5167
enum cstd_t { CUnknown=-1, C89, C99, C11, C17, C23 };
@@ -217,10 +233,27 @@ namespace simplecpp {
217233
explicit TokenList(std::vector<std::string> &filenames);
218234
/** generates a token list from the given std::istream parameter */
219235
TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
236+
#ifdef SIMPLECPP_UNSAFE_API
220237
/** generates a token list from the given buffer */
221238
TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
222239
/** generates a token list from the given buffer */
223240
TokenList(const char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
241+
#endif
242+
#if __cplusplus >= 201703L
243+
/** generates a token list from the given buffer */
244+
TokenList(std::string_view data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
245+
: TokenList(data.data(), data.size(), filenames, filename, outputList)
246+
{
247+
}
248+
#endif
249+
#if __cplusplus >= 202002L
250+
/** generates a token list from the given buffer */
251+
TokenList(std::span<char> data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
252+
: TokenList(data.data(), data.size(), filenames, filename, outputList)
253+
{
254+
}
255+
#endif
256+
224257
/** generates a token list from the given filename parameter */
225258
TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList = nullptr);
226259
TokenList(const TokenList &other);

0 commit comments

Comments
 (0)