Skip to content

Commit 02af994

Browse files
committed
added constructors with modern buffer wrappers to TokenList and made it possible to hide "unsafe" ones
1 parent cfd1797 commit 02af994

2 files changed

Lines changed: 45 additions & 10 deletions

File tree

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 };
@@ -216,10 +232,34 @@ namespace simplecpp {
216232
explicit TokenList(std::vector<std::string> &filenames);
217233
/** generates a token list from the given std::istream parameter */
218234
TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
235+
#ifdef SIMPLECPP_UNSAFE_API
219236
/** generates a token list from the given buffer */
220-
TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
237+
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(data, size, filenames, filename, outputList, 0)
239+
{}
240+
/** generates a token list from the given buffer */
241+
TokenList(const char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
242+
: TokenList(reinterpret_cast<const unsigned char*>(data), size, filenames, filename, outputList, 0)
243+
{}
244+
#endif
245+
#if (__cplusplus >= 201703L) && (__cplusplus < 202002L)
221246
/** generates a token list from the given buffer */
222-
TokenList(const char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
247+
TokenList(std::string_view data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
248+
: TokenList(reinterpret_cast<const unsigned char*>(data.data()), data.size(), filenames, filename, outputList, 0)
249+
{}
250+
#endif
251+
#if __cplusplus >= 202002L
252+
/** generates a token list from the given buffer */
253+
TokenList(std::span<const char> data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
254+
: TokenList(reinterpret_cast<const unsigned char*>(data.data()), data.size(), filenames, filename, outputList, 0)
255+
{}
256+
257+
/** generates a token list from the given buffer */
258+
TokenList(std::span<const unsigned char> data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
259+
: TokenList(data.data(), data.size(), filenames, filename, outputList, 0)
260+
{}
261+
#endif
262+
223263
/** generates a token list from the given filename parameter */
224264
TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList = nullptr);
225265
TokenList(const TokenList &other);
@@ -295,6 +335,8 @@ namespace simplecpp {
295335
}
296336

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

300342
void constFoldUnaryNotPosNeg(Token *tok);

0 commit comments

Comments
 (0)