/usr/include/gpsim/ValueCollections.h is in gpsim-dev 0.27.0-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | /*
Copyright (C) 1998-2004 Scott Dattalo
This file is part of the libgpsim library of gpsim
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see
<http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
/*
This file originated by J.R. Heisey
*/
#ifndef __VALUECOLLECTIONS_H__
#define __VALUECOLLECTIONS_H__
#include <glib.h>
#include <cstdio>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include "value.h"
#include "expr.h"
#include "operator.h"
#include "errors.h"
/*
The IIndexedCollection class is an abstract class used to
expose an array of values of a given name to the command
prompt interface. These arrays are not dynamically resizable.
The scripting language does not provide a way of allocating
an array at gpsim runtime. These arrays are only definable
at gpsim compile time.
Values based on the class Value type are implemented via the
template class IndexedCollection. The first class Value based
array is implemented by CIndexedIntegerCollection.
The CIndexedIntegerCollection class provides its own storage
for the Integer values and is best used for Integer array based
attributes.
To derive your own arrays you will primarily need to derive from
IIndexedCollection and define implementations for all the pure
virutal functions.
I (JR) have provided an example of an array implementation as an example.
The class RegisterCollection is declared in register.h and is
an example of how to expose data values that are not based on
the Value class. The RegisterCollection exposes a gpsim command
prompt array variable called ramData that exposes the data value
in simulated RAM memory. When you type 'ramData' from the command
prompt, all memory contents will be displayed. To display one data
value type 'ramData[expr]' were expr may be an integer value or
and expression that evaluates to an integer value. (i.e. To see
the contents of the RAM where X contains the value of the RAM
address type 'ramData[X] at the command prompt.)
You may also type 'ramData[expr_list]' where expr_list are one or
more expressions delimited by commas.
all of the
ConsolidateValues()
*/
class IIndexedCollection : public Value {
public:
IIndexedCollection(const char *pName, const char *pDesc, int iAddressRadix = 10);
IIndexedCollection(int iAddressRadix = 10);
virtual unsigned int GetSize() = 0;
virtual Value &GetAt(unsigned int uIndex, Value *pValue=0) = 0;
virtual void SetAt(unsigned int uIndex, Value *pValue) = 0;
virtual void Set(Value *pValue);
virtual void SetAt(ExprList_t* pIndexers, Expression *pExpr);
virtual unsigned int GetLowerBound() = 0;
virtual unsigned int GetUpperBound() = 0;
virtual bool bIsIndexInRange(unsigned int uIndex) {
return uIndex >= GetLowerBound() && uIndex <= GetUpperBound();
}
void SetAddressRadix(int iRadix);
virtual string toString(ExprList_t* pIndexerExprs);
virtual char * toString(char *pBuffer, int len);
virtual string toString();
inline Value & operator[](unsigned int uIndex) {
return GetAt(uIndex);
}
protected:
virtual void ConsolidateValues(int &iColumnWidth,
vector<string> &aList,
vector<string> &aValue) = 0;
template<class _ValueType>
void ConsolidateValues(int &iColumnWidth,
vector<string> &aList,
vector<string> &aValue,
_ValueType *_V = 0) {
unsigned int uFirstIndex = GetLowerBound();
unsigned int uIndex;
unsigned int uUpper = GetUpperBound() + 1;
_ValueType LastValue((_ValueType&)GetAt(uFirstIndex));
for(uIndex = uFirstIndex + 1; uIndex < uUpper; uIndex++) {
_ValueType &curValue = (_ValueType&)GetAt(uIndex);
if(LastValue != curValue) {
PushValue(uFirstIndex, uIndex - 1,
&LastValue, aList, aValue);
iColumnWidth = max(iColumnWidth, (int)aList.back().size());
uFirstIndex = uIndex;
LastValue = curValue;
}
}
uIndex--;
// Record the last set of elements
if(uFirstIndex <= uIndex) {
PushValue(uFirstIndex, uIndex,
&LastValue, aList, aValue);
iColumnWidth = max(iColumnWidth, (int)aList.back().size());
}
}
void PushValue(int iFirstIndex, int iCurrentIndex,
Value *pValue,
vector<string> &asIndexes, vector<string> &asValue);
virtual string toString(int iColumnWidth, vector<string> &asIndexes,
vector<string> &asValue);
// virtual string toString(ExprList_t* pIndexers, Expression *pExpr);
virtual string ElementIndexedName(unsigned int iIndex);
Integer * FindInteger(const char *s);
protected:
char m_szPrefix[3];
int m_iAddressRadix;
};
template<class _CT, class _ST>
class IndexedCollection : public IIndexedCollection {
protected:
typedef vector<_CT*> VectorType;
typedef _CT* _ElementType;
public:
explicit IndexedCollection(
const char * pName = NULL, const char *pDesc = 0) {
m_uLower = 0;
if(pName == NULL)
pName = "unnamed";
new_name(pName);
set_description(pDesc);
}
explicit IndexedCollection(unsigned int uSize, _ST stDefValue,
const char * pName = NULL, const char *pDesc = 0) {
m_uLower = 0;
if(pName == NULL)
pName = "unnamed";
Value::new_name(pName);
set_description(pDesc);
m_Array.reserve(uSize);
string sName;
char szIndex[12];
for(unsigned int uIndex = 0; uIndex < uSize; uIndex++) {
sName = pName;
sprintf(szIndex, "[%d]", uIndex + m_uLower);
sName.append(szIndex);
// Hmm... Do we really want to create new object for every array entry?
m_Array.push_back(new _CT(sName.c_str(), stDefValue, pDesc));
}
}
virtual unsigned int GetSize() {
return (unsigned int)m_Array.size();
}
// void SetSize(unsigned int) {
// }
virtual Value &GetAt(unsigned int uIndex, Value * /*placeholder*/) {
return GetAt(uIndex);
}
_CT &GetAt(unsigned int uIndex) {
if(uIndex <= GetUpperBound() && uIndex >= m_uLower) {
return static_cast<_CT&>(*(m_Array.at(uIndex - m_uLower)));
}
else {
throw Error(string("Error: index out of range"));
}
}
virtual void SetAt(unsigned int uIndex, Value *pValue) {
_CT * pCTValue = dynamic_cast<_CT*>(pValue);
if(pCTValue != NULL) {
SetAt(uIndex, pCTValue);
}
else {
}
}
void SetAt(unsigned int uIndex, _CT *pValue)
{
if((uIndex + 1 - m_uLower) < m_Array.size() && uIndex >= m_uLower) {
_ST stValue;
pValue->get(stValue);
_CT * pElement = static_cast<_CT*>(m_Array.at(uIndex - m_uLower));
if (pElement)
pElement->set(stValue);
} else {
char szIndex[10];
sprintf(szIndex, "%d", uIndex);
string sMsg("invalid array index of ");
sMsg.append(szIndex);
throw Error(sMsg);
}
}
inline _CT & operator[](unsigned int uIndex) {
return GetAt(uIndex);
}
_CT & operator[](Expression *pIndexExpr) {
Value *pIndex = pIndexExpr->evaluate();
String *pStr;
if((pStr = dynamic_cast<String*>(pIndex)) != NULL) {
// pIndex = get_symbol_table().findInteger(pStr->getVal());
pIndex = FindInteger(pStr->getVal());
}
if(dynamic_cast<Integer*>(pIndex) != NULL) {
unsigned int uIndex = (unsigned int)*pIndex;
return GetAt(uIndex);
}
// else if(dynamic_cast<String*>(pIndex) != NULL) {
// Future: Implement a indexed collection that accepts
// strings where the string is used for a hashed based
// collection.
// }
else {
string sMsg;
sMsg = "Indexer expression does not evaluate to an Integer for " + name();
throw sMsg.c_str();
}
}
virtual void ConsolidateValues(int &iColumnWidth,
vector<string> &aList,
vector<string> &aValue) {
typename VectorType::iterator it;
typename VectorType::iterator itLastEqualed;
typename VectorType::iterator itEnd = m_Array.end();
unsigned int iCurrentIndex = m_uLower, iFirstIndex = m_uLower;
itLastEqualed = itEnd;
// The purpose of the two loops it to collapse consecutive
// elements of equal value onto one display line.
// This loop examines every element's value and records
// the value in aValue. aList is used to record an
// appropriate label for one or more elements.
for(it = itLastEqualed = m_Array.begin(); it != itEnd; ++it) {
ostringstream sIndex;
if(*(_CT*)(*itLastEqualed) != *(_CT*)(*it)) {
PushValue(iFirstIndex, iCurrentIndex - 1,
*itLastEqualed, aList, aValue);
iFirstIndex = iCurrentIndex;
iColumnWidth = max(iColumnWidth, (int)aList.back().size());
itLastEqualed = it;
}
iCurrentIndex++;
}
iCurrentIndex--;
// Record the last set of elements
if(iFirstIndex <= iCurrentIndex) {
PushValue(iFirstIndex, iCurrentIndex,
*itLastEqualed, aList, aValue);
iColumnWidth = max(iColumnWidth, (int)aList.back().size());
}
}
unsigned int GetLowerBound() {
return m_uLower;
}
unsigned int GetUpperBound() {
return (unsigned int)m_Array.size();
}
/* --- This isn't used and I (Scott) don't understand the intent...
void SetLowerBound(unsigned int uIndex) {
m_uLower = uIndex;
// Now iterate through the elements and change
// and pre-formatted element names.
typename VectorType::iterator it;
typename VectorType::iterator itEnd = m_Array.end();
unsigned int iNameBufferLen = 256;
char *pNameBuffer = (char*)malloc(iNameBufferLen);
unsigned int uLoopIndex = m_uLower;
for(it = m_Array.begin(); it != itEnd; it++) {
if(iNameBufferLen < (*it)->name().size() + 2) {
pNameBuffer = (char*)realloc(pNameBuffer, iNameBufferLen += 100);
}
sprintf(pNameBuffer, "%s[%d]", name().c_str(), uLoopIndex);
(*it)->new_name(pNameBuffer);
uLoopIndex++;
}
free(pNameBuffer);
}
*/
protected:
void push_back(_CT *p) {
m_Array.push_back(p);
}
unsigned int m_uLower;
private:
VectorType m_Array;
};
class CIndexedIntegerCollection : public IndexedCollection<Integer, gint64> {
public:
CIndexedIntegerCollection(unsigned int uSize, gint64 stDefValue,
const char * pName = NULL, const char *pDesc = 0)
: IndexedCollection<Integer, gint64>(uSize, stDefValue,
pName, pDesc ) {
}
};
#endif // __VALUECOLLECTIONS_H__
|