OCCT-Light 0.1
C ABI and C++ veneer for multi-language CAD workflows
Loading...
Searching...
No Matches
core.hpp
Go to the documentation of this file.
1// Copyright (c) 2026 Capgemini Engineering Research and Development.
2//
3// This file is part of OCCT-Light software library.
4//
5// This library is free software; you can redistribute it and/or modify it under
6// the terms of the GNU Affero General Public License version 3 as published
7// by the Free Software Foundation, with an option to use any later version.
8// Consult the file LICENSE_AGPL_30.txt included in OCCT-Light distribution
9// for complete text of the license and disclaimer of any warranty.
10//
11// Alternatively, this file may be used under the terms of a commercial
12// license or contractual agreement.
13//
14// SPDX-License-Identifier: AGPL-3.0-or-later
15
24#ifndef OCCTL_HPP_CORE_HPP
25#define OCCTL_HPP_CORE_HPP
26
27#include <occtl/occtl_core.h>
28
29#include <cstdint>
30#include <stdexcept>
31#include <string>
32#include <string_view>
33#include <tuple>
34#include <utility>
35
36namespace occtl
37{
38
44class Error : public std::runtime_error
45{
46public:
48 static Error from_last(const ::occtl_status_t theFallback) noexcept
49 {
50 const ::occtl_error_t* anErr = ::occtl_error_last();
51 if (anErr != nullptr && anErr->status != OCCTL_OK)
52 {
53 return Error(anErr->status,
54 anErr->message != nullptr ? anErr->message : "",
55 anErr->source.bits,
56 anErr->extended);
57 }
58 return Error(theFallback, ::occtl_status_to_string(theFallback), 0, 0);
59 }
60
61 Error(const ::occtl_status_t theCode,
62 std::string theMessage,
63 const std::uint64_t theSourceUidBits,
64 const std::uint32_t theExtended) noexcept
65 : std::runtime_error(std::move(theMessage)),
66 myCode(theCode),
67 mySourceUidBits(theSourceUidBits),
68 myExtended(theExtended)
69 {
70 }
71
72 ::occtl_status_t code() const noexcept { return myCode; }
73
74 std::uint64_t source_uid_bits() const noexcept { return mySourceUidBits; }
75
76 std::uint32_t extended() const noexcept { return myExtended; }
77
78private:
79 ::occtl_status_t myCode;
80 std::uint64_t mySourceUidBits;
81 std::uint32_t myExtended;
82};
83
85inline void check(const ::occtl_status_t theStatus)
86{
87 if (theStatus != OCCTL_OK)
88 {
89 throw Error::from_last(theStatus);
90 }
91}
92
94inline std::tuple<std::uint32_t, std::uint32_t, std::uint32_t> version() noexcept
95{
96 std::uint32_t aMajor = 0;
97 std::uint32_t aMinor = 0;
98 std::uint32_t aPatch = 0;
99 ::occtl_runtime_version(&aMajor, &aMinor, &aPatch);
100 return {aMajor, aMinor, aPatch};
101}
102
104inline std::uint32_t abi_version() noexcept
105{
106 return ::occtl_runtime_abi_version();
107}
108
110inline std::string_view occt_version() noexcept
111{
112 const char* aStr = ::occtl_runtime_occt_version();
113 return aStr != nullptr ? std::string_view{aStr} : std::string_view{};
114}
115
122
125inline int32_t uid_equal(const ::occtl_uid_t theA, const ::occtl_uid_t theB) noexcept
126{
127 return ::occtl_uid_equal(theA, theB);
128}
129
136{
137public:
140 {
141 check(::occtl_runtime_init(nullptr));
142 myInitialised = true;
143 }
144
146 explicit Runtime(const ::occtl_runtime_init_info_t& theInfo)
147 {
148 check(::occtl_runtime_init(&theInfo));
149 myInitialised = true;
150 }
151
155 ~Runtime() noexcept
156 {
157 if (myInitialised)
158 {
160 }
161 }
162
163 Runtime(const Runtime&) = delete;
164 Runtime& operator=(const Runtime&) = delete;
165
166 Runtime(Runtime&& theOther) noexcept
167 : myInitialised(theOther.myInitialised)
168 {
169 theOther.myInitialised = false;
170 }
171
172 Runtime& operator=(Runtime&& theOther) noexcept
173 {
174 if (this != &theOther)
175 {
176 if (myInitialised)
177 {
179 }
180 myInitialised = theOther.myInitialised;
181 theOther.myInitialised = false;
182 }
183 return *this;
184 }
185
186private:
187 bool myInitialised = false;
188};
189
190} // namespace occtl
191
192#endif // OCCTL_HPP_CORE_HPP
Exception thrown by the veneer on any non-OK status code.
Definition core.hpp:45
static Error from_last(const ::occtl_status_t theFallback) noexcept
Build from the current thread-local error state.
Definition core.hpp:48
RAII handle for the process-wide runtime.
Definition core.hpp:136
Runtime(const ::occtl_runtime_init_info_t &theInfo)
Initialise with explicit options.
Definition core.hpp:146
~Runtime() noexcept
Shuts down the runtime. Silently swallows any shutdown failure because this is called from destructor...
Definition core.hpp:155
Runtime()
Initialise with defaults.
Definition core.hpp:139
int32_t uid_equal(const ::occtl_uid_t theA, const ::occtl_uid_t theB) noexcept
Tests two UIDs for bitwise equality.
Definition core.hpp:125
std::uint32_t abi_version() noexcept
Returns the runtime ABI version.
Definition core.hpp:104
std::tuple< std::uint32_t, std::uint32_t, std::uint32_t > version() noexcept
Returns the runtime SemVer as a (major, minor, patch) tuple.
Definition core.hpp:94
void check(const ::occtl_status_t theStatus)
Throw on non-OK; otherwise a no-op.
Definition core.hpp:85
std::string_view occt_version() noexcept
Returns the OCCT version OCCT-Light was built against, as a string view.
Definition core.hpp:110
void init_runtime_info(::occtl_runtime_init_info_t *theInfo)
Initialises an occtl_runtime_init_info_t to defaults and throws on a non-OK status (which would mean ...
Definition core.hpp:118
OCCT-Light: core public API.
const char * occtl_status_to_string(occtl_status_t status)
occtl_status_t occtl_runtime_init(const occtl_runtime_init_info_t *info)
const occtl_error_t * occtl_error_last(void)
enum occtl_status occtl_status_t
const char * occtl_runtime_occt_version(void)
@ OCCTL_OK
Definition occtl_core.h:133
void occtl_runtime_shutdown(void)
occtl_status_t occtl_runtime_init_info_init(occtl_runtime_init_info_t *info)
void occtl_runtime_version(uint32_t *out_major, uint32_t *out_minor, uint32_t *out_patch)
Definition occtl_core.h:349