-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #842 from mtao/mtao/attribute_fetcher
Method for using structs / json to access attributes
- Loading branch information
Showing
27 changed files
with
930 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
components/multimesh/src/wmtk/components/multimesh/utils/AttributeDescription.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include "AttributeDescription.hpp" | ||
#include <nlohmann/json.hpp> | ||
|
||
#include <wmtk/components/utils/json_serialize_enum.hpp> | ||
namespace wmtk::attribute { | ||
// TODO: this definitely will cause a conflict someday if someone else wants to serialize | ||
// attributetype | ||
WMTK_NLOHMANN_JSON_SERIALIZE_ENUM( | ||
AttributeType, | ||
{ | ||
{AttributeType::Char, "char"}, | ||
{AttributeType::Double, "double"}, | ||
{AttributeType::Int64, "int"}, | ||
{AttributeType::Rational, "rational"}, | ||
}) | ||
} // namespace wmtk::attribute | ||
|
||
namespace wmtk::components::multimesh::utils { | ||
auto AttributeDescription::operator<=>(const AttributeDescription&) const -> std::strong_ordering = | ||
default; | ||
auto AttributeDescription::operator==(const AttributeDescription&) const -> bool = default; | ||
|
||
namespace { | ||
NLOHMANN_JSON_SERIALIZE_ENUM( | ||
PrimitiveType, | ||
{ | ||
{PrimitiveType::Vertex, 0}, | ||
{PrimitiveType::Edge, 1}, | ||
{PrimitiveType::Triangle, 2}, | ||
{PrimitiveType::Tetrahedron, 3}, | ||
}) | ||
} // namespace | ||
WMTK_NLOHMANN_JSON_FRIEND_TO_JSON_PROTOTYPE(AttributeDescription) | ||
{ | ||
WMTK_NLOHMANN_ASSIGN_TYPE_TO_JSON(path); | ||
if (nlohmann_json_t.dimension.has_value()) { | ||
nlohmann_json_j["dimension"] = nlohmann_json_t.dimension.value(); | ||
} | ||
if (nlohmann_json_t.type.has_value()) { | ||
nlohmann_json_j["type"] = nlohmann_json_t.type.value(); | ||
} | ||
} | ||
WMTK_NLOHMANN_JSON_FRIEND_FROM_JSON_PROTOTYPE(AttributeDescription) | ||
{ | ||
if (nlohmann_json_j.is_string()) { | ||
nlohmann_json_t.path = nlohmann_json_j.get<std::string>(); | ||
} else { | ||
nlohmann_json_t.path = nlohmann_json_j["path"]; | ||
} | ||
if (nlohmann_json_j.contains("dimension")) { | ||
nlohmann_json_t.dimension = nlohmann_json_j["dimension"]; | ||
} | ||
if (nlohmann_json_j.contains("type")) { | ||
nlohmann_json_t.type = nlohmann_json_j["type"]; | ||
} | ||
} | ||
|
||
std::optional<PrimitiveType> AttributeDescription::primitive_type() const | ||
{ | ||
if (this->dimension.has_value()) { | ||
int8_t d = this->dimension.value(); | ||
assert(d < 4); | ||
|
||
return get_primitive_type_from_id(d); | ||
} else { | ||
return {}; | ||
} | ||
} | ||
|
||
} // namespace wmtk::components::multimesh::utils |
59 changes: 59 additions & 0 deletions
59
components/multimesh/src/wmtk/components/multimesh/utils/AttributeDescription.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#pragma once | ||
#include <compare> | ||
#include <optional> | ||
#include <wmtk/PrimitiveType.hpp> | ||
#include <wmtk/attribute/AttributeType.hpp> | ||
#include <wmtk/components/utils/json_macros.hpp> | ||
|
||
namespace wmtk::attribute { | ||
WMTK_NLOHMANN_JSON_DECLARATION(AttributeType) | ||
} | ||
|
||
namespace wmtk::components::multimesh::utils { | ||
|
||
|
||
// the minimal information to uniquely extract an attribute handle | ||
struct AttributeDescription | ||
{ | ||
std::string path; | ||
std::optional<uint8_t> dimension; | ||
std::optional<attribute::AttributeType> type; | ||
|
||
|
||
AttributeDescription() = default; | ||
AttributeDescription(const AttributeDescription&) = default; | ||
AttributeDescription(AttributeDescription&&) = default; | ||
AttributeDescription& operator=(const AttributeDescription&) = default; | ||
AttributeDescription& operator=(AttributeDescription&&) = default; | ||
~AttributeDescription() = default; | ||
|
||
AttributeDescription( | ||
const std::string_view& p, | ||
const std::optional<uint8_t>& dim, | ||
const std::optional<attribute::AttributeType>& t) | ||
: path(p) | ||
, dimension(dim) | ||
, type(t) | ||
{} | ||
|
||
explicit AttributeDescription( | ||
const std::string_view& p, | ||
const std::optional<PrimitiveType>& pt, | ||
const std::optional<attribute::AttributeType>& t) | ||
: AttributeDescription( | ||
p, | ||
pt.has_value() ? std::optional<uint8_t>{wmtk::get_primitive_type_id(pt.value())} | ||
: std::optional<uint8_t>{}, | ||
t) | ||
{} | ||
|
||
|
||
std::optional<PrimitiveType> primitive_type() const; | ||
|
||
auto operator<=>(const AttributeDescription&) const -> std::strong_ordering; | ||
auto operator==(const AttributeDescription&) const -> bool; | ||
|
||
|
||
WMTK_NLOHMANN_JSON_FRIEND_DECLARATION(AttributeDescription) | ||
}; | ||
} // namespace wmtk::components::multimesh::utils |
23 changes: 23 additions & 0 deletions
23
...onents/multimesh/src/wmtk/components/multimesh/utils/detail/attribute_ambiguous_error.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
#include "attribute_ambiguous_error.hpp" | ||
#include <fmt/format.h> | ||
#include <fmt/ranges.h> | ||
#include <algorithm> | ||
#include <iterator> | ||
#include "named_error_text.hpp" | ||
namespace wmtk::components::multimesh::utils::detail { | ||
|
||
std::string attribute_ambiguous_error::make_message( | ||
const AttributeDescription& ad, | ||
const std::vector<AttributeDescription>& possibilities) | ||
{ | ||
std::vector<std::string> names; | ||
|
||
std::string (*maker)(const AttributeDescription&) = make_named_error_string; | ||
std::transform(possibilities.begin(), possibilities.end(), std::back_inserter(names), maker); | ||
return fmt::format( | ||
"Multiple options for an attribute {} found: [{}]", | ||
make_named_error_string(ad), | ||
fmt::join(names, ",")); | ||
} | ||
} // namespace wmtk::components::multimesh::utils::detail |
32 changes: 32 additions & 0 deletions
32
...onents/multimesh/src/wmtk/components/multimesh/utils/detail/attribute_ambiguous_error.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
#include "attribute_error.hpp" | ||
namespace wmtk::components::multimesh::utils { | ||
struct AttributeDescription; | ||
} | ||
|
||
namespace wmtk::components::multimesh::utils::detail { | ||
|
||
class attribute_ambiguous_error : public attribute_error | ||
{ | ||
public: | ||
static std::string make_message( | ||
const AttributeDescription& description, | ||
const std::vector<AttributeDescription>& possibilities); | ||
attribute_ambiguous_error( | ||
const AttributeDescription& d, | ||
const std::vector<AttributeDescription>& possibilities) | ||
: attribute_error(make_message(d, possibilities), d) | ||
{} | ||
template <typename... Args> | ||
static attribute_ambiguous_error make( | ||
const std::vector<AttributeDescription>& possiblities, | ||
Args&&... args) | ||
{ | ||
return attribute_ambiguous_error( | ||
AttributeDescription{std::forward<Args>(args)...}, | ||
possiblities); | ||
} | ||
}; | ||
|
||
|
||
} // namespace wmtk::components::multimesh::utils::detail |
20 changes: 20 additions & 0 deletions
20
components/multimesh/src/wmtk/components/multimesh/utils/detail/attribute_error.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#pragma once | ||
#include <optional> | ||
#include <stdexcept> | ||
#include <wmtk/Primitive.hpp> | ||
#include <wmtk/attribute/AttributeType.hpp> | ||
#include "../AttributeDescription.hpp" | ||
|
||
namespace wmtk::components::multimesh::utils::detail { | ||
|
||
class attribute_error : public std::range_error | ||
{ | ||
public: | ||
template <typename... Args> | ||
attribute_error(const std::string_view& message, Args&&... args) | ||
: std::range_error(std::string(message)) | ||
, description(std::forward<Args>(args)...) | ||
{} | ||
AttributeDescription description; | ||
}; | ||
} // namespace wmtk::components::multimesh::utils::detail |
9 changes: 9 additions & 0 deletions
9
components/multimesh/src/wmtk/components/multimesh/utils/detail/attribute_missing_error.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include "attribute_missing_error.hpp" | ||
#include "named_error_text.hpp" | ||
namespace wmtk::components::multimesh::utils::detail { | ||
|
||
std::string attribute_missing_error::make_message(const AttributeDescription& ad) | ||
{ | ||
return "Could not find attribute " + make_named_error_string(ad); | ||
} | ||
} // namespace wmtk::components::multimesh::utils::detail |
24 changes: 24 additions & 0 deletions
24
components/multimesh/src/wmtk/components/multimesh/utils/detail/attribute_missing_error.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
#include "attribute_error.hpp" | ||
namespace wmtk::components::multimesh::utils { | ||
struct AttributeDescription; | ||
} | ||
|
||
namespace wmtk::components::multimesh::utils::detail { | ||
|
||
class attribute_missing_error : public attribute_error | ||
{ | ||
public: | ||
static std::string make_message(const AttributeDescription& description); | ||
attribute_missing_error(const AttributeDescription& d) | ||
: attribute_error(make_message(d), d) | ||
{} | ||
template <typename... Args> | ||
static attribute_missing_error make(Args&&... args) | ||
{ | ||
return attribute_missing_error(AttributeDescription{std::forward<Args>(args)...}); | ||
} | ||
}; | ||
|
||
|
||
} // namespace wmtk::components::multimesh::utils::detail |
Oops, something went wrong.