SUAPI-CppWrapper
C++WrapperforSketchUpCAPI
Opening.cpp
1 //
2 // Opening.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/Opening.hpp"
32 
33 #include <cassert>
34 #include <stdexcept>
35 
36 namespace CW {
37 /******************************
38 ** Constructors / Destructor **
39 *******************************/
41  : m_opening(std::shared_ptr<SUOpeningRef>SU_INVALID)
42 {}
43 
44 
45 Opening::Opening(SUOpeningRef ref)
46  : m_opening(std::shared_ptr<SUOpeningRef>(&ref))
47 {}
48 
49 
50 Opening::Opening(std::shared_ptr<SUOpeningRef> opening)
51  : m_opening(opening)
52 {}
53 
54 
56  : m_opening(!other ? std::shared_ptr<SUOpeningRef>SU_INVALID : other.m_opening)
57 {}
58 
59 
61 {
62  if (!!(*this) && m_opening.use_count() < 2) {
63  SU_RESULT res = SUOpeningRelease(m_opening.get());
64  assert (res != SU_ERROR_INVALID_INPUT);
65  }
66 }
67 
68 
69 bool Opening::operator!() const {
70  return SUIsInvalid(*m_opening.get());
71 }
72 
73 
75  // Destroy this object if there are no other references
76  if (!!(*this) && m_opening.use_count() < 2) {
77  SU_RESULT res = SUOpeningRelease(m_opening.get());
78  if (res == SU_ERROR_INVALID_INPUT) {
79  throw std::logic_error("CW::Opening::operator=(): Opening reference is invalid. The code may point to dereferenced pointer.");
80  }
81  }
82  m_opening = other.m_opening;
83  return (*this);
84 }
85 
86 
87 /*******************
88 ** Public Methods **
89 ********************/
90 
91 size_t Opening::get_num_points() const {
92  if (!(*this)) {
93  throw std::logic_error("CW::Opening::get_num_points(): Opening is invalid");
94  }
95  size_t count = 0;
96  SU_RESULT res = SUOpeningGetNumPoints(*m_opening.get(), &count);
97  if (res == SU_ERROR_NULL_POINTER_OUTPUT) {
98  throw std::logic_error("CW::Opening::get_num_points(): points or count is NULL");
99  }
100  assert(res == SU_ERROR_NONE); _unused(res);
101  return count;
102 }
103 
104 
105 std::vector<Point3D> Opening::get_points() const {
106  if (!(*this)) {
107  throw std::logic_error("CW::Opening::get_points(): Opening is invalid");
108  }
109  size_t count = this->get_num_points();
110  std::vector<SUPoint3D> points_array(count, SU_INVALID);
111  size_t count_returned = 0;
112  SU_RESULT res = SUOpeningGetPoints(*m_opening.get(), count, points_array.data(), &count_returned);
113  if (res == SU_ERROR_NULL_POINTER_OUTPUT) {
114  throw std::logic_error("CW::Opening::get_points(): points or count is NULL");
115  }
116  assert(res == SU_ERROR_NONE); _unused(res);
117  assert(count_returned == count);
118  std::vector<Point3D> points;
119  points.reserve(count_returned);
120  for (SUPoint3D p : points_array) {
121  points.push_back(p);
122  }
123  return points;
124 }
125 
126 } // end namespace CW
std::vector< Point3D > get_points() const
Definition: Opening.cpp:105
STL namespace.
size_t get_num_points() const
Definition: Opening.cpp:91
Definition: Color.hpp:34
Opening & operator=(const Opening &other)
Definition: Opening.cpp:74