Тема: Зацените!
Написал C++ like реализацию AcDbDwgFiler, юзайте! если будут найдены ошибки, прошу сообщить!
/*
FILE: iostreamdwg.hpp
AUTHOR: shedever
DESCRIPTION: c++ like realisation of AcDbDwgFiler
DATE: 27.1.2007
NAMESPACE: dwginout
CLASSES: istreamdwg, ostreamdwg
THROWS: objectarx_error
*/
#pragma once
#ifndef DWG_SAVE_FUNCTIONS_H
#define DWG_SAVE_FUNCTIONS_H
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <deque>
#include <string>
#include <iterator>
#include <boost/format.hpp>
namespace dwginout
{
class objectarx_error: public std::exception
{
public:
objectarx_error(Acad::ErrorStatus e,
const std::string& msg,
const std::string& file,
long line)
{
this->msg_ = boost::str(boost::format("%1% (Error: %2%") % msg %
acadErrorStatusText(e));
}
inline const char* what() const
{
return msg_.c_str();
}
private:
std::string msg_;
};
namespace dwginout
{
template<typename Tsize = Adesk::UInt16>
class istreamdwg
{
AcDbDwgFiler* filer_;
public:
typename Tsize count_type;
explicit istreamdwg(AcDbDwgFiler* filer): filer_(filer)
{
if (filer == 0)
THROW_ERROR("Ошибка при чтении объекта");
this->check_status();
}
template<typename Conteiner> inline
istreamdwg& operator >> (std::front_insert_iterator<Conteiner> & iter)
{
typedef std::front_insert_iterator<Conteiner> iter_type;
return insert_value<iter_type, Conteiner::value_type>(iter);
}
template<typename Conteiner> inline
istreamdwg& operator >> (std::back_insert_iterator<Conteiner> & iter)
{
typedef std::back_insert_iterator<Conteiner> iter_type;
return insert_value<iter_type, Conteiner::value_type>(iter);
}
template<typename T> inline
istreamdwg& operator >> (std::vector<T> & cont)
{
typedef std::back_insert_iterator<std::vector<T> > iter_type;
return insert_value<iter_type,T>(std::back_inserter(cont));
}
template<typename T> inline
istreamdwg& operator >> (std::list<T> & cont)
{
typedef std::back_insert_iterator<std::list<T> > iter_type;
return insert_value<iter_type,T>(std::back_inserter(cont));
}
template<typename T> inline
istreamdwg& operator >> (std::set<T> & cont)
{
typedef std::insert_iterator<std::set<T> > iter_type;
return insert_value<iter_type,T>(std::inserter(cont, cont.end()));
}
template<typename T> inline
istreamdwg& operator >> (std::multiset<T> & cont)
{
typedef std::insert_iterator<std::multiset<T> > iter_type;
return insert_value<iter_type,T>(std::inserter(cont, cont.end()));
}
template<typename T> inline
istreamdwg& operator >> (std::deque<T> & cont)
{
typedef std::back_insert_iterator<std::deque<T> > iter_type;
return insert_value<iter_type,T>(std::back_inserter(cont));
}
template<typename key_type, typename val_type> inline
istreamdwg& operator >> (std::map<key_type, val_type> & cont)
{
typedef std::insert_iterator<std::map<key_type, val_type> > iter_type;
typedef std::pair<key_type,val_type> value_type;
return insert_value<iter_type,value_type>(std::inserter(cont, cont.end()));
}
template<typename key_type, typename val_type>
istreamdwg& operator >> (std::multimap<key_type, val_type> & cont)
{
Tsize count;
for ( *this >> count; count >=0; --count)
{
key_type k;
*this >> k;
Tsize sub_count;
for ( *this >> sub_count; sub_count >= 0; --sub_count)
{
val_type v;
*this >> v;
cont.insert( std::make_pair(k , v));
}
}
return *this;
}
inline istreamdwg& operator >> (int& val)
{
BOOST_STATIC_ASSERT(sizeof(int) == sizeof(Adesk::Int32));
return *this >> reinterpret_cast<Adesk::Int32&>(val);
}
template<typename _T1, typename _T2> inline
istreamdwg& operator >> (std::pair<_T1, _T2> & val)
{
*this >> val.first;
this->check_status();
*this >> val.second;
this->check_status();
return *this;
}
inline istreamdwg& operator >> (AcDbHardOwnershipId*& val)
{
this->filer_->readHardOwnershipId(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (AcDbSoftPointerId& val)
{
this->filer_->readSoftPointerId(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (AcGePoint3d& val)
{
this->filer_->readPoint3d(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (AcGePoint2d& val)
{
this->filer_->readPoint2d(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (char& val)
{
this->filer_->readChar(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (bool& val)
{
this->filer_->readBool(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (AcGeVector2d& val)
{
this->filer_->readVector2d(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (AcGeVector3d& val)
{
this->filer_->readVector3d(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (AcGeScale3d& val)
{
this->filer_->readScale3d(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (Adesk::UInt16& val)
{
this->filer_->readUInt16(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (Adesk::UInt32& val)
{
this->filer_->readUInt32(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (Adesk::Int16& val)
{
this->filer_->readInt16(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (Adesk::Int32& val)
{
this->filer_->readInt32(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (AcDbObjectId& val)
{
this->filer_->readSoftPointerId(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (std::string& val)
{
char* str;
*this >> str;
val = str;
return *this;
}
inline istreamdwg& operator >> (double& val)
{
this->filer_->readDouble(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (char*& val)
{
this->filer_->readString(&val);
this->check_status();
return *this;
}
inline void check_status()
{
if (filer_->filerStatus() != Acad::eOk)
THROW_OBJECTARX(filer_->filerStatus(), "Ошибка при чтении объекта");
}
template<typename iter_type, typename val_type> inline
istreamdwg& insert_value(iter_type & iter)
{
Tsize count;
for ( *this >> count; count >=0; --count, ++iter)
{
val_type val;
*this >> val;
iter = val;
}
return *this;
}
};
template<typename Tsize = Adesk::UInt16>
class ostreamdwg
{
AcDbDwgFiler* filer_;
public:
typename Tsize count_type;
explicit ostreamdwg(AcDbDwgFiler* filer): filer_(filer)
{
if (filer == 0)
THROW_ERROR("Ошибка при чтении объекта");
this->check_status();
}
template<typename T> inline
ostreamdwg& operator << (const std::vector<T> & cont)
{
return this->write_range(cont.begin(), cont.end());
}
template<typename T> inline
ostreamdwg& operator << (const std::list<T> & cont)
{
return this->write_range(cont.begin(), cont.end());
}
template<typename T> inline
ostreamdwg& operator << (const std::set<T> & cont)
{
return this->write_range(cont.begin(), cont.end());
}
template<typename T> inline
ostreamdwg& operator << (const std::multiset<T> & cont)
{
return this->write_range(cont.begin(), cont.end());
}
template<typename T> inline
ostreamdwg& operator << (const std::deque<T> & cont)
{
return this->write_range(cont.begin(), cont.end());
}
template<typename key_type, typename val_type> inline
ostreamdwg& operator << (const std::map<key_type, val_type> & cont)
{
return this->write_range(cont.begin(), cont.end());
}
template<typename key_type, typename val_type>
ostreamdwg& operator << (const std::multimap<key_type, val_type> & cont)
{
typedef std::multimap<key_type, val_type>::const_iterator iter_type;
key_type last_key;
for (iter_type itb = cont.begin(), ite = cont.end();
itb != ite; ++itb)
{
if ( itb->first == last_key)
{
*this << reinterpret_cast<Tsize>(std::distance(
cont.lower_bound(last_key), cont.upper_bound(last_key) ));
for ( ; first != last; ++first)
*this << first->second;
}
else
{
last_key = itb->first;
*this->last_key;
}
}
return *this;
}
inline ostreamdwg& operator << (int val)
{
BOOST_STATIC_ASSERT(sizeof(int) == sizeof(Adesk::Int32));
return *this << reinterpret_cast<Adesk::Int32>(val);
}
template<typename _T1, typename _T2> inline
ostreamdwg& operator << (const std::pair<_T1, _T2> & val)
{
*this << val.first;
this->check_status();
*this << val.second;
this->check_status();
return *this;
}
inline ostreamdwg& operator << (AcDbHardOwnershipId* val)
{
this->filer_->writeHardOwnershipId(val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (AcDbSoftPointerId& val)
{
this->filer_->readSoftPointerId(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (AcGePoint3d& val)
{
this->filer_->readPoint3d(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (AcGePoint2d& val)
{
this->filer_->readPoint2d(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (char& val)
{
this->filer_->readChar(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (bool& val)
{
this->filer_->readBool(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (AcGeVector2d& val)
{
this->filer_->readVector2d(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (AcGeVector3d& val)
{
this->filer_->readVector3d(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (AcGeScale3d& val)
{
this->filer_->readScale3d(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (Adesk::UInt8& val)
{
this->filer_->readUInt8(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (Adesk::UInt16& val)
{
this->filer_->readUInt16(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (Adesk::UInt32& val)
{
this->filer_->readUInt32(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (Adesk::Int16& val)
{
this->filer_->readInt16(&val);
this->check_status();
return *this;
}
inline istreamdwg& operator >> (Adesk::Int32& val)
{
this->filer_->readInt32(&val);
this->check_status();
return *this;
}
inline ostreamdwg& operator << (AcDbObjectId val)
{
this->filer_->writeSoftPointerId(val);
this->check_status();
return *this;
}
inline ostreamdwg& operator << (const std::string& val)
{
return *this << val.c_str();
}
inline ostreamdwg& operator << (double val)
{
this->filer_->writeDouble(val);
this->check_status();
return *this;
}
inline ostreamdwg& operator << (const char* val)
{
this->filer_->writeString(val);
this->check_status();
return *this;
}
inline void check_status()
{
if (filer_->filerStatus() != Acad::eOk)
THROW_OBJECTARX(filer_->filerStatus(), "Ошибка при чтении объекта");
}
template<typename iter_type> inline
ostreamdwg& write_range(iter_type first, iter_type last)
{
*this << reinterpret_cast<Tsize>(std::distance(first,last));
for ( ; first != last; ++first)
*this << *first;
}
};
}
#endif