修复C++14兼容性问题& 逻辑检查 - 详解

在这里插入图片描述
解决办法:特化类型的输出

#include <iostream>
  #include <string>
    #include <vector>
      #include <memory>
        #include <map>
          // 模拟Alembic的V3f类型
          struct MockV3f {
          float x, y, z;
          MockV3f(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {
          }
          };
          // 模拟Alembic的V2f类型
          struct MockV2f {
          float x, y;
          MockV2f(float x_, float y_) : x(x_), y(y_) {
          }
          };
          // 模拟属性基类
          class MockProperty
          {
          protected:
          std::string name;
          public:
          MockProperty(const std::string& propName) : name(propName) {
          }
          virtual ~MockProperty() = default;
          const std::string&
          getName() const {
          return name;
          }
          virtual void printValue() const = 0;
          };
          // 模拟标量属性
          template<
          typename T>
          class MockScalarProperty
          : public MockProperty {
          private:
          T value;
          public:
          MockScalarProperty(const std::string& propName, const T& val)
          : MockProperty(propName), value(val) {
          }
          void set(const T& val) { value = val;
          }
          T get() const {
          return value;
          }
          void printValue() const override {
          std::cout <<
          " " << name <<
          ": " << value << std::endl;
          }
          };
          // 特化bool类型的输出
          template<
          >
          void MockScalarProperty<
          bool>
          ::printValue() const {
          std::cout <<
          " " << name <<
          ": " <<
          (value ? "true" : "false") << std::endl;
          }
          // 模拟数组属性
          template<
          typename T>
          class MockArrayProperty
          : public MockProperty {
          private:
          std::vector<T> values;
            public:
            MockArrayProperty(const std::string& propName) : MockProperty(propName) {
            }
            void set(const std::vector<T>
              & vals) { values = vals;
              }
              const std::vector<T>
                &
                get() const {
                return values;
                }
                void printValue() const override {
                std::cout <<
                " " << name <<
                " (" << values.size() <<
                " 个元素):" << std::endl;
                for (size_t i = 0; i < values.size();
                ++i) {
                std::cout <<
                " [" << i <<
                "]: " << values[i] << std::endl;
                }
                }
                };
                // 特化V3f类型的输出
                template<
                >
                void MockArrayProperty<MockV3f>
                  ::printValue() const {
                  std::cout <<
                  " " << name <<
                  " (" << values.size() <<
                  " 个元素):" << std::endl;
                  for (size_t i = 0; i < values.size();
                  ++i) {
                  std::cout <<
                  " [" << i <<
                  "]: (" << values[i].x <<
                  ", " << values[i].y <<
                  ", " << values[i].z <<
                  ")" << std::endl;
                  }
                  }
                  // 特化V2f类型的输出
                  template<
                  >
                  void MockArrayProperty<MockV2f>
                    ::printValue() const {
                    std::cout <<
                    " " << name <<
                    " (" << values.size() <<
                    " 个元素):" << std::endl;
                    for (size_t i = 0; i < values.size();
                    ++i) {
                    std::cout <<
                    " [" << i <<
                    "]: (" << values[i].x <<
                    ", " << values[i].y <<
                    ")" << std::endl;
                    }
                    }
                    // 模拟复合属性
                    class MockCompoundProperty
                    : public MockProperty {
                    private:
                    std::map<std::string, std::unique_ptr<MockProperty>> properties;
                      public:
                      MockCompoundProperty(const std::string& propName) : MockProperty(propName) {
                      }
                      template<
                      typename T>
                      void addScalarProperty(const std::string& name, const T& value) {
                      properties[name] = std::make_unique<MockScalarProperty<T>>
                        (name, value);
                        }
                        template<
                        typename T>
                        void addArrayProperty(const std::string& name, const std::vector<T>
                          & values) {
                          auto prop = std::make_unique<MockArrayProperty<T>>
                            (name);
                            prop->
                            set(values);
                            properties[name] = std::move(prop);
                            }
                            MockProperty* getProperty(const std::string& name) {
                            auto it = properties.find(name);
                            return it != properties.end() ? it->second.get() : nullptr;
                            }
                            void printValue() const override {
                            std::cout <<
                            " " << name <<
                            " (复合属性):" << std::endl;
                            for (const auto& pair : properties) {
                            pair.second->
                            printValue();
                            }
                            }
                            };
                            int main() {
                            try {
                            std::cout <<
                            "=== Alembic 属性模拟测试 ===" << std::endl;
                            // 创建复合属性容器
                            MockCompoundProperty deskProps("desk_properties");
                            // ==================== 写入标量属性 ====================
                            std::cout <<
                            "\n1. 写入标量属性:" << std::endl;
                            deskProps.addScalarProperty("desk_height", 0.75);
                            std::cout <<
                            " 设置desk_height为: 0.75" << std::endl;
                            deskProps.addScalarProperty("desk_width", 1.2);
                            std::cout <<
                            " 设置desk_width为: 1.2" << std::endl;
                            deskProps.addScalarProperty("material_name", std::string("Oak Wood"));
                            std::cout <<
                            " 设置material_name为: Oak Wood" << std::endl;
                            deskProps.addScalarProperty("is_movable", true);
                            std::cout <<
                            " 设置is_movable为: true" << std::endl;
                            // ==================== 写入数组属性 ====================
                            std::cout <<
                            "\n2. 写入数组属性:" << std::endl;
                            std::vector<MockV3f> vertices = {
                              {
                              0.0f, 0.0f, 0.0f
                              }, {
                              1.2f, 0.0f, 0.0f
                              }, {
                              1.2f, 0.0f, 0.6f
                              }, {
                              0.0f, 0.0f, 0.6f
                              }
                              };
                              deskProps.addArrayProperty("desk_vertices", vertices);
                              std::cout <<
                              " 写入" << vertices.size() <<
                              "个顶点位置" << std::endl;
                              std::vector<MockV2f> uvs = {
                                {
                                0.0f, 0.0f
                                }, {
                                1.0f, 0.0f
                                }, {
                                1.0f, 1.0f
                                }, {
                                0.0f, 1.0f
                                }
                                };
                                deskProps.addArrayProperty("desk_uvs", uvs);
                                std::cout <<
                                " 写入" << uvs.size() <<
                                "个UV坐标" << std::endl;
                                // ==================== 读取属性 ====================
                                std::cout <<
                                "\n--- 读取属性 ---" << std::endl;
                                // 读取标量属性
                                auto heightProp = dynamic_cast<MockScalarProperty<
                                double>
                                *>
                                (deskProps.getProperty("desk_height"));
                                if (heightProp) {
                                std::cout <<
                                " 读取desk_height: " << heightProp->
                                  get() << std::endl;
                                  }
                                  auto widthProp = dynamic_cast<MockScalarProperty<
                                  double>
                                  *>
                                  (deskProps.getProperty("desk_width"));
                                  if (widthProp) {
                                  std::cout <<
                                  " 读取desk_width: " << widthProp->
                                    get() << std::endl;
                                    }
                                    auto materialNameProp = dynamic_cast<MockScalarProperty<std::string>
                                      *>
                                      (deskProps.getProperty("material_name"));
                                      if (materialNameProp) {
                                      std::cout <<
                                      " 读取material_name: " << materialNameProp->
                                        get() << std::endl;
                                        }
                                        auto movableProp = dynamic_cast<MockScalarProperty<
                                        bool>
                                        *>
                                        (deskProps.getProperty("is_movable"));
                                        if (movableProp) {
                                        std::cout <<
                                        " 读取is_movable: " <<
                                        (movableProp->
                                        get() ? "true" : "false") << std::endl;
                                        }
                                        // 读取数组属性
                                        auto verticesProp = dynamic_cast<MockArrayProperty<MockV3f>
                                          *>
                                          (deskProps.getProperty("desk_vertices"));
                                          if (verticesProp) {
                                          const auto& vertices_read = verticesProp->
                                          get();
                                          std::cout <<
                                          " 读取" << vertices_read.size() <<
                                          "个顶点位置:" << std::endl;
                                          for (size_t i = 0; i < vertices_read.size();
                                          ++i) {
                                          const auto& vertex = vertices_read[i];
                                          std::cout <<
                                          " 顶点" << i <<
                                          ": (" << vertex.x <<
                                          ", " << vertex.y <<
                                          ", " << vertex.z <<
                                          ")" << std::endl;
                                          }
                                          }
                                          auto uvsProp = dynamic_cast<MockArrayProperty<MockV2f>
                                            *>
                                            (deskProps.getProperty("desk_uvs"));
                                            if (uvsProp) {
                                            const auto& uvs_read = uvsProp->
                                            get();
                                            std::cout <<
                                            " 读取" << uvs_read.size() <<
                                            "个UV坐标:" << std::endl;
                                            for (size_t i = 0; i < uvs_read.size();
                                            ++i) {
                                            const auto& uv = uvs_read[i];
                                            std::cout <<
                                            " UV" << i <<
                                            ": (" << uv.x <<
                                            ", " << uv.y <<
                                            ")" << std::endl;
                                            }
                                            }
                                            // ==================== 显示所有属性 ====================
                                            std::cout <<
                                            "\n--- 所有属性 ---" << std::endl;
                                            deskProps.printValue();
                                            // ==================== 总结 ====================
                                            std::cout <<
                                            "\n=== 测试总结 ===" << std::endl;
                                            std::cout <<
                                            "✓ 成功写入和读取标量属性 (double, string, bool)" << std::endl;
                                            std::cout <<
                                            "✓ 成功写入和读取数组属性 (V3f, V2f)" << std::endl;
                                            std::cout <<
                                            "✓ 模拟属性系统工作正常" << std::endl;
                                            std::cout <<
                                            "\n注意:这是一个模拟版本,用于演示属性概念。" << std::endl;
                                            std::cout <<
                                            "要使用真实的Alembic库,请安装依赖:sudo apt-get install libalembic-dev libhdf5-dev" << std::endl;
                                            } catch (const std::exception& e) {
                                            std::cerr <<
                                            "错误: " << e.what() << std::endl;
                                            return 1;
                                            }
                                            return 0;
                                            }

在这里插入图片描述

posted on 2025-08-10 22:12  ljbguanli  阅读(8)  评论(0)    收藏  举报