| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <memory.h> |
|---|
| 15 | #include <stdlib.h> |
|---|
| 16 | #include <string.h> |
|---|
| 17 | |
|---|
| 18 | #include <osgSim/ShapeAttribute> |
|---|
| 19 | |
|---|
| 20 | namespace osgSim |
|---|
| 21 | { |
|---|
| 22 | ShapeAttribute::ShapeAttribute() : |
|---|
| 23 | _type(UNKNOWN), |
|---|
| 24 | _integer(0) |
|---|
| 25 | {} |
|---|
| 26 | |
|---|
| 27 | ShapeAttribute::ShapeAttribute(const char * name) : |
|---|
| 28 | _name(name), |
|---|
| 29 | _type(UNKNOWN), |
|---|
| 30 | _integer(0) |
|---|
| 31 | {} |
|---|
| 32 | |
|---|
| 33 | ShapeAttribute::ShapeAttribute(const char * name, int value) : |
|---|
| 34 | _name(name), |
|---|
| 35 | _type(INTEGER), |
|---|
| 36 | _integer(value) |
|---|
| 37 | {} |
|---|
| 38 | |
|---|
| 39 | ShapeAttribute::ShapeAttribute(const char * name, double value) : |
|---|
| 40 | _name(name), |
|---|
| 41 | _type(DOUBLE), |
|---|
| 42 | _double(value) |
|---|
| 43 | {} |
|---|
| 44 | |
|---|
| 45 | ShapeAttribute::ShapeAttribute(const char * name, const char * value) : |
|---|
| 46 | _name(name), |
|---|
| 47 | _type(STRING), |
|---|
| 48 | _string(value ? strdup(value) : 0) |
|---|
| 49 | { |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | ShapeAttribute::ShapeAttribute(const ShapeAttribute & sa) |
|---|
| 53 | { |
|---|
| 54 | copy(sa); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | ShapeAttribute::~ShapeAttribute() |
|---|
| 59 | { |
|---|
| 60 | free(); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | void ShapeAttribute::free() |
|---|
| 64 | { |
|---|
| 65 | if ((_type == STRING) && (_string)) |
|---|
| 66 | { |
|---|
| 67 | ::free(_string); |
|---|
| 68 | _string = 0; |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | void ShapeAttribute::setValue(const char * value) |
|---|
| 73 | { |
|---|
| 74 | free(); |
|---|
| 75 | _type = STRING; |
|---|
| 76 | _string = (value ? strdup(value) : 0); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | void 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 | |
|---|
| 110 | ShapeAttribute& 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 | |
|---|
| 121 | int 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 | |
|---|
| 157 | int 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 | } |
|---|