Skip to content
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
2 changes: 1 addition & 1 deletion src/core/Core/Config/Command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct Command {

explicit Command(std::string name) : Name(std::move(name)) {
}
};
} __attribute__((aligned(128)));

} // namespace Litr::Config

Expand Down
2 changes: 1 addition & 1 deletion src/core/Core/Config/Parameter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct Parameter {

explicit Parameter(std::string name) : Name(std::move(name)) {
}
};
} __attribute__((aligned(128)));

} // namespace Litr::Config

Expand Down
4 changes: 2 additions & 2 deletions src/tests/Helpers/TOML.cpp → src/tests/Helpers/Toml.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "TOML.hpp"

#include <fmt/format.h>

#include "Toml.hpp"

std::pair<toml::table, toml::value> CreateTOMLMock(const std::string& name, const std::string& toml) {
std::string rFile{fmt::format(R"([{}]
{})", name, toml)};
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/tests/Tests/Core/Config/CommandBuilder.unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <doctest/doctest.h>

#include "Core/Error/Handler.hpp"
#include "Helpers/TOML.hpp"
#include "Helpers/Toml.hpp"

TEST_SUITE("Config::CommandBuilder") {
TEST_CASE("Initiates a Command on construction") {
Expand Down
10 changes: 10 additions & 0 deletions src/toms/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set(NAME "Toms")

include(${CMAKE_SOURCE_DIR}/cmake/StaticAnalyzers.cmake)

add_library(${NAME} STATIC Toms.hpp Toms/Value.cpp Toms/Value.hpp)

target_include_directories(${NAME} PUBLIC ${CMAKE_SOURCE_DIR}/src/toms)
set_target_properties(${NAME} PROPERTIES LINKER_LANGUAGE CXX)
target_compile_features(${NAME} PRIVATE cxx_std_17)
target_link_libraries(${NAME} PRIVATE project_warnings tsl::ordered_map toml11::toml11)
3 changes: 3 additions & 0 deletions src/toms/Toms.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

#include "Toms/Value.hpp"
66 changes: 66 additions & 0 deletions src/toms/Toms/Value.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "Value.hpp"

namespace Litr::Toms {

Value::Value(const std::string& path) {
try {
m_Data = toml::parse<toml::discard_comments, tsl::ordered_map>(path);
} catch (const toml::syntax_error& err) {
const toml::source_location& location{err.location()};
m_HasError = true;
m_Line = location.line();
m_Column = location.column();
m_Content = location.line_str();
}
}

Value::Value(const toml::value& data)
: m_Data(data),
m_Line(data.location().line()),
m_Column(data.location().column()),
m_Content(data.location().line_str()) {
}

Value Value::Find(const std::string& key) const {
return Value(toml::find<toml::table, toml::discard_comments, tsl::ordered_map>(m_Data, key));
}

bool Value::Contains(const std::string& key) const {
return m_Data.contains(key);
}

bool Value::IsTable() const {
return m_Data.is_table();
}

bool Value::IsString() const {
return m_Data.is_string();
}

bool Value::IsArray() const {
return m_Data.is_array();
}

Value Value::AsTable() const {
return Value(m_Data.as_table());
}

std::string Value::AsString() const {
return m_Data.as_string();
}

std::vector<Value> Value::AsArray() const {
std::vector<Value> target{};
std::copy(m_Data.as_array().begin(), m_Data.as_array().end(), std::back_inserter(target));
return target;
}

Value Value::At(size_t key) const {
return Value(m_Data.at(key));
}

Value Value::At(const std::string& key) const {
return Value(m_Data.at(key));
}

} // namespace Litr::Toms
128 changes: 128 additions & 0 deletions src/toms/Toms/Value.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#pragma once

#include <toml.hpp>
#include <tsl/ordered_map.h>

#include <variant>
#include <string>
#include <vector>

namespace Litr::Toms {

template<typename V>
class ValueIterator {
public:
using ValueType = typename V::ValueType;
using PointerType = ValueType*;
using ReferenceType = ValueType&;

public:
explicit ValueIterator(PointerType ptr) : m_Ptr(ptr) {}

// Prefix increment
ValueIterator& operator++() {
m_Ptr++;
return *this;
}

// Postfix increment
ValueIterator operator++(int) {
ValueIterator iterator{*this};
++(*this);
return iterator;
}

// Prefix decrement
ValueIterator& operator--() {
m_Ptr--;
return *this;
}

// Postfix decrement
ValueIterator operator--(int) {
ValueIterator iterator{*this};
--(*this);
return iterator;
}

ReferenceType operator[](int index) {
return *(m_Ptr + index);
}

PointerType operator->() {
return m_Ptr;
}

ReferenceType operator*() {
return *m_Ptr;
}

bool operator==(const ValueIterator& other) const {
return m_Ptr == other.m_Ptr;
}

bool operator!=(const ValueIterator& other) const {
return !(*this == other);
}

private:
PointerType m_Ptr;
};

class Value {
public:
using T = toml::basic_value<toml::discard_comments, tsl::ordered_map>;
using ValueType = T;
using Iterator = ValueIterator<Value>;

Value() = default;
explicit Value(const std::string& path);
explicit Value(const toml::value& data);

[[nodiscard]] Value Find(const std::string& key) const;
[[nodiscard]] bool Contains(const std::string& key) const;

[[nodiscard]] bool IsTable() const;
[[nodiscard]] bool IsString() const;
[[nodiscard]] bool IsArray() const;

[[nodiscard]] Value AsTable() const;
[[nodiscard]] std::string AsString() const;
[[nodiscard]] std::vector<Value> AsArray() const;

[[nodiscard]] Value At(size_t key) const;
[[nodiscard]] Value At(const std::string& key) const;

[[nodiscard]] uint32_t GetLine() const { return m_Line; }
[[nodiscard]] uint32_t GetColumn() const { return m_Column; }
[[nodiscard]] std::string GetContent() const { return m_Content; }

[[nodiscard]] bool HasError() const { return m_HasError; }
[[nodiscard]] size_t Size() const { return m_Data->size(); }

Value operator[](size_t index) const {
return At(index);
}

Value operator[](size_t index) {
return At(index);
}

Iterator begin() {
return Iterator(m_Data);
}

Iterator end() {
return Iterator(m_Data + Size());
}

private:
bool m_HasError{false};

T* m_Data{};
uint32_t m_Line{};
uint32_t m_Column{};
std::string m_Content{};
};

} // namespace Litr::Toms