Show IMAPTypes.h syntax highlighted
#ifndef IMAP_TYPES
#define IMAP_TYPES
#include <map>
#include <vector>
#include <string>
#include <iostream>
#include <exception>
#include <sstream>
#include <time.h>
#include <sys/types.h>
#include <boost/utility.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <imap_debug.h>
namespace IMAP
{
class MessageAttributes;
using boost::shared_ptr;
class CacheError : public std::exception {
const char *msg;
public:
CacheError(const char *m) throw() { msg = m; }
const char *what() const throw() {
return msg;
}
};
/** A CacheEntry implements a reference counted, time-stamped scheme for
* items. It allows you to use assigment to copy pointers to a single item
* in a way that insures the item continues to exist until no more
* CacheEntry objects exists that have reference to it. It is a subclass of
* boost::shared_ptr.
*/
template<class T>
class CacheEntry : public shared_ptr<T>
{
public:
/* Methods to override: */
CacheEntry() : shared_ptr<T>() { time(&access_time); }
CacheEntry(CacheEntry const &r) : shared_ptr<T>(r) {
time(&access_time);
}
explicit CacheEntry(T *p) : shared_ptr<T>(p) { time(&access_time); }
template<class Y> CacheEntry(CacheEntry<Y> const & r) : shared_ptr<Y>(r)
{
time(&access_time);
}
CacheEntry& operator=(CacheEntry const & r)
{
time(&access_time);
shared_ptr<T>::operator=(r);
}
template<class Y> CacheEntry& operator=(CacheEntry<Y> const & r)
{
time(&access_time);
shared_ptr<Y>::operator=(r);
}
/* These do not update the access times, since they are called by
* general std lib containers at random pts. Use getItem */
T & operator*() const {
return shared_ptr<T>::operator*();
}
T * operator->() const {
return shared_ptr<T>::operator->();
}
T * get() const {
return shared_ptr<T>::get();
}
/**
* Check to see if this CacheEntry is managing an object.
*/
bool isEmpty() const {
boost::recursive_mutex::scoped_lock lk(const_cast<boost::recursive_mutex&>(object_mutex));
return (this->get() == NULL);
}
/**
* Get the managed item. This returns a reference to the acual
* underlying item. Changes to the item are seen by all users, so
* multi-threaded applications should implement locking when
* necessary.
*
* @returns A reference to the cached item
* @throws CacheError if an attempt is made to get a non-existent
* item.
*/
T &getItem(bool update_access_time = true) {
boost::recursive_mutex::scoped_lock lk(object_mutex);
if(update_access_time) time(&access_time);
if(this->get() != NULL)
return *(this->get());
else
throw(CacheError("Attempt to dereference an empty cache cell"));
}
/**
* Determine how long it has been since the managed object has been
* touched.
* @returns The number of seconds since the object was touched.
*/
int getIdleTime() const {
boost::recursive_mutex::scoped_lock lk(const_cast<boost::recursive_mutex&>(object_mutex));
time_t now;
time(&now);
return (now - getLastAccessTime());
}
/**
* Get the time of last touch.
* @returns The last touch time, in a format as reported by the
* time(2) system call.
*/
time_t getLastAccessTime() const {
boost::recursive_mutex::scoped_lock lk(const_cast<boost::recursive_mutex&>(object_mutex));
return access_time;
}
/**
* Update of the last access time
*/
void touch() {
boost::recursive_mutex::scoped_lock lk(object_mutex);
time(&access_time);
}
private:
time_t access_time;
boost::recursive_mutex object_mutex;
};
// Message attributes should include a UID
typedef u_int32_t IMAPUID;
typedef int IMAPFlags;
// A (possibly sorted) list of cache entries of attributes for messages
typedef std::vector<CacheEntry<MessageAttributes> > MessageList;
// A map of message UIDs to their attributes
typedef std::map<IMAPUID, CacheEntry<MessageAttributes> > MessageMap;
typedef std::vector<IMAPUID> UIDList;
// UID->thread level map
typedef std::map<IMAPUID, u_int8_t> ThreadLevelMap;
typedef std::vector<std::string> FolderList;
class IMAPError : public std::exception {
const char *msg;
public:
IMAPError(const char *m) throw() { msg = m; }
const char *what() const throw() {
return msg;
}
};
class BadFolder : public IMAPError {
public:
BadFolder(const char *m) throw() : IMAPError(m) {}
};
class InvalidCommand : public IMAPError {
public:
InvalidCommand(const char *m) throw() : IMAPError(m) {}
};
class IMAPFolder;
};
namespace Middleware {
typedef std::map<std::string, IMAP::CacheEntry<IMAP::IMAPFolder> > CacheMap;
typedef unsigned long RequestProcessorID;
};
#endif
See more files for this project here