1 /* 2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab 3 * Copyright (C) 2014-2016 - Scilab Enterprises - Clement DAVID 4 * 5 * Copyright (C) 2012 - 2016 - Scilab Enterprises 6 * 7 * This file is hereby licensed under the terms of the GNU GPL v2.0, 8 * pursuant to article 5.3.4 of the CeCILL v.2.1. 9 * This file was originally licensed under the terms of the CeCILL v2.1, 10 * and continues to be available under such terms. 11 * For more information, see the COPYING file which you should have received 12 * along with this program. 13 * 14 */ 15 16 #ifndef BASEOBJECT_HXX_ 17 #define BASEOBJECT_HXX_ 18 19 #include <vector> 20 #include <initializer_list> 21 22 #include "utilities.hxx" 23 24 namespace org_scilab_modules_scicos 25 { 26 namespace model 27 { 28 29 class BaseObject 30 { 31 public: BaseObject(kind_t k)32 explicit BaseObject(kind_t k) : 33 m_id(ScicosID()), m_kind(k), m_refCount() 34 { 35 // m_id will be set by the caller 36 } BaseObject(const BaseObject & b)37 BaseObject(const BaseObject& b) : 38 m_id(b.m_id), m_kind(b.m_kind), m_refCount() 39 { 40 } BaseObject(BaseObject && b)41 BaseObject(BaseObject&& b) : 42 m_id(b.m_id), m_kind(b.m_kind), m_refCount() 43 { 44 } BaseObject(ScicosID id,kind_t k)45 BaseObject(ScicosID id, kind_t k) : 46 m_id(id), m_kind(k), m_refCount() 47 { 48 } 49 50 BaseObject& operator=(const BaseObject&) = default; 51 operator =(BaseObject && o)52 inline BaseObject& operator=(BaseObject&& o) 53 { 54 m_id = o.m_id; 55 m_kind = o.m_kind; 56 return *this; 57 } operator <(BaseObject o) const58 inline bool operator<(BaseObject o) const 59 { 60 return m_id < o.m_id; 61 } operator ==(BaseObject o) const62 inline bool operator==(BaseObject o) const 63 { 64 return m_id == o.m_id; 65 } 66 id() const67 inline ScicosID id() const 68 { 69 return m_id; 70 } id(ScicosID _id)71 inline void id(ScicosID _id) 72 { 73 m_id = _id; 74 } 75 kind() const76 inline kind_t kind() const 77 { 78 return m_kind; 79 } 80 refCount()81 inline unsigned& refCount() 82 { 83 return m_refCount; 84 } 85 86 private: 87 /** 88 * An id is used as a reference to the current object 89 */ 90 ScicosID m_id; 91 92 /** 93 * Kind of the Object 94 */ 95 kind_t m_kind; 96 97 /** 98 * Refcount of this object 99 */ 100 unsigned m_refCount; 101 }; 102 103 /** @defgroup utilities Shared utility classes 104 * @{ 105 */ 106 107 /* 108 * Represent a graphical object 109 */ 110 struct Geometry 111 { 112 double m_x; 113 double m_y; 114 double m_width; 115 double m_height; 116 Geometryorg_scilab_modules_scicos::model::Geometry117 Geometry() : m_x(0), m_y(0), m_width(20), m_height(20) {}; Geometryorg_scilab_modules_scicos::model::Geometry118 Geometry(const Geometry& g) : m_x(g.m_x), m_y(g.m_y), m_width(g.m_width), m_height(g.m_height) {}; Geometryorg_scilab_modules_scicos::model::Geometry119 Geometry(const std::vector<double>& v) : m_x(v[0]), m_y(v[1]), m_width(v[2]), m_height(v[3]) {}; Geometryorg_scilab_modules_scicos::model::Geometry120 Geometry(std::initializer_list<double> l) : m_x(*l.begin()), m_y(*(l.begin() + 1)), m_width(*(l.begin() + 2)), m_height(*(l.begin() + 3)) {}; 121 fillorg_scilab_modules_scicos::model::Geometry122 void fill(std::vector<double>& v) const 123 { 124 v.resize(4); 125 v[0] = m_x; 126 v[1] = m_y; 127 v[2] = m_width; 128 v[3] = m_height; 129 } operator ==org_scilab_modules_scicos::model::Geometry130 bool operator==(const Geometry& g) const 131 { 132 return m_x == g.m_x && m_y == g.m_y && m_width == g.m_width && m_height == g.m_height; 133 } 134 }; 135 136 /** 137 * Per port type descriptor 138 * 139 * FIXME: should reuse Scilab datatypes descriptors 140 */ 141 struct Datatype 142 { 143 public: Datatypeorg_scilab_modules_scicos::model::Datatype144 Datatype(const Datatype& d) : 145 m_refCount(0), m_datatype_id(d.m_datatype_id), m_rows(d.m_rows), m_columns(d.m_columns) {}; Datatypeorg_scilab_modules_scicos::model::Datatype146 Datatype(const std::vector<int>& v) : 147 m_refCount(0), m_datatype_id(v[2]), m_rows(v[0]), m_columns(v[1]) {}; 148 149 // reference counter for the flyweight pattern 150 int m_refCount; 151 152 const int m_datatype_id; 153 const int m_rows; 154 const int m_columns; 155 operator ==org_scilab_modules_scicos::model::Datatype156 bool operator==(const Datatype& d) const 157 { 158 return m_datatype_id == d.m_datatype_id && m_rows == d.m_rows && m_columns == d.m_columns; 159 } 160 operator <org_scilab_modules_scicos::model::Datatype161 bool operator<(const Datatype& d) const 162 { 163 // Lexicographical order 164 return m_datatype_id < d.m_datatype_id || 165 (m_datatype_id == d.m_datatype_id && m_rows < d.m_rows) || 166 (m_datatype_id == d.m_datatype_id && m_rows == d.m_rows && m_columns < d.m_columns); 167 } 168 }; 169 170 /** @}*/ 171 172 } /* namespace model */ 173 } /* namespace org_scilab_modules_scicos */ 174 175 #endif /* BASEOBJECT_HXX_ */ 176