xref: /6.0.3/subjson/subdoc/util.h (revision 121b39be)
1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3*     Copyright 2015 Couchbase, Inc
4*
5*   Licensed under the Apache License, Version 2.0 (the "License");
6*   you may not use this file except in compliance with the License.
7*   You may obtain a copy of the License at
8*
9*       http://www.apache.org/licenses/LICENSE-2.0
10*
11*   Unless required by applicable law or agreed to in writing, software
12*   distributed under the License is distributed on an "AS IS" BASIS,
13*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*   See the License for the specific language governing permissions and
15*   limitations under the License.
16*/
17#ifndef SUBDOC_UTIL_H
18#define SUBDOC_UTIL_H
19
20#include "operations.h"
21#include <iostream>
22
23namespace Subdoc {
24
25/// This class contains various utilities, mainly useful for testing/debugging
26class Util {
27public:
28    /// Gets the actual match string
29    static std::string match_match(const Match&);
30
31    /// Gets the string of the match's parent container
32    static std::string match_parent(const Match&);
33
34    /// Gets the string of the match's key
35    static std::string match_key(const Match&);
36
37    /// Prints a representation of the various segments of the new document
38    static void dump_newdoc(const Result&, std::ostream& = std::cerr);
39
40    static const char *jsonerr(jsonsl_error_t err);
41
42    static void do_assert(const char *e,
43            const char *func, const char *file, int line);
44
45    /// Determines the type of container the parent object must be. It derives
46    /// this from the path.
47    /// @param command the command associated with this path
48    /// @param path the path
49    /// @param length of the path
50    /// @return JSONL_T_ROOT or JSONSL_T_LIST if the path parent should be
51    ///     a list or or dictionary. JSONSL_T_UNKNOWN if the parent type cannot
52    ///     be determined.
53    static jsonsl_type_t get_root_type(Command command, const char *path, size_t len);
54
55    static jsonsl_type_t get_root_type(Command command, const std::string& s) {
56        return get_root_type(command, s.c_str(), s.size());
57    }
58private:
59    Util();
60};
61} // namespace
62
63namespace std {
64ostream& operator<<(ostream&, const Subdoc::Error::Code&);
65inline ostream& operator<<(ostream& os, const Subdoc::Error& err) {
66    return os << err.code();
67}
68}
69
70#ifdef _MSC_VER
71#define SUBDOC__func__ __FUNCTION__
72#else
73#define SUBDOC__func__ __func__
74#endif
75
76#define SUBDOC_ASSERT(e) \
77    if (!(e)) { Util::do_assert(#e, SUBDOC__func__, __FILE__, __LINE__); }
78
79#endif /* SUBDOC_UTIL_H */
80