Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++11 good practices: constexpr instead of #define #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/gason.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "gason.h"
#include <stdlib.h>

#define JSON_ZONE_SIZE 4096
#define JSON_STACK_SIZE 32
constexpr size_t JSON_ZONE_SIZE = 4096;
constexpr size_t JSON_STACK_SIZE = 32;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use int, as (a) that is the old type, and (b) you are casting to int anyway!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this is not correct.

The parser compares this value to signed integers and the allocator to unsigned integers.

When you use a macro, it is not an int ! It is a integer literal which behavior depends on how you use it in the code. This demonstrates why macros are bad practices.

If you use an int, you have to cast to unsigned int in the allocator. If you use an unsigned int, you would have to cast it to an int in the parser.


const char *jsonStrError(int err) {
switch (err) {
Expand Down Expand Up @@ -272,15 +272,15 @@ int jsonParse(char *s, char **endptr, JsonValue *value, JsonAllocator &allocator
o = listToValue(JSON_OBJECT, tails[pos--]);
break;
case '[':
if (++pos == JSON_STACK_SIZE)
if (++pos == static_cast<int>(JSON_STACK_SIZE))
return JSON_STACK_OVERFLOW;
tails[pos] = nullptr;
tags[pos] = JSON_ARRAY;
keys[pos] = nullptr;
separator = true;
continue;
case '{':
if (++pos == JSON_STACK_SIZE)
if (++pos == static_cast<int>(JSON_STACK_SIZE))
return JSON_STACK_OVERFLOW;
tails[pos] = nullptr;
tags[pos] = JSON_OBJECT;
Expand Down
9 changes: 5 additions & 4 deletions src/gason.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once


#include <stdint.h>
#include <stddef.h>
#include <assert.h>
Expand All @@ -16,10 +17,10 @@ enum JsonTag {

struct JsonNode;

#define JSON_VALUE_PAYLOAD_MASK 0x00007FFFFFFFFFFFULL
#define JSON_VALUE_NAN_MASK 0x7FF8000000000000ULL
#define JSON_VALUE_TAG_MASK 0xF
#define JSON_VALUE_TAG_SHIFT 47
constexpr unsigned long long JSON_VALUE_PAYLOAD_MASK = 0x00007FFFFFFFFFFFULL;
constexpr unsigned long long JSON_VALUE_NAN_MASK = 0x7FF8000000000000ULL;
constexpr unsigned long long JSON_VALUE_TAG_MASK = 0xF;
constexpr unsigned long long JSON_VALUE_TAG_SHIFT = 47;

union JsonValue {
uint64_t ival;
Expand Down