int_u.hh 5.77 KB
Newer Older
1
// Copyright (C) 2007, 2008, 2009, 2010, 2012, 2013 EPITA Research and
2
// Development Laboratory (LRDE)
3
//
4
// This file is part of Olena.
5
//
6
7
8
9
10
// Olena is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, version 2 of the License.
//
// Olena is distributed in the hope that it will be useful,
11
12
13
14
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
16
// along with Olena.  If not, see <http://www.gnu.org/licenses/>.
17
18
//
// As a special exception, you may use this file as part of a free
19
// software project without restriction.  Specifically, if other files
20
// instantiate templates or use macros or inline functions from this
21
22
23
24
25
// file, or you compile this file and link it with other files to produce
// an executable, this file does not by itself cause the resulting
// executable to be covered by the GNU General Public License.  This
// exception does not however invalidate any other reasons why the
// executable file might be covered by the GNU General Public License.
26
27
28
29

#ifndef MLN_VALUE_INT_U_HH
# define MLN_VALUE_INT_U_HH

30
/// \file
Guillaume Lazzara's avatar
Guillaume Lazzara committed
31
///
32
33
/// Declaration of mln::value::int_u, a generic class for unsigned
/// integers.
34

35
36
# include <mln/value/ops.hh>

37
# include <mln/metal/math/pow.hh>
38
# include <mln/value/internal/value_like.hh>
39
# include <mln/value/internal/encoding.hh>
40
# include <mln/value/concept/integer.hh>
41
# include <mln/trait/value_.hh>
42

43
# include <mln/value/internal/make_generic_name.hh>
44

45

46
47
48
namespace mln
{

Roland Levillain's avatar
Roland Levillain committed
49
50
51
52
  /// Forward declarations.
  /// \{
  namespace value
  {
Simon Nivault's avatar
Simon Nivault committed
53
    template <unsigned n> struct int_u;
54
    template <unsigned n> struct rgb;
Roland Levillain's avatar
Roland Levillain committed
55
56
57
58
59

    namespace qt
    {
      struct rgb32;
    }
Simon Nivault's avatar
Simon Nivault committed
60
  }
Matthieu Garrigues's avatar
Matthieu Garrigues committed
61

Simon Nivault's avatar
Simon Nivault committed
62
63
64
65
66
  namespace literal
  {
    struct zero_t;
    struct one_t;
  }
Roland Levillain's avatar
Roland Levillain committed
67
68
  /// \}

Thierry Geraud's avatar
Thierry Geraud committed
69
70
71
72

  namespace trait
  {

73
74
75
76
77
78
    template <unsigned n>
    struct set_precise_unary_< op::uminus, mln::value::int_u<n> >
    {
      typedef int ret;
    };

79

Thierry Geraud's avatar
Thierry Geraud committed
80
    template <unsigned n>
81
    struct value_< mln::value::int_u<n> >
82
    {
83
84
    private:
      typedef mln::value::int_u<n> self_;
Matthieu Garrigues's avatar
Matthieu Garrigues committed
85
      typedef typename mln::value::internal::encoding_unsigned_<n>::ret enc_;
86
87
    public:

88
      enum constants_ {
89
	dim = 1,
90
91
92
93
94
95
96
97
98
	nbits = n,
	card  = mln_value_card_from_(n)
      };

      typedef trait::value::nature::integer nature;
      typedef trait::value::kind::data      kind;
      typedef mln_value_quant_from_(card)   quant;

      static const self_ min() { return 0; }
Matthieu Garrigues's avatar
Matthieu Garrigues committed
99
      static const self_ max() { return mlc_pow_int(2, n) - 1; }
100
101
      static const self_ epsilon() { return 0; }

102
103
      typedef unsigned comp;

104
      typedef float sum;
105
106
107

      static const char* name()
      {
108
109
	static std::string
	  s = mln::value::internal::make_generic_name("int_u", n);
110
111
112
	return s.c_str();
      }

113
114
    };

Thierry Geraud's avatar
Thierry Geraud committed
115
116
117
  } // end of namespace mln::trait


118
119
120
  namespace value
  {

121
122
123
124
125
126
127
    /*!
      \brief Unsigned integer value class.

      The parameter is \c n the number of encoding bits.

      \ingroup valueuint
    */
128
    template <unsigned n>
129
    struct int_u
Thierry Geraud's avatar
Thierry Geraud committed
130
131
132
133
134
135
136
      :
      public Integer< int_u<n> >,

      public internal::value_like_< unsigned,    // Equivalent.
				    typename internal::encoding_unsigned_<n>::ret, // Enc.
				    int,         // Interoperation.
				    int_u<n> >   // Exact.
137
138
    {
    protected:
Thierry Geraud's avatar
Thierry Geraud committed
139
140
      /// Encoding associated type.
      typedef typename internal::encoding_unsigned_<n>::ret enc_;
141
142
143
144

    public:

      /// Constructor without argument.
145
      int_u();
146
147

      /// Constructor from an integer.
Matthieu Garrigues's avatar
Matthieu Garrigues committed
148
      int_u(int i);
149

150
      /// \{ Constructors/assignments with literals.
151
152
153
154
      int_u(const mln::literal::zero_t&);
      int_u& operator=(const mln::literal::zero_t&);
      int_u(const mln::literal::one_t&);
      int_u& operator=(const mln::literal::one_t&);
155
156
157
158
159
160
161
      /// \}

      /// Conversion to an unsigned integer.
      operator unsigned() const;

      /// Unary operator minus.
      int operator-() const;
Thierry Geraud's avatar
Thierry Geraud committed
162

163
      /// Assignment from an integer.
Matthieu Garrigues's avatar
Matthieu Garrigues committed
164
      int_u<n>& operator=(int i);
165
166
167

      /// Give the next value (i.e., i + 1).
      int_u<n> next() const;
168
169
170
    };


171
172
173
174
175
    // Safety.
    template <> struct int_u<0>;
    template <> struct int_u<1>;


176

Guillaume Lazzara's avatar
Guillaume Lazzara committed
177
178
179
180
181
182
183
    /// \brief Print an unsigned integer \p i into the output stream \p ostr.
    ///
    /// \param[in,out] ostr An output stream.
    /// \param[in] i An unsigned integer.
    ///
    /// \return The modified output stream \p ostr.
    ///
184
185
    template <unsigned n>
    std::ostream& operator<<(std::ostream& ostr, const int_u<n>& i);
186
187


188
189
190
191
192
    // FIXME: Doc!
    template <unsigned n>
    std::istream& operator>>(std::istream& istr, int_u<n>& i);


193

194
    // Conversions
195

196
197
198
    /// \internal Conversion: int_u -> unsigned.
    template <unsigned n>
    void from_to_(const int_u<n>& from, unsigned& to_);
199

200
201
202
    /// \internal Conversion: int_u -> bool.
    template <unsigned n>
    void from_to_(const int_u<n>& from, bool& to_);
Guillaume Lazzara's avatar
Guillaume Lazzara committed
203

204
205
206
    /// \internal Conversion: int_u -> float.
    template <unsigned n>
    void from_to_(const int_u<n>& from, float& to_);
207

208
209
    /// \internal Conversion: int_u -> double.
    template <unsigned n>
Roland Levillain's avatar
Roland Levillain committed
210
    void from_to_(const int_u<n>& from, double& to_);
211

212
213
214
    /// \internal Conversion: Conversion: int_u -> rgb.
    template <unsigned m>
    void from_to_(const int_u<m>& from, qt::rgb32& to);
215

216
217
218
219
220
221
    /// \internal Conversion: int_u -> rgb.
    template <unsigned m>
    void from_to_(const int_u<m>& from, rgb<m>& to);

  } // end of namespace mln::value

222
} // end of namespace mln
223
224


225
226
227
// Required by mln::values::int_u's from_to_ routines.
# include <mln/value/rgb.hh>
# include <mln/value/qt/rgb32.hh>
228

229

230
231
# ifndef MLN_INCLUDE_ONLY
#  include <mln/value/int_u.hxx>
232
233
# endif // ! MLN_INCLUDE_ONLY

234
235

#endif // ! MLN_VALUE_INT_U_HH