~ubuntu-branches/ubuntu/trusty/liblas/trusty-proposed

« back to all changes in this revision

Viewing changes to include/liblas/guid.hpp

  • Committer: Package Import Robot
  • Author(s): Francesco Paolo Lovergine
  • Date: 2014-01-05 17:00:29 UTC
  • mfrom: (7.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20140105170029-ddtp0j63x5jvck2u
Tags: 1.7.0+dfsg-2
Fixed missing linking of system boost component.
(closes: #733282)

Show diffs side-by-side

added added

removed removed

Lines of Context:
59
59
#ifndef LIBLAS_GUID_HPP_INCLUDED
60
60
#define LIBLAS_GUID_HPP_INCLUDED
61
61
 
62
 
#include <liblas/cstdint.hpp>
63
62
#include <liblas/detail/sha1.hpp>
64
 
#include <liblas/detail/utility.hpp>
 
63
#include <liblas/detail/private_utility.hpp>
 
64
#include <liblas/export.hpp>
 
65
// boost
 
66
#include <boost/array.hpp>
 
67
#include <boost/cstdint.hpp>
 
68
#include <boost/random.hpp>
 
69
// std
65
70
#include <iosfwd>
66
71
#include <iomanip>
67
72
#include <algorithm>
76
81
 
77
82
namespace liblas {
78
83
 
 
84
namespace detail {
 
85
 
 
86
        inline boost::uint8_t random_byte()
 
87
    {
 
88
        // Change seed to something better?
 
89
 
 
90
        typedef boost::mt19937 engine_t;
 
91
        typedef boost::uniform_int<unsigned long> distribution_t;
 
92
        typedef boost::variate_generator<engine_t, distribution_t> generator_t;
 
93
 
 
94
        static generator_t generator(
 
95
            engine_t(static_cast<engine_t::result_type>( std::time(0) )),
 
96
            // this line should work and does, but it produces lots of warnings
 
97
            // thus we will use unsigned long and cast it to a uint8_t
 
98
            //distribution_t((std::numeric_limits<uint8_t>::min)(), (std::numeric_limits<uint8_t>::max)()));
 
99
            distribution_t((std::numeric_limits<unsigned long>::min)(), (std::numeric_limits<unsigned long>::max)()));
 
100
 
 
101
                return static_cast<boost::uint8_t>(generator() & 0xFF);
 
102
    }
 
103
 
 
104
} // namespace detail
 
105
 
79
106
/// Definition of Globally Unique Identifier type.
80
107
/// The GUID is a 16-byte (128-bit) number.
81
108
/// This class is used to represent value stored as Project Identifier
82
 
/// in public header block (see LASHeader) of a LAS file.
 
109
/// in public header block (see Header) of a LAS file.
83
110
/// All files in a unique project should have the same value of
84
111
/// the Project Identifier. It is used together with File Source ID to
85
112
/// uniquely identify every LAS, globally.
86
113
///
87
114
/// \see About GUID in Wikipedia http://en.wikipedia.org/wiki/Globally_Unique_Identifier 
88
 
class guid
 
115
class LAS_DLL guid
89
116
{
90
117
public:
91
118
 
94
121
    /// \post guid::is_null() == true.
95
122
    guid() /* throw() */
96
123
    {
97
 
        std::fill(data_, data_ + static_size, 0);
 
124
        data_.assign(0);
98
125
    }
99
126
 
100
127
    /// Initializes from textual representation of valid GUID.
125
152
    /// \param d4 - last 64 bits of GUID number.
126
153
    /// \exception std::invalid_argument if construction failed.
127
154
    /// \post guid::is_null() == false.
128
 
    guid(liblas::uint32_t const& d1, liblas::uint16_t const& d2, liblas::uint16_t const& d3, liblas::uint8_t const (&d4)[8])
 
155
    guid(boost::uint32_t const& d1, boost::uint16_t const& d2, boost::uint16_t const& d3, boost::uint8_t const (&d4)[8])
129
156
    {
130
157
        construct(d1, d2, d3, d4);
131
158
    }
134
161
    /// \exception nothrow
135
162
    guid(guid const& rhs) /* throw() */
136
163
    {
137
 
        std::copy(rhs.data_, rhs.data_ + static_size, data_);
 
164
        data_ = rhs.data_;
138
165
    }
139
166
 
140
167
    /// Destructor.
148
175
    {
149
176
        if (&rhs != this)
150
177
        {
151
 
            std::copy(rhs.data_, rhs.data_ + static_size, data_);
 
178
             data_ = rhs.data_;
152
179
        }
153
180
        return *this;
154
181
    }
157
184
    /// \exception nothrow
158
185
    bool operator==(guid const& rhs) const /* throw() */
159
186
    {
160
 
        return std::equal(data_, data_ + static_size, rhs.data_);
 
187
        return data_ == rhs.data_;
161
188
    }
162
189
 
163
190
    /// Inequality operator.
174
201
    /// \exception nothrow
175
202
    bool operator<(guid const& rhs) const /* throw() */
176
203
    {
177
 
        return std::lexicographical_compare(data_, data_ + static_size, rhs.data_, rhs.data_ + static_size);
 
204
        return data_ < rhs.data_;
178
205
    }
179
206
    
180
207
    /// More-than operator.
184
211
    /// \exception nothrow
185
212
    bool operator>(guid const& rhs) const /* throw() */
186
213
    {
187
 
        return std::lexicographical_compare(rhs.data_, rhs.data_ + static_size, data_, data_ + static_size);
 
214
        return data_ > rhs.data_;
188
215
    }
189
216
 
190
217
    /// Less-than-or-equal-to operator.
192
219
    /// \exception nothrow
193
220
    bool operator<=(guid const& rhs) const /* throw() */
194
221
    {
195
 
        return (*this == rhs) || (*this < rhs);
 
222
        return data_ <= rhs.data_;
196
223
    }
197
224
 
198
225
    /// More-than-or-equal-to operator.
200
227
    /// \exception nothrow
201
228
    bool operator>=(guid const& rhs) const /* throw() */
202
229
    {
203
 
        return (*this == rhs) || (*this > rhs);
 
230
        return data_ >= rhs.data_;
204
231
    }
205
232
 
206
233
    /// Test if the GUID object is null GUID or not.
246
273
    /// and equal to 16 bytes (128-bit number).
247
274
    size_t byte_count() const /* throw() */
248
275
    {
249
 
        return static_size;
 
276
        return data_.size();
250
277
    }
251
278
 
 
279
    size_t size() const /* throw() */
 
280
    {
 
281
        return byte_count();
 
282
    }
252
283
 
253
284
    /// Send bytes of GUID data to sequenec of bytes using given output iterator.
254
285
    /// \exception nothrow
255
286
    template <typename ByteOutputIterator>
256
287
    void output_bytes(ByteOutputIterator out) const
257
288
    {
258
 
        std::copy(data_, data_ + static_size, out);
 
289
        std::copy(data_.begin(), data_.end(), out);
259
290
    }
260
291
 
261
292
    /// Separate bytes of GUID data to distinct buffers.
264
295
    /// \param d3 - buffer for 16 bits of third chunk of GUID number.
265
296
    /// \param d4 - buffer for last 64 bits of GUID number.
266
297
    /// \exception nothrow
267
 
    void output_data(liblas::uint32_t& d1, liblas::uint16_t& d2, liblas::uint16_t& d3, liblas::uint8_t (&d4)[8]) const
 
298
    void output_data(boost::uint32_t& d1, boost::uint16_t& d2, boost::uint16_t& d3, boost::uint8_t (&d4)[8]) const
268
299
    {
269
300
        d1 = d2 = d3 = 0;
270
301
        std::size_t pos = 0;
271
 
        int const charbit = std::numeric_limits<liblas::uint8_t>::digits;
 
302
        int const charbit = std::numeric_limits<boost::uint8_t>::digits;
272
303
        
273
304
        for (; pos < 4; ++pos)
274
305
        {
369
400
        }
370
401
    }
371
402
 
372
 
    void construct(liblas::uint32_t const& d1, liblas::uint16_t const& d2, liblas::uint16_t const& d3, liblas::uint8_t const (&d4)[8])
 
403
    void construct(boost::uint32_t const& d1, boost::uint16_t const& d2, boost::uint16_t const& d3, boost::uint8_t const (&d4)[8])
373
404
    {
374
405
        std::ostringstream ss;
375
406
        ss.flags(std::ios::hex);        
390
421
        for (std::size_t i = 0; i < sizeof(d4); ++i)
391
422
        {
392
423
            ss.width(2);
393
 
            ss << static_cast<liblas::uint32_t>(d4[i]);
 
424
            ss << static_cast<boost::uint32_t>(d4[i]);
394
425
            if (1 == i)
395
426
                ss << '-';
396
427
        }
409
440
            init_rand = false;
410
441
        }
411
442
        
412
 
        for (size_t i = 0; i < static_size; i++)
 
443
        for (size_t i = 0; i < result.data_.size(); i++)
413
444
        {
414
 
            result.data_[i] = detail::generate_random_byte<liblas::uint8_t>();
 
445
            result.data_[i] = detail::random_byte();
415
446
        }
416
447
    
417
448
        // set variant
430
461
    // name based
431
462
    static guid create_name_based(guid const& namespace_guid, char const* name, int name_length)
432
463
    {
433
 
        using liblas::uint8_t;
 
464
        using boost::uint8_t;
434
465
        
435
466
        detail::SHA1 sha1;
436
 
        sha1.Input(namespace_guid.data_, namespace_guid.static_size);
437
 
        sha1.Input(name, name_length);
438
 
        unsigned int digest[5];
 
467
 
 
468
        sha1.addBytes(name, name_length);
439
469
        
440
 
        if (sha1.Result(digest) == false)
 
470
        unsigned char* digest = sha1.getDigest();
 
471
        if (!digest)
441
472
        {
442
 
            throw std::runtime_error("create error");
 
473
            throw std::runtime_error("create_name_based sha error");
443
474
        }
444
475
        
445
476
        guid result;
473
504
    
474
505
private:
475
506
 
476
 
    static const std::size_t static_size = 16;
477
 
    liblas::uint8_t data_[static_size];
 
507
    ::boost::array<boost::uint8_t, 16> data_;
478
508
};
479
509
 
480
510
inline std::ostream& operator<<(std::ostream& os, guid const& g)
497
527
        }
498
528
        os << hex;
499
529
        os.fill('0');
500
 
        for (size_t i = 0; i < g.static_size; ++i)
 
530
        for (size_t i = 0; i < g.size(); ++i)
501
531
        {
502
532
            os.width(2);
503
533
            os << static_cast<unsigned int>(g.data_[i]);
537
567
            is >> c; // read brace
538
568
        }
539
569
 
540
 
        for (size_t i = 0; i < temp_guid.static_size && is; ++i)
 
570
        for (size_t i = 0; i < temp_guid.size() && is; ++i)
541
571
        {
542
572
            std::stringstream ss;
543
573
 
559
589
                is.setstate(ios_base::badbit);
560
590
            }
561
591
 
562
 
            temp_guid.data_[i] = static_cast<liblas::uint8_t>(val);
 
592
            temp_guid.data_[i] = static_cast<boost::uint8_t>(val);
563
593
 
564
594
            if (is)
565
595
            {