<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6114569110436058817</id><updated>2011-12-31T17:04:11.581+01:00</updated><category term='archlinux'/><category term='bash'/><category term='useful'/><title type='text'>litb's Blog</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://bloglitb.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6114569110436058817/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://bloglitb.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Johannes (litb)</name><uri>http://www.blogger.com/profile/07110437420755294372</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6114569110436058817.post-8860645878382806146</id><published>2011-12-30T00:10:00.003+01:00</published><updated>2011-12-31T17:04:11.587+01:00</updated><title type='text'>Access to private members: Safer nastiness.</title><content type='html'>In one of my previous posts I showed some code that accesses a private member, that now has been posted on the boost mailing list as a possible utility for boost serialization to access private attributes (OH MY, I had no idea such a thing actually could have uses!).&lt;br /&gt;
&lt;p&gt;I realized that it would be beneficial to have an alternative implementation because the previous code suffered from the Static Initialization Order Fiasco. The following code does not suffer from that problem anymore:&lt;br /&gt;
&lt;pre class="prettyprint"&gt;template&amp;lt;typename Tag, typename Tag::type M&amp;gt;
struct Rob { 
  friend typename Tag::type get(Tag) {
    return M;
  }
};&lt;/pre&gt;This defines a friend function that can be called by ADL using the tag type.&lt;br /&gt;
&lt;pre class="prettyprint"&gt;// use
struct A {
  A(int a):a(a) { }
private:
  int a;
};

// tag used to access A::a
struct A_f { 
  typedef int A::*type;
  friend type get(A_f);
};

template struct Rob&amp;lt;A_f, &amp;A::a&amp;gt;;

int main() {
  A a(42);
  std::cout &amp;lt;&amp;lt; "proof: " &amp;lt;&amp;lt; a.*get(A_f()) &amp;lt;&amp;lt; std::endl;
}&lt;/pre&gt;Note that the explicit declaration of "get" inside of "A_f" is important, because we want "get" to be visible to ADL, and the rob template specialization is not an associated class of "A_f" (we could have declared it globally too, if we wanted, but then it would be visible for anyone). To overcome that, we could introduce some utility class we derive from:&lt;br /&gt;
&lt;pre class="prettyprint"&gt;template&amp;lt;typename Tag, typename Member&amp;gt;
struct TagBase {
  typedef Member type;
  friend type get(Tag);
};&lt;/pre&gt;&lt;/p&gt;The definition of a Tag then becomes very simple&lt;br /&gt;
&lt;pre class="prettyprint"&gt;struct A_f : TagBase&amp;lt;A_f, int A::*&amp;gt; { };&lt;/pre&gt;Unfortunately with the TagBase, GCC spits out a warning about the "friend type get(Tag)" that it declares a non-template function. The warning can be ignored, but it's annoying :).&lt;br /&gt;
&lt;br /&gt;
Hope you enjoyed!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6114569110436058817-8860645878382806146?l=bloglitb.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bloglitb.blogspot.com/feeds/8860645878382806146/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6114569110436058817&amp;postID=8860645878382806146' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6114569110436058817/posts/default/8860645878382806146'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6114569110436058817/posts/default/8860645878382806146'/><link rel='alternate' type='text/html' href='http://bloglitb.blogspot.com/2011/12/access-to-private-members-safer.html' title='Access to private members: Safer nastiness.'/><author><name>Johannes (litb)</name><uri>http://www.blogger.com/profile/07110437420755294372</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6114569110436058817.post-8647408327996817115</id><published>2010-11-27T03:42:00.000+01:00</published><updated>2010-11-27T04:12:39.105+01:00</updated><title type='text'>Fun with switch statements</title><content type='html'>Since my coworker proudly told me about some switch macro he created for himself with C that can process strings, I thought I wanted to do that too, but with C++. Here is what I ended up with.

&lt;pre class="prettyprint"&gt;#include &amp;lt;boost/typeof/typeof.hpp&amp;gt;
#include &amp;lt;cassert&amp;gt;

/* Store a value of type V */
template&amp;lt;typename V&amp;gt;
struct S {
  operator int() { return -1; }
  S(V const&amp;amp; data):data(data), matched(false) { }

  V data;
  bool matched;
};

#define sswitch(V)                                      \
  switch(S&amp;lt;BOOST_TYPEOF(V)&amp;gt; _s_ = V)                    \
  case -1: { if(0)                                           

#define scase(V)                                        \
  } if(!(_s_.matched || _s_.data == V)) ; else          \
    if(!(_s_.matched = true)) ; else { case __LINE__

#define sdefault() \
  } default

#define snodefault(MSG) \
  } do { assert(!MSG); __builtin_unreachable(); } while(0)
&lt;/pre&gt;

That looks pretty weird! What are its drawbacks?

&lt;ul&gt;
&lt;li&gt;A default case must always be present and &lt;i&gt;must&lt;/i&gt; be put last&lt;/li&gt;
&lt;li&gt;Can't put a block around several cases, so &lt;code&gt;sswitch&lt;/code&gt; is incompatible to
&lt;a href="http://en.wikipedia.org/wiki/Duffs_device"&gt;Duffs device&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The to-be-switched value is always copied - there is no automatic deduction for lvalues
such that they would use references and that only rvalues would be copied&lt;/li&gt;
&lt;/ul&gt;

On the goodness sides, &lt;code&gt;sswitch&lt;/code&gt; can use native break keywords with the expected semantics, works with any type (not just strings) and also supports fall-through like the built-in switch.

Here is how it can be used. 

&lt;pre class="prettyprint"&gt;  sswitch(s) {
    scase(&amp;quot;foo&amp;quot;): {
      std::cout &amp;lt;&amp;lt; &amp;quot;s is foo&amp;quot; &amp;lt;&amp;lt; std::endl;
      break; // could fall-through if we wanted
    }

    // supports brace-less style too
    scase(&amp;quot;bar&amp;quot;):
      std::cout &amp;lt;&amp;lt; &amp;quot;s is bar&amp;quot; &amp;lt;&amp;lt; std::endl;
      break;

    // default must be at the end
    sdefault():
      std::cout &amp;lt;&amp;lt; &amp;quot;neither of those!&amp;quot; &amp;lt;&amp;lt; std::endl;
      break;
  }
&lt;/pre&gt;

Notice that since we insert extra braces between each case label (to prevent the code in between to be executed if we haven't hit a label yet), we can't use a plain "default:" at the end, as would be desired by me.

We can jump inside if we want

&lt;pre class="prettyprint"&gt;  sswitch(s) {
  test:
    std::cout &amp;lt;&amp;lt; &amp;quot;this will be output after foo/bar!&amp;quot; &amp;lt;&amp;lt; std::endl;
    break;

    scase(&amp;quot;foo&amp;quot;): {
      std::cout &amp;lt;&amp;lt; &amp;quot;s is foo&amp;quot; &amp;lt;&amp;lt; std::endl;
      goto test;
    }

    // supports brace-less style too
    scase(&amp;quot;bar&amp;quot;):
      std::cout &amp;lt;&amp;lt; &amp;quot;s is bar&amp;quot; &amp;lt;&amp;lt; std::endl;
      goto test;

    // default must be at the end
    snodefault(&amp;quot;neither foo nor bar!?&amp;quot;);
  }
&lt;/pre&gt;

&lt;p&gt;
Please tell me what you think about it and whether you find any problem! Hope you like this little helper!&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6114569110436058817-8647408327996817115?l=bloglitb.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bloglitb.blogspot.com/feeds/8647408327996817115/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6114569110436058817&amp;postID=8647408327996817115' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6114569110436058817/posts/default/8647408327996817115'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6114569110436058817/posts/default/8647408327996817115'/><link rel='alternate' type='text/html' href='http://bloglitb.blogspot.com/2010/11/fun-with-switch-statements.html' title='Fun with switch statements'/><author><name>Johannes (litb)</name><uri>http://www.blogger.com/profile/07110437420755294372</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6114569110436058817.post-6055494420137177557</id><published>2010-07-03T03:59:00.000+02:00</published><updated>2010-07-04T21:33:16.989+02:00</updated><title type='text'>Access to private members. That's easy!</title><content type='html'>&lt;p&gt;So,  always thought that it's impossible without undefined behavior to access private members of arbitrary classes without being friend.&lt;/p&gt;

&lt;p&gt;
Today I noticed I've been horribly wrong, after reading some insightful commit to the clang compiler, that enabled it to allow explicit instantiation to disregard accessibilities, as per the Standard. This enables us to access private members of others. As an experiment, I created some class templates

&lt;pre class="prettyprint"&gt;template&amp;lt;typename Tag&amp;gt;
struct result {
  /* export it ... */
  typedef typename Tag::type type;
  static type ptr;
};

template&amp;lt;typename Tag&amp;gt;
typename result&amp;lt;Tag&amp;gt;::type result&amp;lt;Tag&amp;gt;::ptr;

template&amp;lt;typename Tag, typename Tag::type p&amp;gt;
struct rob : result&amp;lt;Tag&amp;gt; {
  /* fill it ... */
  struct filler {
    filler() { result&amp;lt;Tag&amp;gt;::ptr = p; }
  };
  static filler filler_obj;
};

template&amp;lt;typename Tag, typename Tag::type p&amp;gt;
typename rob&amp;lt;Tag, p&amp;gt;::filler rob&amp;lt;Tag, p&amp;gt;::filler_obj;
&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;
So, how is it used? Let's have an example
&lt;pre class="prettyprint"&gt;struct A {
private:
  void f() {
    std::cout &amp;lt;&amp;lt; "proof!" &amp;lt;&amp;lt; std::endl;
  }
};

struct Af { typedef void(A::*type)(); };
template class rob&amp;lt;Af, &amp;amp;A::f&amp;gt;;
&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;
Ah, that's all to expose poor A's "f" member. Now anyone can use them using the member pointer snytax, as does the main function below
&lt;pre class="prettyprint"&gt;int main() {
  A a;
  (a.*result&amp;lt;Af&amp;gt;::ptr)();
}
&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;
Of course, as &lt;a href="http://www.gotw.ca/gotw/076.htm"&gt;Herb Sutter told us&lt;/a&gt;, don't do these things in real code.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6114569110436058817-6055494420137177557?l=bloglitb.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bloglitb.blogspot.com/feeds/6055494420137177557/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6114569110436058817&amp;postID=6055494420137177557' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6114569110436058817/posts/default/6055494420137177557'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6114569110436058817/posts/default/6055494420137177557'/><link rel='alternate' type='text/html' href='http://bloglitb.blogspot.com/2010/07/access-to-private-members-thats-easy.html' title='Access to private members. That&apos;s easy!'/><author><name>Johannes (litb)</name><uri>http://www.blogger.com/profile/07110437420755294372</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6114569110436058817.post-8022221810968052943</id><published>2008-11-01T06:38:00.000+01:00</published><updated>2010-07-04T21:53:29.835+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='archlinux'/><category scheme='http://www.blogger.com/atom/ns#' term='bash'/><category scheme='http://www.blogger.com/atom/ns#' term='useful'/><title type='text'>My new blog and a Pacman surprise</title><content type='html'>&lt;p&gt;Hello there.&lt;/p&gt;

&lt;p&gt;So now I've read many blogs out there. And I think it's time that I also own some blogs about what I'm doing with my spare time and whatnot :P So I came across blogger.com and thought it would be a good idea to get my hands dirty and create one. So here we are.&lt;/p&gt;

&lt;p&gt;
First of all I want to tell you that pacman.d mirrorlist is sometimes really not ordered all that well :P So after waiting another round of minute for the &lt;a href="http://www.videolan.org/"&gt;VLC&lt;/a&gt; package to download completely, I figured it would be the best to just sit down and write this script:

&lt;pre class="prettyprint"&gt;
tmpf=$(mktemp)
trap &amp;quot;rm -f $tmpf; exit&amp;quot; INT TERM
gawk '
/#?Server =/ {
  host = gensub(/^[^:]+:\/\/([^/]+).*/, &amp;quot;\\1&amp;quot;, 1, $3)
  print host, $0
}' /etc/pacman.d/mirrorlist |
while read host line; do
  echo &amp;quot;trying to ping '$host'&amp;quot; &amp;gt;/dev/stderr
  timed=$(ping -i 0.3 -c 1 -W 1 -n -q $host 2&amp;gt;/dev/null |
    tail -n1 |
    gawk '{ print $4 }' |
    cut -f2 -d/)
  [ x != x$timed ] &amp;amp;&amp;amp; echo $timed ${line%#*};
done | LC_ALL=C sort -n | 
while read crap remains; do
  echo $remains \# $crap ms;
done &amp;gt; $tmpf

echo 'the following was created: '
cat $tmpf
echo -e '\nwant me to use it? (y|n)'
read reply
if [ x$reply = xy ]; then
  mv $tmpf /etc/pacman.d/mirrorlist
else
  rm -f $tmpf
fi
&lt;/pre&gt;
&lt;/p&gt;

&lt;p&gt;
What does it do? Well, it basicially finds out the currently best order for the mirrorlist file, so that the fastest mirror is at the top. After running it, I was really impressed about the speed of pacman. It rushed through like a tiger :P&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6114569110436058817-8022221810968052943?l=bloglitb.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bloglitb.blogspot.com/feeds/8022221810968052943/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6114569110436058817&amp;postID=8022221810968052943' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6114569110436058817/posts/default/8022221810968052943'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6114569110436058817/posts/default/8022221810968052943'/><link rel='alternate' type='text/html' href='http://bloglitb.blogspot.com/2008/10/my-new-blog-and-pacman-surprise.html' title='My new blog and a Pacman surprise'/><author><name>Johannes (litb)</name><uri>http://www.blogger.com/profile/07110437420755294372</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry></feed>
