File: ReaderWriter

package info (click to toggle)
openscenegraph 2.8.3-5
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 33,968 kB
  • ctags: 30,935
  • sloc: cpp: 287,169; ansic: 9,050; sh: 654; yacc: 548; objc: 374; makefile: 264; lex: 151; perl: 119
file content (276 lines) | stat: -rw-r--r-- 10,721 bytes parent folder | download | duplicates (2)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 
 *
 * This library is open source and may be redistributed and/or modified under  
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * OpenSceneGraph Public License for more details.
*/
//osgIntrospection - Copyright (C) 2005 Marco Jez

#ifndef OSGINTROSPECTION_READERWRITER_
#define OSGINTROSPECTION_READERWRITER_

#include <osgIntrospection/Value>
#include <osgIntrospection/Type>
#include <osgIntrospection/Exceptions>
#include <osgIntrospection/variant_cast>

#include <osg/Vec2>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/ref_ptr>

#include <iostream>
#include <sstream>


namespace osgIntrospection
{

    /// This is the base class for reader/writer objects. A ReaderWriter's
    /// purpose is to provide the means for writing the content of a Value
    /// object to a stream and for reading it back. Descendants can either
    /// be specialized for just one data type or they can handle several
    /// types, that's up to the implementor. A derived class is not required
    /// to support all streaming operations (text write, text read, bin write
    /// and bin read), it can implement just some of them, although full
    /// support is strongly encouraged.
    class ReaderWriter
    {
    public:
        class Options
        {
        public:
            Options(): fno_(false) {}
            virtual ~Options() {}

            bool getForceNumericOutput() const    { return fno_; }
            void setForceNumericOutput(bool fno)  { fno_ = fno; }

        private:
            bool fno_;
        };

        /// Writes a textual representation of the value's content to a stream.
        virtual std::ostream &writeTextValue(std::ostream &, const Value& v, const Options* = 0) const { throw StreamingNotSupportedException(StreamingNotSupportedException::TEXT_WRITE, v.getType().getExtendedTypeInfo()); }

        /// Reads a textual representation of the value's content from a stream.
        virtual std::istream &readTextValue(std::istream &, Value& v, const Options* = 0) const { throw StreamingNotSupportedException(StreamingNotSupportedException::TEXT_READ, v.getType().getExtendedTypeInfo()); }

        /// Writes a textual representation of the value's content to a stream.
      virtual std::wostream &writeTextValue(std::wostream & wos, const Value& v, const Options* op = 0) const { std::ostringstream os; writeTextValue(os, v, op); wos << os; return (wos);}

        /// Reads a textual representation of the value's content from a stream.
        virtual std::wistream &readTextValue(std::wistream& , Value& v, const Options* = 0) const { throw StreamingNotSupportedException(StreamingNotSupportedException::TEXT_READ, v.getType().getExtendedTypeInfo()); }

        /// Writes a binary representation of the value's content to a stream.
        virtual std::ostream &writeBinaryValue(std::ostream &, const Value& v, const Options* = 0) const { throw StreamingNotSupportedException(StreamingNotSupportedException::BINARY_WRITE, v.getType().getExtendedTypeInfo()); }

        /// Reads a binary representation of the value's content from a stream.
        virtual std::istream &readBinaryValue(std::istream &, Value& v, const Options* = 0) const { throw StreamingNotSupportedException(StreamingNotSupportedException::BINARY_READ, v.getType().getExtendedTypeInfo()); }

        /// Virtual destructor.
        virtual ~ReaderWriter() {}
    };

    /// This class template provides basic default streaming capabilities
    /// for all types that define streaming operators (<< and >>). Most of
    /// the standard types are able to be read and written this way, so the
    /// StdReaderWriter template can be a convenient default for several
    /// types. The binary representation is a raw copy of the memory content.
    ///
    /// TO-DO: improve binary streaming and avoid arch dependency.
    ///
    template<typename T>
    class StdReaderWriter: public ReaderWriter
    {
    public:
        virtual std::ostream &writeTextValue(std::ostream &os, const Value& v, const Options * = 0) const
        {
            return (os << variant_cast<T>(v));
        }

        virtual std::istream &readTextValue(std::istream &is, Value& v, const Options * = 0) const
        {
            if (v.isEmpty()) v = Value(T());
            return (is >> variant_cast<T &>(v));
        }

        virtual std::ostream &writeBinaryValue(std::ostream &os, const Value& v, const Options * = 0) const
        {
            return os.write(reinterpret_cast<const char *>(extract_raw_data<T>(v)), sizeof(T));
        }

        virtual std::istream &readBinaryValue(std::istream &is, Value& v, const Options * = 0) const
        {
            if (v.isEmpty()) v = Value(T());
            return is.read(reinterpret_cast<char *>(extract_raw_data<T>(v)), sizeof(T));
        }

    };

    template<typename T>
    class StdWReaderWriter: public ReaderWriter
    {
    public:
        virtual std::wostream &writeTextValue(std::wostream &wos, const Value& v, const Options * = 0) const
        {
            return (wos << variant_cast<T>(v));
        }

        virtual std::wistream &readTextValue(std::wistream &wis, Value& v, const Options * = 0) const
        {
            if (v.isEmpty()) v = Value(T());
            return (wis >> variant_cast<T &>(v));
        }

        virtual std::ostream &writeBinaryValue(std::ostream &os, const Value& v, const Options * = 0) const
        {
            return os.write(reinterpret_cast<const char *>(extract_raw_data<T>(v)), sizeof(T));
        }

        virtual std::istream &readBinaryValue(std::istream &is, Value& v, const Options * = 0) const
        {
            if (v.isEmpty()) v = Value(T());
            return is.read(reinterpret_cast<char *>(extract_raw_data<T>(v)), sizeof(T));
        }

    };

    /// This ReaderWriter can be used to read and write enumeration values.
    /// The textual representation will be the enum label, if found, or the
    /// numerical value. The binary representation doesn't take label names
    /// into account.
    template<typename T>
    class EnumReaderWriter: public ReaderWriter
    {
        virtual std::ostream &writeTextValue(std::ostream &os, const Value& v, const Options *options = 0) const
        {
            int numeric = static_cast<int>(variant_cast<T>(v));

            if (!options || !options->getForceNumericOutput())
            {
                const Type& type = v.getType();
                const EnumLabelMap& elm = type.getEnumLabels();
                EnumLabelMap::const_iterator i = elm.find(numeric);
                if (i != elm.end())
                {
                    os << i->second;
                    return os;
                }
                else
                {
                    std::vector<std::string> labels;

                    // it could be a bitmask
                    for (EnumLabelMap::const_iterator i=elm.begin(); i!=elm.end(); ++i)
                    {
                        if (i->first != 0 && ((i->first & numeric) == i->first))
                        {
                            numeric ^= i->first;
                            labels.push_back(i->second);
                        }
                    }

                    // check whether all bits were discovered
                    if (numeric == 0)
                    {
                        for (std::vector<std::string>::const_iterator i=labels.begin(); i!=labels.end(); ++i)
                        {
                            os << *i;
                            if ((i+1) != labels.end()) os << " | ";
                        }
                        return os;
                    }
                }
            }

            return os << numeric;
        }

        virtual std::istream &readTextValue(std::istream &is, Value& v, const Options * = 0) const
        {
            if (v.isEmpty()) v = Value(T());

            int i;
            if (is >> i)
            {
                variant_cast<T &>(v) = static_cast<T>(i);
                return is;
            }

            is.clear();

            std::string s;            
            if (is >> s)
            {
                const Type& type = v.getType();
                const EnumLabelMap& elm = type.getEnumLabels();
                for (EnumLabelMap::const_iterator i=elm.begin(); i!=elm.end(); ++i)
                {
                    if (i->second.compare(s) == 0)
                    {
                        variant_cast<T &>(v) = static_cast<T>(i->first);
                        return is;
                    }
                }
            }

            return is;
        }

        virtual std::ostream &writeBinaryValue(std::ostream &os, const Value& v, const Options * = 0) const
        {
            return os.write(reinterpret_cast<const char *>(extract_raw_data<T>(v)), sizeof(T));
        }

        virtual std::istream &readBinaryValue(std::istream &is, Value& v, const Options * = 0) const
        {
            if (v.isEmpty())
                v = Value(T());
            return is.read(reinterpret_cast<char *>(extract_raw_data<T>(v)), sizeof(T));
        }

    };

    /// This is a ReaderWriter class that can be used to read and write
    /// pointer values. Note: template parameter T must be a pointer!
    template<typename T>
    class PtrReaderWriter: public ReaderWriter
    {
    public:
        virtual std::ostream &writeTextValue(std::ostream &os, const Value& v, const Options* = 0) const
        {
            return (os << (void*)variant_cast<T>(v));
        }

        virtual std::istream &readTextValue(std::istream &is, Value& v, const Options* = 0) const
        {
            void *ptr;
            is >> ptr;
            v = Value(T(ptr));
            return is;
        }

        virtual std::ostream &writeBinaryValue(std::ostream &os, const Value& v, const Options* = 0) const
        {
            return os.write(reinterpret_cast<const char *>(extract_raw_data<T>(v)), sizeof(T));
        }

        virtual std::istream &readBinaryValue(std::istream &is, Value& v, const Options* = 0) const
        {
            T ptr;
            is.read(reinterpret_cast<char *>(&ptr), sizeof(T));
            v = Value(ptr);
            return is;
        }
    };

}

#endif