liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#ifndef BOOST_SERIALIZATION_COLLECTION_SIZE_TYPE_HPP
#define BOOST_SERIALIZATION_COLLECTION_SIZE_TYPE_HPP
 
// (C) Copyright 2005  Matthias Troyer
// (C) Copyright 2020  Robert Ramey
 
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
 
#include <cstddef> // size_t
#include <boost/assert.hpp>
#include <boost/operators.hpp>
#include <boost/serialization/level.hpp>
#include <boost/serialization/is_bitwise_serializable.hpp>
 
namespace boost {
namespace serialization {
 
template<typename T> // T is the basic type holding the integer
struct cardinal_number : private
    boost::totally_ordered<cardinal_number<T> >,
    boost::additive<cardinal_number<T> >,
    boost::unit_steppable<cardinal_number<T> >
{
private:
    template<typename CharType, typename Traits>
    friend std::basic_ostream<CharType, Traits> & operator<<(
        const std::basic_ostream<CharType, Traits> & bos,
        const cardinal_number & rhs
    );
    template<typename CharType, typename Traits>
    friend std::basic_istream<CharType, Traits> & operator>>(
        std::basic_istream<CharType, Traits> & bis,
        const cardinal_number & rhs
    );
public:
    T m_t;
    cardinal_number(T t = 0) :
        m_t(t)
    {}
    cardinal_number(unsigned int t) :
        m_t(t)
    {}
    cardinal_number(int t) :
        m_t(t)
    {
        BOOST_ASSERT(t >= 0);
    }
    operator const T (){
        return m_t;
    }
    // assignment operator
    cardinal_number & operator=(const cardinal_number & rhs){
        m_t = rhs.m_t;
        return *this;
    }
    // basic operations upon which others depend
    // totally ordered / less_than_comparable
    bool operator<(const cardinal_number & rhs) const {
        return m_t < rhs.m_t;
    }
    bool operator==(const cardinal_number & rhs) const {
        return m_t == rhs.m_t;
    }
    // additive
    cardinal_number & operator+=(const cardinal_number & rhs){
        m_t += rhs.m_t;
        return *this;
    }
    // subtractive
    cardinal_number & operator-=(const cardinal_number & rhs){
        BOOST_ASSERT(m_t >= rhs.m_t);
        m_t -= rhs.m_t;
        return *this;
    }
    // increment
    cardinal_number operator++(){
        ++m_t;
        return *this;
     }
     // decrement
    cardinal_number operator--(){
        BOOST_ASSERT(m_t > T(0));
        --m_t;
        return *this;
     }
};
 
typedef cardinal_number<std::size_t> collection_size_type;
 
} } // end namespace boost::serialization
 
#include <ostream>
#include <istream>
 
namespace std {
 
template<typename CharType, typename Traits>
basic_ostream<CharType, Traits> & operator<<(
    std::basic_ostream<CharType, Traits> & bos,
    const boost::serialization::collection_size_type & rhs
){
    bos << rhs.m_t;
    return bos;
}
 
template<typename CharType, typename Traits>
basic_istream<CharType, Traits> & operator>>(
    std::basic_istream<CharType, Traits> & bis,
    boost::serialization::collection_size_type & rhs
){
    bis >> rhs.m_t;
    return bis;
}
 
} // std
 
BOOST_CLASS_IMPLEMENTATION(                     \
    boost::serialization::collection_size_type, \
    primitive_type                              \
)
BOOST_IS_BITWISE_SERIALIZABLE(boost::serialization::collection_size_type)
 
#endif //BOOST_SERIALIZATION_COLLECTION_SIZE_TYPE_HPP