File: zeroconf-util.hpp

package info (click to toggle)
opencpn 5.2.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 101,540 kB
  • sloc: ansic: 414,598; cpp: 253,008; xml: 83,748; sh: 409; python: 353; makefile: 110; javascript: 87; perl: 83
file content (72 lines) | stat: -rw-r--r-- 1,941 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifndef ZEROCONF_UTIL_HPP
#define ZEROCONF_UTIL_HPP

//////////////////////////////////////////////////////////////////////////
// zeroconf-util.hpp

// (C) Copyright 2016 Yuri Yakovlev <yvzmail@gmail.com>
// Use, modification and distribution is subject to the GNU General Public License

#include <string>
#include <istream>
#include <streambuf>

#if _MSC_VER == 1700
#define thread_local __declspec(thread)
#endif

namespace Zeroconf
{
    namespace Detail
    {
        namespace stdext
        {
            class membuf : public std::streambuf
            {
            public: 
                membuf(const unsigned char* base, int size) 
                {
                    auto p = reinterpret_cast<char*>(const_cast<unsigned char*>(base));
                    setg(p, p, p + size);
                }

            protected:
                virtual pos_type seekoff(off_type offset, std::ios_base::seekdir, std::ios_base::openmode mode) override
                {
                    if (offset == 0 && mode == std::ios_base::in)
                        return pos_type(gptr() - eback());

                    return pos_type(-1);
                }
            };
        }

        namespace Log
        {
            enum class LogLevel { Error, Warning };

            typedef void(*LogCallback)(LogLevel, const std::string&);

            static thread_local LogCallback g_logcb = nullptr;

            inline void Error(const std::string& message) 
            {
                if (g_logcb != nullptr)
                    g_logcb(LogLevel::Error, message);
            }

            inline void Warning(const std::string& message) 
            {
                if (g_logcb != nullptr)
                    g_logcb(LogLevel::Warning, message);
            }

            inline void SetLogCallback(LogCallback logcb)
            {
                g_logcb = logcb;
            }
        }
    }
}

#endif // ZEROCONF_UTIL_HPP