OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [language/] [cxx/] [ustl/] [current/] [include/] [ustl/] [uctrstrm.h] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
// This file is part of the uSTL library, an STL implementation.
2
//
3
// Copyright (c) 2005-2009 by Mike Sharov <msharov@users.sourceforge.net>
4
// This file is free software, distributed under the MIT License.
5
//
6
/// \file uctrstrm.h
7
///
8
/// \brief Serialization templates for standard containers.
9
/// Because containers are templates, a single operator>> is impossible.
10
/// Making virtual read/write is also impossible because not all containers
11
/// contain serializable elements. Therefore, use the macros in this file.
12
 
13
#ifndef UCTRSTRM_H_75B2C3EA4980DDDC6B6DFFF767A3B7AC
14
#define UCTRSTRM_H_75B2C3EA4980DDDC6B6DFFF767A3B7AC
15
 
16
#include "mistream.h"
17
#include "sostream.h"
18
#include "uiosfunc.h"
19
#include <typeinfo>
20
 
21
namespace ustl {
22
 
23
//----------------------------------------------------------------------
24
// Macros for easily declaring a container streamable.
25
//----------------------------------------------------------------------
26
 
27
/// \brief Declares container template \p type streamable.
28
///
29
/// Use TEMPLATE_TYPE and TEMPLATE_DECL macros to pass in templated
30
/// type with commas and the template declaration.
31
///
32
#define STD_TEMPLATE_CTR_STREAMABLE(type, template_decl)        \
33
    template_decl                                               \
34
    inline istream& operator>> (istream& is, type& v)           \
35
    { return (container_read (is, v)); }                        \
36
    template_decl                                               \
37
    inline ostream& operator<< (ostream& os, const type& v)     \
38
    { return (container_write (os, v)); }                       \
39
    template_decl                                               \
40
    inline ostringstream& operator<< (ostringstream& os, const type& v) \
41
    { return (container_text_write (os, v)); }                  \
42
    template_decl                                               \
43
    struct object_stream_size<type > {                          \
44
        inline size_t operator()(const type& v) const           \
45
            { return (container_stream_size (v)); }             \
46
    };
47
 
48
/// \brief Declares non-resizable container template \p type streamable.
49
#define STD_TEMPLATE_NR_CTR_STREAMABLE(type, template_decl)     \
50
    template_decl                                               \
51
    inline istream& operator>> (istream& is, type& v)           \
52
    { return (nr_container_read (is, v)); }                     \
53
    template_decl                                               \
54
    inline ostream& operator<< (ostream& os, const type& v)     \
55
    { return (nr_container_write (os, v)); }                    \
56
    template_decl                                               \
57
    inline ostringstream& operator<< (ostringstream& os, const type& v) \
58
    { return (container_text_write (os, v)); }                  \
59
    template_decl                                               \
60
    struct object_stream_size<type > {                          \
61
        inline size_t operator()(const type& v) const           \
62
            { return (nr_container_stream_size (v)); }          \
63
    };
64
 
65
//----------------------------------------------------------------------
66
// Fixed size container serialization.
67
//----------------------------------------------------------------------
68
 
69
/// Reads fixed size container \p v from stream \p is.
70
template <typename Container>
71
inline istream& nr_container_read (istream& is, Container& v)
72
{
73
    foreach (typename Container::iterator, i, v)
74
        is >> *i;
75
    return (is);
76
}
77
 
78
/// Writes fixed size container \p v into stream \p os.
79
template <typename Container>
80
inline ostream& nr_container_write (ostream& os, const Container& v)
81
{
82
    foreach (typename Container::const_iterator, i, v)
83
        os << *i;
84
    return (os);
85
}
86
 
87
/// Computes the stream size of a fixed size standard container.
88
template <typename Container>
89
inline size_t nr_container_stream_size (const Container& v)
90
{
91
    typedef typename Container::const_iterator vciter_t;
92
    typedef typename iterator_traits<vciter_t>::value_type value_type;
93
    if (!v.size())
94
        return (0);
95
    size_t s = 0, dvs;
96
    vciter_t i = v.begin();
97
    do {
98
        dvs = stream_size_of(*i);
99
        s += dvs;
100
    } while (++i != v.end() && !__builtin_constant_p(dvs));
101
    if (__builtin_constant_p(dvs))
102
        s *= v.size();
103
    return (s);
104
}
105
 
106
//----------------------------------------------------------------------
107
// Resizable container serialization.
108
//----------------------------------------------------------------------
109
 
110
/// Reads container \p v from stream \p is.
111
template <typename Container>
112
istream& container_read (istream& is, Container& v)
113
{
114
    typedef typename Container::value_type value_type;
115
    typedef typename Container::iterator iterator;
116
    typedef typename Container::written_size_type written_size_type;
117
    written_size_type n = 0;
118
    is >> n;
119
    const size_t expectedSize = n * stream_size_of(value_type());
120
    if (!is.verify_remaining ("read", USTL_TYPENAME(v), expectedSize))
121
        return (is);
122
    if (alignof(NullValue<value_type>()) > alignof(n))
123
        is >> ios::talign<value_type>();
124
    v.resize (n);
125
    nr_container_read (is, v);
126
    is >> ios::talign<written_size_type>();
127
    return (is);
128
}
129
 
130
/// Writes the vector to stream \p os.
131
template <typename Container>
132
ostream& container_write (ostream& os, const Container& v)
133
{
134
    typedef typename Container::value_type value_type;
135
    typedef typename Container::written_size_type written_size_type;
136
    const written_size_type sz (v.size());
137
    os << sz;
138
    if (alignof(NullValue<value_type>()) > alignof(sz))
139
        os << ios::talign<value_type>();
140
    nr_container_write (os, v);
141
    os << ios::talign<written_size_type>();
142
    return (os);
143
}
144
 
145
/// Computes the stream size of a standard container.
146
template <typename Container>
147
size_t container_stream_size (const Container& v)
148
{
149
    typedef typename Container::value_type value_type;
150
    typedef typename Container::written_size_type written_size_type;
151
    const written_size_type sz (v.size());
152
    size_t sizeSize = stream_size_of (sz);
153
    if (alignof(NullValue<value_type>()) > alignof(sz))
154
        sizeSize = Align (sizeSize, alignof(NullValue<value_type>()));
155
    return (Align (sizeSize + nr_container_stream_size (v), alignof(sz)));
156
}
157
 
158
/// \brief Writes element \p v into stream \p os as text.
159
/// Specialize to custom print elements.
160
template <typename T>
161
inline ostringstream& container_element_text_write (ostringstream& os, const T& v)
162
{ return (os << v); }
163
 
164
/// Writes container \p v into stream \p os as text.
165
template <typename Container>
166
ostringstream& container_text_write (ostringstream& os, const Container& v)
167
{
168
    typename Container::const_iterator i = v.begin();
169
    os << '(';
170
    while (i < v.end()) {
171
        container_element_text_write (os, *i);
172
        os << ",)"[++i == v.end()];
173
    }
174
    return (os);
175
}
176
 
177
//----------------------------------------------------------------------
178
 
179
} // namespace ustl
180
 
181
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.