| 1 |
// |
| 2 |
// SPDX-License-Identifier: BSD-3-Clause |
| 3 |
// Copyright Contributors to the OpenEXR Project. |
| 4 |
// |
| 5 |
|
| 6 |
// clang-format off |
| 7 |
|
| 8 |
#ifndef _PyImathStringTable_h_ |
| 9 |
#define _PyImathStringTable_h_ |
| 10 |
|
| 11 |
#include <string> |
| 12 |
#include <stdint.h> |
| 13 |
#include <boost/multi_index_container.hpp> |
| 14 |
#include <boost/multi_index/ordered_index.hpp> |
| 15 |
#include <boost/multi_index/identity.hpp> |
| 16 |
#include <boost/multi_index/member.hpp> |
| 17 |
|
| 18 |
namespace PyImath { |
| 19 |
|
| 20 |
// define a separate index type so as not to have |
| 21 |
// any confusion between ints and indices |
| 22 |
struct StringTableIndex |
| 23 |
{ |
| 24 |
public: |
| 25 |
typedef uint32_t index_type; |
| 26 |
|
| 27 |
// the default constructor was a private member before to prevent |
| 28 |
// the empty instantiation. But, it became public now to resolve |
| 29 |
// a linking error on windows platform. PyImathStringArray is |
| 30 |
// exposed with PYIMATH_EXPORT, This causes to expose all the members |
| 31 |
// of PyImathFixedArray<StringTableIndex> also. |
| 32 |
|
| 33 |
StringTableIndex() : _index(0) {} |
| 34 |
StringTableIndex (const StringTableIndex &si) : _index (si._index) {} |
| 35 |
explicit StringTableIndex (index_type i) : _index (i) {} |
| 36 |
~StringTableIndex() = default; |
| 37 |
|
| 38 |
const StringTableIndex & operator = (const StringTableIndex &si) |
| 39 |
{ |
| 40 |
if (&si != this) |
| 41 |
_index = si._index; |
| 42 |
|
| 43 |
return *this; |
| 44 |
} |
| 45 |
|
| 46 |
bool operator == (const StringTableIndex &si) const |
| 47 |
{ |
| 48 |
return _index == si._index; |
| 49 |
} |
| 50 |
|
| 51 |
bool operator != (const StringTableIndex &si) const |
| 52 |
{ |
| 53 |
return _index != si._index; |
| 54 |
} |
| 55 |
|
| 56 |
// operator less for sorting |
| 57 |
bool operator < (const StringTableIndex &si) const |
| 58 |
{ |
| 59 |
return _index < si._index; |
| 60 |
} |
| 61 |
|
| 62 |
index_type index() const { return _index; } |
| 63 |
|
| 64 |
private: |
| 65 |
index_type _index; |
| 66 |
}; |
| 67 |
|
| 68 |
} // namespace PyImath |
| 69 |
|
| 70 |
// Add a type trait for string indices to allow use in an AlignedArray |
| 71 |
namespace boost { |
| 72 |
template <> struct is_pod< ::PyImath::StringTableIndex> |
| 73 |
{ |
| 74 |
BOOST_STATIC_CONSTANT(bool,value=true); |
| 75 |
}; |
| 76 |
} // namespace boost |
| 77 |
|
| 78 |
namespace PyImath { |
| 79 |
|
| 80 |
// |
| 81 |
// A string table entry containing a unique index and string |
| 82 |
template<class T> |
| 83 |
struct StringTableEntry |
| 84 |
{ |
| 85 |
StringTableEntry(StringTableIndex ii,const T &ss) : i(ii), s(ss) {} |
| 86 |
StringTableIndex i; |
| 87 |
T s; |
| 88 |
}; |
| 89 |
|
| 90 |
namespace { |
| 91 |
|
| 92 |
using boost::multi_index_container; |
| 93 |
using namespace boost::multi_index; |
| 94 |
|
| 95 |
// |
| 96 |
// A map data structure for string strings. |
| 97 |
// It exposes two index types : StringTableIndex and string |
| 98 |
// |
| 99 |
template<class T> |
| 100 |
class StringTableDetailT { |
| 101 |
public: |
| 102 |
typedef boost::multi_index_container< |
| 103 |
StringTableEntry<T>, |
| 104 |
indexed_by< |
| 105 |
ordered_unique<member<StringTableEntry<T>,StringTableIndex,&StringTableEntry<T>::i> >, |
| 106 |
ordered_unique<member<StringTableEntry<T>,T,&StringTableEntry<T>::s> > |
| 107 |
> |
| 108 |
> StringTableContainer; |
| 109 |
}; |
| 110 |
|
| 111 |
} // namespace |
| 112 |
|
| 113 |
typedef StringTableDetailT<std::string> StringTableDetail; |
| 114 |
typedef StringTableDetailT<std::wstring> WStringTableDetail; |
| 115 |
|
| 116 |
// |
| 117 |
// Storage class for storing unique string elements. |
| 118 |
// |
| 119 |
// |
| 120 |
template<class T> |
| 121 |
class StringTableT |
| 122 |
{ |
| 123 |
public: |
| 124 |
|
| 125 |
// look up a string table entry either by value or index |
| 126 |
StringTableIndex lookup(const T &s) const; |
| 127 |
const T & lookup(StringTableIndex index) const; |
| 128 |
|
| 129 |
// return the index to a string table entry, adding if not found |
| 130 |
StringTableIndex intern(const T &i); |
| 131 |
|
| 132 |
size_t size() const; |
| 133 |
bool hasString(const T &s) const; |
| 134 |
bool hasStringIndex(const StringTableIndex &s) const; |
| 135 |
|
| 136 |
private: |
| 137 |
|
| 138 |
typedef typename StringTableDetailT<T>::StringTableContainer Table; |
| 139 |
Table _table; |
| 140 |
}; |
| 141 |
|
| 142 |
typedef StringTableT<std::string> StringTable; |
| 143 |
typedef StringTableT<std::wstring> WStringTable; |
| 144 |
|
| 145 |
} // namespace PyImath |
| 146 |
|
| 147 |
#endif |