Making sure code compiles with STL library that comes with OSX 10.9
diff --git a/src/sync-digest.cc b/src/sync-digest.cc
index 07b5248..6f41d3d 100644
--- a/src/sync-digest.cc
+++ b/src/sync-digest.cc
@@ -45,6 +45,7 @@
// Other options: VP_md2, EVP_md5, EVP_sha, EVP_sha1, EVP_sha256, EVP_dss, EVP_dss1, EVP_mdc2, EVP_ripemd160
#define HASH_FUNCTION EVP_sha256
+#define HASH_FUNCTION_LEN 32
// #ifndef DIGEST_BASE64
@@ -62,8 +63,8 @@
}
};
-typedef transform_iterator<hex_from_4_bit<string::const_iterator::value_type>,
- transform_width<string::const_iterator, 4, 8, string::const_iterator::value_type> > string_from_binary;
+typedef transform_iterator<hex_from_4_bit<std::vector<uint8_t>::const_iterator::value_type>,
+ transform_width<std::vector<uint8_t>::const_iterator, 4, 8, std::vector<uint8_t>::const_iterator::value_type> > string_from_binary;
template<class CharType>
@@ -96,18 +97,9 @@
typedef transform_width<transform_iterator<hex_to_4_bit<string::const_iterator::value_type>, string::const_iterator>, 8, 4> string_to_binary;
-// #else
-
-// typedef base64_from_binary<transform_width<string::const_iterator, 6, 8> > string_from_binary;
-// typedef binary_from_base64<transform_width<string::const_iterator, 8, 6> > string_to_binary;
-
-// #endif
-
namespace Sync {
Digest::Digest ()
- : m_buffer (0)
- , m_hashLength (0)
{
m_context = EVP_MD_CTX_create ();
@@ -116,37 +108,30 @@
Digest::~Digest ()
{
- if (m_buffer != 0)
- delete [] m_buffer;
-
EVP_MD_CTX_destroy (m_context);
}
bool
Digest::empty () const
{
- return m_buffer == 0;
+ return m_buffer.empty ();
}
bool
Digest::isZero () const
{
- if (m_buffer == 0)
+ if (m_buffer.empty ())
BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
<< errmsg_info_str ("Digest has not been yet finalized"));
- return (m_hashLength == 1 && m_buffer[0] == 0);
+ return (m_buffer.size () == 1 && m_buffer[0] == 0);
}
void
Digest::reset ()
{
- if (m_buffer != 0)
- {
- delete [] m_buffer;
- m_buffer = 0;
- }
+ m_buffer.clear ();
int ok = EVP_DigestInit_ex (m_context, HASH_FUNCTION (), 0);
if (!ok)
@@ -159,12 +144,13 @@
void
Digest::finalize ()
{
- if (m_buffer != 0) return;
+ if (!m_buffer.empty ()) return;
- m_buffer = new uint8_t [EVP_MAX_MD_SIZE];
-
+ m_buffer.resize (HASH_FUNCTION_LEN);
+
+ unsigned int tmp;
int ok = EVP_DigestFinal_ex (m_context,
- m_buffer, &m_hashLength);
+ &m_buffer[0], &tmp);
if (!ok)
BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
<< errmsg_info_str ("EVP_DigestFinal_ex returned error")
@@ -176,39 +162,30 @@
{
if (isZero ()) return 0;
- if (sizeof (std::size_t) > m_hashLength)
+ if (sizeof (std::size_t) > m_buffer.size ())
{
BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
<< errmsg_info_str ("Hash is not zero and length is less than size_t")
- << errmsg_info_int (m_hashLength));
+ << errmsg_info_int (m_buffer.size ()));
}
// just getting first sizeof(std::size_t) bytes
// not ideal, but should work pretty well
- return *(reinterpret_cast<std::size_t*> (m_buffer));
+ return *(reinterpret_cast<const std::size_t*> (&m_buffer[0]));
}
bool
Digest::operator == (const Digest &digest) const
{
- if (m_buffer == 0)
+ if (m_buffer.empty ())
BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
<< errmsg_info_str ("Digest1 is empty"));
- if (digest.m_buffer == 0)
+ if (digest.m_buffer.empty ())
BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
<< errmsg_info_str ("Digest2 is empty"));
- if (m_hashLength != digest.m_hashLength)
- return false;
-
- // Allow different hash size
- // BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
- // << errmsg_info_str ("Digest lengths are not the same")
- // << errmsg_info_int (m_hashLength)
- // << errmsg_info_int (digest.m_hashLength));
-
- return memcmp (m_buffer, digest.m_buffer, m_hashLength) == 0;
+ return m_buffer == digest.m_buffer;
}
@@ -218,7 +195,7 @@
// cout << "Update: " << (void*)buffer << " / size: " << size << "\n";
// cannot update Digest when it has been finalized
- if (m_buffer != 0)
+ if (!m_buffer.empty ())
BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
<< errmsg_info_str ("Digest has been already finalized"));
@@ -233,11 +210,11 @@
Digest &
Digest::operator << (const Digest &src)
{
- if (src.m_buffer == 0)
+ if (src.m_buffer.empty ())
BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
<< errmsg_info_str ("Digest has not been yet finalized"));
- update (src.m_buffer, src.m_hashLength);
+ update (&src.m_buffer[0], src.m_buffer.size ());
return *this;
}
@@ -245,12 +222,12 @@
std::ostream &
operator << (std::ostream &os, const Digest &digest)
{
- BOOST_ASSERT (digest.m_hashLength != 0);
+ BOOST_ASSERT (!digest.m_buffer.empty ());
ostreambuf_iterator<char> out_it (os); // ostream iterator
// need to encode to base64
- copy (string_from_binary (reinterpret_cast<const char*> (digest.m_buffer)),
- string_from_binary (reinterpret_cast<const char*> (digest.m_buffer+digest.m_hashLength)),
+ copy (string_from_binary (digest.m_buffer.begin ()),
+ string_from_binary (digest.m_buffer.end ()),
out_it);
return os;
@@ -270,16 +247,15 @@
// for (uint8_t i = 0; i < padding; i++) str.push_back ('=');
// only empty digest object can be used for reading
- if (digest.m_buffer != 0)
+ if (!digest.m_buffer.empty ())
BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
<< errmsg_info_str ("Digest has been already finalized"));
- digest.m_buffer = new uint8_t [EVP_MAX_MD_SIZE];
- uint8_t *end = copy (string_to_binary (str.begin ()),
- string_to_binary (str.end ()),
- digest.m_buffer);
-
- digest.m_hashLength = end - digest.m_buffer;
+ digest.m_buffer.clear ();
+
+ copy (string_to_binary (str.begin ()),
+ string_to_binary (str.end ()),
+ std::back_inserter (digest.m_buffer));
return is;
}
diff --git a/src/sync-digest.h b/src/sync-digest.h
index d980afb..7cef28f 100644
--- a/src/sync-digest.h
+++ b/src/sync-digest.h
@@ -26,6 +26,7 @@
#include <boost/exception/all.hpp>
#include <openssl/evp.h>
#include <boost/cstdint.hpp>
+#include <vector>
namespace Sync {
@@ -134,8 +135,7 @@
private:
EVP_MD_CTX *m_context;
- uint8_t *m_buffer;
- uint32_t m_hashLength;
+ std::vector<uint8_t> m_buffer;
};
namespace Error {
diff --git a/src/sync-full-state.cc b/src/sync-full-state.cc
index 9b7c42c..25d01c6 100644
--- a/src/sync-full-state.cc
+++ b/src/sync-full-state.cc
@@ -62,7 +62,7 @@
DigestConstPtr
FullState::getDigest ()
{
- if (m_digest == 0)
+ if (!m_digest)
{
m_digest = make_shared<Digest> ();
if (m_leaves.get<ordered> ().size () > 0)
diff --git a/src/sync-interest-table.cc b/src/sync-interest-table.cc
index 6fbf784..d966d09 100644
--- a/src/sync-interest-table.cc
+++ b/src/sync-interest-table.cc
@@ -45,7 +45,7 @@
Interest
SyncInterestTable::pop ()
{
- recursive_mutex::scoped_lock lock (m_mutex);
+ boost::recursive_mutex::scoped_lock lock (m_mutex);
if (m_table.size () == 0)
BOOST_THROW_EXCEPTION (Error::InterestTableIsEmpty ());
@@ -61,7 +61,7 @@
{
bool existent = false;
- recursive_mutex::scoped_lock lock (m_mutex);
+ boost::recursive_mutex::scoped_lock lock (m_mutex);
InterestContainer::index<named>::type::iterator it = m_table.get<named> ().find (name);
if (it != m_table.end())
{
@@ -76,14 +76,14 @@
uint32_t
SyncInterestTable::size () const
{
- recursive_mutex::scoped_lock lock (m_mutex);
+ boost::recursive_mutex::scoped_lock lock (m_mutex);
return m_table.size ();
}
bool
SyncInterestTable::remove (const string &name)
{
- recursive_mutex::scoped_lock lock (m_mutex);
+ boost::recursive_mutex::scoped_lock lock (m_mutex);
InterestContainer::index<named>::type::iterator item = m_table.get<named> ().find (name);
if (item != m_table.get<named> ().end ())
@@ -98,7 +98,7 @@
bool
SyncInterestTable::remove (DigestConstPtr digest)
{
- recursive_mutex::scoped_lock lock (m_mutex);
+ boost::recursive_mutex::scoped_lock lock (m_mutex);
InterestContainer::index<hashed>::type::iterator item = m_table.get<hashed> ().find (digest);
if (item != m_table.get<hashed> ().end ())
{
@@ -110,7 +110,7 @@
void SyncInterestTable::expireInterests ()
{
- recursive_mutex::scoped_lock lock (m_mutex);
+ boost::recursive_mutex::scoped_lock lock (m_mutex);
uint32_t count = 0;
TimeAbsolute expireTime = TIME_NOW - m_entryLifetime;
diff --git a/src/sync-logic.cc b/src/sync-logic.cc
index b346739..06d142a 100644
--- a/src/sync-logic.cc
+++ b/src/sync-logic.cc
@@ -190,7 +190,7 @@
_LOG_TRACE (hash << ", " << interestType);
- DigestPtr digest = make_shared<Digest> ();
+ DigestPtr digest = boost::make_shared<Digest> ();
istringstream is (hash);
is >> *digest;
@@ -289,7 +289,7 @@
_LOG_DEBUG("processSyncInterest");
DigestConstPtr rootDigest;
{
- recursive_mutex::scoped_lock lock (m_stateMutex);
+ boost::recursive_mutex::scoped_lock lock (m_stateMutex);
rootDigest = m_state->getDigest();
}
@@ -299,7 +299,7 @@
SyncStateMsg ssm;
{
- recursive_mutex::scoped_lock lock (m_stateMutex);
+ boost::recursive_mutex::scoped_lock lock (m_stateMutex);
ssm << (*m_state);
}
sendSyncData (name, digest, ssm);
@@ -351,7 +351,7 @@
void
SyncLogic::processSyncData (const std::string &name, DigestConstPtr digest, const char *wireData, size_t len)
{
- DiffStatePtr diffLog = make_shared<DiffState> ();
+ DiffStatePtr diffLog = boost::make_shared<DiffState> ();
bool ownInterestSatisfied = false;
try
@@ -385,7 +385,7 @@
bool updated = false;
SeqNo oldSeq;
{
- recursive_mutex::scoped_lock lock (m_stateMutex);
+ boost::recursive_mutex::scoped_lock lock (m_stateMutex);
tie (inserted, updated, oldSeq) = m_state->update (info, seq);
}
@@ -419,7 +419,7 @@
}
else if (diffLeaf->getOperation() == REMOVE)
{
- recursive_mutex::scoped_lock lock (m_stateMutex);
+ boost::recursive_mutex::scoped_lock lock (m_stateMutex);
if (m_state->remove (info))
{
diffLog->remove (info);
@@ -482,7 +482,7 @@
SyncStateMsg ssm;
{
- recursive_mutex::scoped_lock lock (m_stateMutex);
+ boost::recursive_mutex::scoped_lock lock (m_stateMutex);
ssm << (*m_state);
}
sendSyncData (name, digest, ssm);
@@ -491,9 +491,9 @@
void
SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog)
{
- DiffStatePtr fullStateLog = make_shared<DiffState> ();
+ DiffStatePtr fullStateLog = boost::make_shared<DiffState> ();
{
- recursive_mutex::scoped_lock lock (m_stateMutex);
+ boost::recursive_mutex::scoped_lock lock (m_stateMutex);
BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/)
{
fullStateLog->update (leaf->getInfo (), leaf->getSeq ());
@@ -547,7 +547,7 @@
DiffStatePtr diff;
{
//cout << "Add local names" <<endl;
- recursive_mutex::scoped_lock lock (m_stateMutex);
+ boost::recursive_mutex::scoped_lock lock (m_stateMutex);
NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
_LOG_DEBUG ("addLocalNames (): old state " << *m_state->getDigest ());
@@ -557,7 +557,7 @@
_LOG_DEBUG ("addLocalNames (): new state " << *m_state->getDigest ());
- diff = make_shared<DiffState>();
+ diff = boost::make_shared<DiffState>();
diff->update(info, seqN);
insertToDiffLog (diff);
}
@@ -571,7 +571,7 @@
{
DiffStatePtr diff;
{
- recursive_mutex::scoped_lock lock (m_stateMutex);
+ boost::recursive_mutex::scoped_lock lock (m_stateMutex);
NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
m_state->remove(info);
@@ -587,7 +587,7 @@
}
m_state->update (forwarderInfo, seqNo);
- diff = make_shared<DiffState>();
+ diff = boost::make_shared<DiffState>();
diff->remove(info);
diff->update(forwarderInfo, seqNo);
@@ -604,7 +604,7 @@
ostringstream os;
{
- recursive_mutex::scoped_lock lock (m_stateMutex);
+ boost::recursive_mutex::scoped_lock lock (m_stateMutex);
os << m_syncPrefix << "/" << *m_state->getDigest();
m_outstandingInterestName = os.str ();
@@ -694,7 +694,7 @@
// checking if our own interest got satisfied
bool satisfiedOwnInterest = false;
{
- recursive_mutex::scoped_lock lock (m_stateMutex);
+ boost::recursive_mutex::scoped_lock lock (m_stateMutex);
satisfiedOwnInterest = (m_outstandingInterestName == name);
}
@@ -713,7 +713,7 @@
SyncLogic::getRootDigest()
{
ostringstream os;
- recursive_mutex::scoped_lock lock (m_stateMutex);
+ boost::recursive_mutex::scoped_lock lock (m_stateMutex);
os << *m_state->getDigest();
return os.str();
}
@@ -721,14 +721,14 @@
size_t
SyncLogic::getNumberOfBranches () const
{
- recursive_mutex::scoped_lock lock (m_stateMutex);
+ boost::recursive_mutex::scoped_lock lock (m_stateMutex);
return m_state->getLeaves ().size ();
}
void
SyncLogic::printState () const
{
- recursive_mutex::scoped_lock lock (m_stateMutex);
+ boost::recursive_mutex::scoped_lock lock (m_stateMutex);
BOOST_FOREACH (const boost::shared_ptr<Sync::Leaf> leaf, m_state->getLeaves ())
{
@@ -739,7 +739,7 @@
std::map<std::string, bool>
SyncLogic::getBranchPrefixes() const
{
- recursive_mutex::scoped_lock lock (m_stateMutex);
+ boost::recursive_mutex::scoped_lock lock (m_stateMutex);
std::map<std::string, bool> m;
diff --git a/src/sync-logic.h b/src/sync-logic.h
index e18a01b..3b7df8c 100644
--- a/src/sync-logic.h
+++ b/src/sync-logic.h
@@ -72,7 +72,7 @@
public:
//typedef boost::function< void ( const std::string &/*prefix*/, const SeqNo &/*newSeq*/, const SeqNo &/*oldSeq*/ ) > LogicUpdateCallback;
typedef boost::function< void (const std::vector<MissingDataInfo> & ) > LogicUpdateCallback;
- typedef boost::function< void ( const std::string &/*prefix*/ ) > LogicRemoveCallback;
+ typedef boost::function< void (const std::string &/*prefix*/ ) > LogicRemoveCallback;
typedef boost::function< void (const std::string &)> LogicPerBranchCallback;
/**
diff --git a/src/sync-scheduler.cc b/src/sync-scheduler.cc
index fc0ca23..5c19f76 100644
--- a/src/sync-scheduler.cc
+++ b/src/sync-scheduler.cc
@@ -54,7 +54,7 @@
{
boost::system_time nextTime;
{
- unique_lock<mutex> lock (m_eventsMutex);
+ boost::unique_lock<boost::mutex> lock (m_eventsMutex);
while (m_threadRunning && m_events.size () == 0)
{
m_eventsCondition.wait (lock);
@@ -84,7 +84,7 @@
Event event;
{
- lock_guard<mutex> lock (m_eventsMutex);
+ boost::lock_guard<boost::mutex> lock (m_eventsMutex);
if (m_events.size () == 0)
{
@@ -110,7 +110,7 @@
void
Scheduler::schedule (const TimeDuration &reltime, Event event, uint32_t label)
{
- lock_guard<mutex> lock (m_eventsMutex);
+ boost::lock_guard<boost::mutex> lock (m_eventsMutex);
m_events.insert (LogicEvent (boost::get_system_time () + reltime, event, label));
m_eventsCondition.notify_one ();
@@ -121,7 +121,7 @@
Scheduler::cancel (uint32_t label)
{
// cout << "Canceling label " << label << " size: " << m_events.size () << endl;
- lock_guard<mutex> lock (m_eventsMutex);
+ boost::lock_guard<boost::mutex> lock (m_eventsMutex);
m_events.get<byLabel> ().erase (label);
// cout << "Canceled label " << label << " size: " << m_events.size () << endl;
diff --git a/src/sync-state.cc b/src/sync-state.cc
index d9fe538..71d664e 100644
--- a/src/sync-state.cc
+++ b/src/sync-state.cc
@@ -30,10 +30,10 @@
#include <boost/throw_exception.hpp>
#include <boost/lexical_cast.hpp>
-using namespace std;
+// using namespace std;
using namespace boost;
-typedef error_info<struct tag_errmsg, string> info_str;
+typedef error_info<struct tag_errmsg, std::string> info_str;
using namespace Sync::Error;
diff --git a/src/sync-std-name-info.cc b/src/sync-std-name-info.cc
index a6636b6..95169c4 100644
--- a/src/sync-std-name-info.cc
+++ b/src/sync-std-name-info.cc
@@ -23,7 +23,7 @@
#include "sync-std-name-info.h"
#include "boost/thread/locks.hpp"
-using namespace std;
+// using namespace std;
using namespace boost;
namespace Sync {
@@ -48,7 +48,7 @@
{
ret = NameInfoPtr (new StdNameInfo (key));
weak_ptr<const NameInfo> value (ret);
- pair<NameMap::iterator,bool> inserted =
+ std::pair<NameMap::iterator,bool> inserted =
m_names.insert (make_pair (key, value));
BOOST_ASSERT (inserted.second); // previous call has to insert value
@@ -76,7 +76,7 @@
m_names.erase (toString ());
}
-string
+std::string
StdNameInfo::toString () const
{
return m_name;