commit 0078850db4ba0b597176f0c7705d0d8559f4fd6a Author: Green Sky Date: Sun Oct 29 02:21:40 2023 +0200 start with doc store diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..56f48bf --- /dev/null +++ b/.gitignore @@ -0,0 +1,26 @@ +.vs/ +*.o +*.swp +~* +*~ +.idea/ +cmake-build-debug/ +cmake-build-debugandtest/ +cmake-build-release/ +*.stackdump +*.coredump +compile_commands.json +/build* +/result* +.clangd +.cache + +.DS_Store +.AppleDouble +.LSOverride + +CMakeLists.txt.user* +CMakeCache.txt + +*.tox +imgui.ini diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..d256c5b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,67 @@ +cmake_minimum_required(VERSION 3.24 FATAL_ERROR) + +# cmake setup begin +project(solanaceae_crdtnotes) + +if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) + set(SOLANACEAE_CRDTNOTES_STANDALONE ON) + # why the f do i need this >:( + set(NOT_SOLANACEAE_CRDTNOTES_STANDALONE OFF) +else() + set(SOLANACEAE_CRDTNOTES_STANDALONE OFF) + set(NOT_SOLANACEAE_CRDTNOTES_STANDALONE ON) +endif() +message("II SOLANACEAE_CRDTNOTES_STANDALONE " ${SOLANACEAE_CRDTNOTES_STANDALONE}) + +if (SOLANACEAE_CRDTNOTES_STANDALONE) + set(CMAKE_POSITION_INDEPENDENT_CODE ON) + + # defaulting to debug mode, if not specified + if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE "Debug") + endif() + + # setup my vim ycm :D + set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + + # more paths + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +endif() + +# external libs +add_subdirectory(./external) # before increasing warn levels, sad :( + +if (SOLANACEAE_CRDTNOTES_STANDALONE) + set(CMAKE_CXX_EXTENSIONS OFF) + + # bump up warning levels appropriately for clang, gcc & msvc + if (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") + add_compile_options( + -Wall -Wextra # Reasonable and standard + -Wpedantic # Warn if non-standard C++ is used + -Wunused # Warn on anything being unused + #-Wconversion # Warn on type conversions that may lose data + #-Wsign-conversion # Warn on sign conversions + -Wshadow # Warn if a variable declaration shadows one from a parent context + ) + + if (NOT WIN32) + #link_libraries(-fsanitize=address,undefined) + #link_libraries(-fsanitize=undefined) + endif() + elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC") + if (CMAKE_CXX_FLAGS MATCHES "/W[0-4]") + string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") + endif() + endif() + +endif() + +# cmake setup end + +add_subdirectory(./src) + diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt new file mode 100644 index 0000000..fabd5a0 --- /dev/null +++ b/external/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.24 FATAL_ERROR) + +include(FetchContent) + +if (NOT TARGET crdt_version3) + FetchContent_Declare(crdt_version3 + GIT_REPOSITORY https://github.com/Green-Sky/green_crdt.git + GIT_TAG master + ) + FetchContent_MakeAvailable(crdt_version3) +endif() + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..9e32f5f --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.24 FATAL_ERROR) + +add_library(solanaceae_crdtnotes + ./solanaceae/crdtnotes/crdtnotes.hpp + ./solanaceae/crdtnotes/crdtnotes.cpp +) +target_include_directories(solanaceae_crdtnotes PUBLIC .) +target_compile_features(solanaceae_crdtnotes PUBLIC cxx_std_17) +target_link_libraries(solanaceae_crdtnotes PUBLIC + crdt_version3 + #solanaceae_util +) + +######################################## + diff --git a/src/solanaceae/crdtnotes/crdtnotes.cpp b/src/solanaceae/crdtnotes/crdtnotes.cpp new file mode 100644 index 0000000..ab6fcd5 --- /dev/null +++ b/src/solanaceae/crdtnotes/crdtnotes.cpp @@ -0,0 +1,39 @@ +#include "./crdtnotes.hpp" + +CRDTNotes::CRDTNotes(void) { +} + +CRDTNotes::~CRDTNotes(void) { +} + +std::vector CRDTNotes::getDocList(void) { + std::vector list; + for (const auto& [id, doc] : _docs) { + list.push_back(id); + } + return list; +} + +const CRDTNotes::Doc* CRDTNotes::getDoc(const DocID& id) const { + auto res = _docs.find(id); + return res != _docs.cend() ? &(res->second) : nullptr; +} + +CRDTNotes::Doc* CRDTNotes::getDoc(const DocID& id) { + auto res = _docs.find(id); + return res != _docs.cend() ? &(res->second) : nullptr; +} + +CRDTNotes::Doc* CRDTNotes::addDoc(const CRDTAgent& self_agent, const DocID& id) { + if (_docs.count(id)) { + // error exists + // noop? + return nullptr; + } + + // create and set local_actor + auto& doc = _docs[id]; + doc.local_actor = self_agent; + return &doc; +} + diff --git a/src/solanaceae/crdtnotes/crdtnotes.hpp b/src/solanaceae/crdtnotes/crdtnotes.hpp new file mode 100644 index 0000000..876f0d1 --- /dev/null +++ b/src/solanaceae/crdtnotes/crdtnotes.hpp @@ -0,0 +1,50 @@ +#pragma once + +#include + +#include +#include +#include +#include + +using ID32 = std::array; + +template<> +struct std::hash { + std::size_t operator()(ID32 const& s) const noexcept { + static_assert(sizeof(size_t) == 8); + // TODO: maybe shuffle the indices a bit + return + (static_cast(s[0]) << 8*0) | + (static_cast(s[1]) << 8*1) | + (static_cast(s[2]) << 8*2) | + (static_cast(s[3]) << 8*3) | + (static_cast(s[4]) << 8*4) | + (static_cast(s[5]) << 8*5) | + (static_cast(s[6]) << 8*6) | + (static_cast(s[7]) << 8*7) + ; + } +}; + +class CRDTNotes { + using CRDTAgent = ID32; + using DocID = ID32; + using Doc = GreenCRDT::V3::TextDocument; + + // TODO: add metadata to docs + std::unordered_map _docs; + + public: + // config? + CRDTNotes(void); + ~CRDTNotes(void); + + std::vector getDocList(void); + + const Doc* getDoc(const DocID& id) const; + Doc* getDoc(const DocID& id); + + Doc* addDoc(const CRDTAgent& self_agent, const DocID& doc); +}; +