// Returns a <string, bool> pair. The string is the C++ type name to be used for

// attr_type when defining an object of that type. The bool is a flag to

// indicate whether to treat the type as const when accepting the C++ type as an

// argument to a function.

std::pair<const char*, bool> AttrTypeName(StringPiece attr_type) {

  static const std::unordered_map<StringPiece, std::pair<const char*, bool>,

                                  StringPiece::Hasher>

      attr_type_map{

          {"string", {"StringPiece", false}},

          {"list(string)", {"gtl::ArraySlice<string>", true}},

          {"int", {"int64", false}},

          {"list(int)", {"gtl::ArraySlice<int>", true}},

          {"float", {"float", false}},

          {"list(float)", {"gtl::ArraySlice<float>", true}},

          {"bool", {"bool", false}},

          {"list(bool)", {"gtl::ArraySlice<bool>", true}},

          {"type", {"DataType", false}},

          {"list(type)", {"DataTypeSlice", true}},

          {"shape", {"PartialTensorShape", false}},

          {"list(shape)", {"gtl::ArraySlice<PartialTensorShape>", true}},

          {"tensor", {"TensorProto", true}},

          {"list(tensor)", {"gtl::ArraySlice<TensorProto>", true}},

          {"func", {"NameAttrList", true}},

      };


  auto entry = attr_type_map.find(attr_type);

  if (entry == attr_type_map.end()) {

    LOG(FATAL) << "Unsupported Attr type: " << attr_type;

    return {"", false};

  }

  return entry->second;

}


이게 원본 소스인데,

주석도 네이밍도 엉망진창임.

그리고 솔직히 텐서플로우의 Node 랑 그에 딸린 Attr 에 대해 알지 못하면 주석가지고 비빌수 있는 내용이 아님.


하지만 코드를 정리해 보았다.


using   cpp_type_name   = const char*;

using   treat_as_const  = bool;

using   cpp_type_info   = std::pair< cpp_type_name, treat_as_const >;


cpp_type_info   ActualTypeInfo( StringPiece attr_type )

{

    static  const   std::unordered_map< StringPiece, cpp_type_info, StringPiece::Hasher >

        attr_type_map

    {

        {      "string",    {      "StringPiece",                   false } },

        { "list(string)",   { "gtl::ArraySlice<string>",             true } },

        {      "int",       {      "int64",                         false } },

        { "list(int)",      { "gtl::ArraySlice<int>",                true } },

        {      "float",     {      "float",                         false } },

        { "list(float)",    { "gtl::ArraySlice<float>",              true } },

        {      "bool",      {      "bool",                          false } },

        { "list(bool)",     { "gtl::ArraySlice<bool>",               true } },

        {      "type",      {      "DataType",                      false } },

        { "list(type)",     { "DataTypeSlice",                       true } }, // 이렇게 정리하니까 얘만 특이한거 보임?

        {      "shape",     {      "PartialTensorShape",            false } },

        { "list(shape)",    { "gtl::ArraySlice<PartialTensorShape>", true } },

        {      "tensor",    {      "TensorProto",                    true } },

        { "list(tensor)",   { "gtl::ArraySlice<TensorProto>",        true } },

        {      "func",      {      "NameAttrList",                   true } },

    };

    

    auto    entry   = attr_type_map.find( attr_type );

    if( entry == attr_type_map.end() )

    {

        LOG( FATAL ) << "Unsupported Attr type: " << attr_type;

        return  { "", false };

    }

    return  entry->second;

}


결국 하는 일 별거 아닌 코드임.

내부적으로 사용하는 alias 타입의 이름으로, 그에 대응되는 실제 cpp 타입의 정보를 얻는 것임.

추가로 상수 취급이냐 아니냐 플래그가 딸려 있는거고.


이런걸 왜 코드로 말 못하는데? 솔까말 저 위에 주석 보고 감이 오냐?


이따위가 좋은 예라니 어이없다.