| 1 |
// Copyright (c) 2009, Whispersoft s.r.l. |
|---|
| 2 |
// All rights reserved. |
|---|
| 3 |
// |
|---|
| 4 |
// Redistribution and use in source and binary forms, with or without |
|---|
| 5 |
// modification, are permitted provided that the following conditions are |
|---|
| 6 |
// met: |
|---|
| 7 |
// |
|---|
| 8 |
// * Redistributions of source code must retain the above copyright |
|---|
| 9 |
// notice, this list of conditions and the following disclaimer. |
|---|
| 10 |
// * Redistributions in binary form must reproduce the above |
|---|
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
|---|
| 12 |
// in the documentation and/or other materials provided with the |
|---|
| 13 |
// distribution. |
|---|
| 14 |
// * Neither the name of Whispersoft s.r.l. nor the names of its |
|---|
| 15 |
// contributors may be used to endorse or promote products derived from |
|---|
| 16 |
// this software without specific prior written permission. |
|---|
| 17 |
// |
|---|
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 29 |
// |
|---|
| 30 |
// Author: Cosmin Tudorache |
|---|
| 31 |
|
|---|
| 32 |
#ifndef __NET_RPC_PARSER_RPC_PTYPES_H__ |
|---|
| 33 |
#define __NET_RPC_PARSER_RPC_PTYPES_H__ |
|---|
| 34 |
|
|---|
| 35 |
#include <string> |
|---|
| 36 |
#include <vector> |
|---|
| 37 |
#include <list> |
|---|
| 38 |
#include <iostream> |
|---|
| 39 |
|
|---|
| 40 |
#include <whisperlib/common/base/types.h> |
|---|
| 41 |
struct PFileInfo { |
|---|
| 42 |
string filename_; |
|---|
| 43 |
unsigned lineno_; |
|---|
| 44 |
}; |
|---|
| 45 |
|
|---|
| 46 |
// A type name = a tree of simple types. Every simple type is a string. |
|---|
| 47 |
// |
|---|
| 48 |
// e.g. "int" -> "int" |
|---|
| 49 |
// | | |
|---|
| 50 |
// NULL NULL |
|---|
| 51 |
// |
|---|
| 52 |
// "array<T>" -> "array" |
|---|
| 53 |
// | | |
|---|
| 54 |
// T NULL |
|---|
| 55 |
// |
|---|
| 56 |
// "map<K,V>" -> "map" |
|---|
| 57 |
// | | |
|---|
| 58 |
// K V |
|---|
| 59 |
// |
|---|
| 60 |
// "array<map<float, array<int>>>" -> "array" |
|---|
| 61 |
// | | |
|---|
| 62 |
// "map" NULL |
|---|
| 63 |
// | | |
|---|
| 64 |
// "float" "array" |
|---|
| 65 |
// | | | | |
|---|
| 66 |
// NULL NULL "int" NULL |
|---|
| 67 |
// | | |
|---|
| 68 |
// NULL NULL |
|---|
| 69 |
// |
|---|
| 70 |
struct PType { |
|---|
| 71 |
string name_; |
|---|
| 72 |
PType* parent_; // parent branh |
|---|
| 73 |
PType* subtype1_; // left branch |
|---|
| 74 |
PType* subtype2_; // right branch |
|---|
| 75 |
|
|---|
| 76 |
// if typename_ == "map" then [subtype1_, subtype2_] is the mapped pair |
|---|
| 77 |
// if typename_ == "array" then [subtype1_] is the element type. |
|---|
| 78 |
// (and subtype2_ should be NULL) |
|---|
| 79 |
// else both subtype1_ and subtype2_ should be NULL |
|---|
| 80 |
|
|---|
| 81 |
PType() |
|---|
| 82 |
: name_(), |
|---|
| 83 |
parent_(NULL), |
|---|
| 84 |
subtype1_(NULL), |
|---|
| 85 |
subtype2_(NULL) { |
|---|
| 86 |
} |
|---|
| 87 |
PType(const PType& t) |
|---|
| 88 |
: name_(), |
|---|
| 89 |
parent_(NULL), |
|---|
| 90 |
subtype1_(NULL), |
|---|
| 91 |
subtype2_(NULL) { |
|---|
| 92 |
*this = t; |
|---|
| 93 |
} |
|---|
| 94 |
virtual ~PType() { |
|---|
| 95 |
Clear(); |
|---|
| 96 |
} |
|---|
| 97 |
PType& operator=(const PType& t) { |
|---|
| 98 |
name_ = t.name_; |
|---|
| 99 |
if ( t.subtype1_ ) { |
|---|
| 100 |
delete subtype1_; |
|---|
| 101 |
subtype1_ = new PType(); |
|---|
| 102 |
subtype1_->parent_ = this; |
|---|
| 103 |
*subtype1_ = *(t.subtype1_); |
|---|
| 104 |
} |
|---|
| 105 |
if ( t.subtype2_ ) { |
|---|
| 106 |
delete subtype2_; |
|---|
| 107 |
subtype2_ = new PType(); |
|---|
| 108 |
subtype2_->parent_ = this; |
|---|
| 109 |
*subtype2_ = *(t.subtype2_); |
|---|
| 110 |
} |
|---|
| 111 |
return *this; |
|---|
| 112 |
} |
|---|
| 113 |
string ToString() const; |
|---|
| 114 |
void Clear() { |
|---|
| 115 |
name_.clear(); |
|---|
| 116 |
parent_ = NULL; |
|---|
| 117 |
delete subtype1_; |
|---|
| 118 |
subtype1_ = NULL; |
|---|
| 119 |
delete subtype2_; |
|---|
| 120 |
subtype2_ = NULL; |
|---|
| 121 |
} |
|---|
| 122 |
}; |
|---|
| 123 |
|
|---|
| 124 |
ostream& operator<<(ostream& out, const PType& p); |
|---|
| 125 |
typedef vector<PType> PTypeArray; |
|---|
| 126 |
|
|---|
| 127 |
// A custom type definition. |
|---|
| 128 |
struct PCustomType : public PFileInfo { |
|---|
| 129 |
struct Attribute : public PFileInfo { |
|---|
| 130 |
PType type_; // attribute's type name |
|---|
| 131 |
string name_; // attribute's name |
|---|
| 132 |
bool isOptional_; // true = OPTIONAL, false = REQUIRED |
|---|
| 133 |
|
|---|
| 134 |
Attribute() |
|---|
| 135 |
: type_(), |
|---|
| 136 |
name_(), |
|---|
| 137 |
isOptional_(false) { |
|---|
| 138 |
} |
|---|
| 139 |
void Clear() { |
|---|
| 140 |
type_.Clear(); |
|---|
| 141 |
name_.clear(); |
|---|
| 142 |
isOptional_ = false; |
|---|
| 143 |
} |
|---|
| 144 |
}; |
|---|
| 145 |
typedef vector<Attribute> AttributeArray; |
|---|
| 146 |
|
|---|
| 147 |
string name_; // the name of this custom type |
|---|
| 148 |
AttributeArray attributes_; // attributes of this custom type |
|---|
| 149 |
|
|---|
| 150 |
PCustomType() |
|---|
| 151 |
: name_() { |
|---|
| 152 |
} |
|---|
| 153 |
void Clear() { |
|---|
| 154 |
name_.clear(); |
|---|
| 155 |
attributes_.clear(); |
|---|
| 156 |
} |
|---|
| 157 |
}; |
|---|
| 158 |
typedef PCustomType::Attribute PAttribute; |
|---|
| 159 |
typedef PCustomType::AttributeArray PAttributeArray; |
|---|
| 160 |
typedef list<PCustomType> PCustomTypeArray; |
|---|
| 161 |
|
|---|
| 162 |
// a function parameter |
|---|
| 163 |
struct PParam { |
|---|
| 164 |
PType type_; |
|---|
| 165 |
string name_; |
|---|
| 166 |
|
|---|
| 167 |
PParam() |
|---|
| 168 |
: type_(), |
|---|
| 169 |
name_() { |
|---|
| 170 |
} |
|---|
| 171 |
void Clear() { |
|---|
| 172 |
type_.Clear(); |
|---|
| 173 |
name_.clear(); |
|---|
| 174 |
} |
|---|
| 175 |
}; |
|---|
| 176 |
typedef vector<PParam> PParamArray; |
|---|
| 177 |
|
|---|
| 178 |
// a function definition |
|---|
| 179 |
struct PFunction : public PFileInfo { |
|---|
| 180 |
string name_; // function name |
|---|
| 181 |
PParamArray input_; // input parameters types and names |
|---|
| 182 |
PType output_; // output type name |
|---|
| 183 |
|
|---|
| 184 |
void Clear() { |
|---|
| 185 |
name_.clear(); |
|---|
| 186 |
input_.clear(); |
|---|
| 187 |
output_.Clear(); |
|---|
| 188 |
} |
|---|
| 189 |
}; |
|---|
| 190 |
typedef vector<PFunction> PFunctionArray; |
|---|
| 191 |
|
|---|
| 192 |
// a service definition |
|---|
| 193 |
struct PService : public PFileInfo { |
|---|
| 194 |
string name_; // service name |
|---|
| 195 |
PFunctionArray functions_; // service functions |
|---|
| 196 |
|
|---|
| 197 |
void Clear() { |
|---|
| 198 |
name_.clear(); |
|---|
| 199 |
functions_.clear(); |
|---|
| 200 |
} |
|---|
| 201 |
}; |
|---|
| 202 |
typedef list<PService> PServiceArray; |
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
// some verbatim text |
|---|
| 206 |
struct PVerbatim : public PFileInfo { |
|---|
| 207 |
string language_; |
|---|
| 208 |
string verbatim_; |
|---|
| 209 |
void Clear() { |
|---|
| 210 |
language_.clear(); |
|---|
| 211 |
verbatim_.clear(); |
|---|
| 212 |
} |
|---|
| 213 |
}; |
|---|
| 214 |
typedef list<PVerbatim> PVerbatimArray; |
|---|
| 215 |
|
|---|
| 216 |
#endif // __NET_RPC_PARSER_RPC_PTYPES_H__ |
|---|