Commit 24f96b7c authored by Isaak Lim's avatar Isaak Lim
Browse files

use serialize and deserialize functionality provided by Serializers.hh (thanks to Max Lyon)

git-svn-id: http://www.openvolumemesh.org/svnrepo/OpenVolumeMesh/trunk@290 66977474-1d4b-4f09-8fe9-267525286df2
parent e08e0d1c
...@@ -54,6 +54,8 @@ ...@@ -54,6 +54,8 @@
#include "OpenVolumeMeshBaseProperty.hh" #include "OpenVolumeMeshBaseProperty.hh"
#include "Serializers.hh"
namespace OpenVolumeMesh { namespace OpenVolumeMesh {
//== CLASS DEFINITION ========================================================= //== CLASS DEFINITION =========================================================
...@@ -146,16 +148,14 @@ public: ...@@ -146,16 +148,14 @@ public:
virtual void serialize(std::ostream& _ostr) const { virtual void serialize(std::ostream& _ostr) const {
for(typename vector_type::const_iterator it = data_.begin(); for(typename vector_type::const_iterator it = data_.begin();
it != data_.end(); ++it) { it != data_.end(); ++it) {
_ostr << *it << std::endl; OpenVolumeMesh::serialize(_ostr, *it) << std::endl;
} }
} }
// Function to deserialize a property // Function to deserialize a property
virtual void deserialize(std::istream& _istr) { virtual void deserialize(std::istream& _istr) {
for(unsigned int i = 0; i < n_elements(); ++i) { for(unsigned int i = 0; i < n_elements(); ++i) {
value_type val; OpenVolumeMesh::deserialize(_istr, data_[i]);
_istr >> val;
data_[i] = val;
} }
} }
...@@ -240,98 +240,98 @@ public: ...@@ -240,98 +240,98 @@ public:
template <class PropT, class HandleT> friend class PropertyPtr; template <class PropT, class HandleT> friend class PropertyPtr;
typedef std::vector<bool> vector_type; typedef std::vector<bool> vector_type;
typedef bool value_type; typedef bool value_type;
typedef vector_type::reference reference; typedef vector_type::reference reference;
typedef vector_type::const_reference const_reference; typedef vector_type::const_reference const_reference;
public: public:
OpenVolumeMeshPropertyT(const std::string& _name = "<unknown>", const bool _def = bool()) : OpenVolumeMeshPropertyT(const std::string& _name = "<unknown>", const bool _def = bool()) :
OpenVolumeMeshBaseProperty(_name), OpenVolumeMeshBaseProperty(_name),
def_(_def) { def_(_def) {
} }
public: public:
// inherited from OpenVolumeMeshBaseProperty // inherited from OpenVolumeMeshBaseProperty
virtual void reserve(size_t _n) { virtual void reserve(size_t _n) {
data_.reserve(_n); data_.reserve(_n);
} }
virtual void resize(size_t _n) { virtual void resize(size_t _n) {
data_.resize(_n, def_); data_.resize(_n, def_);
} }
virtual void clear() { virtual void clear() {
data_.clear(); data_.clear();
vector_type().swap(data_); vector_type().swap(data_);
} }
virtual void push_back() { virtual void push_back() {
data_.push_back(def_); data_.push_back(def_);
} }
virtual void swap(size_t _i0, size_t _i1) { virtual void swap(size_t _i0, size_t _i1) {
bool t(data_[_i0]); bool t(data_[_i0]);
data_[_i0] = data_[_i1]; data_[_i0] = data_[_i1];
data_[_i1] = t; data_[_i1] = t;
} }
void delete_element(size_t _idx) { void delete_element(size_t _idx) {
data_.erase(data_.begin() + _idx); data_.erase(data_.begin() + _idx);
} }
public: public:
virtual size_t n_elements() const { virtual size_t n_elements() const {
return data_.size(); return data_.size();
} }
virtual size_t element_size() const { virtual size_t element_size() const {
return OpenVolumeMeshBaseProperty::UnknownSize; return OpenVolumeMeshBaseProperty::UnknownSize;
} }
virtual size_t size_of() const { virtual size_t size_of() const {
return size_of(n_elements()); return size_of(n_elements());
} }
virtual size_t size_of(size_t _n_elem) const { virtual size_t size_of(size_t _n_elem) const {
return _n_elem / 8 + ((_n_elem % 8) != 0); return _n_elem / 8 + ((_n_elem % 8) != 0);
} }
// Function to serialize a property // Function to serialize a property
virtual void serialize(std::ostream& _ostr) const { virtual void serialize(std::ostream& _ostr) const {
for(vector_type::const_iterator it = data_.begin(); for(vector_type::const_iterator it = data_.begin();
it != data_.end(); ++it) { it != data_.end(); ++it) {
_ostr << *it << std::endl; OpenVolumeMesh::serialize(_ostr, *it) << std::endl;
}
} }
}
// Function to deserialize a property // Function to deserialize a property
virtual void deserialize(std::istream& _istr) { virtual void deserialize(std::istream& _istr) {
for(unsigned int i = 0; i < n_elements(); ++i) { for(unsigned int i = 0; i < n_elements(); ++i) {
value_type val; value_type val;
_istr >> val; OpenVolumeMesh::deserialize(_istr, val);
data_[i] = val; data_[i] = val;
}
} }
}
public: public:
/// Access the i'th element. No range check is performed! /// Access the i'th element. No range check is performed!
reference operator[](int _idx) { reference operator[](int _idx) {
assert(size_t(_idx) < data_.size()); assert(size_t(_idx) < data_.size());
return data_[_idx]; return data_[_idx];
} }
/// Const access to the i'th element. No range check is performed! /// Const access to the i'th element. No range check is performed!
const_reference operator[](int _idx) const { const_reference operator[](int _idx) const {
assert(size_t(_idx) < data_.size()); assert(size_t(_idx) < data_.size());
return data_[_idx]; return data_[_idx];
} }
/// Make a copy of self. /// Make a copy of self.
OpenVolumeMeshPropertyT<bool>* clone() const { OpenVolumeMeshPropertyT<bool>* clone() const {
OpenVolumeMeshPropertyT<bool>* p = new OpenVolumeMeshPropertyT<bool> ( OpenVolumeMeshPropertyT<bool>* p = new OpenVolumeMeshPropertyT<bool> (
*this); *this);
return p; return p;
} }
vector_type::const_iterator begin() const { return data_.begin(); } vector_type::const_iterator begin() const { return data_.begin(); }
vector_type::iterator begin() { return data_.begin(); } vector_type::iterator begin() { return data_.begin(); }
...@@ -359,9 +359,9 @@ protected: ...@@ -359,9 +359,9 @@ protected:
private: private:
vector_type data_; vector_type data_;
const bool def_; const bool def_;
}; };
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
...@@ -376,94 +376,109 @@ public: ...@@ -376,94 +376,109 @@ public:
template <class PropT, class HandleT> friend class PropertyPtr; template <class PropT, class HandleT> friend class PropertyPtr;
typedef std::string Value; typedef std::string Value;
typedef std::vector<std::string> vector_type; typedef std::vector<std::string> vector_type;
typedef std::string value_type; typedef std::string value_type;
typedef vector_type::reference reference; typedef vector_type::reference reference;
typedef vector_type::const_reference const_reference; typedef vector_type::const_reference const_reference;
public: public:
OpenVolumeMeshPropertyT(const std::string& _name = "<unknown>", const std::string& _def = "") : OpenVolumeMeshPropertyT(const std::string& _name = "<unknown>", const std::string& _def = "") :
OpenVolumeMeshBaseProperty(_name), OpenVolumeMeshBaseProperty(_name),
def_(_def) { def_(_def) {
} }
public: public:
// inherited from OpenVolumeMeshBaseProperty // inherited from OpenVolumeMeshBaseProperty
virtual void reserve(size_t _n) { virtual void reserve(size_t _n) {
data_.reserve(_n); data_.reserve(_n);
} }
virtual void resize(size_t _n) { virtual void resize(size_t _n) {
data_.resize(_n, def_); data_.resize(_n, def_);
} }
virtual void clear() { virtual void clear() {
data_.clear(); data_.clear();
vector_type().swap(data_); vector_type().swap(data_);
} }
virtual void push_back() { virtual void push_back() {
data_.push_back(def_); data_.push_back(def_);
} }
virtual void swap(size_t _i0, size_t _i1) { virtual void swap(size_t _i0, size_t _i1) {
std::swap(data_[_i0], data_[_i1]); std::swap(data_[_i0], data_[_i1]);
} }
virtual void delete_element(size_t _idx) { virtual void delete_element(size_t _idx) {
data_.erase(data_.begin() + _idx); data_.erase(data_.begin() + _idx);
} }
public: public:
virtual size_t n_elements() const { virtual size_t n_elements() const {
return data_.size(); return data_.size();
} }
virtual size_t element_size() const { virtual size_t element_size() const {
return OpenVolumeMeshBaseProperty::UnknownSize; return OpenVolumeMeshBaseProperty::UnknownSize;
} }
virtual size_t size_of() const { virtual size_t size_of() const {
return sizeof(data_); return sizeof(data_);
} }
virtual size_t size_of(size_t /* _n_elem */) const { virtual size_t size_of(size_t /* _n_elem */) const {
return OpenVolumeMeshBaseProperty::UnknownSize; return OpenVolumeMeshBaseProperty::UnknownSize;
} }
virtual void stats(std::ostream& _ostr) const { virtual void stats(std::ostream& _ostr) const {
for(vector_type::const_iterator it = data_.begin(); for(vector_type::const_iterator it = data_.begin();
it != data_.end(); ++it) { it != data_.end(); ++it) {
_ostr << *it << " "; _ostr << *it << " ";
} }
} }
// Function to serialize a property
virtual void serialize(std::ostream& _ostr) const {
for(vector_type::const_iterator it = data_.begin();
it != data_.end(); ++it) {
OpenVolumeMesh::serialize(_ostr, *it) << std::endl;
}
}
// Function to deserialize a property
virtual void deserialize(std::istream& _istr) {
for(unsigned int i = 0; i < n_elements(); ++i) {
OpenVolumeMesh::deserialize(_istr, data_[i]);
}
}
public: public:
const value_type* data() const { const value_type* data() const {
if (data_.empty()) if (data_.empty())
return 0; return 0;
return (value_type*) &data_[0]; return (value_type*) &data_[0];
} }
/// Access the i'th element. No range check is performed! /// Access the i'th element. No range check is performed!
reference operator[](int _idx) { reference operator[](int _idx) {
assert(size_t(_idx) < data_.size()); assert(size_t(_idx) < data_.size());
return ((value_type*) &data_[0])[_idx]; return ((value_type*) &data_[0])[_idx];
} }
/// Const access the i'th element. No range check is performed! /// Const access the i'th element. No range check is performed!
const_reference operator[](int _idx) const { const_reference operator[](int _idx) const {
assert(size_t(_idx) < data_.size()); assert(size_t(_idx) < data_.size());
return ((value_type*) &data_[0])[_idx]; return ((value_type*) &data_[0])[_idx];
} }
OpenVolumeMeshPropertyT<value_type>* clone() const { OpenVolumeMeshPropertyT<value_type>* clone() const {
OpenVolumeMeshPropertyT<value_type>* p = new OpenVolumeMeshPropertyT< OpenVolumeMeshPropertyT<value_type>* p = new OpenVolumeMeshPropertyT<
value_type> (*this); value_type> (*this);
return p; return p;
} }
vector_type::const_iterator begin() const { return data_.begin(); } vector_type::const_iterator begin() const { return data_.begin(); }
vector_type::iterator begin() { return data_.begin(); } vector_type::iterator begin() { return data_.begin(); }
...@@ -491,9 +506,9 @@ protected: ...@@ -491,9 +506,9 @@ protected:
private: private:
vector_type data_; vector_type data_;
const std::string def_; const std::string def_;
}; };
} // Namespace OpenVolumeMesh } // Namespace OpenVolumeMesh
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment