ImfName.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright (c) Contributors to the OpenEXR Project.
  4. //
  5. #ifndef INCLUDED_IMF_NAME_H
  6. #define INCLUDED_IMF_NAME_H
  7. //-----------------------------------------------------------------------------
  8. //
  9. // class ImfName -- a zero-terminated string
  10. // with a fixed, small maximum length
  11. //
  12. //-----------------------------------------------------------------------------
  13. #include "ImfExport.h"
  14. #include "ImfNamespace.h"
  15. #include <cstring>
  16. #if defined(_MSC_VER)
  17. #pragma warning( push, 0 )
  18. #pragma warning (disable : 4996)
  19. #endif
  20. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  21. class IMF_EXPORT_TYPE Name
  22. {
  23. public:
  24. //-------------
  25. // Constructors
  26. //-------------
  27. Name ();
  28. Name (const char text[]);
  29. Name (const Name &) = default;
  30. Name (Name &&) = default;
  31. ~Name () = default;
  32. //--------------------
  33. // Assignment operator
  34. //--------------------
  35. Name &operator = (const Name &) = default;
  36. Name &operator = (Name &&) = default;
  37. Name &operator = (const char text[]);
  38. //---------------------
  39. // Access to the string
  40. //---------------------
  41. inline
  42. const char * text () const {return _text;}
  43. inline
  44. const char * operator * () const {return _text;}
  45. //---------------
  46. // Maximum length
  47. //---------------
  48. static const int SIZE = 256;
  49. static const int MAX_LENGTH = SIZE - 1;
  50. private:
  51. char _text[SIZE];
  52. };
  53. //-----------------
  54. // Inline functions
  55. //-----------------
  56. inline Name &
  57. Name::operator = (const char text[])
  58. {
  59. strncpy (_text, text, MAX_LENGTH);
  60. return *this;
  61. }
  62. inline
  63. Name::Name ()
  64. {
  65. _text[0] = 0;
  66. }
  67. inline
  68. Name::Name (const char text[])
  69. {
  70. *this = text;
  71. _text [MAX_LENGTH] = 0;
  72. }
  73. inline bool
  74. operator == (const Name &x, const Name &y)
  75. {
  76. return strcmp (*x, *y) == 0;
  77. }
  78. inline bool
  79. operator == (const Name &x, const char text[])
  80. {
  81. return strcmp (*x, text) == 0;
  82. }
  83. inline bool
  84. operator == (const char text[], const Name &y)
  85. {
  86. return strcmp (text, *y) == 0;
  87. }
  88. inline bool
  89. operator != (const Name &x, const Name &y)
  90. {
  91. return !(x == y);
  92. }
  93. inline bool
  94. operator != (const Name &x, const char text[])
  95. {
  96. return !(x == text);
  97. }
  98. inline bool
  99. operator != (const char text[], const Name &y)
  100. {
  101. return !(text == y);
  102. }
  103. inline bool
  104. operator < (const Name &x, const Name &y)
  105. {
  106. return strcmp (*x, *y) < 0;
  107. }
  108. inline bool
  109. operator < (const Name &x, const char text[])
  110. {
  111. return strcmp (*x, text) < 0;
  112. }
  113. inline bool
  114. operator < (const char text[], const Name &y)
  115. {
  116. return strcmp (text, *y) < 0;
  117. }
  118. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  119. #if defined(_MSC_VER)
  120. #pragma warning (pop)
  121. #endif
  122. #endif