SUAPI-CppWrapper
C++WrapperforSketchUpCAPI
Layer.cpp
1 //
2 // Layer.cpp
3 //
4 // Sketchup C++ Wrapper for C API
5 // MIT License
6 //
7 // Copyright (c) 2017 Tom Kaneko
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 
16 // The above copyright notice and this permission notice shall be included in all
17 // copies or substantial portions of the Software.
18 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 // SOFTWARE.
26 //
27 
28 // Macro for getting rid of unused variables commonly for assert checking
29 #define _unused(x) ((void)(x))
30 
31 #include "SUAPI-CppWrapper/model/Layer.hpp"
32 
33 #include <cassert>
34 #include <stdexcept>
35 
36 #include "SUAPI-CppWrapper/String.hpp"
37 
38 
39 namespace CW {
40 /***************************
41 ** Private Static Methods **
42 ****************************/
43 SULayerRef Layer::create_layer() {
44  SULayerRef layer = SU_INVALID;
45  SUResult res = SULayerCreate(&layer);
46  assert(res == SU_ERROR_NONE); _unused(res);
47  return layer;
48 }
49 
50 SULayerRef Layer::copy_reference(const Layer& other) {
51  if (other.m_attached || SUIsInvalid(other.m_entity)) {
52  return other.ref();
53  }
54  SULayerRef new_layer = create_layer();
55  return new_layer;
56 }
57 
58 /******************************
59 ** Constructors / Destructor **
60 *******************************/
62  Entity(SU_INVALID, false)
63 {}
64 
65 
66 Layer::Layer(SULayerRef layer_ref, bool attached):
67  Entity(SULayerToEntity(layer_ref), attached)
68 {}
69 
70 
71 Layer::Layer(const Layer& other):
72  Entity(SULayerToEntity(copy_reference(other)), other.m_attached)
73 {
74  if (!other.m_attached && SUIsValid(other.m_entity)) {
75  this->name(other.name());
76  // TODO: Set layer material
77  }
78 }
79 
80 
82  if (!m_attached && SUIsValid(m_entity)) {
83  SULayerRef layer = this->ref();
84  SUResult res = SULayerRelease(&layer);
85  assert(res == SU_ERROR_NONE); _unused(res);
86  }
87 }
88 
89 
90 /*******************
91 ** Public Methods **
92 ********************/
93 Layer& Layer::operator=(const Layer& other) {
94  if (!m_attached && SUIsValid(m_entity)) {
95  SULayerRef layer = this->ref();
96  SUResult res = SULayerRelease(&layer);
97  assert(res == SU_ERROR_NONE); _unused(res);
98  }
99  m_entity = SULayerToEntity(copy_reference(other));
100  if (!other.m_attached && SUIsValid(other.m_entity)) {
101  this->name(other.name());
102  // TODO: Set layer material
103  }
104  Entity::operator=(other);
105  return (*this);
106 }
107 
108 
109 SULayerRef Layer::ref() const {
110  return SULayerFromEntity(m_entity);
111 }
112 
113 Layer::operator SULayerRef() const {
114  return this->ref();
115 }
116 
117 
118 bool Layer::operator!() const {
119  if (SUIsInvalid(m_entity)) {
120  return true;
121  }
122  String name;
123  SUResult res = SULayerGetName(this->ref(), name);
124  if (res == SU_ERROR_NULL_POINTER_OUTPUT) {
125  return true;
126  }
127  return false;
128 }
129 
130 
132  if(!(*this)) {
133  return Layer(create_layer(), false);
134  }
135  Layer new_layer(create_layer(), false);
136  new_layer.name(this->name());
137  return new_layer;
138 }
139 
140 
142  if(!(*this)) {
143  throw std::logic_error("CW::Layer::name(): Layer is null");
144  }
145  String string;
146  SUResult res = SULayerGetName(this->ref(), string);
147  assert(res == SU_ERROR_NONE); _unused(res);
148  return string;
149 }
150 
151 
152 void Layer::name(const String& string) {
153  if(!(*this)) {
154  throw std::logic_error("CW::Layer::name(): Layer is null");
155  }
156  SUResult res = SULayerSetName(this->ref(), string.std_string().c_str());
157  assert(res == SU_ERROR_NONE); _unused(res);
158 }
159 
160 
161 void Layer::name(const std::string& string) {
162  if(!(*this)) {
163  throw std::logic_error("CW::Layer::name(): Layer is null");
164  }
165  SUResult res = SULayerSetName(this->ref(), string.c_str());
166  assert(res == SU_ERROR_NONE); _unused(res);
167 }
168 
169 
170 } /* namespace CW */
bool m_attached
Indicates whether the Entity has been attached to a model.
Definition: Entity.hpp:64
~Layer()
Definition: Layer.cpp:81
String name() const
Definition: Layer.cpp:141
bool attached() const
Returns true if the entity is attached to another object.
Definition: Entity.cpp:100
Layer copy() const
Definition: Layer.cpp:131
Layer()
Definition: Layer.cpp:61
bool operator!() const
Definition: Layer.cpp:118
SUEntityRef m_entity
The C SUEntityRef that this class wraps.
Definition: Entity.hpp:59
SULayerRef ref() const
Definition: Layer.cpp:109
Layer & operator=(const Layer &other)
Definition: Layer.cpp:93
Definition: Color.hpp:34
Entity & operator=(const Entity &other)
Copy assignment operator.
Definition: Entity.cpp:73