Skip to content

Commit 98e8a6a

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

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

simplecpp.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -466,20 +466,13 @@ simplecpp::TokenList::TokenList(std::istream &istr, std::vector<std::string> &fi
466466
readfile(stream,filename,outputList);
467467
}
468468

469-
simplecpp::TokenList::TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
469+
simplecpp::TokenList::TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList, int /*unused*/)
470470
: frontToken(nullptr), backToken(nullptr), files(filenames)
471471
{
472472
StdCharBufStream stream(data, size);
473473
readfile(stream,filename,outputList);
474474
}
475475

476-
simplecpp::TokenList::TokenList(const char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
477-
: frontToken(nullptr), backToken(nullptr), files(filenames)
478-
{
479-
StdCharBufStream stream(reinterpret_cast<const unsigned char*>(data), size);
480-
readfile(stream,filename,outputList);
481-
}
482-
483476
simplecpp::TokenList::TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList)
484477
: frontToken(nullptr), backToken(nullptr), files(filenames)
485478
{

simplecpp.h

Lines changed: 44 additions & 2 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) && (__cplusplus < 202002L)
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,34 @@ 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 */
221-
TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
238+
TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
239+
: TokenList(data, size, filenames, filename, outputList, 0)
240+
{}
241+
/** generates a token list from the given buffer */
242+
TokenList(const char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
243+
: TokenList(reinterpret_cast<const unsigned char*>(data), size, filenames, filename, outputList, 0)
244+
{}
245+
#endif
246+
#if (__cplusplus >= 201703L) && (__cplusplus < 202002L)
222247
/** generates a token list from the given buffer */
223-
TokenList(const char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
248+
TokenList(std::string_view data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
249+
: TokenList(reinterpret_cast<const unsigned char*>(data.data()), data.size(), filenames, filename, outputList, 0)
250+
{}
251+
#endif
252+
#if __cplusplus >= 202002L
253+
/** generates a token list from the given buffer */
254+
TokenList(std::span<char> data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
255+
: TokenList(data.data(), data.size(), filenames, filename, outputList, 0)
256+
{}
257+
258+
/** generates a token list from the given buffer */
259+
TokenList(std::span<unsigned char> data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
260+
: TokenList(data.data(), data.size(), filenames, filename, outputList, 0)
261+
{}
262+
#endif
263+
224264
/** generates a token list from the given filename parameter */
225265
TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList = nullptr);
226266
TokenList(const TokenList &other);
@@ -296,6 +336,8 @@ namespace simplecpp {
296336
}
297337

298338
private:
339+
TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList, int unused);
340+
299341
void combineOperators();
300342

301343
void constFoldUnaryNotPosNeg(Token *tok);

0 commit comments

Comments
 (0)