direction.cc

Go to the documentation of this file.
00001 /*                            Package   : libhex
00002  * direction.cc               Created   : 2007/10/11
00003  *                            Author    : Alex Tingle
00004  *
00005  *    Copyright (C) 2007-2008, Alex Tingle.
00006  *
00007  *    This file is part of the libhex application.
00008  *
00009  *    libhex is free software; you can redistribute it and/or
00010  *    modify it under the terms of the GNU Lesser General Public
00011  *    License as published by the Free Software Foundation; either
00012  *    version 2.1 of the License, or (at your option) any later version.
00013  *
00014  *    libhex is distributed in the hope that it will be useful,
00015  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  *    Lesser General Public License for more details.
00018  *
00019  *    You should have received a copy of the GNU Lesser General Public
00020  *    License along with this library; if not, write to the Free Software
00021  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  */
00023 
00024 #include "hex.h"
00025 
00026 namespace hex {
00027 
00028 
00029 Direction to_direction(char c) throw(hex::invalid_argument)
00030 {
00031   if(c<'A' || c>'F')
00032       throw hex::invalid_argument(std::string("to_direction:")+c);
00033   return A + static_cast<int>(c-'A');
00034 }
00035 
00036 
00037 char to_char(Direction d)
00038 {
00039   static const char root =static_cast<char>( A );
00040   return static_cast<char>( 'A' + (static_cast<char>(d) - root) );
00041 }
00042 
00043 
00044 Direction& operator+=(Direction& d, int i)
00045 {
00046   int d_ =static_cast<int>( d );
00047   d_= (d_+i) % DIRECTIONS;
00048   while(d_<0)
00049     d_ += DIRECTIONS;
00050   d=static_cast<Direction>( d_ );
00051   return d;
00052 }
00053 
00054 Direction operator+(const Direction& d, int i)
00055 {
00056   Direction result =d;
00057   result+=i;
00058   return result;
00059 }
00060 
00062 Direction& operator++(Direction& d)
00063 {
00064   return d+=1;
00065 }
00066 
00068 Direction operator++(const Direction& d,int)
00069 {
00070   Direction result =d;
00071   ++result;
00072   return result;
00073 }
00074 
00075 Direction& operator-=(Direction& d, int i)
00076 {
00077   return d += (-i);
00078 }
00079 
00080 Direction operator-(const Direction& d, int i)
00081 {
00082   return d + (-i);
00083 }
00084 
00086 Direction& operator--(Direction& d)
00087 {
00088   return d-=1;
00089 }
00090 
00092 Direction operator--(const Direction& d,int)
00093 {
00094   Direction result =d;
00095   --result;
00096   return result;
00097 }
00098 
00099 std::ostream& operator<<(std::ostream& os, const Direction& d)
00100 {
00101   os << to_char(d);
00102   return os;
00103 }
00104 
00105 
00106 std::string rotate(const std::string& steps, int i)
00107 {
00108   // Rotate all letters A-F, but leave other characters alone.
00109   std::string result =steps;
00110   for(std::string::iterator c=result.begin(); c!=result.end(); ++c)
00111       if('A'<=*c && *c<='F')
00112       {
00113         Direction d =to_direction(*c);
00114         *c = to_char(d+i);
00115       }
00116   return result;
00117 }
00118 
00119 
00120 } // end namespace hex

Generated on Thu Feb 21 00:00:54 2008 for libhex by  doxygen 1.5.1