1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #ifndef __VAR__H__
17 #define __VAR__H__
18 
19 #include "VarType.h"
20 #include <string>
21 #include <stdio.h>
22 
23 class Var {
24 public:
25     // pointer data direction - from the client point of view.
26     typedef enum { POINTER_OUT = 0x1, POINTER_IN = 0x2, POINTER_INOUT = 0x3 } PointerDir;
27     Var() = default;
28 
Var(const std::string & name,const VarType * vartype,const std::string & lenExpression,PointerDir dir,const std::string & packExpression,const std::string & unpackExpression,const std::string & writeExpression)29     Var(const std::string & name,
30         const VarType * vartype,
31         const std::string & lenExpression,
32         PointerDir dir,
33         const std::string &packExpression,
34         const std::string &unpackExpression,
35         const std::string &writeExpression) :
36         m_name(name),
37         m_type(const_cast<VarType *>(vartype)),
38         m_lenExpression(lenExpression),
39         m_pointerDir(dir),
40         m_packExpression(packExpression),
41         m_unpackExpression(unpackExpression),
42         m_host_packTmpAllocExpression(""),
43         m_host_packExpression(""),
44         m_guest_unpackExpression(""),
45         m_writeExpression(writeExpression)
46     {
47     }
48 
init(const std::string name,const VarType * vartype,std::string lenExpression,PointerDir dir,std::string packExpression,std::string unpackExpression,std::string writeExpression)49     void init(const std::string name, const VarType * vartype,
50               std::string lenExpression,
51               PointerDir dir,
52               std::string packExpression,
53               std::string unpackExpression,
54               std::string writeExpression) {
55         m_name = name;
56         m_type = vartype;
57         m_lenExpression = lenExpression;
58         m_packExpression = packExpression;
59         m_unpackExpression = unpackExpression;
60         m_host_packTmpAllocExpression = "";
61         m_host_packExpression = "";
62         m_guest_unpackExpression = "";
63         m_writeExpression = writeExpression;
64         m_pointerDir = dir;
65         m_nullAllowed = false;
66         m_isLarge = false;
67 
68     }
69 
name()70     const std::string & name() const { return m_name; }
type()71     const VarType * type() const { return m_type; }
isPointer()72     bool isPointer() const { return m_type->isPointer(); }
isVoid()73     bool isVoid() const { return ((m_type->bytes() == 0) && (!m_type->isPointer())); }
lenExpression()74     const std::string & lenExpression() const { return m_lenExpression; }
packExpression()75     const std::string & packExpression() const { return(m_packExpression); }
unpackExpression()76     const std::string & unpackExpression() const { return(m_unpackExpression); }
hostPackTmpAllocExpression()77     const std::string & hostPackTmpAllocExpression() const { return(m_host_packTmpAllocExpression); }
hostPackExpression()78     const std::string & hostPackExpression() const { return(m_host_packExpression); }
guestUnpackExpression()79     const std::string & guestUnpackExpression() const { return(m_guest_unpackExpression); }
writeExpression()80     const std::string & writeExpression() const { return(m_writeExpression); }
paramCheckExpression()81     const std::string & paramCheckExpression() const { return m_paramCheckExpression; }
setLenExpression(const std::string & lenExpression)82     void setLenExpression(const std::string & lenExpression) { m_lenExpression = lenExpression; }
setPackExpression(const std::string & packExpression)83     void setPackExpression(const std::string & packExpression) { m_packExpression = packExpression; }
setUnpackExpression(const std::string & unpackExpression)84     void setUnpackExpression(const std::string & unpackExpression) { m_unpackExpression = unpackExpression; }
setHostPackTmpAllocExpression(const std::string & expr)85     void setHostPackTmpAllocExpression(const std::string & expr) { m_host_packTmpAllocExpression = expr; }
setHostPackExpression(const std::string & expr)86     void setHostPackExpression(const std::string & expr) { m_host_packExpression = expr; }
setGuestUnpackExpression(const std::string & expr)87     void setGuestUnpackExpression(const std::string & expr) { m_guest_unpackExpression = expr; }
setWriteExpression(const std::string & writeExpression)88     void setWriteExpression(const std::string & writeExpression) { m_writeExpression = writeExpression; }
setParamCheckExpression(const std::string & paramCheckExpression)89     void setParamCheckExpression(const std::string & paramCheckExpression) { m_paramCheckExpression = paramCheckExpression; }
setPointerDir(PointerDir dir)90     void setPointerDir(PointerDir dir) { m_pointerDir = dir; }
pointerDir()91     PointerDir pointerDir() { return m_pointerDir; }
setNullAllowed(bool state)92     void setNullAllowed(bool state) { m_nullAllowed = state; }
setIsLarge(bool state)93     void setIsLarge(bool state) { m_isLarge = state; }
setDMA(bool state)94     void setDMA(bool state) { m_isDMA = state; }
nullAllowed()95     bool nullAllowed() const { return m_nullAllowed; }
isLarge()96     bool isLarge() const { return m_isLarge; }
isDMA()97     bool isDMA() const { return m_isDMA; }
printType(FILE * fp)98     void printType(FILE *fp) { fprintf(fp, "%s", m_type->name().c_str()); }
printTypeName(FILE * fp)99     void printTypeName(FILE *fp) { printType(fp); fprintf(fp, " %s", m_name.c_str()); }
100 
101 private:
102     std::string m_name;
103     const VarType * m_type = nullptr;
104     std::string m_lenExpression; // an expression to calcualte a pointer data size
105     PointerDir m_pointerDir = POINTER_IN;
106     bool m_nullAllowed = false;
107     bool m_isLarge = false;
108     bool m_isDMA = false;
109     std::string m_packExpression; // an expression to pack data into the stream
110     std::string m_unpackExpression; // an expression to unpack data that has arrived from the stream
111     std::string m_host_packTmpAllocExpression; // an expression to create temporaries for getting into packed form for readbacks
112     std::string m_host_packExpression; // an expression to pack data into the stream but for readbacks
113     std::string m_guest_unpackExpression; // an expression to unpack readbacks on the guest
114     std::string m_writeExpression; // an expression to write data into the stream
115     std::string m_paramCheckExpression; //an expression to check parameter value
116 };
117 
118 #endif
119