gwnavruntime/navdata/databasedescriptorindex.h Source File

databasedescriptorindex.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 #ifndef Navigation_AggregatedDataIndex_H
8 #define Navigation_AggregatedDataIndex_H
9 
15 
16 namespace Kaim
17 {
18 
19 
20 class DatabaseDescriptorIndex : public RefCountBaseNTS<DatabaseDescriptorIndex, MemStat_Data_Other>
21 {
22  KY_CLASS_WITHOUT_COPY(DatabaseDescriptorIndex)
23 
24 public:
25  enum DataIndexLoadMode
26  {
27  LoadDataIndex_NavData = 1 << 0,
28  LoadDataIndex_AbstractData = 1 << 1,
29  LoadDataIndex_ColData = 1 << 2,
30 
31  LoadDataIndex_All = KyUInt32MAXVAL,
32  };
33 
34 public:
35  DatabaseDescriptorIndex() {}
36 
37  void Clear()
38  {
39  m_navDataIndexHandler = KY_NULL;
40  m_abstractDataIndexHandler = KY_NULL;
41  m_colDataIndexHandler = KY_NULL;
42  m_databaseDescriptor.m_databaseIndex = 0;
43  }
44 
45 
46  KyResult LoadDataIndex(const char* filename, FileOpenerBase* fileOpener = KY_NULL, DataIndexLoadMode loadMode = LoadDataIndex_All)
47  {
48  DefaultFileOpener defaultFileOpener;
49  FileOpenerBase* opener = fileOpener ? fileOpener : &defaultFileOpener;
50  Ptr<File> file = opener->OpenFile(filename, OpenMode_Read);
51  if (file == KY_NULL)
52  {
53  KY_LOG_ERROR(("Could not open file [%s]", filename));
54  return KY_ERROR;
55  }
56 
57  BlobAggregate blobAggregate;
58 
59  if (blobAggregate.Load(file, MemStat_Blob, KY_NULL, BLOB_AGGREGATE_IGNORE_UNKNOWN_BLOBTYPES) == KY_ERROR)
60  return KY_ERROR;
61 
62  return LoadDataIndex(blobAggregate, loadMode);
63  }
64 
65 
66  KyResult LoadDataIndex(const BlobAggregate& blobAggregate, DataIndexLoadMode loadMode = LoadDataIndex_All)
67  {
68  if (LoadDataIndex_NavData & loadMode)
69  {
70  m_navDataIndexHandler = KY_NULL;
71  BlobAggregate::Collection<NavDataIndex> collection = blobAggregate.GetCollection<NavDataIndex>();
72  if (collection.GetCount() == 1)
73  m_navDataIndexHandler = collection.GetHandler(0);
74  else if (collection.GetCount() > 1)
75  {
76  KY_LOG_ERROR(("ERROR: BlobAggregate contains more than 1 NavDataIndex. No NavDataIndex has been loaded"));
77  Clear();
78  return KY_ERROR;
79  }
80  }
81 
82  if (LoadDataIndex_AbstractData & loadMode)
83  {
84  m_abstractDataIndexHandler = KY_NULL;
85  BlobAggregate::Collection<AbstractDataIndex> collection = blobAggregate.GetCollection<AbstractDataIndex>();
86  if (collection.GetCount() == 1)
87  m_abstractDataIndexHandler = collection.GetHandler(0);
88  else if (collection.GetCount() > 1)
89  {
90  KY_LOG_ERROR(("ERROR: BlobAggregate contains more than 1 AbstractDataIndex. No AbstractDataIndex has been loaded"));
91  Clear();
92  return KY_ERROR;
93  }
94  }
95 
96  if (LoadDataIndex_ColData & loadMode)
97  {
98  m_colDataIndexHandler = KY_NULL;
99  BlobAggregate::Collection<ColDataIndex> collection = blobAggregate.GetCollection<ColDataIndex>();
100  if (collection.GetCount() == 1)
101  m_colDataIndexHandler = collection.GetHandler(0);
102  else if (collection.GetCount() > 1)
103  {
104  KY_LOG_ERROR(("ERROR: BlobAggregate contains more than 1 ColDataIndex. No ColDataIndex has been loaded"));
105  Clear();
106  return KY_ERROR;
107  }
108  }
109 
110  BlobAggregate::Collection<DatabaseDescriptorBlob> databaseDescriptor = blobAggregate.GetCollection<DatabaseDescriptorBlob>();
111  if (databaseDescriptor.GetCount() != 0)
112  {
113  if (m_databaseDescriptor.ReadFromAggregate(blobAggregate) == KY_ERROR)
114  {
115  KY_LOG_ERROR(("ERROR: BlobAggregate contains more than 1 ColDataIndex. No ColDataIndex has been loaded"));
116  Clear();
117  return KY_ERROR;
118  }
119  }
120 
121  return KY_SUCCESS;
122  }
123 
124 
125  KyResult Save(const char* absoluteFilename, FileOpenerBase* fileOpener = KY_NULL)
126  {
127  DefaultFileOpener defaultFileOpener;
128  FileOpenerBase* opener = fileOpener ? fileOpener : &defaultFileOpener;
129  Ptr<File> file = opener->OpenFile(absoluteFilename, OpenMode_Write);
130  if (file == KY_NULL)
131  {
132  KY_LOG_ERROR(("Could not access file [%s]", absoluteFilename));
133  return KY_ERROR;
134  }
135 
136  BlobAggregate blobAggregate;
137 
138  // NavDataIndex
139  if (m_navDataIndexHandler != KY_NULL && m_navDataIndexHandler->Blob() != KY_NULL)
140  blobAggregate.AddBlob(m_navDataIndexHandler);
141 
142  // AbstractDataIndex
143  if (m_abstractDataIndexHandler != KY_NULL && m_abstractDataIndexHandler->Blob() != KY_NULL)
144  blobAggregate.AddBlob(m_abstractDataIndexHandler);
145 
146  // ColDataIndex
147  if (m_colDataIndexHandler != KY_NULL && m_colDataIndexHandler->Blob() != KY_NULL)
148  blobAggregate.AddBlob(m_colDataIndexHandler);
149 
150  // Database descriptor
151  Ptr<BlobHandler<DatabaseDescriptorBlob> > databaseDescriptorBlobHandler = *KY_NEW BlobHandler<DatabaseDescriptorBlob>;
152  DatabaseDescriptorBlobBuilder descriptorBlobBuilder(&m_databaseDescriptor);
153  descriptorBlobBuilder.Build(*databaseDescriptorBlobHandler);
154  blobAggregate.AddBlob(databaseDescriptorBlobHandler);
155 
156  if (Result::Fail(blobAggregate.Save(file)))
157  {
158  KY_LOG_ERROR(("Could not save DatabaseDescriptorIndex to [%s]", absoluteFilename));
159  return KY_ERROR;
160  }
161 
162  return KY_SUCCESS;
163  }
164 
165 public:
166  Ptr< BlobHandler<NavDataIndex> > m_navDataIndexHandler;
167  Ptr< BlobHandler<AbstractDataIndex> > m_abstractDataIndexHandler;
168  Ptr< BlobHandler<ColDataIndex> > m_colDataIndexHandler;
169 
170  DatabaseDescriptor m_databaseDescriptor;
171 };
172 
173 }
174 
175 #endif
KyInt32 KyResult
Defines a type that can be returned by methods or functions in the Gameware Navigation SDK to indicat...
Definition: types.h:254
Opens the file for writing.
Definition: fileopener.h:31
#define KY_NULL
Null value.
Definition: types.h:247
#define KY_CLASS_WITHOUT_COPY(ClassName)
Define to forbid copy constructor and copy assignment.
Definition: types.h:387
Opens the file for reading only.
Definition: fileopener.h:30
bool Fail(KyResult result)
Returns true if the specified result code indicates that the requested operation failed.
Definition: types.h:268
Definition: gamekitcrowddispersion.h:20
#define KY_SUCCESS
Shorthand for Kaim::Result::Success.
Definition: types.h:273
#define KY_ERROR
Shorthand for Kaim::Result::Failure.
Definition: types.h:272
#define KyUInt32MAXVAL
The maximum value that can be stored in the KyUInt32 variable type.
Definition: types.h:226