MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ElementRegistry.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  ElementRegistry.h A simulation-wide registry of available elements by UUID
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
28 #ifndef ELEMENTREGISTRY_H
29 #define ELEMENTREGISTRY_H
30 
31 #include "itype.h"
32 #include "Element.h"
34 
35 namespace MFM {
36 
47  template <class CC>
49  {
50  public:
51  enum {
52  TABLE_SIZE = 100,
53  MAX_PATHS = 20,
54  MAX_PATH_LEN = 256
55  };
56 
57  private:
58 
59  // Extract short type names
60  typedef typename CC::ATOM_TYPE T;
61  typedef typename CC::PARAM_CONFIG P;
63 
64  public:
65 
67 
68  ~ElementRegistry() { }
69 
70  void Init();
71 
72  bool IsRegistered(const UUID & uuid) const;
73 
74  bool IsLoaded(const UUID & uuid) const;
75 
76  bool Load(const UUID & uuid) ;
77 
78  Element<CC> * Lookup(const UUID & uuid) const;
79 
80  Element<CC> * LookupCompatible(const UUID & uuid) const;
81 
86  void AddPath(const char * path) ;
87 
93  bool RegisterUUID(const UUID & uuid) ;
94 
98  bool RegisterElement(Element<CC>& e) ;
99 
104  u32 GetEntryCount() const
105  {
106  return m_registeredElementsCount;
107  }
108 
118  const UUID& GetEntryUUID(u32 entryIdx) const
119  {
120  if (entryIdx >= GetEntryCount())
121  FAIL(ARRAY_INDEX_OUT_OF_BOUNDS);
122  return m_registeredElements[entryIdx].m_uuid;
123  }
124 
134  const Element<CC>* GetEntryElement(u32 entryIdx) const
135  {
136  if (entryIdx >= GetEntryCount())
137  FAIL(ARRAY_INDEX_OUT_OF_BOUNDS);
138  return m_registeredElements[entryIdx].m_element;
139  }
140 
141 
142  private:
143  struct ElementEntry {
144  UUID m_uuid; //< Set in all cases
145  Element<CC>* m_element; //< Set if element is loaded
146  s32 m_pathIndex; //< Set if the element was found in this pathentry
147 
148  ElementEntry() : m_element(0), m_pathIndex(-1) { }
149  } m_registeredElements[TABLE_SIZE];
150  u32 m_registeredElementsCount;
151 
152  const ElementEntry * FindMatching(const UUID & uuid) const {
153  for (u32 i = 0; i < m_registeredElementsCount; ++i) {
154  if (m_registeredElements[i].m_uuid == uuid)
155  return &m_registeredElements[i];
156  }
157  return 0;
158  }
159 
160  ElementEntry * FindMatching(const UUID & uuid) {
161  for (u32 i = 0; i < m_registeredElementsCount; ++i) {
162  if (m_registeredElements[i].m_uuid == uuid)
163  return &m_registeredElements[i];
164  }
165  return 0;
166  }
167 
168  s32 FindCompatibleIndex(const UUID & uuid, s32 lastIndex) const {
169  if (lastIndex < -1)
170  FAIL(ILLEGAL_ARGUMENT);
171  for (s32 i = lastIndex + 1; i < (s32) m_registeredElementsCount; ++i) {
172  if (m_registeredElements[i].m_uuid.Compatible(uuid))
173  return i;
174  }
175  return -1;
176  }
177 
178  PathString m_searchPaths[MAX_PATHS];
179  u32 m_searchPathsCount;
180 
181  };
182 
183 } /* namespace MFM */
184 
185 #include "ElementRegistry.tcc"
186 
187 #endif /*ELEMENTREGISTRY_H*/
bool RegisterElement(Element< CC > &e)
Definition: ElementRegistry.tcc:70
bool RegisterUUID(const UUID &uuid)
Definition: ElementRegistry.tcc:83
void AddPath(const char *path)
Definition: ElementRegistry.tcc:139
Definition: ElementRegistry.h:48
u32 GetEntryCount() const
Definition: ElementRegistry.h:104
const UUID & GetEntryUUID(u32 entryIdx) const
Definition: ElementRegistry.h:118
Definition: UUID.h:44
const Element< CC > * GetEntryElement(u32 entryIdx) const
Definition: ElementRegistry.h:134
Definition: Atom.h:43