88 lines
3.3 KiB
C++
88 lines
3.3 KiB
C++
|
#ifndef slic3r_ObjectID_hpp_
|
||
|
#define slic3r_ObjectID_hpp_
|
||
|
|
||
|
#include <cereal/types/polymorphic.hpp>
|
||
|
#include <cereal/types/map.hpp>
|
||
|
#include <cereal/types/string.hpp>
|
||
|
#include <cereal/types/vector.hpp>
|
||
|
#include <cereal/archives/binary.hpp>
|
||
|
|
||
|
namespace Slic3r {
|
||
|
|
||
|
// Unique identifier of a mutable object accross the application.
|
||
|
// Used to synchronize the front end (UI) with the back end (BackgroundSlicingProcess / Print / PrintObject)
|
||
|
// (for Model, ModelObject, ModelVolume, ModelInstance or ModelMaterial classes)
|
||
|
// and to serialize / deserialize an object onto the Undo / Redo stack.
|
||
|
// Valid IDs are strictly positive (non zero).
|
||
|
// It is declared as an object, as some compilers (notably msvcc) consider a typedef size_t equivalent to size_t
|
||
|
// for parameter overload.
|
||
|
class ObjectID
|
||
|
{
|
||
|
public:
|
||
|
ObjectID(size_t id) : id(id) {}
|
||
|
|
||
|
bool operator==(const ObjectID &rhs) const { return this->id == rhs.id; }
|
||
|
bool operator!=(const ObjectID &rhs) const { return this->id != rhs.id; }
|
||
|
bool operator< (const ObjectID &rhs) const { return this->id < rhs.id; }
|
||
|
bool operator> (const ObjectID &rhs) const { return this->id > rhs.id; }
|
||
|
bool operator<=(const ObjectID &rhs) const { return this->id <= rhs.id; }
|
||
|
bool operator>=(const ObjectID &rhs) const { return this->id >= rhs.id; }
|
||
|
|
||
|
bool valid() const { return id != 0; }
|
||
|
|
||
|
size_t id;
|
||
|
|
||
|
private:
|
||
|
ObjectID() {}
|
||
|
|
||
|
friend class cereal::access;
|
||
|
template<class Archive> void serialize(Archive &ar) { ar(id); }
|
||
|
};
|
||
|
|
||
|
// Base for Model, ModelObject, ModelVolume, ModelInstance or ModelMaterial to provide a unique ID
|
||
|
// to synchronize the front end (UI) with the back end (BackgroundSlicingProcess / Print / PrintObject).
|
||
|
// Achtung! The s_last_id counter is not thread safe, so it is expected, that the ObjectBase derived instances
|
||
|
// are only instantiated from the main thread.
|
||
|
class ObjectBase
|
||
|
{
|
||
|
public:
|
||
|
ObjectID id() const { return m_id; }
|
||
|
|
||
|
protected:
|
||
|
// Constructors to be only called by derived classes.
|
||
|
// Default constructor to assign a unique ID.
|
||
|
ObjectBase() : m_id(generate_new_id()) {}
|
||
|
// Constructor with ignored int parameter to assign an invalid ID, to be replaced
|
||
|
// by an existing ID copied from elsewhere.
|
||
|
ObjectBase(int) : m_id(ObjectID(0)) {}
|
||
|
// The class tree will have virtual tables and type information.
|
||
|
virtual ~ObjectBase() {}
|
||
|
|
||
|
// Use with caution!
|
||
|
void set_new_unique_id() { m_id = generate_new_id(); }
|
||
|
void set_invalid_id() { m_id = 0; }
|
||
|
// Use with caution!
|
||
|
void copy_id(const ObjectBase &rhs) { m_id = rhs.id(); }
|
||
|
|
||
|
// Override this method if a ObjectBase derived class owns other ObjectBase derived instances.
|
||
|
void assign_new_unique_ids_recursive() { this->set_new_unique_id(); }
|
||
|
|
||
|
private:
|
||
|
ObjectID m_id;
|
||
|
|
||
|
static inline ObjectID generate_new_id() { return ObjectID(++ s_last_id); }
|
||
|
static size_t s_last_id;
|
||
|
|
||
|
friend ObjectID wipe_tower_object_id();
|
||
|
friend ObjectID wipe_tower_instance_id();
|
||
|
|
||
|
friend class cereal::access;
|
||
|
template<class Archive> void serialize(Archive &ar) { ar(m_id); }
|
||
|
ObjectBase(const ObjectID id) : m_id(id) {}
|
||
|
template<class Archive> static void load_and_construct(Archive & ar, cereal::construct<ObjectBase> &construct) { ObjectID id; ar(id); construct(id); }
|
||
|
};
|
||
|
|
||
|
} // namespace Slic3r
|
||
|
|
||
|
#endif /* slic3r_ObjectID_hpp_ */
|