Navigation Menu

Skip to content

Commit

Permalink
Use C++11 for-loop
Browse files Browse the repository at this point in the history
  • Loading branch information
sorbits committed Mar 3, 2014
1 parent 60dde1f commit f872370
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 36 deletions.
6 changes: 3 additions & 3 deletions Commands/nib/nib.mm
Expand Up @@ -41,11 +41,11 @@ @interface TMDWindowCommand : TMDCommand
candidates.push_back(nibName);
}

iterate(it, candidates)
for(std::string const& path : candidates)
{
struct stat sb;
if(stat(it->c_str(), &sb) == 0)
return *it;
if(stat(path.c_str(), &sb) == 0)
return path;
}

fprintf(stderr, "nib could not be loaded: %s (does not exist)\n", nibName.c_str());
Expand Down
20 changes: 0 additions & 20 deletions Dialog2.h
Expand Up @@ -11,30 +11,10 @@
#define enumerate(container,var) for(NSEnumerator* _enumerator = [container objectEnumerator]; var = [_enumerator nextObject]; )
#endif

inline char const* beginof (char const* cStr) { return cStr; }
inline char const* endof (char const* cStr) { return cStr + strlen(cStr); }
template <typename T, int N> T* beginof (T (&a)[N]) { return a; }
template <typename T, int N> T* endof (T (&a)[N]) { return a + N; }
template <typename T, int N, int M> T (*beginof(T (&m)[N][M]))[M] { return m; }
template <typename T, int N, int M> T (*endof(T (&m)[N][M]))[M] { return m + N; }
template <class T> typename T::const_iterator beginof (T const& c) { return c.begin(); }
template <class T> typename T::const_iterator endof (T const& c) { return c.end(); }
template <class T> typename T::iterator beginof (T& c) { return c.begin(); }
template <class T> typename T::iterator endof (T& c) { return c.end(); }

#ifndef foreach
#define foreach(v,f,l) for(decltype(f) v = (f), _end = (l); v != _end; ++v)
#endif

#ifndef iterate
#define iterate(v,c) foreach(v, beginof(c), endof(c))
#endif

#ifndef sizeofA
#define sizeofA(a) (sizeof(a)/sizeof(a[0]))
#endif


#define ErrorAndReturn(message) while(1){[proxy writeStringToError:@"Error: " message "\n"];return;};

#endif /* _DIALOG_H_ */
26 changes: 13 additions & 13 deletions tm_dialog2.mm
Expand Up @@ -121,10 +121,10 @@ int main (int argc, char const* argv[])
FD_ZERO(&readfds); FD_ZERO(&writefds);

int num_fds = 0;
iterate(it, fd_map)
for(auto const& pair : fd_map)
{
FD_SET(it->first, &readfds);
num_fds = std::max(num_fds, it->first + 1);
FD_SET(pair.first, &readfds);
num_fds = std::max(num_fds, pair.first + 1);
}

int i = select(num_fds, &readfds, &writefds, NULL, NULL);
Expand All @@ -134,25 +134,25 @@ int main (int argc, char const* argv[])
continue;
}

std::vector<std::map<int, int>::iterator> to_remove;
iterate(it, fd_map)
std::vector<int> to_remove;
for(auto const& pair : fd_map)
{
if(FD_ISSET(it->first, &readfds))
if(FD_ISSET(pair.first, &readfds))
{
char buf[1024];
ssize_t len = read(it->first, buf, sizeof(buf));
ssize_t len = read(pair.first, buf, sizeof(buf));

if(len == 0)
to_remove.push_back(it); // we can’t remove as long as we need the iterator for the ++
else write(it->second, buf, len);
to_remove.push_back(pair.first); // we can’t remove as long as we need the iterator for the ++
else write(pair.second, buf, len);
}
}

iterate(it, to_remove)
for(int key : to_remove)
{
if((*it)->second == stdin_fd)
close((*it)->second);
fd_map.erase(*it);
if(fd_map[key] == stdin_fd)
close(stdin_fd);
fd_map.erase(key);
}
}

Expand Down

0 comments on commit f872370

Please sign in to comment.