root/OpenSceneGraph/trunk/src/osgPlugins/ive/ShapeAttributeList.cpp @ 12292

Revision 12292, 3.0 kB (checked in by robert, 3 years ago)

Ran svn propset -R svn:eol-style native . on the OpenSceneGraph

  • Property svn:eol-style set to native
Line 
1/**********************************************************************
2 *
3 *    FILE:           ShapeAttributeList.cpp
4 *
5 *    DESCRIPTION:    Read/Write osgSim::ShapeAttributeList in binary
6 *                    format to disk.
7 *
8 *    CREATED BY:     John Vidar Larring
9 *
10 *    HISTORY:        Created 25.8.2008
11 *
12 **********************************************************************/
13
14#include "Exception.h"
15#include "ShapeAttributeList.h"
16
17using namespace ive;
18
19void ShapeAttributeList::write(DataOutputStream* out)
20{
21
22    // Write ShapeAttributeList's identification.
23    out->writeInt(IVESHAPEATTRIBUTELIST);
24
25    // Write ShapeAttributeList's properties.
26
27    // Write size of list
28    out->writeUInt(size());
29
30    // Write elements of the list
31    osgSim::ShapeAttributeList::const_iterator it = begin();
32    for (const_iterator it = begin(); it != end(); it++)
33    {
34        write(out, *it);
35    }
36}
37
38void ShapeAttributeList::read(DataInputStream* in)
39{
40    // Peek on ShapeAttributeList's identification.
41    int id = in->peekInt();
42    if(id == IVESHAPEATTRIBUTELIST){
43        // Read ShapeAttributeList's identification.
44        id = in->readInt();
45
46        // Read ShapeAttributeList's properties
47
48        // Read size of the list
49        unsigned int count = in->readUInt();
50       
51        resize(count);
52
53        // Read elements of the list
54        for (unsigned int i=0; i < count; i++)
55        {
56            read(in, (*this)[i]);
57        }
58    }
59    else{
60        in_THROW_EXCEPTION("ShapeAttributeList::read(): Expected ShapeAttributeList identification.");
61    }
62}
63
64void ShapeAttributeList::write(DataOutputStream* out, const osgSim::ShapeAttribute& sa)
65{
66    // Write name
67    out->writeString(sa.getName());
68   
69    // Write datatype
70    osgSim::ShapeAttribute::Type type = sa.getType();
71    out->writeInt((int)type);
72
73    // Write data
74    switch (type)
75    {
76    case osgSim::ShapeAttribute::INTEGER:
77        out->writeInt(sa.getInt());
78        break;
79    case osgSim::ShapeAttribute::DOUBLE:
80        out->writeDouble(sa.getDouble());
81        break;
82    case osgSim::ShapeAttribute::STRING:
83        out->writeBool(sa.getString() != 0);
84        if (sa.getString()) out->writeString(std::string(sa.getString()));
85        break;
86    default:
87        // Ignore Unknown data type
88        break;
89    }
90}
91
92void ShapeAttributeList::read(DataInputStream* in, osgSim::ShapeAttribute& sa)
93{
94    // Read name
95    sa.setName(in->readString());
96
97    // Read type
98    int type = in->readInt();
99
100    // Read data
101    switch (type)
102    {
103        case osgSim::ShapeAttribute::INTEGER:
104            sa.setValue(in->readInt());
105            break;
106        case osgSim::ShapeAttribute::DOUBLE:
107            sa.setValue(in->readDouble());
108            break;
109        case osgSim::ShapeAttribute::STRING:
110            if (in->readBool())
111                sa.setValue(in->readString().c_str());
112            else
113                sa.setValue((char*)0);
114            break;
115        default:
116            // Ignore Unknown data type
117            break;
118    }
119}
Note: See TracBrowser for help on using the browser.