12 namespace MQ0x {
namespace {
namespace prv_impl {
14 template <
typename T_MQOBJECT>
15 struct MQIndexedObjectIFBase {
16 const std::function<int (const MQDocument)> count_function;
17 const std::function<T_MQOBJECT(const MQDocument, int)> get_function;
19 MQIndexedObjectIFBase() {}
20 MQIndexedObjectIFBase(
const std::function<
int (
const MQDocument)> _count_function,
21 const std::function<T_MQOBJECT(
const MQDocument,
int)> _get_function ) :
22 count_function( _count_function ), get_function( _get_function ) {}
24 template <
typename T_MQOBJECT>
25 struct MQIndexedObjectIF :
public MQIndexedObjectIFBase<T_MQOBJECT> {
26 private: MQIndexedObjectIF() : MQIndexedObjectIFBase() {}
29 struct MQIndexedObjectIF<MQObject> :
public MQIndexedObjectIFBase<MQObject> {
30 MQIndexedObjectIF () : MQIndexedObjectIFBase (
31 [](const MQDocument doc) {
return doc->GetObjectCount(); },
32 [](
const MQDocument doc,
int index) {
return doc->GetObject(index); } ) {}
35 struct MQIndexedObjectIF<MQMaterial> :
public MQIndexedObjectIFBase<MQMaterial> {
36 MQIndexedObjectIF () : MQIndexedObjectIFBase (
37 [](const MQDocument doc) {
return doc->GetMaterialCount(); },
38 [](
const MQDocument doc,
int index) {
return doc->GetMaterial(index); } ) {}
40 template <
typename T_MQOBJECT>
41 struct MQIndexObjectCollection {
43 MQIndexObjectCollection();
44 const MQIndexedObjectIF<T_MQOBJECT> functionObject;
45 const MQDocument m_doc;
47 MQIndexObjectCollection(
const MQDocument doc ) : functionObject(), m_doc( doc ) {}
49 if ( m_doc == NULL )
return 0;
50 return functionObject.count_function( m_doc );
52 T_MQOBJECT operator [] (
int index ) {
53 if ( m_doc == NULL )
return 0;
54 return functionObject.get_function( m_doc, index );
58 template <
typename T_MQOBJECT>
inline
62 std::vector<char> tmp;
67 if ( doc != NULL && srcObj != NULL )
69 unsigned long trailNum = 0;
71 std::regex re(
"[0-9]+$");
72 std::match_results<const char *> results;
73 GetNamedName( srcObj, buf );
74 if ( std::regex_search( &(*buf)[0], results, re, std::regex_constants::match_default ) ) {
75 if ( 0 < results.str().length() ) {
77 trailNum = ::strtoul( results.str().c_str(), &end, 10 );
79 prefix = results.prefix().str();
81 else prefix = &(*buf)[0];
84 std::stringstream strstr;
85 strstr << std::setw( results.str().length() ) << std::setfill(
'0') << (trailNum += 1);
86 ret = prefix + strstr.str();
88 while ( GetNamed<T_MQOBJECT>(doc, ret.c_str() ) != NULL );
93 template <
typename T_MQNAMED>
inline
94 size_t GetNamedNameImplement(
const T_MQNAMED obj, std::vector<char>*
const buf,
const std::vector<char>::size_type initial_size )
97 std::vector<char>::size_type _initial_size = std::max(initial_size, (std::vector<char>::size_type)2);
98 for ( buf->resize( std::max( buf->size(), _initial_size ),
'\0' );
true; buf->resize( buf->size() * 2,
'\0' ) )
100 obj->GetName( &(*buf)[0], buf->size() );
101 if ( (*buf)[buf->size() - 2] ==
'\0' )
return strlen(&(*buf)[0]);
109 template <
typename T_MQNAMED>
110 size_t GetNamedName(
const T_MQNAMED obj, std::vector<char>*
const buf = NULL,
113 if ( obj == NULL )
return -1;
114 else if ( buf == NULL ) {
115 std::vector<char> tmp;
116 return GetNamedNameImplement( obj, &tmp, initial_size );
118 else return GetNamedNameImplement( obj, buf, initial_size );
120 template <
typename T_MQOBJECT>
inline std::string
GetName(
const T_MQOBJECT objNamed ) {
121 if ( objNamed == NULL )
return "";
122 std::vector<char> buf;
123 prv_impl::GetNamedName( objNamed, &buf );
126 template <
typename T_MQNAMED>
inline T_MQNAMED GetNamed(
const MQDocument doc,
const char* name ) {
127 if ( doc != NULL && name != NULL ) {
128 MQIndexObjectCollection<T_MQNAMED> namedObjects(doc);
129 std::vector<char> buf;
130 for (
int index = 0, ctObject = namedObjects.Count(); index < ctObject; index++ ) {
131 T_MQNAMED obj = namedObjects[index];
133 GetNamedName(obj, &buf);
134 if ( ::strcmp(&buf[0], name) == 0 )
return obj;
140 template <
typename T_MQNAMED>
inline int GetNamedIndex(
const MQDocument doc,
const char* name ) {
141 if ( doc != NULL && name != NULL ) {
142 MQIndexObjectCollection<T_MQNAMED> namedObjects(doc);
143 std::vector<char> buf;
144 for (
int index = 0, ctObject = namedObjects.Count(); index < ctObject; index++ ) {
145 T_MQNAMED obj = namedObjects[index];
147 GetNamedName(obj, &buf);
148 if ( ::strcmp(&buf[0], name) == 0 )
return index;
154 template <
typename T_MQIDENTIFIED>
inline
155 T_MQIDENTIFIED GetIdentified(
const MQDocument doc,
const UINT
id )
158 MQIndexObjectCollection<T_MQIDENTIFIED> identifiedObjects(doc);
159 for (
int index = 0, ctObject = identifiedObjects.Count(); index < ctObject; index++ ) {
160 T_MQIDENTIFIED obj = identifiedObjects[index];
161 if ( obj != NULL && (obj->GetUniqueID() == id) )
return obj;
166 template <
typename T_MQIDENTIFIED>
inline
167 int GetIdentifiedIndex(
const MQDocument doc,
const UINT
id )
170 MQIndexObjectCollection<T_MQIDENTIFIED> identifiedObjects(doc);
171 for (
int index = 0, ctObject = identifiedObjects.Count(); index < ctObject; index++ ) {
172 T_MQIDENTIFIED obj = identifiedObjects[index];
173 if ( obj != NULL && (obj->GetUniqueID() == id) )
return index;
178 template <
typename T_MQIDENTIFIED>
inline
179 int GetIdentifiedIndex(
const MQDocument doc,
const T_MQIDENTIFIED src )
181 if ( (doc != NULL) && (src!=NULL) )
return GetIdentifiedIndex<T_MQIDENTIFIED>( doc, src->GetUniqueID() );
184 inline void GetMaterialTextureName( std::vector<char>* buf,
const MQMaterial mat, DWORD map_type = MQMAPPING_TEXTURE ) {
185 if ( mat == NULL )
return;
187 const std::vector<char>::size_type _initial_size =
193 for ( buf->resize( std::max( buf->size(), _initial_size ),
'\0' );
true; buf->resize( buf->size() * 2,
'\0' ) )
195 switch ( map_type ) {
196 case MQMAPPING_TEXTURE:
197 mat->GetTextureName( &(*buf)[0], buf->size() );
199 case MQMAPPING_ALPHA:
200 mat->GetAlphaName( &(*buf)[0], buf->size() );
203 mat->GetBumpName( &(*buf)[0], buf->size() );
208 if ( (*buf)[buf->size() - 2] ==
'\0' )
return;
212 #if 0x0310 <= MQPLUGIN_VERSION
213 template <
typename T_MQOBJECT>
void get_unused_name_function(
const MQDocument,
char*,
int,
const char*);
214 template <>
void get_unused_name_function<MQMaterial>(
const MQDocument doc,
char* buffer,
int buffer_size,
const char* base_name) {
215 doc->GetUnusedMaterialName(buffer,buffer_size,base_name);
217 template <>
void get_unused_name_function<MQObject>(
const MQDocument doc,
char* buffer,
int buffer_size,
const char* base_name) {
218 doc->GetUnusedObjectName(buffer,buffer_size,base_name);
221 template <
typename T_MQNAMED>
inline
222 size_t GetUnusedNameImplement(
const MQDocument doc, std::vector<char>*
const buf,
const std::vector<char>::size_type initial_size,
const char* base_name )
224 std::vector<char>::size_type _initial_size = std::max(initial_size, (std::vector<char>::size_type)2);
225 for ( buf->resize( std::max( buf->size(), _initial_size ),
'\0' );
true; buf->resize( buf->size() * 2,
'\0' ) )
227 get_unused_name_function<T_MQNAMED>( doc, &(*buf)[0], buf->size(), base_name );
228 if ( (*buf)[buf->size() - 2] ==
'\0' )
return strlen(&(*buf)[0]);
236 template <
typename T_MQNAMED>
inline
237 size_t GetUnusedName(
const MQDocument doc,
const char* base_name, std::vector<char>*
const buf = NULL,
240 if ( doc == NULL )
return -1;
241 else if ( buf == NULL ) {
242 std::vector<char> tmp;
243 return GetUnusedNameImplement<T_MQNAMED>( doc, &tmp, initial_size, base_name );
245 return GetUnusedNameImplement<T_MQNAMED>( doc, buf, initial_size, base_name );
247 template <
typename T_MQNAMED>
inline
248 std::string GetUnusedNameString(
const MQDocument doc,
const char* base_name = NULL ) {
249 if ( doc == NULL )
return "";
250 std::vector<char> tmp;
251 if ( GetUnusedName<T_MQNAMED>( doc, base_name, &tmp ) <= 0 )
return "";