SUAPI-CppWrapper
C++WrapperforSketchUpCAPI
OptionsManager.cpp
1 //
2 // OptionsManager.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 <cassert>
32 
33 #include "SUAPI-CppWrapper/model/OptionsManager.hpp"
34 
35 namespace CW {
36 
37 OptionsManager::OptionsManager()
38 {
39 }
40 OptionsManager::OptionsManager(SUOptionsManagerRef ref): m_options_manager(ref)
41 {}
42 
43 std::vector<std::string> OptionsManager::get_provider_names() const
44 {
45  size_t num_requested = 0;
46  size_t num_returned = 0;
47  SUResult res = SUOptionsManagerGetNumOptionsProviders(m_options_manager, &num_requested);
48  std::vector<SUStringRef> refs(num_requested);
49  for(auto i = 0; i < num_requested; i++) {
50  res = SUStringCreate(&refs[i]);
51  assert(res == SU_ERROR_NONE);
52  }
53  res = SUOptionsManagerGetOptionsProviderNames(m_options_manager, num_requested, &refs[0], &num_returned);
54  assert(res == SU_ERROR_NONE); _unused(res);
55  std::vector<std::string> keys;
56  for(auto i = 0; i < refs.size(); i++) {
57  keys.push_back(String(refs[i]));
58  }
59  return keys;
60 }
61 
62 OptionsProvider OptionsManager::get_provider(const std::string& name)
63 {
64  SUOptionsProviderRef provider = SU_INVALID;
65  SUResult res = SUOptionsManagerGetOptionsProviderByName(m_options_manager, &name[0], &provider);
66  assert(res == SU_ERROR_NONE); _unused(res);
67  return OptionsProvider(provider);
68 }
69 
70 
71 //
72 // OptionsProvider
73 //
74 
75 OptionsProvider::OptionsProvider()
76 {
77 }
78 
79 OptionsProvider::OptionsProvider(SUOptionsProviderRef provider_ref):
80  m_options_provider(provider_ref)
81 { }
82 
83 std::vector<std::string> OptionsProvider::keys() const
84 {
85  std::vector<std::string> keys;
86  size_t num_requested = 0;
87  SUResult res = SUOptionsProviderGetNumKeys(m_options_provider, &num_requested);
88  assert(res == SU_ERROR_NONE);
89  if(num_requested == 0)
90  return keys;
91 
92  size_t num_returned = 0;
93  std::vector<SUStringRef> refs(num_requested);
94  for(auto i = 0; i < num_requested; i++) {
95  res = SUStringCreate(&refs[i]);
96  assert(res == SU_ERROR_NONE);
97  }
98  res = SUOptionsProviderGetKeys(m_options_provider, num_requested, &refs[0], &num_returned);
99  assert(res == SU_ERROR_NONE); _unused(res);
100 
101  for(auto i = 0; i < refs.size(); i++) {
102  keys.push_back(String(refs[i]));
103  }
104  return keys;
105 }
106 
107 
108 TypedValue OptionsProvider::get_value(std::string key) const
109 {
110  const char* key_char = key.c_str();
111  TypedValue tval;
112  SUTypedValueRef *t = tval;
113  SUResult res = SUOptionsProviderGetValue(m_options_provider, key_char, t);
114  assert(res == SU_ERROR_NONE); _unused(res);
115  return tval;
116 }
117 
118 bool OptionsProvider::set_value(std::string key, const TypedValue& tval)
119 {
120  SUTypedValueRef t = tval.ref();
121  SUResult res = SUOptionsProviderSetValue(m_options_provider, key.c_str(), t);
122  if(res == SU_ERROR_NONE)
123  return true;
124  else
125  return false;
126 }
127 
128 } /* namespace CW */
Definition: Color.hpp:34