Skip to content

Commit

Permalink
Merge pull request #683 from cschreib/const
Browse files Browse the repository at this point in the history
Add missing const to mustache `render` and `json::wvalue::operator[]`
  • Loading branch information
The-EDev authored Jul 22, 2023
2 parents af6b7ed + 42fab72 commit c755c4c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
20 changes: 15 additions & 5 deletions include/crow/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -1656,7 +1656,7 @@ namespace crow
}
else
{
#if defined(__APPLE__) || defined(__MACH__) || defined (__FreeBSD__) || defined (__ANDROID__)
#if defined(__APPLE__) || defined(__MACH__) || defined(__FreeBSD__) || defined(__ANDROID__)
o = std::unique_ptr<object>(new object(initializer_list));
#else
(*o) = initializer_list;
Expand All @@ -1675,7 +1675,7 @@ namespace crow
}
else
{
#if defined(__APPLE__) || defined(__MACH__) || defined (__FreeBSD__)
#if defined(__APPLE__) || defined(__MACH__) || defined(__FreeBSD__)
o = std::unique_ptr<object>(new object(value));
#else
(*o) = value;
Expand Down Expand Up @@ -1719,7 +1719,12 @@ namespace crow
return (*l)[index];
}

int count(const std::string& str)
const wvalue& operator[](unsigned index) const
{
return const_cast<wvalue*>(this)->operator[](index);
}

int count(const std::string& str) const
{
if (t_ != type::Object)
return 0;
Expand All @@ -1738,6 +1743,11 @@ namespace crow
return (*o)[str];
}

const wvalue& operator[](const std::string& str) const
{
return const_cast<wvalue*>(this)->operator[](str);
}

std::vector<std::string> keys() const
{
if (t_ != type::Object)
Expand Down Expand Up @@ -1841,9 +1851,9 @@ namespace crow
} f_state;
char outbuf[128];
#ifdef _MSC_VER
sprintf_s(outbuf, sizeof(outbuf), "%f", v.num.d);
sprintf_s(outbuf, sizeof(outbuf), "%f", v.num.d);
#else
snprintf(outbuf, sizeof(outbuf), "%f", v.num.d);
snprintf(outbuf, sizeof(outbuf), "%f", v.num.d);
#endif
char *p = &outbuf[0], *o = nullptr; // o is the position of the first trailing 0
f_state = start;
Expand Down
22 changes: 11 additions & 11 deletions include/crow/mustache.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ namespace crow
{
return body_.substr(action.start, action.end - action.start);
}
auto find_context(const std::string& name, const std::vector<context*>& stack, bool shouldUseOnlyFirstStackValue = false) const -> std::pair<bool, context&>
auto find_context(const std::string& name, const std::vector<const context*>& stack, bool shouldUseOnlyFirstStackValue = false) const -> std::pair<bool, const context&>
{
if (name == ".")
{
Expand Down Expand Up @@ -123,7 +123,7 @@ namespace crow

for (auto it = stack.rbegin(); it != stack.rend(); ++it)
{
context* view = *it;
const context* view = *it;
bool found = true;
for (auto jt = names.begin(); jt != names.end(); ++jt)
{
Expand Down Expand Up @@ -170,7 +170,7 @@ namespace crow
}
}

bool isTagInsideObjectBlock(const int& current, const std::vector<context*>& stack) const
bool isTagInsideObjectBlock(const int& current, const std::vector<const context*>& stack) const
{
int openedBlock = 0;
for (int i = current; i > 0; --i)
Expand All @@ -194,7 +194,7 @@ namespace crow
return false;
}

void render_internal(int actionBegin, int actionEnd, std::vector<context*>& stack, std::string& out, int indent) const
void render_internal(int actionBegin, int actionEnd, std::vector<const context*>& stack, std::string& out, int indent) const
{
int current = actionBegin;

Expand Down Expand Up @@ -360,7 +360,7 @@ namespace crow
rendered_template render() const
{
context empty_ctx;
std::vector<context*> stack;
std::vector<const context*> stack;
stack.emplace_back(&empty_ctx);

std::string ret;
Expand All @@ -369,9 +369,9 @@ namespace crow
}

/// Apply the values from the context provided and output a returnable template from this mustache template
rendered_template render(context& ctx) const
rendered_template render(const context& ctx) const
{
std::vector<context*> stack;
std::vector<const context*> stack;
stack.emplace_back(&ctx);

std::string ret;
Expand All @@ -380,7 +380,7 @@ namespace crow
}

/// Apply the values from the context provided and output a returnable template from this mustache template
rendered_template render(context&& ctx) const
rendered_template render(const context&& ctx) const
{
return render(ctx);
}
Expand All @@ -389,7 +389,7 @@ namespace crow
std::string render_string() const
{
context empty_ctx;
std::vector<context*> stack;
std::vector<const context*> stack;
stack.emplace_back(&empty_ctx);

std::string ret;
Expand All @@ -398,9 +398,9 @@ namespace crow
}

/// Apply the values from the context provided and output a returnable template from this mustache template
std::string render_string(context& ctx) const
std::string render_string(const context& ctx) const
{
std::vector<context*> stack;
std::vector<const context*> stack;
stack.emplace_back(&ctx);

std::string ret;
Expand Down

0 comments on commit c755c4c

Please sign in to comment.