Guest User

Untitled

a guest
Jan 19th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.37 KB | None | 0 0
  1. #ifndef ANALOGLITERALS_HPP
  2. #define ANALOGLITERALS_HPP
  3.  
  4. namespace analog_literals {
  5.  
  6. typedef unsigned int uint;
  7.  
  8. // Symbols
  9.  
  10.   enum line_end { o, I };
  11.   enum Lsymbol { L };
  12.  
  13. // Intermediary types used during construction
  14.  
  15.   struct eLsymbol {};
  16.   eLsymbol operator! (Lsymbol) { return eLsymbol(); }
  17.  
  18.   struct gen { template <typename T> operator T () const { return T(); } };
  19.  
  20.   template <typename T, uint n> struct excls { excls<T, n + 1> operator! () const { return gen(); } };
  21.  
  22.   template <typename T, uint n> struct dashes: excls<dashes<T, n>, 0>
  23.   { dashes<T, n + 1> operator-- (int) const { return gen(); } };
  24.  
  25.   template <typename, uint> struct L_symbols {}; // represents a L|L|L|.. series
  26.   template <typename T, uint n> L_symbols<T, n + 1> operator| (L_symbols<T, n>, Lsymbol) { return gen(); }
  27.  
  28.   template <typename, uint> struct eL_symbols {}; // represents a !L|!L|!L|.. series
  29.   template <typename T, uint n> eL_symbols<T, n + 1> operator| (eL_symbols<T, n>, eLsymbol) { return gen(); }
  30.  
  31.   dashes<line_end, 1> operator-- (line_end, int) { return gen(); }
  32.   excls<line_end, 1> operator! (line_end) { return gen(); }
  33.  
  34. // Result types
  35.  
  36.   template <uint len> struct line: L_symbols<line<len>, 0>
  37.   { static uint const length; operator uint () const { return len; } };
  38.   template <uint x, uint y> struct rectangle { static uint const width, height, area; };
  39.   template <uint x, uint y, uint z> struct cuboid { static uint const width, height, depth, volume; };
  40.  
  41. // Static members
  42.  
  43.   template <uint len> uint const line<len>::length = len;
  44.  
  45.   template <uint x, uint y> uint const rectangle<x, y>::width = x;
  46.   template <uint x, uint y> uint const rectangle<x, y>::height = y;
  47.   template <uint x, uint y> uint const rectangle<x, y>::area = x * y;
  48.  
  49.   template <uint x, uint y, uint z> uint const cuboid<x, y, z>::width = x;
  50.   template <uint x, uint y, uint z> uint const cuboid<x, y, z>::height = y;
  51.   template <uint x, uint y, uint z> uint const cuboid<x, y, z>::depth = z;
  52.   template <uint x, uint y, uint z> uint const cuboid<x, y, z>::volume = x * y * z;
  53.  
  54.   template <uint x, uint y, uint z> rectangle<x, y> front (cuboid<x, y, z>) { return gen(); }
  55.   template <uint x, uint y, uint z> rectangle<z, y> side (cuboid<x, y, z>) { return gen(); }
  56.   template <uint x, uint y, uint z> rectangle<x, z> top (cuboid<x, y, z>) { return gen(); }
  57.  
  58. // Equality
  59.  
  60.   template <uint ax, uint bx> bool operator== (line<ax>, line<bx>) { return ax == bx; }
  61.  
  62.   template <uint ax, uint ay, uint bx, uint by> bool operator== (rectangle<ax, ay>, rectangle<bx, by>)
  63.   { return ax == bx && ay == by; }
  64.  
  65.   template <uint ax, uint ay, uint az, uint bx, uint by, uint bz>
  66.   bool operator== (cuboid<ax, ay, az>, cuboid<bx, by, bz>) { return ax == bx && ay == by && az == bz; }
  67.  
  68. // Construction
  69.  
  70.   // line
  71.  
  72.   line<0> operator- (line_end, line_end) { return gen(); }
  73.   template <uint x> line<x> operator- (dashes<line_end, x>, line_end) { return gen(); }
  74.  
  75.   // rectangle
  76.  
  77.   template <uint x, uint y> struct lower_rectangle {}; // with lower right corner
  78.  
  79.   template <uint excl_marks, uint x>
  80.   lower_rectangle<x, (excl_marks + 1) / 2> operator- (excls<dashes<line_end, x>, excl_marks>, line_end)
  81.   { return gen(); }
  82.  
  83.   template <uint x, uint y> rectangle<x, y> operator| (line<x>, lower_rectangle<x, y>) { return gen(); }
  84.  
  85.   // cuboid
  86.  
  87.   template <uint x, uint y, uint z> struct cuboid_top {};
  88.   template <uint x, uint y, uint z> struct half_cuboid {};
  89.     // dimensions of complete cuboid known, rest is for show
  90.  
  91.   template <uint x, uint n>
  92.   cuboid_top<x, n + 1, n> operator| (L_symbols<line<x>, n>, line<x>) { return gen(); }
  93.  
  94.   template <uint x, uint y, uint z, uint n>
  95.   eL_symbols<half_cuboid<x, y + (n + 1) / 3, z>, 0> // todo: assert: n%3=2
  96.     operator| (cuboid_top<x, y, z>, excls<line_end, n>) { return gen(); }
  97.  
  98.   template <uint x, uint y, uint z>
  99.   cuboid<x, y, z> operator| (eL_symbols<half_cuboid<x, y, z>, z>, lower_rectangle<x, 1>) { return gen(); }
  100.  
  101. // Convenience namespaces that can be "using namespace"'d:
  102.  
  103.   namespace symbols
  104.   {
  105.     using analog_literals::o;
  106.     using analog_literals::I;
  107.     using analog_literals::L;
  108.   }
  109.  
  110.   namespace shapes
  111.   {
  112.     using analog_literals::line;
  113.     using analog_literals::rectangle;
  114.     using analog_literals::cuboid;
  115.   }
  116.  
  117. } // analog_literals
  118.  
  119. #endif // header guard
  120.  
  121. #ifdef ANALOGLITERALS_TEST
  122.  
  123. int main ()
  124. {
  125.   using namespace analog_literals::symbols;
  126.   using namespace analog_literals::shapes;
  127.  
  128.   line<3>(I-------I);
  129.  
  130.   rectangle<2, 3>(o-----o
  131.                   |     !
  132.                   !     !
  133.                   !     !
  134.                   o-----o);
  135.  
  136.   cuboid<6, 6, 3>(o-------------o
  137.                   |L             \
  138.                   | L             \
  139.                   |  L             \
  140.                   |   o-------------o
  141.                   |   !             !
  142.                   !   !             !
  143.                   o   |             !
  144.                    L  |             !
  145.                     L |             !
  146.                      L|             !
  147.                       o-------------o );
  148.  
  149.   cuboid<3, 4, 2>(o-------o
  150.                   |L       \
  151.                   | L       \
  152.                   |  o-------o
  153.                   |  !       !
  154.                   o  |       !
  155.                    L |       !
  156.                     L|       !
  157.                      o-------o);
  158. }
  159.  
  160. #endif // testing
Advertisement
Add Comment
Please, Sign In to add comment