scene/view.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | /// @file view.cpp | ||
| 3 | /// @brief Schema views' checks. | ||
| 4 | #include "ernie/scene/view.h" | ||
| 5 | |||
| 6 | #include <stdexcept> | ||
| 7 | #include <string> | ||
| 8 | |||
| 9 | namespace ernie { | ||
| 10 | namespace { | ||
| 11 | |||
| 12 | 1297 | bool IsApi(const SchemaDef& schema) { | |
| 13 | 1297 | return schema.kind == SchemaKind::SingleApply || schema.kind == SchemaKind::MultipleApply; | |
| 14 | } | ||
| 15 | |||
| 16 | 1298 | PrimId Check(const Scene& scene, const Path& path, const SchemaDef& schema, | |
| 17 | std::string_view instance) { | ||
| 18 | 1298 | const PrimId prim = scene.Require(path); | |
| 19 | 1297 | if (IsApi(schema)) { | |
| 20 | 39 | const std::string api = ApiName{&schema, instance}.ToString(); | |
| 21 | 39 | if (!scene.HasApi(prim, api)) { | |
| 22 | 12 | throw std::invalid_argument("'" + path.String() + "' has no " + api + " applied"); | |
| 23 | } | ||
| 24 | 36 | return prim; | |
| 25 | 39 | } | |
| 26 | 1258 | const SchemaDef* type = scene.Schema(prim); | |
| 27 | 1258 | if (!IsA(type, schema)) { | |
| 28 | 1 | const std::string_view name = type != nullptr ? type->name : "a prim without a type"; | |
| 29 | 4 | throw std::invalid_argument("'" + path.String() + "' is " + std::string(name) + ", not a " + | |
| 30 | 3 | std::string(schema.name)); | |
| 31 | } | ||
| 32 | 1257 | return prim; | |
| 33 | } | ||
| 34 | |||
| 35 | } // namespace | ||
| 36 | |||
| 37 | 11 | SchemaView::SchemaView(const Scene& scene, const Path& path, const SchemaDef& schema, | |
| 38 | 11 | std::string_view instance) | |
| 39 | 22 | : scene_(&scene), prim_(Check(scene, path, schema, instance)), schema_(&schema), | |
| 40 | 11 | instance_(instance) {} | |
| 41 | |||
| 42 | 1287 | SchemaView::SchemaView(Scene& scene, const Path& path, const SchemaDef& schema, | |
| 43 | 1287 | std::string_view instance) | |
| 44 | 1287 | : scene_(&scene), mutable_(&scene), prim_(Check(scene, path, schema, instance)), | |
| 45 | 1282 | schema_(&schema), instance_(instance) {} | |
| 46 | |||
| 47 | 1101 | Scene& SchemaView::Edit() const { | |
| 48 | 1101 | if (mutable_ == nullptr) { | |
| 49 | 3 | throw std::logic_error("'" + GetPath().String() + "' was viewed in a const scene"); | |
| 50 | } | ||
| 51 | 1100 | return *mutable_; | |
| 52 | } | ||
| 53 | |||
| 54 | } // namespace ernie | ||
| 55 |