Manifold 3.0
Robust geometry
 
Loading...
Searching...
No Matches
optional_assert.h
1// Copyright 2022 The Manifold Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#ifdef MANIFOLD_DEBUG
18#include <iomanip>
19#include <iostream>
20#include <sstream>
21#include <stdexcept>
22#include <string>
23
27struct userErr : public virtual std::runtime_error {
28 using std::runtime_error::runtime_error;
29};
30struct topologyErr : public virtual std::runtime_error {
31 using std::runtime_error::runtime_error;
32};
33struct geometryErr : public virtual std::runtime_error {
34 using std::runtime_error::runtime_error;
35};
36using logicErr = std::logic_error;
37#endif
38
39#if defined(MANIFOLD_ASSERT) && defined(MANIFOLD_DEBUG)
40
41template <typename Ex>
42void AssertFail(const char* file, int line, const char* cond, const char* msg) {
43 std::ostringstream output;
44 output << "Error in file: " << file << " (" << line << "): \'" << cond
45 << "\' is false: " << msg;
46 throw Ex(output.str());
47}
48
49template <typename Ex>
50void AssertFail(const char* file, int line, const std::string& cond,
51 const std::string& msg) {
52 std::ostringstream output;
53 output << "Error in file: " << file << " (" << line << "): \'" << cond
54 << "\' is false: " << msg;
55 throw Ex(output.str());
56}
57
58// DEBUG_ASSERT is slightly slower due to the function call, but gives more
59// detailed info.
60#define DEBUG_ASSERT(condition, EX, msg) \
61 if (!(condition)) AssertFail<EX>(__FILE__, __LINE__, #condition, msg);
62// ASSERT has almost no overhead, so better to use for frequent calls like
63// vector bounds checking.
64#define ASSERT(condition, EX) \
65 if (!(condition)) throw(EX);
66#else
67#define DEBUG_ASSERT(condition, EX, msg)
68#define ASSERT(condition, EX)
69#endif
Definition optional_assert.h:33
Definition optional_assert.h:30
Definition optional_assert.h:27