Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuria-Shikibe committed May 5, 2024
1 parent a036426 commit 6eafd27
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/arc/io/SpecializedIO.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace Core::IO{
}

static void read(const ext::json::JsonValue& jsonValue, T& data){
data = jsonValue.as<Ty>();
data = jsonValue.asType<Ty>();
}
};

Expand All @@ -72,8 +72,8 @@ namespace Core::IO{

static void read(const ext::json::JsonValue& jsonValue, Math::Section<T>& data){
if(auto* ptr = jsonValue.tryGetValue<ext::json::array>(); ptr && ptr->size() >= 2){
data.from = ptr->at(0).as<Ty>();
data.to = ptr->at(1).as<Ty>();
data.from = ptr->at(0).asType<Ty>();
data.to = ptr->at(1).asType<Ty>();
}
}
};
Expand All @@ -92,8 +92,8 @@ namespace Core::IO{

static void read(const ext::json::JsonValue& jsonValue, Geom::Vector2D<T>& data){
if(auto* ptr = jsonValue.tryGetValue<ext::json::array>(); ptr && ptr->size() >= 2){
data.x = ptr->at(0).as<Ty>();
data.y = ptr->at(1).as<Ty>();
data.x = ptr->at(0).asType<Ty>();
data.y = ptr->at(1).asType<Ty>();
}
}
};
Expand All @@ -115,10 +115,10 @@ namespace Core::IO{
static void read(const ext::json::JsonValue& jsonValue, Geom::Rect_Orthogonal<T>& data){
if(auto* ptr = jsonValue.tryGetValue<ext::json::array>(); ptr && ptr->size() >= 4){
data.set(
ptr->at(0).as<Ty>(),
ptr->at(1).as<Ty>(),
ptr->at(2).as<Ty>(),
ptr->at(3).as<Ty>());
ptr->at(0).asType<Ty>(),
ptr->at(1).asType<Ty>(),
ptr->at(2).asType<Ty>(),
ptr->at(3).asType<Ty>());
}
}
};
Expand All @@ -130,7 +130,7 @@ namespace Core::IO{
}

static void read(const ext::json::JsonValue& jsonValue, Graphic::Color& data){
Graphic::Color::valueOf(data, jsonValue.as<std::string>());
Graphic::Color::valueOf(data, jsonValue.as<ext::json::string>());
}
};

Expand Down
21 changes: 21 additions & 0 deletions src/arc/util/Json.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,27 @@ export namespace ext::json{
return data.index();
}


template <typename T>
requires validType<T> || (std::is_arithmetic_v<T> && validType<JsonScalarType<T>>)
[[nodiscard]] constexpr decltype(auto) asType(){
if constexpr (std::is_arithmetic_v<T>){
return std::get<JsonScalarType<T>>(data);
}else{
return std::get<T>(data);
}
}

template <typename T>
requires validType<T> || (std::is_arithmetic_v<T> && validType<JsonScalarType<T>>)
[[nodiscard]] constexpr decltype(auto) asType() const{
if constexpr (std::is_arithmetic_v<T>){
return std::get<JsonScalarType<T>>(data);
}else{
return std::get<T>(data);
}
}

template <JsonValueTag tag>
requires requires{ requires static_cast<size_t>(tag) < std::variant_size_v<decltype(data)>; }
[[nodiscard]] constexpr decltype(auto) as(){
Expand Down

0 comments on commit 6eafd27

Please sign in to comment.