Skip to content

Commit

Permalink
[struct_pack][doc] fix cnapi document (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
poor-circle authored Jan 11, 2024
1 parent bffaf8f commit 2a53295
Show file tree
Hide file tree
Showing 11 changed files with 1,307 additions and 661 deletions.
210 changes: 105 additions & 105 deletions include/ylt/struct_pack.hpp

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions include/ylt/struct_pack/compatible.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ struct compatible : public std::optional<T> {
: std::optional<T>(other){};
constexpr compatible &operator=(const compatible &other) = default;
constexpr compatible &operator=(compatible &&other) = default;
using base = std::optional<T>;
using base::base;
using std::optional<T>::optional;
friend bool operator==(const compatible<T, version> &self,
const compatible<T, version> &other) {
return static_cast<bool>(self) == static_cast<bool>(other) &&
Expand Down
4 changes: 2 additions & 2 deletions include/ylt/struct_pack/error_code.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ enum class errc {
};

namespace detail {

class struct_pack_category : public std::error_category {
public:
virtual const char *name() const noexcept override {
Expand All @@ -44,7 +43,8 @@ class struct_pack_category : public std::error_category {
return "invalid argument";
case errc::hash_conflict:
return "hash conflict";

case errc::invalid_width_of_container_length:
return "invalid width of container length";
default:
return "(unrecognized error)";
}
Expand Down
70 changes: 35 additions & 35 deletions include/ylt/struct_pack/md5_constexpr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace struct_pack {
template <typename CharType, std::size_t Size>
struct string_literal {
constexpr string_literal() = default;
constexpr string_literal(std::string_view str) : ar{} {
constexpr string_literal(std::basic_string_view<CharType> str) : ar{} {
for (size_t i = 0; i < Size; ++i) {
ar[i] = str[i];
}
Expand All @@ -39,6 +39,40 @@ struct string_literal {
}
}

template <std::size_t Size2>
constexpr bool operator!=(
const string_literal<CharType, Size2> &other) const {
if constexpr (Size == Size2) {
for (int i = 0; i < Size; ++i) {
if ((*this)[i] != other[i])
return true;
}
return false;
}
else {
return true;
}
}

template <std::size_t Size2>
constexpr bool operator==(
const string_literal<CharType, Size2> &other) const {
return !(*this != other);
}

template <size_t Size2>
string_literal<CharType, Size + Size2> constexpr operator+(
string_literal<CharType, Size2> other) const {
string_literal<CharType, Size + Size2> ret{};
for (size_t i = 0; i < Size; ++i) {
ret[i] = (*this)[i];
}
for (size_t i = 0; i < Size2; ++i) {
ret[i + Size] = other[i];
}
return ret;
}

constexpr std::size_t size() const { return Size; }

constexpr bool empty() const { return !Size; }
Expand All @@ -53,44 +87,10 @@ struct string_literal {
CharType ar[Size + 1];
};

template <typename Char, std::size_t Size1, std::size_t Size2>
constexpr bool operator!=(const string_literal<Char, Size1> &s1,
const string_literal<Char, Size2> &s2) {
if constexpr (Size1 == Size2) {
for (int i = 0; i < Size1; ++i) {
if (s1[i] != s2[i])
return true;
}
return false;
}
else {
return true;
}
}

template <typename Char, std::size_t Size1, std::size_t Size2>
constexpr bool operator==(const string_literal<Char, Size1> &s1,
const string_literal<Char, Size2> &s2) {
return !(s1 != s2);
}

template <typename CharType, std::size_t Size>
string_literal(const CharType (&value)[Size])
-> string_literal<CharType, Size - 1>;

template <typename CharType, size_t Len1, size_t Len2>
decltype(auto) constexpr operator+(string_literal<CharType, Len1> str1,
string_literal<CharType, Len2> str2) {
string_literal<CharType, Len1 + Len2> ret{};
for (size_t i = 0; i < Len1; ++i) {
ret[i] = str1[i];
}
for (size_t i = 0; i < Len2; ++i) {
ret[i + Len1] = str2[i];
}
return ret;
}

namespace MD5 {
// The implementation here is based on the pseudocode provided by Wikipedia:
// https://en.wikipedia.org/wiki/MD5#Pseudocode
Expand Down
8 changes: 3 additions & 5 deletions include/ylt/struct_pack/trivial_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

#include "reflection.hpp"

namespace struct_pack {
/*!
* \ingroup struct_pack
* \struct trivial_view
* \tparam T trivial_view指向的类型
* \brief
* trivial_view<T> is a view for trivial struct. It's equals T in type system.
* It can decrease memory copy in proto initialization/deserialization
Expand Down Expand Up @@ -74,8 +76,6 @@
* ```
*
*/

namespace struct_pack {
template <typename T, typename>
struct trivial_view {
private:
Expand All @@ -85,14 +85,12 @@ struct trivial_view {
trivial_view(const T* t) : ref(t){};
trivial_view(const T& t) : ref(&t){};
trivial_view(const trivial_view&) = default;
trivial_view(trivial_view&&) = default;
trivial_view() : ref(nullptr){};

trivial_view& operator=(const trivial_view&) = default;
trivial_view& operator=(trivial_view&&) = default;

using value_type = T;

void set(const T& obj) { ref = &obj; }
const T& get() const {
assert(ref != nullptr);
return *ref;
Expand Down
4 changes: 2 additions & 2 deletions website/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ PROJECT_NAME=yaLanTingLibs
GENERATE_HTML=yes
GENERATE_LATEX=no
INPUT=../include/ylt/struct_pack.hpp \

../include/ylt/coro_rpc/coro_rpc_server.hpp \
../include/ylt/coro_rpc/coro_rpc_client.hpp \
docs/en/coro_rpc/coro_rpc_introduction.md \
docs/en/struct_pack/struct_pack_intro.md


MARKDOWN_SUPPORT = YES
GENERATE_TREEVIEW = YES # required!
DISABLE_INDEX = NO
FULL_SIDEBAR = NO
Expand Down
3 changes: 2 additions & 1 deletion website/Doxyfile_cn
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ INPUT=docs/zh/coro_rpc/coro_rpc_doc.hpp \
docs/zh/coro_rpc/coro_rpc_intro.md \
docs/zh/struct_pack/struct_pack_doc.hpp \
docs/zh/struct_pack/struct_pack_intro.md


MARKDOWN_SUPPORT = YES
GENERATE_TREEVIEW = YES # required!
DISABLE_INDEX = NO
FULL_SIDEBAR = NO
Expand Down
2 changes: 1 addition & 1 deletion website/docs/en/struct_pack/struct_pack_intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ user can also serialize type `base` then deserialize it to the `std::unique<base
```cpp
auto ret = struct_pack::serialize(obj3{});
auto result =
struct_pack::deserialize_derived_class<base, obj1, obj2, obj3>(buffer);
struct_pack::deserialize_derived_class<base, obj1, obj2, obj3>(ret);
assert(result.has_value()); // check deserialize ok
std::unique_ptr<base> ptr = std::move(result.value());
assert(ptr != nullptr);
Expand Down
Loading

0 comments on commit 2a53295

Please sign in to comment.