root/OpenSceneGraph/trunk/src/osgSim/ShapeAttribute.cpp @ 13041

Revision 13041, 3.5 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2007 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#include <memory.h>
15#include <stdlib.h>
16#include <string.h>
17
18#include <osgSim/ShapeAttribute>
19
20namespace osgSim
21{
22ShapeAttribute::ShapeAttribute() :
23    _type(UNKNOWN),
24    _integer(0)
25{}
26
27ShapeAttribute::ShapeAttribute(const char * name) :
28    _name(name),
29    _type(UNKNOWN),
30    _integer(0)
31{}
32
33ShapeAttribute::ShapeAttribute(const char * name, int value) :
34    _name(name),
35    _type(INTEGER),
36    _integer(value)
37{}
38
39ShapeAttribute::ShapeAttribute(const char * name, double value) :
40    _name(name),
41    _type(DOUBLE),
42    _double(value)
43{}
44
45ShapeAttribute::ShapeAttribute(const char * name, const char * value) :
46    _name(name),
47    _type(STRING),
48    _string(value ? strdup(value) : 0)
49{
50}
51
52ShapeAttribute::ShapeAttribute(const ShapeAttribute & sa)
53{
54    copy(sa);
55}
56
57
58ShapeAttribute::~ShapeAttribute()
59{
60    free();
61}
62
63void ShapeAttribute::free()
64{
65    if ((_type == STRING) && (_string))
66    {
67        ::free(_string);
68        _string = 0;
69    }
70}
71
72void ShapeAttribute::setValue(const char * value)
73{
74    free();
75    _type = STRING;
76    _string = (value ? strdup(value) : 0);
77}
78
79void ShapeAttribute::copy(const ShapeAttribute& sa)
80{
81    _name = sa._name;
82    _type = sa._type;
83
84    switch (_type)
85    {
86        case INTEGER:
87        {
88            _integer = sa._integer;
89            break;
90        }
91        case STRING:
92        {
93            _string = sa._string ? strdup(sa._string) : 0;
94            break;
95        }
96        case DOUBLE:
97        {
98            _double = sa._double;
99            break;
100        }
101        case UNKNOWN:
102        default:
103        {
104            _integer = 0;
105            break;
106        }
107    }
108}
109
110ShapeAttribute& ShapeAttribute::operator = (const ShapeAttribute& sa)
111{
112    if (&sa == this) return *this;
113
114    free();
115    copy(sa);
116
117    return *this;
118}
119
120
121int ShapeAttribute::compare(const osgSim::ShapeAttribute& sa) const
122{
123    if (_name<sa._name) return -1;
124    if (sa._name<_name) return 1;
125
126    if (_type<sa._type) return -1;
127    if (sa._type<_type) return 1;
128
129    if (_name<sa._name) return -1;
130    if (sa._name<_name) return 1;
131
132    switch (_type)
133    {
134        case STRING:
135        {
136            if (_string<sa._string) return -1;
137            if (sa._string<_string) return 1;
138        }
139        case DOUBLE:
140        {
141            if (_double<sa._double) return -1;
142            if (sa._double<_double) return 1;
143        }
144        case INTEGER:
145        case UNKNOWN:
146        default:
147        {
148            if (_integer<sa._integer) return -1;
149            if (sa._integer<_integer) return 1;
150        }
151    }
152    return 0;
153}
154
155
156/** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
157int ShapeAttributeList::compare(const osgSim::ShapeAttributeList& sal) const
158{
159    const_iterator salIt, thisIt, thisEnd = end();
160
161    int ret;
162    for (thisIt = begin(), salIt = sal.begin(); thisIt!= thisEnd; ++thisIt, ++salIt)
163        if ((ret = thisIt->compare(*salIt)) != 0) return ret;
164
165    return 0;
166}
167
168}
Note: See TracBrowser for help on using the browser.