Skip to content
This repository

Add support for C++11 range-based for-loop #21

Merged
merged 2 commits into from 11 months ago

2 participants

Nicholas "LB" Braden James McLaughlin
Nicholas "LB" Braden
LB-- commented March 26, 2013

I added support for C++11 range-based for-loops:

   for(auto &o : my_json_value->u.object)
   {
      std::cout << o.name << std::endl;
   }

Similar for arrays. No type checking, unfortunately - I couldn't manage that.
Technically if the begin and end functions were compatible with C++03 then the STL algorithms could be used on them with C++03 compilers, but that's for another day - they're only compatible with C++11, which is why the check for __cplusplus >= 201103L is in there. I tested with clang++ -std=c++11 and tried to follow the same conding style as you. (Edit: just fixed a minor indentation mistake)

Fixes #14

added some commits March 26, 2013
Nicholas "LB" Braden Add support for C++11 range-based for-loop
for(auto &o : my_json_value->u.object)
{
std::cout << o.name << std::endl;
}
And similar for arrays. No type checking, unfortunately - I couldn't
manage that.

Fix indentation
b445038
Nicholas "LB" Braden Merge pull request #1 from udp/master
Merge upstream changes
83fd768
James McLaughlin
Owner
udp commented May 22, 2013

Thanks Nicholas, looks good to me.

James McLaughlin udp merged commit daf2065 into from May 22, 2013
James McLaughlin udp closed this May 22, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Mar 26, 2013
Nicholas "LB" Braden Add support for C++11 range-based for-loop
for(auto &o : my_json_value->u.object)
{
std::cout << o.name << std::endl;
}
And similar for arrays. No type checking, unfortunately - I couldn't
manage that.

Fix indentation
b445038
Apr 24, 2013
Nicholas "LB" Braden Merge pull request #1 from udp/master
Merge upstream changes
83fd768
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 18 additions and 0 deletions. Show diff stats Hide diff stats

  1. 18  json.h
18  json.h
@@ -117,6 +117,15 @@ typedef struct _json_value
117 117
 
118 118
          } * values;
119 119
 
  120
+         #if defined(__cplusplus) && __cplusplus >= 201103L
  121
+         decltype(values) begin () const
  122
+         {  return values;
  123
+         }
  124
+         decltype(values) end () const
  125
+         {  return values + length;
  126
+         }
  127
+         #endif
  128
+
120 129
       } object;
121 130
 
122 131
       struct
@@ -124,6 +133,15 @@ typedef struct _json_value
124 133
          unsigned int length;
125 134
          struct _json_value ** values;
126 135
 
  136
+         #if defined(__cplusplus) && __cplusplus >= 201103L
  137
+         decltype(values) begin () const
  138
+         {  return values;
  139
+         }
  140
+         decltype(values) end () const
  141
+         {  return values + length;
  142
+         }
  143
+         #endif
  144
+
127 145
       } array;
128 146
 
129 147
    } u;
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.