Various cleanups, mostly style and whitespace.

This commit is contained in:
JoelKatz
2012-04-17 02:34:24 -07:00
parent cc4b350eb7
commit 1ea7b56fa8
7 changed files with 523 additions and 528 deletions

View File

@@ -1,5 +1,4 @@
#include <iostream>
#include <cstdlib>
@@ -19,9 +18,7 @@
#include "Config.h"
#include "BitcoinUtil.h"
using namespace boost::asio;
inline bool isSwitchChar(char c)
static inline bool isSwitchChar(char c)
{
#ifdef __WXMSW__
return c == '-' || c == '/';
@@ -56,13 +53,13 @@ int commandLineRPC(int argc, char *argv[])
try
{
// Skip switches
while(argc > 1 && isSwitchChar(argv[1][0]))
while ((argc > 1) && isSwitchChar(argv[1][0]))
{
argc--;
argv++;
}
if(argc < 2) return(0);
if (argc < 2) return 0;
std::string strMethod = argv[1];
@@ -75,22 +72,20 @@ int commandLineRPC(int argc, char *argv[])
Json::Value reply = callRPC(strMethod, params);
// Parse reply
Json::Value result=reply.get("result", Json::Value());
Json::Value error=reply.get("error", Json::Value());
Json::Value result = reply.get("result", Json::Value());
Json::Value error = reply.get("error", Json::Value());
if(result.isString() && result.asString()=="unknown command")
if (result.isString() && (result.asString() == "unknown command"))
nRet=1;
if(!error.isNull())
{
// Error
if (!error.isNull())
{ // Error
strPrint = "error: " + error.toStyledString();
int code = error["code"].asInt();
nRet = abs(code);
}
else
{
// Result
{ // Result
if (result.isNull())
strPrint = "";
else if (result.isString())
@@ -109,7 +104,7 @@ int commandLineRPC(int argc, char *argv[])
std::cout << "Exception CommandLineRPC()" << std::endl;
}
if(strPrint != "")
if (strPrint != "")
{
std::cout << strPrint << std::endl;
}
@@ -119,27 +114,24 @@ int commandLineRPC(int argc, char *argv[])
Json::Value callRPC(const std::string& strMethod, const Json::Value& params)
{
if(theConfig.RPC_USER == "" && theConfig.RPC_PASSWORD == "")
if (theConfig.RPC_USER.empty() && theConfig.RPC_PASSWORD.empty())
throw std::runtime_error("You must set rpcpassword=<password> in the configuration file"
"If the file does not exist, create it with owner-readable-only file permissions.");
// Connect to localhost
std::cout << "Connecting to port:" << theConfig.RPC_PORT << std::endl;
ip::tcp::endpoint endpoint(ip::address::from_string(theConfig.RPC_IP), theConfig.RPC_PORT);
ip::tcp::iostream stream;
boost::asio::ip::tcp::endpoint
endpoint(boost::asio::ip::address::from_string(theConfig.RPC_IP), theConfig.RPC_PORT);
boost::asio::ip::tcp::iostream stream;
stream.connect(endpoint);
if(stream.fail())
if (stream.fail())
throw std::runtime_error("couldn't connect to server");
// HTTP basic authentication
std::string strUserPass64 = EncodeBase64(theConfig.RPC_USER + ":" + theConfig.RPC_PASSWORD);
std::map<std::string, std::string> mapRequestHeaders;
mapRequestHeaders["Authorization"] = std::string("Basic ") + strUserPass64;
// Send request
std::string strRequest = JSONRPCRequest(strMethod, params, Json::Value(1));
std::cout << "send request " << strMethod << " : " << strRequest << std::endl;
@@ -154,7 +146,7 @@ Json::Value callRPC(const std::string& strMethod, const Json::Value& params)
int nStatus = ReadHTTP(stream, mapHeaders, strReply);
if (nStatus == 401)
throw std::runtime_error("incorrect rpcuser or rpcpassword (authorization failed)");
else if (nStatus >= 400 && nStatus != 400 && nStatus != 404 && nStatus != 500)
else if ((nStatus >= 400) && (nStatus != 400) && (nStatus != 404) && (nStatus != 500)) // ?
throw std::runtime_error(strprintf("server returned HTTP error %d", nStatus));
else if (strReply.empty())
throw std::runtime_error("no response from server");
@@ -170,3 +162,4 @@ Json::Value callRPC(const std::string& strMethod, const Json::Value& params)
return valReply;
}
// vim:ts=4

View File

@@ -19,12 +19,12 @@ Ledger::Ledger(const NewcoinAddress& masterID, uint64 startAmount) :
mFeeHeld(0), mTimeStamp(0), mLedgerSeq(0),
mClosed(false), mValidHash(false), mAccepted(false), mImmutable(false)
{
mTransactionMap=boost::make_shared<SHAMap>();
mAccountStateMap=boost::make_shared<SHAMap>();
mTransactionMap = boost::make_shared<SHAMap>();
mAccountStateMap = boost::make_shared<SHAMap>();
AccountState::pointer startAccount=boost::make_shared<AccountState>(masterID);
AccountState::pointer startAccount = boost::make_shared<AccountState>(masterID);
startAccount->credit(startAmount);
if(!addAccountState(startAccount))
if (!addAccountState(startAccount))
assert(false);
}
@@ -41,8 +41,8 @@ Ledger::Ledger(Ledger &prevLedger, uint64 ts) : mTimeStamp(ts),
mClosed(false), mValidHash(false), mAccepted(false), mImmutable(false),
mTransactionMap(new SHAMap()), mAccountStateMap(prevLedger.mAccountStateMap)
{
mParentHash=prevLedger.getHash();
mLedgerSeq=prevLedger.mLedgerSeq+1;
mParentHash = prevLedger.getHash();
mLedgerSeq = prevLedger.mLedgerSeq+1;
mAccountStateMap->setSeq(mLedgerSeq);
}
@@ -51,17 +51,17 @@ Ledger::Ledger(const std::vector<unsigned char>& rawLedger) : mFeeHeld(0), mTime
{
Serializer s(rawLedger);
// 32seq, 64fee, 256phash, 256thash, 256ahash, 64ts
if(!s.get32(mLedgerSeq, BLgPIndex)) return;
if(!s.get64(mFeeHeld, BLgPFeeHeld)) return;
if(!s.get256(mParentHash, BLgPPrevLg)) return;
if(!s.get256(mTransHash, BLgPTxT)) return;
if(!s.get256(mAccountHash, BLgPAcT)) return;
if(!s.get64(mTimeStamp, BLgPClTs)) return;
if (!s.get32(mLedgerSeq, BLgPIndex)) return;
if (!s.get64(mFeeHeld, BLgPFeeHeld)) return;
if (!s.get256(mParentHash, BLgPPrevLg)) return;
if (!s.get256(mTransHash, BLgPTxT)) return;
if (!s.get256(mAccountHash, BLgPAcT)) return;
if (!s.get64(mTimeStamp, BLgPClTs)) return;
updateHash();
if(mValidHash)
{
mTransactionMap=boost::make_shared<SHAMap>();
mAccountStateMap=boost::make_shared<SHAMap>();
mTransactionMap = boost::make_shared<SHAMap>();
mAccountStateMap = boost::make_shared<SHAMap>();
}
}
@@ -70,17 +70,17 @@ Ledger::Ledger(const std::string& rawLedger) : mFeeHeld(0), mTimeStamp(0),
{
Serializer s(rawLedger);
// 32seq, 64fee, 256phash, 256thash, 256ahash, 64ts
if(!s.get32(mLedgerSeq, BLgPIndex)) return;
if(!s.get64(mFeeHeld, BLgPFeeHeld)) return;
if(!s.get256(mParentHash, BLgPPrevLg)) return;
if(!s.get256(mTransHash, BLgPTxT)) return;
if(!s.get256(mAccountHash, BLgPAcT)) return;
if(!s.get64(mTimeStamp, BLgPClTs)) return;
if (!s.get32(mLedgerSeq, BLgPIndex)) return;
if (!s.get64(mFeeHeld, BLgPFeeHeld)) return;
if (!s.get256(mParentHash, BLgPPrevLg)) return;
if (!s.get256(mTransHash, BLgPTxT)) return;
if (!s.get256(mAccountHash, BLgPAcT)) return;
if (!s.get64(mTimeStamp, BLgPClTs)) return;
updateHash();
if(mValidHash)
{
mTransactionMap=boost::make_shared<SHAMap>();
mAccountStateMap=boost::make_shared<SHAMap>();
mTransactionMap = boost::make_shared<SHAMap>();
mAccountStateMap = boost::make_shared<SHAMap>();
}
}
@@ -88,16 +88,16 @@ void Ledger::updateHash()
{
if(!mImmutable)
{
if(mTransactionMap) mTransHash=mTransactionMap->getHash();
if (mTransactionMap) mTransHash = mTransactionMap->getHash();
else mTransHash.zero();
if(mAccountStateMap) mAccountHash=mAccountStateMap->getHash();
if (mAccountStateMap) mAccountHash = mAccountStateMap->getHash();
else mAccountHash.zero();
}
Serializer s(116);
addRaw(s);
mHash=s.getSHA512Half();
mValidHash=true;
mHash =s.getSHA512Half();
mValidHash = true;
}
void Ledger::addRaw(Serializer &s)
@@ -116,8 +116,8 @@ AccountState::pointer Ledger::getAccountState(const NewcoinAddress& accountID)
std::cerr << "Ledger:getAccountState(" << accountID.humanAccountID() << ")" << std::endl;
#endif
ScopedLock l(mTransactionMap->Lock());
SHAMapItem::pointer item=mAccountStateMap->peekItem(accountID.getAccountID().to256());
if(!item)
SHAMapItem::pointer item = mAccountStateMap->peekItem(accountID.getAccountID().to256());
if (!item)
{
#ifdef DEBUG
std::cerr << " notfound" << std::endl;
@@ -130,23 +130,23 @@ AccountState::pointer Ledger::getAccountState(const NewcoinAddress& accountID)
uint64 Ledger::getBalance(const NewcoinAddress& accountID) const
{
ScopedLock l(mTransactionMap->Lock());
SHAMapItem::pointer item=mAccountStateMap->peekItem(accountID.getAccountID().to256());
if(!item) return 0;
SHAMapItem::pointer item = mAccountStateMap->peekItem(accountID.getAccountID().to256());
if (!item) return 0;
return AccountState(item->getData()).getBalance();
}
bool Ledger::updateAccountState(AccountState::pointer state)
{
assert(!mAccepted);
SHAMapItem::pointer item=boost::make_shared<SHAMapItem>(state->getAccountID().getAccountID(), state->getRaw());
return mAccountStateMap->updateGiveItem(item, false);
return mAccountStateMap->updateGiveItem(boost::make_shared<SHAMapItem>(state->getAccountID().getAccountID(),
state->getRaw()), false);
}
bool Ledger::addAccountState(AccountState::pointer state)
{
assert(!mAccepted);
assert( (state->getBalance()==0) || (state->getSeq()>0) );
SHAMapItem::pointer item=boost::make_shared<SHAMapItem>(state->getAccountID().getAccountID(), state->getRaw());
SHAMapItem::pointer item = boost::make_shared<SHAMapItem>(state->getAccountID().getAccountID(), state->getRaw());
return mAccountStateMap->addGiveItem(item, false);
}
@@ -156,7 +156,7 @@ bool Ledger::addTransaction(Transaction::pointer trans)
assert(!!trans->getID());
Serializer s;
trans->getSTransaction()->getTransaction(s, false);
SHAMapItem::pointer item=boost::make_shared<SHAMapItem>(trans->getID(), s.peekData());
SHAMapItem::pointer item = boost::make_shared<SHAMapItem>(trans->getID(), s.peekData());
return mTransactionMap->addGiveItem(item, true);
}
@@ -173,14 +173,15 @@ bool Ledger::hasTransaction(const uint256& transID) const
Transaction::pointer Ledger::getTransaction(const uint256& transID) const
{
SHAMapItem::pointer item=mTransactionMap->peekItem(transID);
if(!item) return Transaction::pointer();
SHAMapItem::pointer item = mTransactionMap->peekItem(transID);
if (!item) return Transaction::pointer();
Transaction::pointer txn=theApp->getMasterTransaction().fetch(transID, false);
if(txn) return txn;
Transaction::pointer txn = theApp->getMasterTransaction().fetch(transID, false);
if (txn) return txn;
txn=boost::make_shared<Transaction>(item->getData(), true);
if(txn->getStatus()==NEW) txn->setStatus(mClosed ? COMMITTED : INCLUDED, mLedgerSeq);
txn = boost::make_shared<Transaction>(item->getData(), true);
if (txn->getStatus() == NEW)
txn->setStatus(mClosed ? COMMITTED : INCLUDED, mLedgerSeq);
theApp->getMasterTransaction().canonicalize(txn, false);
return txn;
@@ -190,9 +191,9 @@ Ledger::TransResult Ledger::applyTransaction(Transaction::pointer trans)
{
assert(!mAccepted);
boost::recursive_mutex::scoped_lock sl(mLock);
if(trans->getSourceLedger()>mLedgerSeq) return TR_BADLSEQ;
if (trans->getSourceLedger() > mLedgerSeq) return TR_BADLSEQ;
if(trans->getAmount()<trans->getFee())
if (trans->getAmount()<trans->getFee())
{
#ifdef DEBUG
std::cerr << "Transaction for " << trans->getAmount() << ", but fee is " <<
@@ -519,38 +520,37 @@ Ledger::pointer Ledger::switchPreviousLedger(Ledger::pointer oldPrevious, Ledger
do
{
count=0;
std::map<uint256, std::pair<Transaction::pointer, Transaction::pointer> >::iterator it=TxnDiff.begin();
while(it!=TxnDiff.end())
std::map<uint256, std::pair<Transaction::pointer, Transaction::pointer> >::iterator it = TxnDiff.begin();
while (it != TxnDiff.end())
{
Transaction::pointer& tx=it->second.second;
if(!tx || newLedger->addTransaction(tx))
Transaction::pointer& tx = it->second.second;
if (!tx || newLedger->addTransaction(tx))
{
count++;
TxnDiff.erase(it++);
}
else ++it;
}
} while(count!=0);
} while (count!=0);
// WRITEME: Handle rejected transactions left in TxnDiff
// 5) Try to add transactions from this ledger to the new ledger.
std::map<uint256, Transaction::pointer> txnMap;
for(SHAMapItem::pointer mit=peekTransactionMap()->peekFirstItem();
!!mit;
mit=peekTransactionMap()->peekNextItem(mit->getTag()))
for(SHAMapItem::pointer mit = peekTransactionMap()->peekFirstItem();
!!mit; mit = peekTransactionMap()->peekNextItem(mit->getTag()))
{
uint256 txnID=mit->getTag();
Transaction::pointer tx=theApp->getMasterTransaction().fetch(txnID, false);
if(!tx) tx=boost::make_shared<Transaction>(mit->peekData(), false);
uint256 txnID = mit->getTag();
Transaction::pointer tx = theApp->getMasterTransaction().fetch(txnID, false);
if(!tx) tx = boost::make_shared<Transaction>(mit->peekData(), false);
txnMap.insert(std::make_pair(txnID, tx));
}
do
{
count=0;
std::map<uint256, Transaction::pointer>::iterator it=txnMap.begin();
while(it!=txnMap.end())
std::map<uint256, Transaction::pointer>::iterator it = txnMap.begin();
while (it != txnMap.end())
{
if(newLedger->addTransaction(it->second))
{

View File

@@ -77,16 +77,17 @@ SerializedType* STObject::makeDeserializedObject(SerializedTypeID id, const char
STObject::STObject(SOElement* elem, const char *name) : SerializedType(name), mFlagIdx(-1)
{
while(elem->e_id!=STI_DONE)
while (elem->e_id != STI_DONE)
{
if(elem->e_type==SOE_FLAGS) mFlagIdx=mType.size();
if (elem->e_type == SOE_FLAGS)
mFlagIdx = mType.size();
mType.push_back(elem);
if( (elem->e_type==SOE_IFFLAG) || (elem->e_type==SOE_IFNFLAG) )
if ( (elem->e_type == SOE_IFFLAG) || (elem->e_type == SOE_IFNFLAG) )
giveObject(new STObject(elem->e_name));
else
{
SerializedType* t=makeDefaultObject(elem->e_id, elem->e_name);
if(!t) throw(std::runtime_error("invalid transaction element"));
SerializedType* t = makeDefaultObject(elem->e_id, elem->e_name);
if (!t) throw std::runtime_error("invalid transaction element");
giveObject(t);
}
elem++;
@@ -95,32 +96,32 @@ STObject::STObject(SOElement* elem, const char *name) : SerializedType(name), mF
STObject::STObject(SOElement* elem, SerializerIterator& sit, const char *name) : SerializedType(name), mFlagIdx(-1)
{
int flags=-1;
while(elem->e_id!=STI_DONE)
int flags = -1;
while (elem->e_id != STI_DONE)
{
mType.push_back(elem);
bool done=false;
if(elem->e_type==SOE_IFFLAG)
bool done = false;
if (elem->e_type == SOE_IFFLAG)
{
assert(flags>=0);
if((flags&elem->e_flags)==0) done=true;
assert(flags >= 0);
if ((flags&elem->e_flags) == 0) done = true;
}
else if(elem->e_type==SOE_IFNFLAG)
else if (elem->e_type == SOE_IFNFLAG)
{
assert(flags>=0);
if((flags&elem->e_flags)!=0) done=true;
assert(flags >= 0);
if ((flags&elem->e_flags) != 0) done = true;
}
else if(elem->e_type==SOE_FLAGS)
else if (elem->e_type == SOE_FLAGS)
{
assert(elem->e_id==STI_UINT32);
flags=sit.get32();
mFlagIdx=giveObject(new STUInt32(elem->e_name, flags));
done=true;
assert(elem->e_id == STI_UINT32);
flags = sit.get32();
mFlagIdx = giveObject(new STUInt32(elem->e_name, flags));
done = true;
}
if(!done)
{
SerializedType* t=makeDeserializedObject(elem->e_id, elem->e_name, sit);
if(!t) throw(std::runtime_error("invalid transaction element"));
SerializedType* t = makeDeserializedObject(elem->e_id, elem->e_name, sit);
if (!t) throw std::runtime_error("invalid transaction element");
giveObject(t);
}
elem++;
@@ -130,59 +131,59 @@ STObject::STObject(SOElement* elem, SerializerIterator& sit, const char *name) :
std::string STObject::getFullText() const
{
std::string ret;
if(name!=NULL)
if (name != NULL)
{
ret=name;
ret+=" = {";
ret = name;
ret += " = {";
}
else ret="{";
for(boost::ptr_vector<SerializedType>::const_iterator it=mData.begin(), end=mData.end(); it!=end; ++it)
ret+=it->getFullText();
ret+="}";
else ret = "{";
for (boost::ptr_vector<SerializedType>::const_iterator it = mData.begin(), end = mData.end(); it != end; ++it)
ret += it->getFullText();
ret += "}";
return ret;
}
int STObject::getLength() const
{
int ret=0;
for(boost::ptr_vector<SerializedType>::const_iterator it=mData.begin(), end=mData.end(); it!=end; ++it)
ret+=it->getLength();
int ret = 0;
for (boost::ptr_vector<SerializedType>::const_iterator it = mData.begin(), end = mData.end(); it != end; ++it)
ret += it->getLength();
return ret;
}
void STObject::add(Serializer& s) const
{
for(boost::ptr_vector<SerializedType>::const_iterator it=mData.begin(), end=mData.end(); it!=end; ++it)
for (boost::ptr_vector<SerializedType>::const_iterator it = mData.begin(), end = mData.end(); it != end; ++it)
it->add(s);
}
std::string STObject::getText() const
{
std::string ret="{";
bool first=false;
for(boost::ptr_vector<SerializedType>::const_iterator it=mData.begin(), end=mData.end(); it!=end; ++it)
std::string ret = "{";
bool first = false;
for (boost::ptr_vector<SerializedType>::const_iterator it = mData.begin(), end = mData.end(); it != end; ++it)
{
if(!first)
if (!first)
{
ret+=", ";
first=false;
ret += ", ";
first = false;
}
ret+=it->getText();
ret += it->getText();
}
ret+="}";
ret += "}";
return ret;
}
bool STObject::isEquivalent(const SerializedType& t) const
{
const STObject* v=dynamic_cast<const STObject*>(&t);
if(!v) return false;
boost::ptr_vector<SerializedType>::const_iterator it1=mData.begin(), end1=mData.end();
boost::ptr_vector<SerializedType>::const_iterator it2=v->mData.begin(), end2=v->mData.end();
while((it1!=end1) && (it2!=end2))
const STObject* v = dynamic_cast<const STObject*>(&t);
if (!v) return false;
boost::ptr_vector<SerializedType>::const_iterator it1 = mData.begin(), end1 = mData.end();
boost::ptr_vector<SerializedType>::const_iterator it2 = v->mData.begin(), end2 = v->mData.end();
while ((it1 != end1) && (it2 != end2))
{
if(it1->getSType() != it2->getSType()) return false;
if(!it1->isEquivalent(*it2)) return false;
if (it1->getSType() != it2->getSType()) return false;
if (!it1->isEquivalent(*it2)) return false;
++it1;
++it2;
}
@@ -191,51 +192,51 @@ bool STObject::isEquivalent(const SerializedType& t) const
int STObject::getFieldIndex(SOE_Field field) const
{
int i=0;
for(std::vector<SOElement*>::const_iterator it=mType.begin(), end=mType.end(); it!=end; ++it, ++i)
if((*it)->e_field==field) return i;
int i = 0;
for (std::vector<SOElement*>::const_iterator it = mType.begin(), end = mType.end(); it != end; ++it, ++i)
if( (*it)->e_field == field) return i;
return -1;
}
const SerializedType& STObject::peekAtField(SOE_Field field) const
{
int index=getFieldIndex(field);
if(index==-1) throw std::runtime_error("Field not found");
int index = getFieldIndex(field);
if (index == -1) throw std::runtime_error("Field not found");
return peekAtIndex(index);
}
SerializedType& STObject::getField(SOE_Field field)
{
int index=getFieldIndex(field);
if(index==-1) throw std::runtime_error("Field not found");
int index = getFieldIndex(field);
if (index==-1) throw std::runtime_error("Field not found");
return getIndex(index);
}
const SerializedType* STObject::peekAtPField(SOE_Field field) const
{
int index=getFieldIndex(field);
if(index==-1) return NULL;
int index = getFieldIndex(field);
if (index == -1) return NULL;
return peekAtPIndex(index);
}
SerializedType* STObject::getPField(SOE_Field field)
{
int index=getFieldIndex(field);
if(index==-1) return NULL;
int index = getFieldIndex(field);
if (index == -1) return NULL;
return getPIndex(index);
}
bool STObject::isFieldPresent(SOE_Field field) const
{
int index=getFieldIndex(field);
if(index==-1) return false;
return peekAtIndex(field).getSType()==STI_OBJECT;
int index = getFieldIndex(field);
if (index == -1) return false;
return peekAtIndex(field).getSType() == STI_OBJECT;
}
bool STObject::setFlag(uint32 f)
{
if(mFlagIdx<0) return false;
STUInt32* t=dynamic_cast<STUInt32*>(getPIndex(mFlagIdx));
if (mFlagIdx<0) return false;
STUInt32* t = dynamic_cast<STUInt32*>(getPIndex(mFlagIdx));
assert(t);
t->setValue(t->getValue() | f);
return true;
@@ -243,8 +244,8 @@ bool STObject::setFlag(uint32 f)
bool STObject::clearFlag(uint32 f)
{
if(mFlagIdx<0) return false;
STUInt32* t=dynamic_cast<STUInt32*>(getPIndex(mFlagIdx));
if (mFlagIdx<0) return false;
STUInt32* t = dynamic_cast<STUInt32*>(getPIndex(mFlagIdx));
assert(t);
t->setValue(t->getValue() & ~f);
return true;
@@ -252,247 +253,248 @@ bool STObject::clearFlag(uint32 f)
uint32 STObject::getFlags(void) const
{
if(mFlagIdx<0) return 0;
const STUInt32* t=dynamic_cast<const STUInt32*>(peekAtPIndex(mFlagIdx));
if (mFlagIdx<0) return 0;
const STUInt32* t = dynamic_cast<const STUInt32*>(peekAtPIndex(mFlagIdx));
assert(t);
return t->getValue();
}
void STObject::makeFieldPresent(SOE_Field field)
{
int index=getFieldIndex(field);
if(index==-1) throw std::runtime_error("Field not found");
if(peekAtIndex(field).getSType()!=STI_OBJECT) return;
int index = getFieldIndex(field);
if (index == -1) throw std::runtime_error("Field not found");
if (peekAtIndex(field).getSType() != STI_OBJECT) return;
mData.replace(index, makeDefaultObject(mType[index]->e_id, mType[index]->e_name));
setFlag(mType[index]->e_flags);
}
void STObject::makeFieldAbsent(SOE_Field field)
{
int index=getFieldIndex(field);
if(index==-1) throw std::runtime_error("Field not found");
if(peekAtIndex(field).getSType()==STI_OBJECT) return;
int index = getFieldIndex(field);
if (index == -1) throw std::runtime_error("Field not found");
if (peekAtIndex(field).getSType() == STI_OBJECT) return;
mData.replace(index, new STObject(mType[index]->e_name));
clearFlag(mType[index]->e_flags);
}
std::string STObject::getFieldString(SOE_Field field) const
{
const SerializedType* rf=peekAtPField(field);
if(!rf) throw std::runtime_error("Field not found");
const SerializedType* rf = peekAtPField(field);
if (!rf) throw std::runtime_error("Field not found");
return rf->getText();
}
unsigned char STObject::getValueFieldU8(SOE_Field field) const
{
const SerializedType* rf=peekAtPField(field);
if(!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType();
if(id==STI_OBJECT) return 0; // optional field not present
const STUInt8 *cf=dynamic_cast<const STUInt8 *>(rf);
if(!cf) throw std::runtime_error("Wrong field type");
const SerializedType* rf = peekAtPField(field);
if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id = rf->getSType();
if (id == STI_OBJECT) return 0; // optional field not present
const STUInt8 *cf = dynamic_cast<const STUInt8 *>(rf);
if (!cf) throw std::runtime_error("Wrong field type");
return cf->getValue();
}
uint16 STObject::getValueFieldU16(SOE_Field field) const
{
const SerializedType* rf=peekAtPField(field);
if(!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType();
if(id==STI_OBJECT) return 0; // optional field not present
const STUInt16 *cf=dynamic_cast<const STUInt16 *>(rf);
if(!cf) throw std::runtime_error("Wrong field type");
const SerializedType* rf = peekAtPField(field);
if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id = rf->getSType();
if (id == STI_OBJECT) return 0; // optional field not present
const STUInt16 *cf = dynamic_cast<const STUInt16 *>(rf);
if (!cf) throw std::runtime_error("Wrong field type");
return cf->getValue();
}
uint32 STObject::getValueFieldU32(SOE_Field field) const
{
const SerializedType* rf=peekAtPField(field);
if(!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType();
if(id==STI_OBJECT) return 0; // optional field not present
const STUInt32 *cf=dynamic_cast<const STUInt32 *>(rf);
if(!cf) throw std::runtime_error("Wrong field type");
const SerializedType* rf = peekAtPField(field);
if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id = rf->getSType();
if (id == STI_OBJECT) return 0; // optional field not present
const STUInt32 *cf = dynamic_cast<const STUInt32 *>(rf);
if (!cf) throw std::runtime_error("Wrong field type");
return cf->getValue();
}
uint64 STObject::getValueFieldU64(SOE_Field field) const
{
const SerializedType* rf=peekAtPField(field);
if(!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType();
if(id==STI_OBJECT) return 0; // optional field not present
const STUInt64 *cf=dynamic_cast<const STUInt64 *>(rf);
if(!cf) throw std::runtime_error("Wrong field type");
const SerializedType* rf = peekAtPField(field);
if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id = rf->getSType();
if (id == STI_OBJECT) return 0; // optional field not present
const STUInt64 *cf = dynamic_cast<const STUInt64 *>(rf);
if (!cf) throw std::runtime_error("Wrong field type");
return cf->getValue();
}
uint160 STObject::getValueFieldH160(SOE_Field field) const
{
const SerializedType* rf=peekAtPField(field);
if(!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType();
if(id==STI_OBJECT) return uint160(); // optional field not present
const STHash160 *cf=dynamic_cast<const STHash160 *>(rf);
if(!cf) throw std::runtime_error("Wrong field type");
const SerializedType* rf = peekAtPField(field);
if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id = rf->getSType();
if (id == STI_OBJECT) return uint160(); // optional field not present
const STHash160 *cf = dynamic_cast<const STHash160 *>(rf);
if (!cf) throw std::runtime_error("Wrong field type");
return cf->getValue();
}
uint256 STObject::getValueFieldH256(SOE_Field field) const
{
const SerializedType* rf=peekAtPField(field);
if(!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType();
if(id==STI_OBJECT) return uint256(); // optional field not present
const STHash256 *cf=dynamic_cast<const STHash256 *>(rf);
if(!cf) throw std::runtime_error("Wrong field type");
const SerializedType* rf = peekAtPField(field);
if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id = rf->getSType();
if (id == STI_OBJECT) return uint256(); // optional field not present
const STHash256 *cf = dynamic_cast<const STHash256 *>(rf);
if (!cf) throw std::runtime_error("Wrong field type");
return cf->getValue();
}
std::vector<unsigned char> STObject::getValueFieldVL(SOE_Field field) const
{
const SerializedType* rf=peekAtPField(field);
if(!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType();
if(id==STI_OBJECT) return std::vector<unsigned char>(); // optional field not present
const STVariableLength *cf=dynamic_cast<const STVariableLength *>(rf);
if(!cf) throw std::runtime_error("Wrong field type");
const SerializedType* rf = peekAtPField(field);
if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id = rf->getSType();
if (id == STI_OBJECT) return std::vector<unsigned char>(); // optional field not present
const STVariableLength *cf = dynamic_cast<const STVariableLength *>(rf);
if (!cf) throw std::runtime_error("Wrong field type");
return cf->getValue();
}
std::vector<TaggedListItem> STObject::getValueFieldTL(SOE_Field field) const
{
const SerializedType* rf=peekAtPField(field);
if(!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType();
if(id==STI_OBJECT) return std::vector<TaggedListItem>(); // optional field not present
const STTaggedList *cf=dynamic_cast<const STTaggedList *>(rf);
if(!cf) throw std::runtime_error("Wrong field type");
const SerializedType* rf = peekAtPField(field);
if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id = rf->getSType();
if (id == STI_OBJECT) return std::vector<TaggedListItem>(); // optional field not present
const STTaggedList *cf = dynamic_cast<const STTaggedList *>(rf);
if (!cf) throw std::runtime_error("Wrong field type");
return cf->getValue();
}
void STObject::setValueFieldU8(SOE_Field field, unsigned char v)
{
SerializedType* rf=getPField(field);
if(!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType();
if(id==STI_OBJECT)
SerializedType* rf = getPField(field);
if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id = rf->getSType();
if (id == STI_OBJECT)
{
makeFieldPresent(field);
rf=getPField(field);
id=rf->getSType();
rf = getPField(field);
id = rf->getSType();
}
STUInt8* cf=dynamic_cast<STUInt8*>(rf);
if(!cf) throw(std::runtime_error("Wrong field type"));
STUInt8* cf = dynamic_cast<STUInt8*>(rf);
if (!cf) throw std::runtime_error("Wrong field type");
cf->setValue(v);
}
void STObject::setValueFieldU16(SOE_Field field, uint16 v)
{
SerializedType* rf=getPField(field);
if(!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType();
if(id==STI_OBJECT)
SerializedType* rf = getPField(field);
if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id = rf->getSType();
if (id == STI_OBJECT)
{
makeFieldPresent(field);
rf=getPField(field);
id=rf->getSType();
rf = getPField(field);
id = rf->getSType();
}
STUInt16* cf=dynamic_cast<STUInt16*>(rf);
if(!cf) throw(std::runtime_error("Wrong field type"));
STUInt16* cf = dynamic_cast<STUInt16*>(rf);
if (!cf) throw std::runtime_error("Wrong field type");
cf->setValue(v);
}
void STObject::setValueFieldU32(SOE_Field field, uint32 v)
{
SerializedType* rf=getPField(field);
if(!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType();
if(id==STI_OBJECT)
SerializedType* rf = getPField(field);
if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id = rf->getSType();
if (id == STI_OBJECT)
{
makeFieldPresent(field);
rf=getPField(field);
id=rf->getSType();
rf = getPField(field);
id = rf->getSType();
}
STUInt32* cf=dynamic_cast<STUInt32*>(rf);
if(!cf) throw(std::runtime_error("Wrong field type"));
STUInt32* cf = dynamic_cast<STUInt32*>(rf);
if (!cf) throw std::runtime_error("Wrong field type");
cf->setValue(v);
}
void STObject::setValueFieldU64(SOE_Field field, uint64 v)
{
SerializedType* rf=getPField(field);
if(!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType();
if(id==STI_OBJECT)
SerializedType* rf = getPField(field);
if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id = rf->getSType();
if (id == STI_OBJECT)
{
makeFieldPresent(field);
rf=getPField(field);
id=rf->getSType();
rf = getPField(field);
id = rf->getSType();
}
STUInt64* cf=dynamic_cast<STUInt64*>(rf);
if(!cf) throw(std::runtime_error("Wrong field type"));
STUInt64* cf = dynamic_cast<STUInt64*>(rf);
if (!cf) throw std::runtime_error("Wrong field type");
cf->setValue(v);
}
void STObject::setValueFieldH160(SOE_Field field, const uint160& v)
{
SerializedType* rf=getPField(field);
if(!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType();
if(id==STI_OBJECT)
SerializedType* rf = getPField(field);
if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id = rf->getSType();
if (id == STI_OBJECT)
{
makeFieldPresent(field);
rf=getPField(field);
id=rf->getSType();
rf = getPField(field);
id = rf->getSType();
}
STHash160* cf=dynamic_cast<STHash160*>(rf);
if(!cf) throw(std::runtime_error("Wrong field type"));
STHash160* cf = dynamic_cast<STHash160*>(rf);
if (!cf) throw std::runtime_error("Wrong field type");
cf->setValue(v);
}
void STObject::setValueFieldVL(SOE_Field field, const std::vector<unsigned char>& v)
{
SerializedType* rf=getPField(field);
if(!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType();
if(id==STI_OBJECT)
SerializedType* rf = getPField(field);
if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id = rf->getSType();
if (id == STI_OBJECT)
{
makeFieldPresent(field);
rf=getPField(field);
id=rf->getSType();
}
STVariableLength* cf=dynamic_cast<STVariableLength*>(rf);
if(!cf) throw(std::runtime_error("Wrong field type"));
STVariableLength* cf = dynamic_cast<STVariableLength*>(rf);
if (!cf) throw std::runtime_error("Wrong field type");
cf->setValue(v);
}
void STObject::setValueFieldTL(SOE_Field field, const std::vector<TaggedListItem>& v)
{
SerializedType* rf=getPField(field);
if(!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType();
if(id==STI_OBJECT)
SerializedType* rf = getPField(field);
if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id = rf->getSType();
if (id == STI_OBJECT)
{
makeFieldPresent(field);
rf=getPField(field);
id=rf->getSType();
}
STTaggedList* cf=dynamic_cast<STTaggedList*>(rf);
if(!cf) throw(std::runtime_error("Wrong field type"));
STTaggedList* cf = dynamic_cast<STTaggedList*>(rf);
if (!cf) throw std::runtime_error("Wrong field type");
cf->setValue(v);
}
Json::Value STObject::getJson(int options) const
{
Json::Value ret(Json::objectValue);
int index=1;
for(boost::ptr_vector<SerializedType>::const_iterator it=mData.begin(), end=mData.end(); it!=end; ++it, ++index)
int index = 1;
for(boost::ptr_vector<SerializedType>::const_iterator it = mData.begin(), end = mData.end(); it != end;
++it, ++index)
{
if(it->getSType()!=STI_NOTPRESENT)
if (it->getSType() != STI_NOTPRESENT)
{
if(it->getName()==NULL)
ret[boost::lexical_cast<std::string>(index)]=it->getText();
if (it->getName() == NULL)
ret[boost::lexical_cast<std::string>(index)] = it->getText();
else ret[it->getName()]=it->getText();
}
}

View File

@@ -11,13 +11,17 @@
enum SOE_Type
{
SOE_NEVER=-1, SOE_REQUIRED=0, SOE_FLAGS, SOE_IFFLAG=1, SOE_IFNFLAG=2
SOE_NEVER = -1, // never occurs (marks end of object)
SOE_REQUIRED = 0, // required
SOE_FLAGS = 1, // flags field
SOE_IFFLAG = 2, // present if flag set
SOE_IFNFLAG = 3 // present if flag not set
};
enum SOE_Field
{
sfInvalid=-1,
sfGeneric=0,
sfInvalid = -1,
sfGeneric = 0,
// common fields
sfFlags, sfExtensions, sfTargetLedger, sfSourceTag, sfIdentifier,
@@ -50,9 +54,9 @@ protected:
static SerializedType* makeDeserializedObject(SerializedTypeID id, const char *name, SerializerIterator&);
public:
STObject(const char *n=NULL) : SerializedType(n), mFlagIdx(-1) { ; }
STObject(SOElement *t, const char *n=NULL);
STObject(SOElement *t, SerializerIterator& u, const char *n=NULL);
STObject(const char *n = NULL) : SerializedType(n), mFlagIdx(-1) { ; }
STObject(SOElement *t, const char *n = NULL);
STObject(SOElement *t, SerializerIterator& u, const char *n = NULL);
virtual ~STObject() { ; }
int getLength() const;

View File

@@ -4,7 +4,7 @@
SerializedTransaction::SerializedTransaction(TransactionType type)
{
mFormat=getTxnFormat(type);
if(mFormat==NULL) throw(std::runtime_error("invalid transaction type"));
if (mFormat == NULL) throw std::runtime_error("invalid transaction type");
mMiddleTxn.giveObject(new STUInt32("Magic", TransactionMagic));
mMiddleTxn.giveObject(new STVariableLength("SigningAccount"));
@@ -17,27 +17,27 @@ SerializedTransaction::SerializedTransaction(TransactionType type)
SerializedTransaction::SerializedTransaction(SerializerIterator& sit, int length)
{
if(length==-1) length=sit.getBytesLeft();
else if(length==0) length=sit.get32();
if( (length<TransactionMinLen) || (length>TransactionMaxLen) )
throw(std::runtime_error("Transaction length invalid"));
if (length == -1) length=sit.getBytesLeft();
else if (length == 0) length=sit.get32();
if ( (length < TransactionMinLen) || (length > TransactionMaxLen) )
throw std::runtime_error("Transaction length invalid");
mSignature.setValue(sit.getVL());
if(sit.get32()!=TransactionMagic)
throw(std::runtime_error("Transaction has invalid magic"));
if (sit.get32() != TransactionMagic)
throw std::runtime_error("Transaction has invalid magic");
mMiddleTxn.giveObject(new STUInt32("Magic", TransactionMagic));
mMiddleTxn.giveObject(new STVariableLength("SigningAccount", sit.getVL()));
mMiddleTxn.giveObject(new STUInt32("Sequence", sit.get32()));
int type=sit.get32();
int type = sit.get32();
mMiddleTxn.giveObject(new STUInt32("Type", type));
mFormat=getTxnFormat(static_cast<TransactionType>(type));
if(!mFormat) throw(std::runtime_error("Transaction has invalid type"));
mFormat = getTxnFormat(static_cast<TransactionType>(type));
if (!mFormat) throw std::runtime_error("Transaction has invalid type");
mMiddleTxn.giveObject(new STUInt64("Fee", sit.get64()));
mInnerTxn=STObject(mFormat->elements, sit, "InnerTransaction");
mInnerTxn = STObject(mFormat->elements, sit, "InnerTransaction");
}
int SerializedTransaction::getLength() const
@@ -47,30 +47,30 @@ int SerializedTransaction::getLength() const
std::string SerializedTransaction::getFullText() const
{
std::string ret="\"";
ret+=getTransactionID().GetHex();
ret+="\" = {";
ret+=mSignature.getFullText();
ret+=mMiddleTxn.getFullText();
ret+=mInnerTxn.getFullText();
ret+="}";
std::string ret = "\"";
ret += getTransactionID().GetHex();
ret += "\" = {";
ret += mSignature.getFullText();
ret += mMiddleTxn.getFullText();
ret += mInnerTxn.getFullText();
ret += "}";
return ret;
}
std::string SerializedTransaction::getText() const
{
std::string ret="{";
ret+=mSignature.getText();
ret+=mMiddleTxn.getText();
ret+=mInnerTxn.getText();
ret+="}";
std::string ret = "{";
ret += mSignature.getText();
ret += mMiddleTxn.getText();
ret += mInnerTxn.getText();
ret += "}";
return ret;
}
int SerializedTransaction::getTransaction(Serializer& s, bool include_length) const
{
int l=getLength();
if(include_length) s.add32(l);
int l = getLength();
if (include_length) s.add32(l);
mSignature.add(s);
mMiddleTxn.add(s);
mInnerTxn.add(s);
@@ -79,11 +79,11 @@ int SerializedTransaction::getTransaction(Serializer& s, bool include_length) co
bool SerializedTransaction::isEquivalent(const SerializedType& t) const
{ // Signatures are not compared
const SerializedTransaction* v=dynamic_cast<const SerializedTransaction*>(&t);
if(!v) return false;
if(type != v->type) return false;
if(mMiddleTxn != v->mMiddleTxn) return false;
if(mInnerTxn != v->mInnerTxn) return false;
const SerializedTransaction* v = dynamic_cast<const SerializedTransaction*>(&t);
if (!v) return false;
if (type != v->type) return false;
if (mMiddleTxn != v->mMiddleTxn) return false;
if (mInnerTxn != v->mInnerTxn) return false;
return true;
}
@@ -131,51 +131,51 @@ void SerializedTransaction::setSignature(const std::vector<unsigned char>& sig)
uint32 SerializedTransaction::getVersion() const
{
const STUInt32* v=dynamic_cast<const STUInt32*>(mMiddleTxn.peekAtPIndex(TransactionIVersion));
if(!v) throw(std::runtime_error("corrupt transaction"));
const STUInt32* v = dynamic_cast<const STUInt32*>(mMiddleTxn.peekAtPIndex(TransactionIVersion));
if (!v) throw std::runtime_error("corrupt transaction");
return v->getValue();
}
void SerializedTransaction::setVersion(uint32 ver)
{
STUInt32* v=dynamic_cast<STUInt32*>(mMiddleTxn.getPIndex(TransactionIVersion));
if(!v) throw(std::runtime_error("corrupt transaction"));
STUInt32* v = dynamic_cast<STUInt32*>(mMiddleTxn.getPIndex(TransactionIVersion));
if (!v) throw std::runtime_error("corrupt transaction");
v->setValue(ver);
}
uint64 SerializedTransaction::getTransactionFee() const
{
const STUInt64* v=dynamic_cast<const STUInt64*>(mMiddleTxn.peekAtPIndex(TransactionIFee));
if(!v) throw(std::runtime_error("corrupt transaction"));
const STUInt64* v = dynamic_cast<const STUInt64*>(mMiddleTxn.peekAtPIndex(TransactionIFee));
if (!v) throw std::runtime_error("corrupt transaction");
return v->getValue();
}
void SerializedTransaction::setTransactionFee(uint64 fee)
{
STUInt64* v=dynamic_cast<STUInt64*>(mMiddleTxn.getPIndex(TransactionIFee));
if(!v) throw(std::runtime_error("corrupt transaction"));
STUInt64* v = dynamic_cast<STUInt64*>(mMiddleTxn.getPIndex(TransactionIFee));
if (!v) throw std::runtime_error("corrupt transaction");
v->setValue(fee);
}
uint32 SerializedTransaction::getSequence() const
{
const STUInt32* v=dynamic_cast<const STUInt32*>(mMiddleTxn.peekAtPIndex(TransactionISequence));
if(!v) throw(std::runtime_error("corrupt transaction"));
const STUInt32* v = dynamic_cast<const STUInt32*>(mMiddleTxn.peekAtPIndex(TransactionISequence));
if (!v) throw std::runtime_error("corrupt transaction");
return v->getValue();
}
void SerializedTransaction::setSequence(uint32 seq)
{
STUInt32* v=dynamic_cast<STUInt32*>(mMiddleTxn.getPIndex(TransactionISequence));
if(!v) throw(std::runtime_error("corrupt transaction"));
STUInt32* v = dynamic_cast<STUInt32*>(mMiddleTxn.getPIndex(TransactionISequence));
if (!v) throw std::runtime_error("corrupt transaction");
v->setValue(seq);
}
std::vector<unsigned char> SerializedTransaction::getSigningAccount() const
{
const STVariableLength* v=
const STVariableLength* v =
dynamic_cast<const STVariableLength*>(mMiddleTxn.peekAtPIndex(TransactionISigningAccount));
if(!v) throw(std::runtime_error("corrupt transaction"));
if (!v) throw std::runtime_error("corrupt transaction");
return v->getValue();
}
@@ -183,21 +183,21 @@ const std::vector<unsigned char>& SerializedTransaction::peekSigningAccount() co
{
const STVariableLength* v=
dynamic_cast<const STVariableLength*>(mMiddleTxn.peekAtPIndex(TransactionISigningAccount));
if(!v) throw(std::runtime_error("corrupt transaction"));
if (!v) throw std::runtime_error("corrupt transaction");
return v->peekValue();
}
std::vector<unsigned char>& SerializedTransaction::peekSigningAccount()
{
STVariableLength* v=dynamic_cast<STVariableLength*>(mMiddleTxn.getPIndex(TransactionISigningAccount));
if(!v) throw(std::runtime_error("corrupt transaction"));
STVariableLength* v = dynamic_cast<STVariableLength*>(mMiddleTxn.getPIndex(TransactionISigningAccount));
if (!v) throw std::runtime_error("corrupt transaction");
return v->peekValue();
}
void SerializedTransaction::setSigningAccount(const std::vector<unsigned char>& s)
{
STVariableLength* v=dynamic_cast<STVariableLength*>(mMiddleTxn.getPIndex(TransactionISigningAccount));
if(!v) throw(std::runtime_error("corrupt transaction"));
STVariableLength* v = dynamic_cast<STVariableLength*>(mMiddleTxn.getPIndex(TransactionISigningAccount));
if (!v) throw std::runtime_error("corrupt transaction");
v->setValue(s);
}
@@ -239,10 +239,10 @@ void SerializedTransaction::makeITFieldAbsent(SOE_Field field)
Json::Value SerializedTransaction::getJson(int options) const
{
Json::Value ret(Json::objectValue);
ret["Type"]=mFormat->t_name;
ret["ID"]=getTransactionID().GetHex();
ret["Signature"]=mSignature.getText();
ret["Middle"]=mMiddleTxn.getJson(options);
ret["Inner"]=mInnerTxn.getJson(options);
ret["Type"] = mFormat->t_name;
ret["ID"] = getTransactionID().GetHex();
ret["Signature"] = mSignature.getText();
ret["Middle"] = mMiddleTxn.getJson(options);
ret["Inner"] = mInnerTxn.getJson(options);
return ret;
}

View File

@@ -6,181 +6,181 @@
int Serializer::add16(uint16 i)
{
int ret=mData.size();
mData.push_back(static_cast<unsigned char>(i>>8));
mData.push_back(static_cast<unsigned char>(i&0xff));
int ret = mData.size();
mData.push_back(static_cast<unsigned char>(i >> 8));
mData.push_back(static_cast<unsigned char>(i & 0xff));
return ret;
}
int Serializer::add32(uint32 i)
{
int ret=mData.size();
mData.push_back(static_cast<unsigned char>(i>>24));
mData.push_back(static_cast<unsigned char>((i>>16)&0xff));
mData.push_back(static_cast<unsigned char>((i>>8)&0xff));
mData.push_back(static_cast<unsigned char>(i&0xff));
int ret = mData.size();
mData.push_back(static_cast<unsigned char>(i >> 24));
mData.push_back(static_cast<unsigned char>((i >> 16) & 0xff));
mData.push_back(static_cast<unsigned char>((i >> 8) & 0xff));
mData.push_back(static_cast<unsigned char>(i & 0xff));
return ret;
}
int Serializer::add64(uint64 i)
{
int ret=mData.size();
mData.push_back(static_cast<unsigned char>(i>>56));
mData.push_back(static_cast<unsigned char>((i>>48)&0xff));
mData.push_back(static_cast<unsigned char>((i>>40)&0xff));
mData.push_back(static_cast<unsigned char>((i>>32)&0xff));
mData.push_back(static_cast<unsigned char>((i>>24)&0xff));
mData.push_back(static_cast<unsigned char>((i>>16)&0xff));
mData.push_back(static_cast<unsigned char>((i>>8)&0xff));
mData.push_back(static_cast<unsigned char>(i&0xff));
int ret = mData.size();
mData.push_back(static_cast<unsigned char>(i >> 56));
mData.push_back(static_cast<unsigned char>((i >> 48) & 0xff));
mData.push_back(static_cast<unsigned char>((i >> 40) & 0xff));
mData.push_back(static_cast<unsigned char>((i >> 32) & 0xff));
mData.push_back(static_cast<unsigned char>((i >> 24) & 0xff));
mData.push_back(static_cast<unsigned char>((i >> 16) & 0xff));
mData.push_back(static_cast<unsigned char>((i >> 8) & 0xff));
mData.push_back(static_cast<unsigned char>(i & 0xff));
return ret;
}
int Serializer::add128(const uint128& i)
{
int ret=mData.size();
int ret = mData.size();
mData.insert(mData.end(), i.begin(), i.end());
return ret;
}
int Serializer::add160(const uint160& i)
{
int ret=mData.size();
int ret = mData.size();
mData.insert(mData.end(), i.begin(), i.end());
return ret;
}
int Serializer::add256(const uint256& i)
{
int ret=mData.size();
int ret = mData.size();
mData.insert(mData.end(), i.begin(), i.end());
return ret;
}
int Serializer::addRaw(const std::vector<unsigned char> &vector)
{
int ret=mData.size();
int ret = mData.size();
mData.insert(mData.end(), vector.begin(), vector.end());
return ret;
}
int Serializer::addRaw(const void *ptr, int len)
{
int ret=mData.size();
int ret = mData.size();
mData.insert(mData.end(), (const char *) ptr, ((const char *)ptr)+len);
return ret;
}
bool Serializer::get16(uint16& o, int offset) const
{
if((offset+2)>mData.size()) return false;
o=mData.at(offset++);
o<<=8; o|=mData.at(offset);
if ((offset + 2) > mData.size()) return false;
o = mData.at(++offset);
o <<= 8; o |= mData.at(offset);
return true;
}
bool Serializer::get32(uint32& o, int offset) const
{
if((offset+4)>mData.size()) return false;
o=mData.at(offset++);
o<<=8; o|=mData.at(offset++); o<<=8; o|=mData.at(offset++);
o<<=8; o|=mData.at(offset);
if ((offset + 4) > mData.size()) return false;
o=mData.at(++offset);
o<<=8; o |= mData.at(++offset); o <<= 8; o |= mData.at(++offset);
o<<=8; o |= mData.at(offset);
return true;
}
bool Serializer::get64(uint64& o, int offset) const
{
if((offset+8)>mData.size()) return false;
o=mData.at(offset++);
o<<=8; o|=mData.at(offset++); o<<=8; o|=mData.at(offset++);
o<<=8; o|=mData.at(offset++); o<<=8; o|=mData.at(offset++);
o<<=8; o|=mData.at(offset++); o<<=8; o|=mData.at(offset++);
o<<=8; o|=mData.at(offset);
if ((offset + 8) > mData.size()) return false;
o=mData.at(++offset);
o<<=8; o|= mData.at(++offset); o <<= 8; o |= mData.at(++offset);
o<<=8; o|= mData.at(++offset); o <<= 8; o |= mData.at(++offset);
o<<=8; o|= mData.at(++offset); o <<= 8; o |= mData.at(++offset);
o<<=8; o|= mData.at(offset);
return true;
}
bool Serializer::get128(uint128& o, int offset) const
{
if((offset+(128/8))>mData.size()) return false;
memcpy(o.begin(), &(mData.front())+offset, (128/8));
if ((offset + (128 / 8)) > mData.size()) return false;
memcpy(o.begin(), &(mData.front()) + offset, (128 / 8));
return true;
}
bool Serializer::get160(uint160& o, int offset) const
{
if((offset+(160/8))>mData.size()) return false;
memcpy(o.begin(), &(mData.front())+offset, (160/8));
if ((offset + (160 / 8)) > mData.size()) return false;
memcpy(o.begin(), &(mData.front()) + offset, (160 / 8));
return true;
}
bool Serializer::get256(uint256& o, int offset) const
{
if((offset+(256/8))>mData.size()) return false;
memcpy(o.begin(), &(mData.front())+offset, (256/8));
if ((offset + (256 / 8)) > mData.size()) return false;
memcpy(o.begin(), &(mData.front()) + offset, (256 / 8));
return true;
}
uint256 Serializer::get256(int offset) const
{
uint256 ret;
if((offset+(256/8))>mData.size()) return ret;
memcpy(&ret, &(mData.front())+offset, (256/8));
if ((offset + (256 / 8)) > mData.size()) return ret;
memcpy(&ret, &(mData.front()) + offset, (256 / 8));
return ret;
}
int Serializer::add8(unsigned char byte)
{
int ret=mData.size();
int ret = mData.size();
mData.push_back(byte);
return ret;
}
bool Serializer::get8(int& byte, int offset) const
{
if(offset>=mData.size()) return false;
byte=mData[offset];
if (offset >= mData.size()) return false;
byte = mData[offset];
return true;
}
bool Serializer::chop(int bytes)
{
if(bytes>mData.size()) return false;
mData.resize(mData.size()-bytes);
if (bytes > mData.size()) return false;
mData.resize(mData.size() - bytes);
return true;
}
int Serializer::removeLastByte()
{
int size=mData.size()-1;
if(size<0)
int size = mData.size()-1;
if (size < 0)
{
assert(false);
return -1;
}
int ret=mData[size];
int ret = mData[size];
mData.resize(size);
return ret;
}
bool Serializer::getRaw(std::vector<unsigned char>& o, int offset, int length) const
{
if((offset+length)>mData.size()) return false;
o.assign(mData.begin()+offset, mData.begin()+offset+length);
if ((offset + length) > mData.size()) return false;
o.assign(mData.begin() + offset, mData.begin() + offset+length);
return true;
}
std::vector<unsigned char> Serializer::getRaw(int offset, int length) const
{
std::vector<unsigned char> o;
if((offset+length)>mData.size()) return o;
o.assign(mData.begin()+offset, mData.begin()+offset+length);
if ((offset + length) > mData.size()) return o;
o.assign(mData.begin() + offset, mData.begin() + offset + length);
return o;
}
uint160 Serializer::getRIPEMD160(int size) const
{
uint160 ret;
if((size<0)||(size>mData.size())) size=mData.size();
if ((size < 0) || (size>mData.size())) size = mData.size();
RIPEMD160(&(mData.front()), size, (unsigned char *) &ret);
return ret;
}
@@ -188,7 +188,7 @@ uint160 Serializer::getRIPEMD160(int size) const
uint256 Serializer::getSHA256(int size) const
{
uint256 ret;
if((size<0)||(size>mData.size())) size=mData.size();
if ((size < 0) || (size > mData.size())) size = mData.size();
SHA256(&(mData.front()), size, (unsigned char *) &ret);
return ret;
}
@@ -201,7 +201,7 @@ uint256 Serializer::getSHA512Half(int size) const
uint256 Serializer::getSHA512Half(const std::vector<unsigned char>& data, int size)
{
uint256 j[2];
if((size<0)||(size>data.size())) size=data.size();
if ((size < 0) || (size > data.size())) size = data.size();
SHA512(&(data.front()), size, (unsigned char *) j);
return j[0];
}
@@ -221,11 +221,11 @@ uint256 Serializer::getSHA512Half(const std::string& strData)
bool Serializer::checkSignature(int pubkeyOffset, int signatureOffset) const
{
std::vector<unsigned char> pubkey, signature;
if(!getRaw(pubkey, pubkeyOffset, 65)) return false;
if(!getRaw(signature, signatureOffset, 72)) return false;
if (!getRaw(pubkey, pubkeyOffset, 65)) return false;
if (!getRaw(signature, signatureOffset, 72)) return false;
CKey pubCKey;
if(!pubCKey.SetPubKey(pubkey)) return false;
if (!pubCKey.SetPubKey(pubkey)) return false;
return pubCKey.Verify(getSHA512Half(signatureOffset), signature);
}
@@ -242,33 +242,33 @@ bool Serializer::makeSignature(std::vector<unsigned char> &signature, CKey& key)
bool Serializer::addSignature(CKey& key)
{
std::vector<unsigned char> signature;
if(!key.Sign(getSHA512Half(), signature)) return false;
assert(signature.size()==72);
if (!key.Sign(getSHA512Half(), signature)) return false;
assert(signature.size() == 72);
addRaw(signature);
return true;
}
int Serializer::addVL(const std::vector<unsigned char>& vector)
{
int ret=addRaw(encodeVL(vector.size()));
int ret = addRaw(encodeVL(vector.size()));
addRaw(vector);
return ret;
}
int Serializer::addVL(const void *ptr, int len)
{
int ret=addRaw(encodeVL(len));
int ret = addRaw(encodeVL(len));
addRaw(ptr, len);
return ret;
}
int Serializer::addTaggedList(const std::list<TaggedListItem>& list)
{
int size=list.size();
if(size>255) return -1;
int ret=add8(size);
if(size!=0)
for(std::list<TaggedListItem>::const_iterator it=list.begin(); it!=list.end(); ++it)
int size = list.size();
if (size > 255) return -1;
int ret = add8(size);
if (size != 0)
for (std::list<TaggedListItem>::const_iterator it = list.begin(); it != list.end(); ++it)
{
add8(it->first);
addVL(it->second);
@@ -278,11 +278,11 @@ int Serializer::addTaggedList(const std::list<TaggedListItem>& list)
int Serializer::addTaggedList(const std::vector<TaggedListItem>& list)
{
int size=list.size();
if(size>255) return -1;
int ret=add8(size);
if(size!=0)
for(std::vector<TaggedListItem>::const_iterator it=list.begin(); it!=list.end(); ++it)
int size = list.size();
if (size > 255) return -1;
int ret = add8(size);
if (size != 0)
for (std::vector<TaggedListItem>::const_iterator it = list.begin(); it != list.end(); ++it)
{
add8(it->first);
addVL(it->second);
@@ -292,48 +292,48 @@ int Serializer::addTaggedList(const std::vector<TaggedListItem>& list)
int Serializer::getTaggedListLength(const std::list<TaggedListItem>& list)
{
int size=list.size();
if(size>255) return -1;
int ret=1;
if(size!=0)
for(std::list<TaggedListItem>::const_iterator it=list.begin(); it!=list.end(); ++it)
ret+=1 + it->second.size() + Serializer::encodeLengthLength(it->second.size());
int size = list.size();
if (size > 255) return -1;
int ret = 1;
if (size != 0)
for (std::list<TaggedListItem>::const_iterator it = list.begin(); it != list.end(); ++it)
ret += 1 + it->second.size() + Serializer::encodeLengthLength(it->second.size());
return ret;
}
int Serializer::getTaggedListLength(const std::vector<TaggedListItem>& list)
{
int size=list.size();
if(size>255) return -1;
int ret=1;
if(size!=0)
for(std::vector<TaggedListItem>::const_iterator it=list.begin(); it!=list.end(); ++it)
ret+=1 + it->second.size() + Serializer::encodeLengthLength(it->second.size());
int size = list.size();
if (size > 255) return -1;
int ret = 1;
if (size != 0)
for (std::vector<TaggedListItem>::const_iterator it = list.begin(); it != list.end(); ++it)
ret += 1 + it->second.size() + Serializer::encodeLengthLength(it->second.size());
return ret;
}
bool Serializer::getVL(std::vector<unsigned char>& objectVL, int offset, int& length) const
{
int b1;
if(!get8(b1, offset++)) return false;
if (!get8(b1, ++offset)) return false;
int datLen, lenLen=decodeLengthLength(b1);
int datLen, lenLen = decodeLengthLength(b1);
try
{
if(lenLen==1)
datLen=decodeVLLength(b1);
else if(lenLen==2)
if (lenLen == 1)
datLen = decodeVLLength(b1);
else if (lenLen == 2)
{
int b2;
if(!get8(b2, offset++)) return false;
if (!get8(b2, ++offset)) return false;
datLen=decodeVLLength(b1, b2);
}
else if(lenLen==3)
else if (lenLen == 3)
{
int b2, b3;
if(!get8(b2, offset++)) return false;
if(!get8(b3, offset++)) return false;
datLen=decodeVLLength(b1, b2, b3);
if (!get8(b2, ++offset)) return false;
if (!get8(b3, ++offset)) return false;
datLen = decodeVLLength(b1, b2, b3);
}
else return false;
}
@@ -341,32 +341,32 @@ bool Serializer::getVL(std::vector<unsigned char>& objectVL, int offset, int& le
{
return false;
}
length=lenLen+datLen;
length = lenLen + datLen;
return getRaw(objectVL, offset, datLen);
}
bool Serializer::getVLLength(int& length, int offset) const
{
int b1;
if(!get8(b1, offset++)) return false;
if (!get8(b1, ++offset)) return false;
int lenLen=decodeLengthLength(b1);
int lenLen = decodeLengthLength(b1);
try
{
if(lenLen==1)
if (lenLen == 1)
length=decodeVLLength(b1);
else if(lenLen==2)
else if (lenLen == 2)
{
int b2;
if(!get8(b2, offset++)) return false;
if (!get8(b2, ++offset)) return false;
length=decodeVLLength(b1, b2);
}
else if(lenLen==3)
else if (lenLen == 3)
{
int b2, b3;
if(!get8(b2, offset++)) return false;
if(!get8(b3, offset++)) return false;
length=decodeVLLength(b1, b2, b3);
if (!get8(b2, ++offset)) return false;
if (!get8(b3, ++offset)) return false;
length = decodeVLLength(b1, b2, b3);
}
else return false;
}
@@ -380,19 +380,19 @@ bool Serializer::getVLLength(int& length, int offset) const
bool Serializer::getTaggedList(std::list<TaggedListItem>& list, int offset, int& length) const
{
list.clear();
int startOffset=offset;
int startOffset = offset;
int numElem;
if(!get8(numElem, offset++)) return false;
for(int i=0; i<numElem; i++)
if (!get8(numElem, ++offset)) return false;
for (int i = 0; i<numElem; i++)
{
int tag, len;
std::vector<unsigned char> data;
if(!get8(tag, offset++)) return false;
if(!getVL(data, offset, len)) return false;
offset+=len;
if (!get8(tag, ++offset)) return false;
if (!getVL(data, offset, len)) return false;
offset += len;
list.push_back(std::make_pair(tag, data));
}
length=offset-startOffset;
length = offset - startOffset;
return true;
}
@@ -401,83 +401,83 @@ bool Serializer::getTaggedList(std::vector<TaggedListItem>& list, int offset, in
list.clear();
int startOffset=offset;
int numElem;
if(!get8(numElem, offset++)) return false;
for(int i=0; i<numElem; i++)
if (!get8(numElem, ++offset)) return false;
for (int i=0; i<numElem; i++)
{
int tag, len;
std::vector<unsigned char> data;
if(!get8(tag, offset++)) return false;
if(!getVL(data, offset, len)) return false;
offset+=len;
if (!get8(tag, ++offset)) return false;
if (!getVL(data, offset, len)) return false;
offset += len;
list.push_back(std::make_pair(tag, data));
}
length=offset-startOffset;
length = offset - startOffset;
return true;
}
std::vector<unsigned char> Serializer::encodeVL(int length)
{
unsigned char lenBytes[4];
if(length<=192)
if (length <= 192)
{
lenBytes[0]=static_cast<unsigned char>(length);
lenBytes[0] = static_cast<unsigned char>(length);
return std::vector<unsigned char>(&lenBytes[0], &lenBytes[1]);
}
else if(length<=12480)
else if (length <= 12480)
{
length-=193;
lenBytes[0]=static_cast<unsigned char>(length>>8);
lenBytes[1]=static_cast<unsigned char>(length&0xff);
length -= 193;
lenBytes[0] = static_cast<unsigned char>(length >> 8);
lenBytes[1] = static_cast<unsigned char>(length & 0xff);
return std::vector<unsigned char>(&lenBytes[0], &lenBytes[2]);
}
else if(length<=918744)
else if (length <= 918744)
{
length-=12481;
lenBytes[0]=static_cast<unsigned char>(length>>16);
lenBytes[1]=static_cast<unsigned char>((length>>8)&0xff);
lenBytes[2]=static_cast<unsigned char>(length&0xff);
length -= 12481;
lenBytes[0] = static_cast<unsigned char>(length >> 16);
lenBytes[1] = static_cast<unsigned char>((length >> 8) & 0xff);
lenBytes[2] = static_cast<unsigned char>(length & 0xff);
return std::vector<unsigned char>(&lenBytes[0], &lenBytes[3]);
}
else throw(std::overflow_error("lenlen"));
else throw std::overflow_error("lenlen");
}
int Serializer::encodeLengthLength(int length)
{
if(length<0) throw(std::overflow_error("len<0"));
if(length<=192) return 1;
if(length<=12480) return 2;
if(length>=918744) return 3;
throw(std::overflow_error("len>918644"));
if (length < 0) throw std::overflow_error("len<0");
if (length <= 192) return 1;
if (length <= 12480) return 2;
if (length >= 918744) return 3;
throw std::overflow_error("len>918644");
}
int Serializer::decodeLengthLength(int b1)
{
if(b1<0) throw(std::overflow_error("b1<0"));
if(b1<=192) return 1;
if(b1<=240) return 2;
if(b1<=254) return 3;
throw(std::overflow_error("b1>254"));
if (b1 < 0) throw std::overflow_error("b1<0");
if (b1 <= 192) return 1;
if (b1 <= 240) return 2;
if (b1 <= 254) return 3;
throw std::overflow_error("b1>254");
}
int Serializer::decodeVLLength(int b1)
{
if(b1<0) throw(std::overflow_error("b1<0"));
if(b1>254) throw(std::overflow_error("b1>254"));
if (b1 < 0) throw std::overflow_error("b1<0");
if (b1 > 254) throw std::overflow_error("b1>254");
return b1;
}
int Serializer::decodeVLLength(int b1, int b2)
{
if(b1<193) throw(std::overflow_error("b1<193"));
if(b1>240) throw(std::overflow_error("b1>240"));
return 193+(b1-193)*256+b2;
if (b1 < 193) throw std::overflow_error("b1<193");
if (b1 > 240) throw std::overflow_error("b1>240");
return 193 + (b1 - 193) * 256 + b2;
}
int Serializer::decodeVLLength(int b1, int b2, int b3)
{
if(b1<241) throw(std::overflow_error("b1<241"));
if(b1>254) throw(std::overflow_error("b1>254"));
return 12481+(b1-241)*65536+b2*256+b3;
if (b1 < 241) throw std::overflow_error("b1<241");
if (b1 > 254) throw std::overflow_error("b1>254");
return 12481 + (b1 - 241) * 65536 + b2 * 256 + b3;
}
void Serializer::TestSerializer()
@@ -493,7 +493,7 @@ int SerializerIterator::getBytesLeft()
unsigned char SerializerIterator::get8()
{
int val;
if(!mSerializer.get8(val, mPos)) throw(0);
if (!mSerializer.get8(val, mPos)) throw std::runtime_error("invalid serializer get");
mPos++;
return val;
}
@@ -501,48 +501,48 @@ unsigned char SerializerIterator::get8()
uint16 SerializerIterator::get16()
{
uint16 val;
if(!mSerializer.get16(val, mPos)) throw(0);
mPos+=16/8;
if (!mSerializer.get16(val, mPos)) throw std::runtime_error("invalid serializer get");
mPos += 16/8;
return val;
}
uint32 SerializerIterator::get32()
{
uint32 val;
if(!mSerializer.get32(val, mPos)) throw(0);
mPos+=32/8;
if (!mSerializer.get32(val, mPos)) throw std::runtime_error("invalid serializer get");
mPos += 32/8;
return val;
}
uint64 SerializerIterator::get64()
{
uint64 val;
if(!mSerializer.get64(val, mPos)) throw(0);
mPos+=64/8;
if (!mSerializer.get64(val, mPos)) throw std::runtime_error("invalid serializer get");
mPos += 64/8;
return val;
}
uint128 SerializerIterator::get128()
{
uint128 val;
if(!mSerializer.get128(val, mPos)) throw(0);
mPos+=128/8;
if (!mSerializer.get128(val, mPos)) throw std::runtime_error("invalid serializer get");
mPos += 128/8;
return val;
}
uint160 SerializerIterator::get160()
{
uint160 val;
if(!mSerializer.get160(val, mPos)) throw(0);
mPos+=160/8;
if (!mSerializer.get160(val, mPos)) throw std::runtime_error("invalid serializer get");
mPos += 160/8;
return val;
}
uint256 SerializerIterator::get256()
{
uint256 val;
if(!mSerializer.get256(val, mPos)) throw(0);
mPos+=256/8;
if (!mSerializer.get256(val, mPos)) throw std::runtime_error("invalid serializer get");
mPos += 256/8;
return val;
}
@@ -550,8 +550,8 @@ std::vector<unsigned char> SerializerIterator::getVL()
{
int length;
std::vector<unsigned char> vl;
if(!mSerializer.getVL(vl, mPos, length)) throw(0);
mPos+=length;
if (!mSerializer.getVL(vl, mPos, length)) throw std::runtime_error("invalid serializer get");
mPos += length;
return vl;
}
@@ -559,7 +559,7 @@ std::vector<TaggedListItem> SerializerIterator::getTaggedList()
{
int length;
std::vector<TaggedListItem> tl;
if(!mSerializer.getTaggedList(tl, mPos, length)) throw(0);
mPos+=length;
if (!mSerializer.getTaggedList(tl, mPos, length)) throw std::runtime_error("invalid serializer get");
mPos += length;
return tl;
}

View File

@@ -52,18 +52,14 @@ public:
template<typename c_Key, typename c_Data> int TaggedCache<c_Key, c_Data>::getTargetSize() const
{
mLock.lock();
int ret=mTargetSize;
mLock.unlock();
return ret;
boost::recursive_mutex::scoped_lock sl(mLock);
return mTargetSize;
}
template<typename c_Key, typename c_Data> int TaggedCache<c_Key, c_Data>::getTargetAge() const
{
mLock.lock();
int ret=mTargetAge;
mLock.unlock();
return ret;
boost::recursive_mutex::scoped_lock sl(mLock);
return mTargetAge;
}
template<typename c_Key, typename c_Data> int TaggedCache<c_Key, c_Data>::getCacheSize()
@@ -76,28 +72,28 @@ template<typename c_Key, typename c_Data> void TaggedCache<c_Key, c_Data>::sweep
{
boost::recursive_mutex::scoped_lock sl(mLock);
if(mCache.size()<mTargetSize) return;
if (mCache.size() < mTargetSize) return;
time_t now=time(NULL);
if((mLastSweep+10)<now) return;
time_t now = time(NULL);
if ((mLastSweep + 10) < now) return;
mLastSweep=now;
time_t target=now-mTargetAge;
mLastSweep = now;
time_t target = now - mTargetAge;
// Pass 1, remove old objects from cache
typename std::map<key_type, cache_entry>::iterator cit=mCache.begin();
while(cit!=mCache.end())
typename std::map<key_type, cache_entry>::iterator cit = mCache.begin();
while (cit != mCache.end())
{
if(cit->second->second.first<target)
if (cit->second->second.first < target)
mCache.erase(cit++);
else ++cit;
}
// Pass 2, remove dead objects from map
typename std::map<key_type, weak_data_ptr>::iterator mit=mMap.begin();
while(mit!=mMap.end())
typename std::map<key_type, weak_data_ptr>::iterator mit = mMap.begin();
while (mit != mMap.end())
{
if(mit->second->expired())
if (mit->second->expired())
mMap.erase(mit++);
else ++mit;
}
@@ -108,19 +104,19 @@ template<typename c_Key, typename c_Data> bool TaggedCache<c_Key, c_Data>::touch
boost::recursive_mutex::scoped_lock sl(mLock);
// Is the object in the map?
typename std::map<key_type, weak_data_ptr>::iterator mit=mMap.find(key);
if(mit==mMap.end()) return false;
if(mit->second->expired())
typename std::map<key_type, weak_data_ptr>::iterator mit = mMap.find(key);
if (mit == mMap.end()) return false;
if (mit->second->expired())
{ // in map, but expired
mMap.erase(mit);
return false;
}
// Is the object in the cache?
typename std::map<key_type, cache_entry>::iterator cit=mCache.find(key);
if(cit!=mCache.end())
typename std::map<key_type, cache_entry>::iterator cit = mCache.find(key);
if (cit != mCache.end())
{ // in both map and cache
cit->second->first=time(NULL);
cit->second->first = time(NULL);
return true;
}
@@ -133,8 +129,8 @@ template<typename c_Key, typename c_Data> bool TaggedCache<c_Key, c_Data>::del(c
{ // Remove from cache, map unaffected
boost::recursive_mutex::scoped_lock sl(mLock);
typename std::map<key_type, cache_entry>::iterator cit=mCache.find(key);
if(cit==mCache.end()) return false;
typename std::map<key_type, cache_entry>::iterator cit = mCache.find(key);
if (cit == mCache.end()) return false;
mCache.erase(cit);
return true;
}
@@ -145,27 +141,27 @@ bool TaggedCache<c_Key, c_Data>::canonicalize(const key_type& key, boost::shared
// Return values: true=we had the data already
boost::recursive_mutex::scoped_lock sl(mLock);
typename std::map<key_type, weak_data_ptr>::iterator mit=mMap.find(key);
if(mit==mMap.end())
typename std::map<key_type, weak_data_ptr>::iterator mit = mMap.find(key);
if (mit == mMap.end())
{ // not in map
mCache.insert(std::make_pair(key, std::make_pair(time(NULL), data)));
mMap.insert(std::make_pair(key, data));
return false;
}
if(mit->second.expired())
if (mit->second.expired())
{ // in map, but expired
mit->second=data;
mit->second = data;
mCache.insert(std::make_pair(key, std::make_pair(time(NULL), data)));
return false;
}
data=mit->second.lock();
data = mit->second.lock();
assert(!!data);
// Valid in map, is it in cache?
typename std::map<key_type, cache_entry>::iterator cit=mCache.find(key);
if(cit!=mCache.end())
cit->second.first=time(NULL); // Yes, refesh
typename std::map<key_type, cache_entry>::iterator cit = mCache.find(key);
if (cit != mCache.end())
cit->second.first = time(NULL); // Yes, refesh
else // no, add to cache
mCache.insert(std::make_pair(key, std::make_pair(time(NULL), data)));
@@ -178,20 +174,20 @@ boost::shared_ptr<c_Data> TaggedCache<c_Key, c_Data>::fetch(const key_type& key)
boost::recursive_mutex::scoped_lock sl(mLock);
// Is it in the map?
typename std::map<key_type, weak_data_ptr>::iterator mit=mMap.find(key);
if(mit==mMap.end()) return data_ptr(); // No, we're done
if(mit->second.expired())
typename std::map<key_type, weak_data_ptr>::iterator mit = mMap.find(key);
if (mit == mMap.end()) return data_ptr(); // No, we're done
if (mit->second.expired())
{ // in map, but expired
mMap.erase(mit);
return data_ptr();
}
boost::shared_ptr<c_Data> data=mit->second.lock();
boost::shared_ptr<c_Data> data = mit->second.lock();
// Valid in map, is it in the cache?
typename std::map<key_type, cache_entry>::iterator cit=mCache.find(key);
if(cit!=mCache.end())
cit->second.first=time(NULL); // Yes, refresh
typename std::map<key_type, cache_entry>::iterator cit = mCache.find(key);
if (cit != mCache.end())
cit->second.first = time(NULL); // Yes, refresh
else // No, add to cache
mCache.insert(std::make_pair(key, std::make_pair(time(NULL), data)));