LCOV - code coverage report
Current view: top level - legacy/ascend950/framework/env_config - cfg_field.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 80.6 % 36 29
Test Date: 2026-08-04 10:52:23 Functions: 100.0 % 51 51

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : #ifndef HCCLV2_CFG_FIELD_H
      11              : #define HCCLV2_CFG_FIELD_H
      12              : 
      13              : #include <string>
      14              : #include <utility>
      15              : #include <vector>
      16              : #include <functional>
      17              : #include "env_func.h"
      18              : 
      19              : #include "sal.h"
      20              : #include "exception_util.h"
      21              : #include "invalid_params_exception.h"
      22              : #include "adapter_error_manager_pub.h"
      23              : 
      24              : namespace Hccl {
      25              : 
      26              : template <typename T> class CfgField {
      27              : public:
      28          955 :     CfgField(std::string name, const T &defaultValue, const std::function<T(const std::string &)> cast,
      29              :              const std::function<void(const T &)> validate = {}, const std::function<void(T &)> postProc = {})
      30          955 :         : name(std::move(name)), value(defaultValue), defaultBackup(defaultValue), cast(cast), validate(validate), postProc(postProc),
      31          955 :           isParsed(false), isSetByEnvironment_(false){};
      32              : 
      33          587 :     void Parse()
      34              :     {
      35          587 :         std::string str = SalGetEnv(name.c_str());
      36          587 :         if (str.empty() || str == "EmptyString") {
      37          473 :             isParsed = true;
      38          473 :             value = defaultBackup;
      39          473 :             return;
      40              :         }
      41          114 :         isSetByEnvironment_ = true;
      42              :         // 类型转换
      43          114 :         if (cast) {
      44              :             try {
      45          114 :                 value = cast(str);
      46           56 :             } catch (const InvalidParamsException &e) {
      47              :                 // 有异常上报故障码EI0001
      48           28 :                 RPT_ENV_ERR(true, "EI0001", std::vector<std::string>({"value", "env", "expect"}),
      49              :                             std::vector<std::string>({str, name, e.what()}));
      50           56 :                 THROW<InvalidParamsException>(StringFormat("[Init][EnvVarParam]Env config \"%s\" value is invalid.%s", name.c_str(), e.what()));
      51            0 :             } catch (const NotSupportException &e) { // 临时修改方案 HCCL_SOCKET_IFNAME等当前不支持配置 且需要报错
      52            0 :                 THROW<NotSupportException>(
      53            0 :                     StringFormat("[Init][EnvVarParam]Env config \"%s\" or its value is currently unsupported.%s", name.c_str(), e.what()));
      54              :             }
      55              :         } else {
      56            0 :             THROW<InvalidParamsException>(
      57            0 :                 StringFormat("[Init][EnvVarParam]Env config \"%s\" No cast function is assigned.", name.c_str()));
      58              :         }
      59              :         // 校验,环境变量取值范围异常时抛异获取到范围值
      60           86 :         if (validate) {
      61              :             try {
      62           53 :                 validate(value);
      63           20 :             } catch (const InvalidParamsException &e) {
      64              :                 // 有异常上报故障码EI0001
      65           10 :                 RPT_ENV_ERR(true, "EI0001", std::vector<std::string>({"value", "env", "expect"}),
      66              :                             std::vector<std::string>({str, name, e.what()}));
      67           20 :                 THROW<InvalidParamsException>(StringFormat("[Init][EnvVarParam]Env config \"%s\" value is invalid.%s", name.c_str(), e.what()));
      68              :             }
      69              :         }
      70              :         // 后处理
      71           76 :         if (postProc) {
      72           24 :             postProc(value);
      73              :         }
      74           74 :         isParsed = true;
      75          587 :     }
      76              : 
      77              :     const std::string &GetEnvName() const
      78              :     {
      79              :         return name;
      80              :     }
      81              : 
      82         1786 :     const T &Get() const
      83              :     {
      84         1786 :         if (UNLIKELY(!isParsed)) {
      85            0 :             THROW<InvalidParamsException>(
      86            0 :                 StringFormat("Env config %s is not parsed. Should not use it.", name.c_str()));
      87              :         }
      88         1786 :         return value;
      89              :     }
      90              : 
      91              :     bool IsSetByEnvironment() const
      92              :     {
      93              :         return isSetByEnvironment_;
      94              :     }
      95              : 
      96          517 :     const char* GetSource() const
      97              :     {
      98          517 :         return isSetByEnvironment_ ? "environment" : "default";
      99              :     }
     100              : 
     101              : private:
     102              :     std::string                           name;
     103              :     T                                     value;
     104              :     T                                     defaultBackup; // 将默认值备份一份,便于后续恢复
     105              :     std::function<T(const std::string &)> cast;
     106              :     std::function<void(const T &)>        validate;
     107              :     std::function<void(T &)>              postProc;
     108              :     bool isParsed;
     109              :     bool isSetByEnvironment_;
     110              : };
     111              : 
     112              : } // namespace Hccl
     113              : 
     114              : #endif // HCCLV2_CFG_FIELD_H
        

Generated by: LCOV version 2.0-1