Skip to content

Commit

Permalink
implement scary null placeholder asdfdsafasd
Browse files Browse the repository at this point in the history
  • Loading branch information
matcool committed Oct 20, 2024
1 parent ef71387 commit ed065b8
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 64 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build/
build-*/
.vscode/
.cache/
.cache/
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if (NOT COMMAND CPMAddPackage)
include(cmake/get_cpm.cmake)
endif()

CPMAddPackage("gh:geode-sdk/result#58c15d8")
CPMAddPackage("gh:geode-sdk/result#ecc518c")

# This option is only useful for Geode itself
if (DEFINED MAT_JSON_AS_INTERFACE AND MAT_JSON_AS_INTERFACE)
Expand All @@ -34,8 +34,12 @@ else()
endif()

if (PROJECT_IS_TOP_LEVEL)
if (NOT WIN32)
add_compile_options(-fsanitize=address,undefined)
add_link_options(-fsanitize=address,undefined)
endif()
target_compile_options(mat-json PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/W4 /WX>
$<$<CXX_COMPILER_ID:MSVC>:/W4>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall -Wextra -Wpedantic>
)
add_subdirectory(test)
Expand Down
6 changes: 3 additions & 3 deletions include/matjson3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ namespace matjson {
Value();
Value(std::string_view value);
Value(char const* value);
Value(bool value);
Value(Array value);
Value(std::nullptr_t);
Value(double value);
Value(std::int64_t value);
Value(std::uint64_t value);
// Value(std::int64_t value);
// Value(std::uint64_t value);
Value(bool value);

template <class T>
// Prevents implicit conversion from pointer to bool
Expand Down
6 changes: 6 additions & 0 deletions src/impl.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
#include "impl.hpp"

// oh this is evil
matjson::Value* getDummyNullValue() {
static matjson::Value nullValue = nullptr;
return &nullValue;
}
23 changes: 5 additions & 18 deletions src/impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,8 @@ class matjson::ValueImpl {
}
};

// oh alk..
#define GEODE_CONCAT_(left, right) left##right
#define GEODE_CONCAT(left, right) GEODE_CONCAT_(left, right)

#define GEODE_UNWRAP_INTO(into, ...) \
auto GEODE_CONCAT(unwrap_res_, __LINE__) = (__VA_ARGS__); \
if (GEODE_CONCAT(unwrap_res_, __LINE__).isErr()) { \
return geode::Err(GEODE_CONCAT(unwrap_res_, __LINE__).unwrapErr()); \
} \
into = GEODE_CONCAT(unwrap_res_, __LINE__).unwrap()

#define GEODE_UNWRAP(...) \
do { \
auto GEODE_CONCAT(unwrap_res_, __LINE__) = (__VA_ARGS__); \
if (GEODE_CONCAT(unwrap_res_, __LINE__).isErr()) { \
return geode::Err(GEODE_CONCAT(unwrap_res_, __LINE__).unwrapErr()); \
} \
} while (false)
using ValuePtr = std::unique_ptr<matjson::ValueImpl>;

matjson::Value* getDummyNullValue();

#define CHECK_DUMMY_NULL (this->m_impl == getDummyNullValue()->m_impl)
2 changes: 0 additions & 2 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ struct StringStream {
}
};

using ValuePtr = std::unique_ptr<ValueImpl>;

Result<ValuePtr, ParseError> parseConstant(StringStream& stream) {
GEODE_UNWRAP_INTO(auto first, stream.peek());
switch (first) {
Expand Down
22 changes: 15 additions & 7 deletions src/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
using namespace matjson;
using namespace geode;

Value::Value() : Value("Hallo") {}
Value::Value() {
m_impl = std::make_unique<ValueImpl>(Type::Object, Array{});
}

Value::Value(char const* str) : Value(std::string(str)) {}

Expand Down Expand Up @@ -36,18 +38,25 @@ Value::Value(Value const& other) {
}

Value::Value(Value&& other) {
if (other.m_impl == getDummyNullValue()->m_impl) {
m_impl = std::make_unique<ValueImpl>(Type::Null, std::monostate{});
return;
}
m_impl.swap(other.m_impl);
// this is silly but its easier than checking m_impl == nullptr for a moved Value everywhere else
other.m_impl = std::make_unique<ValueImpl>(Type::Null, std::monostate{});
}

Value::~Value() {}

Value& Value::operator=(Value value) {
if (CHECK_DUMMY_NULL) return *this;
auto key = m_impl->key();
m_impl.swap(value.m_impl);
if (key) m_impl->setKey(*key);
return *this;
}

Value::~Value() {}

Result<Value, PenisError> Value::get(std::string_view key) const {
if (this->type() != Type::Object) {
return Err("not an object");
Expand All @@ -74,17 +83,16 @@ Result<Value, PenisError> Value::get(size_t index) const {

Value& Value::operator[](std::string_view key) {
if (this->type() != Type::Object) {
return *this;
return *getDummyNullValue();
}
auto& arr = m_impl->asArray();
for (auto& value : arr) {
if (value.m_impl->key().value() == key) {
return value;
}
}
Value val;
val.m_impl->setKey(std::string(key));
arr.push_back(val);
arr.emplace_back(Value());
arr.back().m_impl->setKey(std::string(key));
return arr.back();
}

Expand Down
22 changes: 11 additions & 11 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ Include(FetchContent)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.5.2
)
# FetchContent_Declare(
# Catch2
# GIT_REPOSITORY https://github.com/catchorg/Catch2.git
# GIT_TAG v3.5.2
# )

FetchContent_MakeAvailable(Catch2)
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
# FetchContent_MakeAvailable(Catch2)
# list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)

add_executable(tests test.cpp)
target_link_libraries(tests PRIVATE mat-json Catch2::Catch2 Catch2::Catch2WithMain)
target_link_libraries(tests PRIVATE mat-json)

include(CTest)
include(Catch)
catch_discover_tests(tests)
# include(CTest)
# include(Catch)
# catch_discover_tests(tests)
25 changes: 5 additions & 20 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,10 @@
#include <matjson3.hpp>

int main() {
auto result = matjson::parse("[1,2,3,\"pen\\nis\"]");
if (result.isErr()) {
auto err = result.unwrapErr();
std::cerr << "Error: " << err << std::endl;
}
else {
auto value = result.unwrap();
// std::cout << "Value: " << value << std::endl;

std::cout << "it worked!\n";
for (auto val : value) {
std::cout << "Value type: " << (int)(val.type()) << std::endl;
}
std::cout << value.dump().unwrap() << std::endl;
}

auto json = matjson::parse("{\"hi\": 123}").unwrap();
auto& [key, val] = json["hi"];
std::cout << key << std::endl;
val = matjson::Value("wow");
std::cout << json.dump(0).unwrap() << std::endl;

auto& x = json["wow"]["crazy"];
matjson::Value test = 123.1;
test = std::move(x);
std::cout << (json["hello"]["world"]["lol"].dump().unwrap()) << std::endl;
}

0 comments on commit ed065b8

Please sign in to comment.