tisdag 18 juni 2013

C++11 Overloading operators for strongly typed enumerations.

C++11 has support for strongly typed enumerations. To overload for example &, | and > I have provided an example below:

  enum class CollisionType
  : Uint16 { NONE = 0x0000,
      DYNAMIC = 0x0001,
      STATIC = 0x0002,
      KINEMATIC = 0x0004,
      SENSOR = 0x0008,
      BULLET = 0x0010,      
      };

  inline constexpr CollisionType
  operator&(CollisionType l, CollisionType r){
    return static_cast<CollisionType>(static_cast<Uint16>(l) & static_cast<Uint16>(r));
  }

  inline constexpr bool
  operator>(CollisionType l, int n){
    return static_cast<Uint16>(l) > n;
  }

  inline constexpr CollisionType
  operator|(CollisionType l, CollisionType r){
    return static_cast<CollisionType>(static_cast<Uint16>(l) | static_cast<Uint16>(r));
  }

tisdag 4 juni 2013

Indent whole buffer with a keystroke

(defun my-indent-region ()
  "Mark whole buffer and indent."
  (interactive)
  (save-excursion
    (mark-whole-buffer) 
    (if (region-active-p)
 (progn
   (indent-region (point-min) (point-max))))))

Declare the function in your .emacs file. then bind it with:


(global-set-key (kbd "<f8>") 'my-indent-region)

A way to use C++11 variadic template functions

  
namespace Log {
    inline void write() {
      std::cout << std::endl;
    }

    template<typename T, typename ...U>
    inline void write(T head, U... tail){
      std::cout << head;
      Log::write(tail...);
    }
  }
Overload an empty write() to end recursion. Then you can use it like this:

Log::write("test", 10.0f, 'd'); 

Log::write(1,2,3,4,5,6,7);