~ares-developers/ares/gd03

« back to all changes in this revision

Viewing changes to Enum/_Enumerator.hpp

  • Committer: Renegade
  • Date: 2010-05-29 08:12:17 UTC
  • Revision ID: git-v1:0a1bb6321f04d723afe64d1b843dc87b4da783ec
Creating /trunk/src.

git-svn-id: svn://svn.renegadeprojects.com/ares/trunk@622 859b54a9-7a54-0410-aeb3-f8d2f1fa40fd

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#ifndef ENUMERATOR_H
2
 
#define ENUMERATOR_H
3
 
 
4
 
#include <algorithm>
5
 
#include <functional>
6
 
 
7
 
#include <ArrayClasses.h>
8
 
#include <CCINIClass.h>
9
 
 
10
 
template <typename T> class Enumerable
11
 
{
12
 
public:
13
 
        static DynamicVectorClass< T* > Array;
14
 
 
15
 
        struct comparator : public std::binary_function<Enumerable<T>*, const char *, bool> {
16
 
                bool operator()(Enumerable<T>* Item, const char* title) const { return !_strcmpi(Item->Name, title); }
17
 
        };
18
 
 
19
 
        static T** stl_Find(const char *Title) {
20
 
                return std::find_if(Array.start(), Array.end(), std::bind2nd(comparator(), Title));
21
 
        }
22
 
 
23
 
        static int FindIndex(const char *Title)
24
 
        {
25
 
                for(int i = 0; i < Array.Count; ++i)
26
 
                        if(!_strcmpi(Title, Array.GetItem(i)->Name))
27
 
                                return i;
28
 
                return -1;
29
 
        }
30
 
 
31
 
        static T* Find(const char *Title)
32
 
        {
33
 
/*              for(int i = 0; i < Array.get_Count(); ++i)
34
 
                        if(!_strcmpi(Title, Array.GetItem(i)->Name))
35
 
                                return Array.GetItem(i);
36
 
*/
37
 
                T** result = Enumerable<T>::stl_Find(Title);
38
 
                if(result == Array.end()) {
39
 
                        return NULL;
40
 
                }
41
 
                return *result;
42
 
        }
43
 
 
44
 
        static T* FindOrAllocate(const char *Title)
45
 
        {
46
 
                T *find = Find(Title);
47
 
                return find ? find : new T(Title);
48
 
        }
49
 
 
50
 
        static void ClearArray()
51
 
        {
52
 
                for(int i = Array.Count - 1; i >= 0; --i) {
53
 
                        delete Array[i];
54
 
                        Array.RemoveItem(i);
55
 
                }
56
 
        }
57
 
 
58
 
        static void LoadFromINIList(CCINIClass *pINI)
59
 
        {
60
 
                const char *section = GetMainSection();
61
 
                int len = pINI->GetKeyCount(section);
62
 
                for(int i = 0; i < len; ++i) {
63
 
                        const char *Key = pINI->GetKeyName(section, i);
64
 
                        FindOrAllocate(Key);
65
 
                }
66
 
                for(int i = 0; i < Array.Count; ++i) {
67
 
                        Array[i]->LoadFromINI(pINI);
68
 
                }
69
 
        }
70
 
 
71
 
        Enumerable()
72
 
        {
73
 
                ;
74
 
        }
75
 
 
76
 
        virtual ~Enumerable()
77
 
        {
78
 
                ;
79
 
        }
80
 
 
81
 
//      template <typename T2>
82
 
        static const char * GetMainSection();
83
 
 
84
 
        char Name[32];
85
 
};
86
 
 
87
 
#endif