Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
opentxs::OTStash Class Reference

#include <OTStash.hpp>

Public Member Functions

const std::string GetName () const
 
OTStashItemGetStash (const std::string &str_asset_type_id)
 
int64_t GetAmount (std::string str_asset_type_id)
 
bool CreditStash (std::string str_asset_type_id, const int64_t &lAmount)
 
bool DebitStash (std::string str_asset_type_id, const int64_t &lAmount)
 
void Serialize (OTString &strAppend) const
 
int32_t ReadFromXMLNode (irr::io::IrrXMLReader *&xml, const OTString &strStashName, const OTString &strItemCount)
 
 OTStash ()
 
 OTStash (std::string str_stash_name)
 
 OTStash (const OTString &strAssetTypeID, int64_t lAmount=0)
 
 OTStash (const OTIdentifier &theAssetTypeID, int64_t lAmount=0)
 
virtual ~OTStash ()
 

Detailed Description

Definition at line 160 of file OTStash.hpp.

Constructor & Destructor Documentation

opentxs::OTStash::OTStash ( )

Definition at line 236 of file OTStash.cpp.

237 {
238  // m_mapStashItems
239 }
opentxs::OTStash::OTStash ( std::string  str_stash_name)
inline

Definition at line 183 of file OTStash.hpp.

184  : m_str_stash_name(str_stash_name)
185  {
186  }
opentxs::OTStash::OTStash ( const OTString strAssetTypeID,
int64_t  lAmount = 0 
)

Definition at line 241 of file OTStash.cpp.

242 {
243  OTStashItem* pItem = new OTStashItem(strAssetTypeID, lAmount);
244  OT_ASSERT(nullptr != pItem);
245 
246  m_mapStashItems.insert(
247  std::pair<std::string, OTStashItem*>(strAssetTypeID.Get(), pItem));
248 }
#define OT_ASSERT(x)
Definition: Assert.hpp:150
opentxs::OTStash::OTStash ( const OTIdentifier theAssetTypeID,
int64_t  lAmount = 0 
)

Definition at line 250 of file OTStash.cpp.

251 {
252  OTStashItem* pItem = new OTStashItem(theAssetTypeID, lAmount);
253  OT_ASSERT(nullptr != pItem);
254 
255  OTString strAssetTypeID(theAssetTypeID);
256 
257  m_mapStashItems.insert(
258  std::pair<std::string, OTStashItem*>(strAssetTypeID.Get(), pItem));
259 }
#define OT_ASSERT(x)
Definition: Assert.hpp:150
opentxs::OTStash::~OTStash ( )
virtual

Definition at line 261 of file OTStash.cpp.

262 {
263  while (!m_mapStashItems.empty()) {
264  OTStashItem* pTemp = m_mapStashItems.begin()->second;
265  OT_ASSERT(nullptr != pTemp);
266  delete pTemp;
267  pTemp = nullptr;
268  m_mapStashItems.erase(m_mapStashItems.begin());
269  }
270 }
#define OT_ASSERT(x)
Definition: Assert.hpp:150

Member Function Documentation

bool opentxs::OTStash::CreditStash ( std::string  str_asset_type_id,
const int64_t &  lAmount 
)

Definition at line 306 of file OTStash.cpp.

307 {
308  OTStashItem* pStashItem =
309  GetStash(str_asset_type_id); // (Always succeeds, and will OT_ASSERT()
310  // if failure.)
311 
312  return pStashItem->CreditStash(lAmount);
313 }
OTStashItem * GetStash(const std::string &str_asset_type_id)
Definition: OTStash.cpp:275
bool opentxs::OTStash::DebitStash ( std::string  str_asset_type_id,
const int64_t &  lAmount 
)

Definition at line 315 of file OTStash.cpp.

316 {
317  OTStashItem* pStashItem =
318  GetStash(str_asset_type_id); // (Always succeeds, and will OT_ASSERT()
319  // if failure.)
320 
321  return pStashItem->DebitStash(lAmount);
322 }
OTStashItem * GetStash(const std::string &str_asset_type_id)
Definition: OTStash.cpp:275
int64_t opentxs::OTStash::GetAmount ( std::string  str_asset_type_id)

Definition at line 297 of file OTStash.cpp.

298 {
299  OTStashItem* pStashItem =
300  GetStash(str_asset_type_id); // (Always succeeds, and will OT_ASSERT()
301  // if failure.)
302 
303  return pStashItem->GetAmount();
304 }
OTStashItem * GetStash(const std::string &str_asset_type_id)
Definition: OTStash.cpp:275
const std::string opentxs::OTStash::GetName ( ) const
inline

Definition at line 167 of file OTStash.hpp.

168  {
169  return m_str_stash_name;
170  }
OTStashItem * opentxs::OTStash::GetStash ( const std::string &  str_asset_type_id)

Definition at line 275 of file OTStash.cpp.

276 {
277  auto it = m_mapStashItems.find(str_asset_type_id);
278 
279  if (m_mapStashItems.end() ==
280  it) // It's not already there for this asset type.
281  {
282  const OTString strAssetTypeID(str_asset_type_id.c_str());
283  OTStashItem* pStashItem = new OTStashItem(strAssetTypeID);
284  OT_ASSERT(nullptr != pStashItem);
285 
286  m_mapStashItems.insert(std::pair<std::string, OTStashItem*>(
287  strAssetTypeID.Get(), pStashItem));
288  return pStashItem;
289  }
290 
291  OTStashItem* pStashItem = it->second;
292  OT_ASSERT(nullptr != pStashItem);
293 
294  return pStashItem;
295 }
#define OT_ASSERT(x)
Definition: Assert.hpp:150
int32_t opentxs::OTStash::ReadFromXMLNode ( irr::io::IrrXMLReader *&  xml,
const OTString strStashName,
const OTString strItemCount 
)

Definition at line 165 of file OTStash.cpp.

168 {
169  if (!strStashName.Exists()) {
170  otErr << "OTStash::ReadFromXMLNode: Failed: Empty stash 'name' "
171  "attribute.\n";
172  return (-1);
173  }
174 
175  m_str_stash_name = strStashName.Get();
176 
177  //
178  // Load up the stash items.
179  //
180  int32_t nCount = strItemCount.Exists() ? atoi(strItemCount.Get()) : 0;
181  if (nCount > 0) {
182  while (nCount-- > 0) {
183  // xml->read();
184  if (!OTContract::SkipToElement(xml)) {
185  otOut << "OTStash::ReadFromXMLNode: Failure: Unable to find "
186  "expected element.\n";
187  return (-1);
188  }
189 
190  if ((xml->getNodeType() == irr::io::EXN_ELEMENT) &&
191  (!strcmp("stashItem", xml->getNodeName()))) {
192  OTString strAssetTypeID = xml->getAttributeValue(
193  "assetTypeID"); // Asset Type ID of this account.
194  OTString strAmount = xml->getAttributeValue(
195  "balance"); // Account ID for this account.
196 
197  if (!strAssetTypeID.Exists() || !strAmount.Exists()) {
198  otErr << "OTStash::ReadFromXMLNode: Error loading "
199  "stashItem: Either the assetTypeID ("
200  << strAssetTypeID << "), or the balance ("
201  << strAmount << ") was EMPTY.\n";
202  return (-1);
203  }
204 
205  if (!CreditStash(strAssetTypeID.Get(),
206  atol(strAmount.Get()))) // <===============
207  {
208  otErr << "OTStash::ReadFromXMLNode: Failed crediting "
209  "stashItem for stash " << strStashName
210  << ". assetTypeID (" << strAssetTypeID
211  << "), balance (" << strAmount << ").\n";
212  return (-1);
213  }
214 
215  // (Success)
216  }
217  else {
218  otErr << "OTStash::ReadFromXMLNode: Expected stashItem "
219  "element.\n";
220  return (-1); // error condition
221  }
222  } // while
223  }
224 
225  if (!OTContract::SkipAfterLoadingField(xml)) // </stash>
226  {
227  otOut << "*** OTStash::ReadFromXMLNode: Bad data? Expected "
228  "EXN_ELEMENT_END here, but "
229  "didn't get it. Returning -1.\n";
230  return (-1);
231  }
232 
233  return 1;
234 }
OTLOG_IMPORT OTLogStream otOut
static bool SkipAfterLoadingField(irr::io::IrrXMLReader *&xml)
static bool SkipToElement(irr::io::IrrXMLReader *&xml)
OTLOG_IMPORT OTLogStream otErr
bool CreditStash(std::string str_asset_type_id, const int64_t &lAmount)
Definition: OTStash.cpp:306
void opentxs::OTStash::Serialize ( OTString strAppend) const

Definition at line 147 of file OTStash.cpp.

148 {
149  strAppend.Concatenate("<stash name=\"%s\" count=\"%d\" >\n\n",
150  m_str_stash_name.c_str(), m_mapStashItems.size());
151 
152  for (auto& it : m_mapStashItems) {
153  const std::string str_asset_type_id = it.first;
154  OTStashItem* pStashItem = it.second;
155  OT_ASSERT((str_asset_type_id.size() > 0) && (nullptr != pStashItem));
156 
157  strAppend.Concatenate(
158  "<stashItem assetTypeID=\"%s\" balance=\"%lld\" />\n\n",
159  pStashItem->GetAssetTypeID().Get(), pStashItem->GetAmount());
160  }
161 
162  strAppend.Concatenate("</stash>\n\n");
163 }
#define OT_ASSERT(x)
Definition: Assert.hpp:150

The documentation for this class was generated from the following files: