xref: /aosp_15_r20/external/deqp/framework/common/tcuResource.hpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1 #ifndef _TCURESOURCE_HPP
2 #define _TCURESOURCE_HPP
3 /*-------------------------------------------------------------------------
4  * drawElements Quality Program Tester Core
5  * ----------------------------------------
6  *
7  * Copyright 2014 The Android Open Source Project
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Resource system.
24  *//*--------------------------------------------------------------------*/
25 
26 #include "tcuDefs.hpp"
27 
28 #include <string>
29 
30 // \todo [2010-07-31 pyry] Move Archive and File* to separate files
31 
32 namespace tcu
33 {
34 
35 /*--------------------------------------------------------------------*//*!
36  * \brief Resource object
37  *
38  * Test framework uses abstraction for data access instead of using
39  * files directly to better support platforms where application resources
40  * are not available directly in the filesystem.
41  *
42  * Resource objects are requested from Archive object provided by Platform.
43  * The user is responsible of disposing the objects afterwards.
44  *//*--------------------------------------------------------------------*/
45 class Resource
46 {
47 public:
~Resource(void)48     virtual ~Resource(void)
49     {
50     }
51 
52     virtual void read(uint8_t *dst, int numBytes) = 0;
53     virtual int getSize(void) const               = 0;
54     virtual int getPosition(void) const           = 0;
55     virtual void setPosition(int position)        = 0;
56 
getName(void) const57     const std::string &getName(void) const
58     {
59         return m_name;
60     }
61 
62 protected:
Resource(const std::string & name)63     Resource(const std::string &name) : m_name(name)
64     {
65     }
66 
67 private:
68     std::string m_name;
69 };
70 
71 /*--------------------------------------------------------------------*//*!
72  * \brief Abstract resource archive
73  *//*--------------------------------------------------------------------*/
74 class Archive
75 {
76 public:
~Archive(void)77     virtual ~Archive(void)
78     {
79     }
80 
81     /*--------------------------------------------------------------------*//*!
82      * \brief Open resource
83      *
84      * Throws resource error if no resource with given name exists.
85      *
86      * Resource object must be deleted after use
87      *
88      * \param name Resource path
89      * \return Resource object
90      *//*--------------------------------------------------------------------*/
91     virtual Resource *getResource(const char *name) const = 0;
92 
93 protected:
Archive()94     Archive()
95     {
96     }
97 };
98 
99 /*--------------------------------------------------------------------*//*!
100  * \brief Directory-based archive implementation
101  *//*--------------------------------------------------------------------*/
102 class DirArchive : public Archive
103 {
104 public:
105     DirArchive(const char *path);
106     ~DirArchive(void);
107 
108     Resource *getResource(const char *name) const;
109 
110     // \note Assignment and copy allowed
DirArchive(const DirArchive & other)111     DirArchive(const DirArchive &other) : Archive(), m_path(other.m_path)
112     {
113     }
operator =(const DirArchive & other)114     DirArchive &operator=(const DirArchive &other)
115     {
116         m_path = other.m_path;
117         return *this;
118     }
119 
120 private:
121     std::string m_path;
122 };
123 
124 class FileResource : public Resource
125 {
126 public:
127     FileResource(const char *filename);
128     ~FileResource(void);
129 
130     void read(uint8_t *dst, int numBytes);
131     int getSize(void) const;
132     int getPosition(void) const;
133     void setPosition(int position);
134 
135 private:
136     FileResource(const FileResource &other);
137     FileResource &operator=(const FileResource &other);
138 
139     FILE *m_file;
140 };
141 
142 class ResourcePrefix : public Archive
143 {
144 public:
145     ResourcePrefix(const Archive &archive, const char *prefix);
~ResourcePrefix(void)146     virtual ~ResourcePrefix(void)
147     {
148     }
149 
150     virtual Resource *getResource(const char *name) const;
151 
152 private:
153     const Archive &m_archive;
154     std::string m_prefix;
155 };
156 
157 } // namespace tcu
158 
159 #endif // _TCURESOURCE_HPP
160