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: 84.1 % 44 37
Test Date: 2026-08-18 17:47:01 Functions: 100.0 % 49 49

            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              : 
      11              : #ifndef HCCLV2_CFG_FIELD_H
      12              : #define HCCLV2_CFG_FIELD_H
      13              : 
      14              : #include <string>
      15              : #include <utility>
      16              : #include <vector>
      17              : #include <functional>
      18              : #include "env_func.h"
      19              : 
      20              : #include "sal.h"
      21              : #include "exception_util.h"
      22              : #include "invalid_params_exception.h"
      23              : #include "adapter_error_manager_pub.h"
      24              : 
      25              : namespace Hccl {
      26              : 
      27              : template <typename T>
      28              : class CfgField {
      29              : public:
      30         1114 :     CfgField(
      31              :         std::string name, const T& defaultValue, const std::function<T(const std::string&)> cast,
      32              :         const std::function<void(const T&)> validate = {}, const std::function<void(T&)> postProc = {})
      33         1114 :         : name(std::move(name)),
      34         1114 :           value(defaultValue),
      35         1114 :           defaultBackup(defaultValue),
      36         1114 :           cast(cast),
      37         1114 :           validate(validate),
      38         1114 :           postProc(postProc),
      39         1114 :           isParsed(false),
      40         1114 :           isSetByEnvironment_(false) {};
      41              : 
      42          679 :     void Parse()
      43              :     {
      44          679 :         std::string str = SalGetEnv(name.c_str());
      45          679 :         if (str.empty() || str == "EmptyString") {
      46          560 :             isParsed = true;
      47          560 :             value = defaultBackup;
      48          560 :             return;
      49              :         }
      50          119 :         isSetByEnvironment_ = true;
      51              :         // 类型转换
      52          119 :         if (cast) {
      53              :             try {
      54          119 :                 value = cast(str);
      55           52 :             } catch (const InvalidParamsException& e) {
      56              :                 // 有异常上报故障码EI0001
      57          364 :                 RPT_ENV_ERR(
      58              :                     true, "EI0001", std::vector<std::string>({"value", "env", "expect"}),
      59              :                     std::vector<std::string>({str, name, e.what()}));
      60           26 :                 THROW<InvalidParamsException>(
      61           78 :                     StringFormat("[Init][EnvVarParam]Env config \"%s\" value is invalid.%s", name.c_str(), e.what()));
      62            0 :             } catch (const NotSupportException& e) { // 临时修改方案 HCCL_SOCKET_IFNAME等当前不支持配置 且需要报错
      63            0 :                 THROW<NotSupportException>(StringFormat(
      64              :                     "[Init][EnvVarParam]Env config \"%s\" or its value is currently unsupported.%s", name.c_str(),
      65            0 :                     e.what()));
      66              :             }
      67              :         } else {
      68            0 :             THROW<InvalidParamsException>(
      69            0 :                 StringFormat("[Init][EnvVarParam]Env config \"%s\" No cast function is assigned.", name.c_str()));
      70              :         }
      71              :         // 校验,环境变量取值范围异常时抛异获取到范围值
      72           93 :         if (validate) {
      73              :             try {
      74           61 :                 validate(value);
      75           22 :             } catch (const InvalidParamsException& e) {
      76              :                 // 有异常上报故障码EI0001
      77          154 :                 RPT_ENV_ERR(
      78              :                     true, "EI0001", std::vector<std::string>({"value", "env", "expect"}),
      79              :                     std::vector<std::string>({str, name, e.what()}));
      80           11 :                 THROW<InvalidParamsException>(
      81           33 :                     StringFormat("[Init][EnvVarParam]Env config \"%s\" value is invalid.%s", name.c_str(), e.what()));
      82              :             }
      83              :         }
      84              :         // 后处理
      85           82 :         if (postProc) {
      86           31 :             postProc(value);
      87              :         }
      88           79 :         isParsed = true;
      89          753 :     }
      90              : 
      91              :     const std::string& GetEnvName() const { return name; }
      92              : 
      93         2013 :     const T& Get() const
      94              :     {
      95         2013 :         if (UNLIKELY(!isParsed)) {
      96            0 :             THROW<InvalidParamsException>(
      97            0 :                 StringFormat("Env config %s is not parsed. Should not use it.", name.c_str()));
      98              :         }
      99         2013 :         return value;
     100              :     }
     101              : 
     102           31 :     bool IsSetByEnvironment() const { return isSetByEnvironment_; }
     103              : 
     104          609 :     const char* GetSource() const { return isSetByEnvironment_ ? "environment" : "default"; }
     105              : 
     106              : private:
     107              :     std::string name;
     108              :     T value;
     109              :     T defaultBackup; // 将默认值备份一份,便于后续恢复
     110              :     std::function<T(const std::string&)> cast;
     111              :     std::function<void(const T&)> validate;
     112              :     std::function<void(T&)> postProc;
     113              :     bool isParsed;
     114              :     bool isSetByEnvironment_;
     115              : };
     116              : 
     117              : } // namespace Hccl
     118              : 
     119              : #endif // HCCLV2_CFG_FIELD_H
        

Generated by: LCOV version 2.0-1