void t_cocoa_generator::print_const_value()

in thrift/compiler/generate/t_cocoa_generator.cc [2740:2852]


void t_cocoa_generator::print_const_value(
    std::ofstream& out,
    const std::string& name,
    const t_type* type,
    const t_const_value* value,
    bool defval,
    bool is_property) {
  type = type->get_true_type();

  indent(out);
  if (type->is_base_type()) {
    std::string v2 = render_const_value(out, type, value);
    if (defval)
      out << type_name(type) << " ";
    out << name << " = " << v2 << ";" << std::endl << std::endl;
  } else if (type->is_enum()) {
    if (defval)
      out << type_name(type) << " ";
    out << name << " = " << render_const_value(out, type, value) << ";"
        << std::endl
        << std::endl;
  } else if (type->is_struct() || type->is_xception()) {
    const auto* as_struct = static_cast<const t_struct*>(type);
    if (defval) {
      out << type_name(type) << " ";
    }
    if (defval || is_property) {
      out << name << " = [[[" << type_name(type, true)
          << " alloc] init] autorelease_stub];" << std::endl;
    } else {
      out << name << " = [[" << type_name(type, true) << " alloc] init];"
          << std::endl;
    }
    for (const auto& entry : value->get_map()) {
      const auto* field =
          as_struct->get_field_by_name(entry.first->get_string());
      if (field == nullptr) {
        throw std::runtime_error(
            "type error: " + type->name() + " has no field " +
            entry.first->get_string());
      }
      std::string val =
          render_const_value(out, field->get_type(), entry.second);
      std::string cap_name = capitalize(entry.first->get_string());
      indent(out) << "[" << name << " set" << cap_name << ":" << val << "];"
                  << std::endl;
    }
    out << std::endl;
  } else if (type->is_map()) {
    const t_type* ktype = ((t_map*)type)->get_key_type();
    const t_type* vtype = ((t_map*)type)->get_val_type();
    const std::vector<std::pair<t_const_value*, t_const_value*>>& val =
        value->get_map();
    std::vector<std::pair<t_const_value*, t_const_value*>>::const_iterator
        v_iter;
    if (defval)
      out << "NSMutableDictionary *";
    if (defval || is_property)
      out << name
          << " = [[[NSMutableDictionary alloc] initWithCapacity:" << val.size()
          << "] autorelease_stub]; " << std::endl;
    else
      out << name
          << " = [[NSMutableDictionary alloc] initWithCapacity:" << val.size()
          << "]; " << std::endl;
    for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
      std::string key = render_const_value(out, ktype, v_iter->first, true);
      std::string val = render_const_value(out, vtype, v_iter->second, true);
      indent(out) << "[" << name << " setObject:" << val << " forKey:" << key
                  << "];" << std::endl;
    }
    out << std::endl;
  } else if (type->is_list()) {
    const t_type* etype = ((t_list*)type)->get_elem_type();
    const std::vector<t_const_value*>& val = value->get_list();
    std::vector<t_const_value*>::const_iterator v_iter;
    if (defval)
      out << "NSMutableArray *";
    if (defval || is_property)
      out << name
          << " = [[[NSMutableArray alloc] initWithCapacity:" << val.size()
          << "] autorelease_stub];" << std::endl;
    else
      out << name
          << " = [[NSMutableArray alloc] initWithCapacity:" << val.size()
          << "];" << std::endl;
    for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
      std::string val = render_const_value(out, etype, *v_iter, true);
      indent(out) << "[" << name << " addObject:" << val << "];" << std::endl;
    }
    out << std::endl;
  } else if (type->is_set()) {
    const t_type* etype = ((t_set*)type)->get_elem_type();
    const std::vector<t_const_value*>& val = value->get_list();
    std::vector<t_const_value*>::const_iterator v_iter;
    if (defval)
      out << "NSMutableSet *";
    if (defval || is_property)
      out << name << " = [[[NSMutableSet alloc] initWithCapacity:" << val.size()
          << "] autorelease_stub];" << std::endl;
    else
      out << name << " = [[NSMutableSet alloc] initWithCapacity:" << val.size()
          << "];" << std::endl;
    for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
      std::string val = render_const_value(out, etype, *v_iter, true);
      indent(out) << "[" << name << " addObject:" << val << "];" << std::endl;
    }
    out << std::endl;
  } else {
    throw std::runtime_error(
        "compiler error: no const of type " + type->name());
  }
}