title: protocol buffer GetRepeatedField (reflection) code optimization
body: <p>I'm using the reflection feature of protocol buffer to read message field value at run time.</p>
  
  <p>The proto I have:</p>
  
  <pre><code>package xapp.battle;
      message BATTLE_DATA {
          repeated AInfo aInfo = 1;
          repeated BInfo bInfo = 2;
          repeated CInfo cInfo = 3;
          // a lot other repeated messages
      }
  
      message AInfo {
          int32 test_field = 1;
          // ......
      }
  
      message BInfo {
          int32 test_field = 1;
          // ......
      }
  
      message CInfo {
          int32 test_field = 1;
          // ......
      }
  </code></pre>
  
  <p>The code now I have:</p>
  
  <pre><code>void DO_SOMETHING(messageName) {
  
      const Descriptor* pDescriptor = BATTLE_DATA-&gt;GetDescriptor();
      const FieldDescriptor* pMessageField = pDescriptor-&gt;FindFieldByName(messageName);
      const Reflection* pReflection = BATTLE_DATA-&gt;GetReflection();
  
      if (messageName == "aInfo") {
          const RepeatedPtrField&lt;::xapp::battle::AInfo&gt; repeated_ptr_field = pReflection-&gt;GetRepeatedPtrField&lt;::xapp::battle::AInfo&gt;(*BATTLE_DATA, pMessageField);
          for (int i = 0; i &lt; repeated_ptr_field.size(); i ++) {
              ::xapp::battle::AInfo messageInfo = repeated_ptr_field.Get(i);
              // continue to read the test_field value of messageInfo
          }
      }
      else if (messageName == "bInfo") {
          const RepeatedPtrField&lt;::xapp::battle::BInfo&gt; repeated_ptr_field = pReflection-&gt;GetRepeatedPtrField&lt;::xapp::battle::BInfo&gt;(*BATTLE_DATA, pMessageField);
          for (int i = 0; i &lt; repeated_ptr_field.size(); i ++) {
              ::xapp::battle::BInfo messageInfo = repeated_ptr_field.Get(i);
              // continue to read the test_field value of messageInfo
          }
      }
      else if (messageName == "CInfo") {
          const RepeatedPtrField&lt;::xapp::battle::CInfo&gt; repeated_ptr_field = pReflection-&gt;GetRepeatedPtrField&lt;::xapp::battle::CInfo&gt;(*BATTLE_DATA, pMessageField);
          for (int i = 0; i &lt; repeated_ptr_field.size(); i ++) {
              ::xapp::battle::CInfo messageInfo = repeated_ptr_field.Get(i);
              // continue to read the test_field value of messageInfo
          }
      }
      // ......
      else {
              LOG("loadOneBin - Unknown messageName");
      }
  }
  </code></pre>
  
  <p>This code works but obviously it's not the best solution since there are so many repeated "else-if" code blocks.</p>
  
  <p>What I want is something like (at least get rid of those "else-if" blocks):</p>
  
  <pre><code>const RepeatedPtrField&lt;::xapp::battle::MESSAGE_NAME&gt; repeated_ptr_field = pReflection-&gt;GetRepeatedPtrField&lt;::xapp::battle::MESSAGE_NAME&gt;(*BATTLE_DATA, pMessageField);
  for (int i = 0; i &lt; repeated_ptr_field.size(); i ++) {
      ::xapp::battle::MESSAGE_NAME messageInfo = repeated_ptr_field.Get(i);
      // continue to read the test_field value of messageInfo
  
  }
  </code></pre>
  
  <p>The source code of GetRepeatedPtrField:</p>
  
  <pre><code>template&lt;typename PB&gt;
  inline const RepeatedPtrField&lt;PB&gt;&amp; Reflection::GetRepeatedPtrField(
      const Message&amp; message, const FieldDescriptor* field) const {
    return *static_cast&lt;RepeatedPtrField&lt;PB&gt;* &gt;(
        MutableRawRepeatedField(const_cast&lt;Message*&gt;(&amp;message), field,
            FieldDescriptor::CPPTYPE_MESSAGE, -1,
            PB::default_instance().GetDescriptor()));
  }
  </code></pre>
  
  <p>Any suggestion will be appreciated, thanks :)</p>