source: NonGTP/FCollada/FUtils/FUUniqueStringMap.h @ 964

Revision 964, 2.4 KB checked in by igarcia, 19 years ago (diff)
RevLine 
[964]1/*
2        Copyright (C) 2005-2006 Feeling Software Inc.
3        MIT License: http://www.opensource.org/licenses/mit-license.php
4*/
5
6/**
7        @file FUUniqueStringMap.h
8        This file contains the FUUniqueStringMapT template.
9*/
10
11#ifndef _FU_UNIQUE_ID_MAP_H_
12#define _FU_UNIQUE_ID_MAP_H_
13
14/**
15        A set of unique strings.
16        This class adds three functions to the STL map in order to
17        keep the strings inside unique: AddUniqueString, Exists and Erase.
18       
19        @ingroup FUtils
20*/
21template <typename STRING_BUILDER>
22class FUUniqueStringMapT : map<typename STRING_BUILDER::String, void*>
23{
24private:
25        typedef map<typename STRING_BUILDER::String, void*> Super;
26
27public:
28        /** Adds a string to the map.
29                If the string isn't unique, it will be modified in order to make it unique.
30                @param wantedStr The string to add. This reference will be directly
31                        modified to hold the actual unique string added to the map. */
32        void AddUniqueString(typename STRING_BUILDER::String& wantedStr);
33
34        /** Retrieves whether a given string is contained within the map.
35                @param str The string. */
36        bool Exists(const typename STRING_BUILDER::String& str) const;
37
38        /** Erases a string from the map.
39                @param str A string contained within the map. */
40        void Erase(const typename STRING_BUILDER::String& str);
41};
42
43typedef FUUniqueStringMapT<FUSStringBuilder> FUSUniqueStringMap; /**< A map of unique UTF-8 strings. */
44typedef FUUniqueStringMapT<FUStringBuilder> FUUniqueStringMap; /**< A map of unique Unicode strings. */
45
46template <typename Builder>
47void FUUniqueStringMapT<Builder>::AddUniqueString(typename Builder::String& wantedStr)
48{
49        if (Exists(wantedStr))
50        {
51                // Attempt to generate a new string by appending an increasing counter.
52                uint32 counter = 2;
53                static const maxCounter = 256;
54                Builder buffer;
55                buffer.reserve(wantedStr.length() + 5);
56                do
57                {
58                        buffer.set(wantedStr);
59                        buffer.append(counter);
60                } while (counter++ < maxCounter && Exists(buffer.ToString()));
61           
62                // Hopefully, this is now unique.
63                wantedStr = buffer.ToString();
64        }
65        insert(Super::value_type(wantedStr, NULL));
66}
67
68template <typename Builder>
69bool FUUniqueStringMapT<Builder>::Exists(const typename Builder::String& str) const
70{
71        const_iterator it = find(str);
72        return it != end();
73}
74
75template <typename Builder>
76void FUUniqueStringMapT<Builder>::Erase(const typename Builder::String& str)
77{
78        iterator it = find(str);
79        if (it != end()) erase(it);
80}
81
82#endif // _FU_UNIQUE_ID_MAP_H_
83
Note: See TracBrowser for help on using the repository browser.