aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/framework/include/ApexResourceProvider.h
blob: 17e9d6171d73817c1f256b99bebf6e5b6fb8d3d4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//  * Neither the name of NVIDIA CORPORATION nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2018 NVIDIA Corporation. All rights reserved.



#ifndef __APEX_RESOURCE_PROVIDER_H__
#define __APEX_RESOURCE_PROVIDER_H__

#include "Apex.h"
#include "PsUserAllocated.h"
#include "PsHashMap.h"
#include "ResourceProviderIntl.h"
#include "ApexString.h"
#include "PsArray.h"

namespace nvidia
{
namespace apex
{

class ApexResourceProvider : public ResourceProviderIntl, public UserAllocated
{
public:
	/* == Public ResourceProvider interface == */
	virtual void 			registerCallback(ResourceCallback* impl);
	virtual void 			setResource(const char* nameSpace, const char* name, void* resource, bool incRefCount);
	virtual void   			setResourceU32(const char* nameSpace, const char* name, uint32_t id, bool incRefCount);
	virtual void* 			getResource(const char* nameSpace, const char* name);
	virtual uint32_t  		releaseAllResourcesInNamespace(const char* nameSpace);
	virtual uint32_t  		releaseResource(const char* nameSpace, const char* name);
	virtual bool    		findRefCount(const char* nameSpace, const char* name, uint32_t& refCount);
	virtual void* 			findResource(const char* nameSpace, const char* name);
	virtual uint32_t 		findResourceU32(const char* nameSpace, const char* name); // find an existing resource.
	virtual void** 			findAllResources(const char* nameSpace, uint32_t& count); // find all resources in this namespace
	virtual const char** 	findAllResourceNames(const char* nameSpace, uint32_t& count); // find all resources in this namespace
	virtual const char** 	findNameSpaces(uint32_t& count);
	virtual void		 	dumpResourceTable();

	/* == Internal ResourceProviderIntl interface == */
	void	setResource(const char* nameSpace, const char* name, void* resource, bool valueIsSet, bool incRefCount);
	ResID	createNameSpace(const char* nameSpace, bool releaseAtExit)
	{
		return createNameSpaceInternal(nameSpace, releaseAtExit);
	}
	ResID	createResource(ResID nameSpace, const char* name, bool refCount);
	bool	checkResource(ResID nameSpace, const char* name);
	bool	checkResource(ResID id);
	void    releaseResource(ResID id);
	void	generateUniqueName(ResID nameSpace, ApexSimpleString& name);
	void*   getResource(ResID id);
	const char* getResourceName(ResID id);
	const char* getResourceNameSpace(ResID id);
	bool	getResourceIDs(const char* nameSpace, ResID* outResIDs, uint32_t& outCount, uint32_t inCount);

	// [PVD INTEGRATION CODE] ===========================================
	PX_INLINE uint32_t	getResourceCount() const
	{
		return mResources.size();
	}
	// ==================================================================

	// the NRP can either be case sensitive or not, this method takes care of that option
	PX_INLINE bool isCaseSensitive()
	{
		return mCaseSensitive;
	}
	
	/**
	\brief Sets the resource provider's case sensitive mode.
	
	\note This must be done immediately after initialization so no names are hashed
		  using the wrong mode.
	*/
	void	setCaseSensitivity(bool caseSensitive)
	{
		mCaseSensitive = caseSensitive;
	}

	// uses the correct string matching function based on the case sensitivity mode
	bool	stringsMatch(const char* str0, const char* str1);

protected:
	ApexResourceProvider();
	virtual ~ApexResourceProvider();
	void	destroy();

private:
	ResID	createNameSpaceInternal(const char* &nameSpace, bool releaseAtExit);

	class NameSpace: public UserAllocated
	{
	public:
		NameSpace(ApexResourceProvider* arp, ResID nsid, bool releaseAtExit, const char* nameSpace);
		~NameSpace();

		ResID	getOrCreateID(const char* &name, const char* NSName);
		ResID	getID() const
		{
			return mId;
		}
		bool    releaseAtExit() const
		{
			return mReleaseAtExit;
		}
		const char* getNameSpace(void) const
		{
			return mNameSpace;
		};

	private:
		struct entryHeader
		{
			const char* nextEntry;
			ResID     id;
		};
		enum { HashSize = 1024 };
		bool					mReleaseAtExit;
		uint16_t				genHash(const char* name);
		const char*				hash[HashSize];
		ApexResourceProvider*	mArp;
		ResID					mId;
		char* 					mNameSpace;
	};

	// NOTE: someone thinks this struct needs to be padded to nearest 16 or 32 bytes
	// it's not padded at the moment, but watch out for this
	struct resource
	{
		void*		ptr;
		const char* name;
		const char* nameSpace;
		uint16_t	refCount;
		uint8_t		valueIsSet;
		uint8_t		usedGetResource;
	};

	typedef physx::HashMap<ResID, ResID> HashMapNSID;

	ResID		getNSID(const char* nsName);
	uint32_t	getNSIndex(ResID nameSpace);

	HashMapNSID	mNSID;

	NameSpace					mNSNames;
	physx::Array<resource>		mResources;
	physx::Array<NameSpace*>	mNameSpaces;
	ResourceCallback*   		mUserCallback;
	physx::Array< const char* > mCharResults;
	bool						mCaseSensitive;


	enum { UnknownValue = 0xFFFFFFFF };

	friend class ApexSDKImpl;
};

}
} // end namespace nvidia::apex

#endif // __APEX_RESOURCE_PROVIDER_H__