@@ -75,27 +75,38 @@ namespace asset
75
75
}
76
76
virtual ~ISkeleton ()
77
77
{
78
- _NBL_DELETE_ARRAY (m_stringPool,m_stringPoolSize);
78
+ clearNames ();
79
+ }
80
+
81
+ // map must contain one `const char*` per bone
82
+ inline void setJointNames (const decltype (m_nameToJointID)& nameToJointIDMap)
83
+ {
84
+ clearNames ();
85
+
86
+ // size the pool
87
+ for (const auto & mapping : nameToJointIDMap)
88
+ inName (mapping.first );
89
+
90
+ // useless names
91
+ if (m_stringPoolSize==0ull )
92
+ return ;
93
+
94
+ m_stringPool = _NBL_NEW_ARRAY (char ,m_stringPoolSize);
95
+
96
+ char * outName = m_stringPool;
97
+ for (const auto & mapping : nameToJointIDMap)
98
+ outName = insertName (outName,mapping.first ,mapping.second );
79
99
}
80
100
81
101
// iterator range must contain one `const char*` per bone
82
102
template <typename NameIterator>
83
103
inline void setJointNames (NameIterator begin, NameIterator end)
84
104
{
85
- // deinit
86
- if (m_stringPool)
87
- _NBL_DELETE_ARRAY (m_stringPool,m_stringPoolSize);
88
- m_stringPoolSize = 0ull ;
89
- m_nameToJointID.clear ();
105
+ clearNames ();
90
106
91
107
// size the pool
92
108
for (auto it=begin; it!=end; it++)
93
- {
94
- const char * inName = *it;
95
- const auto nameLen = strlen (inName);
96
- if (nameLen)
97
- m_stringPoolSize += nameLen+1ull ;
98
- }
109
+ inName (*it);
99
110
100
111
// useless names
101
112
if (m_stringPoolSize==0ull )
@@ -106,24 +117,14 @@ namespace asset
106
117
char * outName = m_stringPool;
107
118
joint_id_t jointID = 0u ;
108
119
for (auto it=begin; it!=end; it++,jointID++)
109
- {
110
- const char * name = outName;
111
-
112
- const char * inName = *it;
113
- while (*inName) {*(outName++) = *(inName++);}
114
- if (outName!=name)
115
- {
116
- *(outName++) = 0 ;
117
- m_nameToJointID.emplace (name,jointID);
118
- }
119
- }
120
+ outName = insertName (outName,*it,jointID);
120
121
}
121
122
122
123
struct StringComparator
123
124
{
124
125
inline bool operator ()(const char * lhs, const char * rhs) const
125
126
{
126
- return strcmp (lhs,rhs);
127
+ return strcmp (lhs,rhs)< 0 ;
127
128
}
128
129
};
129
130
core::map<const char *,joint_id_t ,StringComparator> m_nameToJointID;
@@ -132,6 +133,32 @@ namespace asset
132
133
133
134
SBufferBinding<BufferType> m_parentJointIDs,m_inverseBindPoses;
134
135
joint_id_t m_jointCount;
136
+
137
+ private:
138
+ inline void reserveName (const char * inName)
139
+ {
140
+ const auto nameLen = strlen (inName);
141
+ if (nameLen)
142
+ m_stringPoolSize += nameLen+1ull ;
143
+ }
144
+ inline const char * insertName (const char * outName, const char * inName, joint_id_t jointID)
145
+ {
146
+ const char * name = outName;
147
+ while (*inName) {*(outName++) = *(inName++);}
148
+ if (outName!=name)
149
+ {
150
+ *(outName++) = 0 ;
151
+ m_nameToJointID.emplace (name,jointID);
152
+ }
153
+ return outName;
154
+ }
155
+ inline void clearNames ()
156
+ {
157
+ if (m_stringPool)
158
+ _NBL_DELETE_ARRAY (m_stringPool,m_stringPoolSize);
159
+ m_stringPoolSize = 0ull ;
160
+ m_nameToJointID.clear ();
161
+ }
135
162
};
136
163
137
164
} // end namespace asset
0 commit comments