| 1 | // |
| 2 | // SPDX-License-Identifier: BSD-3-Clause |
| 3 | // Copyright (c) Contributors to the OpenEXR Project. |
| 4 | // |
| 5 | |
| 6 | |
| 7 | #ifndef INCLUDED_IMF_NAME_H |
| 8 | #define INCLUDED_IMF_NAME_H |
| 9 | |
| 10 | //----------------------------------------------------------------------------- |
| 11 | // |
| 12 | // class ImfName -- a zero-terminated string |
| 13 | // with a fixed, small maximum length |
| 14 | // |
| 15 | //----------------------------------------------------------------------------- |
| 16 | |
| 17 | #include "ImfExport.h" |
| 18 | #include "ImfNamespace.h" |
| 19 | |
| 20 | #include <cstring> |
| 21 | |
| 22 | #if defined(_MSC_VER) |
| 23 | #pragma warning( push, 0 ) |
| 24 | #pragma warning (disable : 4996) |
| 25 | #endif |
| 26 | |
| 27 | OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER |
| 28 | |
| 29 | |
| 30 | class IMF_EXPORT_TYPE Name |
| 31 | { |
| 32 | public: |
| 33 | |
| 34 | //------------- |
| 35 | // Constructors |
| 36 | //------------- |
| 37 | |
| 38 | Name (); |
| 39 | Name (const char text[]); |
| 40 | Name (const Name &) = default; |
| 41 | Name (Name &&) = default; |
| 42 | ~Name () = default; |
| 43 | |
| 44 | |
| 45 | //-------------------- |
| 46 | // Assignment operator |
| 47 | //-------------------- |
| 48 | |
| 49 | Name &operator = (const Name &) = default; |
| 50 | Name &operator = (Name &&) = default; |
| 51 | Name &operator = (const char text[]); |
| 52 | |
| 53 | |
| 54 | //--------------------- |
| 55 | // Access to the string |
| 56 | //--------------------- |
| 57 | |
| 58 | inline |
| 59 | const char * text () const {return _text;} |
| 60 | inline |
| 61 | const char * operator * () const {return _text;} |
| 62 | |
| 63 | //--------------- |
| 64 | // Maximum length |
| 65 | //--------------- |
| 66 | |
| 67 | static const int SIZE = 256; |
| 68 | static const int MAX_LENGTH = SIZE - 1; |
| 69 | |
| 70 | private: |
| 71 | |
| 72 | char _text[SIZE]; |
| 73 | }; |
| 74 | |
| 75 | //----------------- |
| 76 | // Inline functions |
| 77 | //----------------- |
| 78 | |
| 79 | inline Name & |
| 80 | Name::operator = (const char text[]) |
| 81 | { |
| 82 | strncpy (dest: _text, src: text, n: MAX_LENGTH); |
| 83 | return *this; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | inline |
| 88 | Name::Name () |
| 89 | { |
| 90 | _text[0] = 0; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | inline |
| 95 | Name::Name (const char text[]) |
| 96 | { |
| 97 | *this = text; |
| 98 | _text [MAX_LENGTH] = 0; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | inline bool |
| 103 | operator == (const Name &x, const Name &y) |
| 104 | { |
| 105 | return strcmp (s1: *x, s2: *y) == 0; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | inline bool |
| 110 | operator == (const Name &x, const char text[]) |
| 111 | { |
| 112 | return strcmp (s1: *x, s2: text) == 0; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | inline bool |
| 117 | operator == (const char text[], const Name &y) |
| 118 | { |
| 119 | return strcmp (s1: text, s2: *y) == 0; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | inline bool |
| 124 | operator != (const Name &x, const Name &y) |
| 125 | { |
| 126 | return !(x == y); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | inline bool |
| 131 | operator != (const Name &x, const char text[]) |
| 132 | { |
| 133 | return !(x == text); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | inline bool |
| 138 | operator != (const char text[], const Name &y) |
| 139 | { |
| 140 | return !(text == y); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | inline bool |
| 145 | operator < (const Name &x, const Name &y) |
| 146 | { |
| 147 | return strcmp (s1: *x, s2: *y) < 0; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | inline bool |
| 152 | operator < (const Name &x, const char text[]) |
| 153 | { |
| 154 | return strcmp (s1: *x, s2: text) < 0; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | inline bool |
| 159 | operator < (const char text[], const Name &y) |
| 160 | { |
| 161 | return strcmp (s1: text, s2: *y) < 0; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT |
| 166 | |
| 167 | #if defined(_MSC_VER) |
| 168 | #pragma warning (pop) |
| 169 | #endif |
| 170 | |
| 171 | #endif |
| 172 | |