Skip to content

Commit f8c5394

Browse files
committed
composite
1 parent f306d59 commit f8c5394

File tree

1 file changed

+59
-18
lines changed

1 file changed

+59
-18
lines changed

examples/llava/clip-impl.h

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -189,35 +189,76 @@ static void clip_log_internal(enum ggml_log_level level, const char * format, ..
189189
// cpp wrappers
190190
//
191191

192+
// wrapper for clip_image_size
193+
struct clip_image_size_deleter {
194+
void operator()(clip_image_size * val) { clip_image_size_free(val); }
195+
};
196+
typedef std::unique_ptr<clip_image_size, clip_image_size_deleter> clip_image_size_ptr;
197+
198+
// use composition to avoid problems with inheritance from STL classes
199+
template <typename T, typename Initializer, typename Deleter>
200+
struct clip_image_buffer_base {
201+
std::unique_ptr<T, Deleter> ptr;
202+
clip_image_buffer_base() : ptr(Initializer()()) {}
203+
explicit clip_image_buffer_base(T* p) : ptr(p) {}
204+
clip_image_buffer_base(const clip_image_buffer_base& other) = delete;
205+
clip_image_buffer_base& operator=(const clip_image_buffer_base& other) = delete;
206+
clip_image_buffer_base(clip_image_buffer_base&& other) noexcept = default;
207+
clip_image_buffer_base& operator=(clip_image_buffer_base&& other) noexcept = default;
208+
~clip_image_buffer_base() = default;
209+
void reset(T* p = nullptr) { ptr.reset(p); }
210+
T* get() const noexcept { return ptr.get(); }
211+
T& operator*() const { return *ptr; }
212+
T* operator->() const noexcept { return ptr.get(); }
213+
explicit operator bool() const noexcept { return static_cast<bool>(ptr); }
214+
};
215+
216+
// wrapper for clip_image_u8
217+
struct clip_image_u8_initializer {
218+
clip_image_u8 * operator()() { return clip_image_u8_init(); }
219+
};
192220
struct clip_image_u8_deleter {
193221
void operator()(clip_image_u8 * val) { clip_image_u8_free(val); }
194222
};
223+
using clip_image_u8_ptr = clip_image_buffer_base<clip_image_u8, clip_image_u8_initializer, clip_image_u8_deleter>;
195224

225+
// wrapper for clip_image_f32
226+
struct clip_image_f32_initializer {
227+
clip_image_f32 * operator()() { return clip_image_f32_init(); }
228+
};
196229
struct clip_image_f32_deleter {
197230
void operator()(clip_image_f32 * val) { clip_image_f32_free(val); }
198231
};
199-
200-
struct clip_image_size_deleter {
201-
void operator()(clip_image_size * val) { clip_image_size_free(val); }
202-
};
203-
204-
struct clip_image_u8_ptr : std::unique_ptr<clip_image_u8, clip_image_u8_deleter> {
205-
clip_image_u8_ptr() : std::unique_ptr<clip_image_u8, clip_image_u8_deleter>(clip_image_u8_init()) {}
232+
using clip_image_f32_ptr = clip_image_buffer_base<clip_image_f32, clip_image_f32_initializer, clip_image_f32_deleter>;
233+
234+
// use composition to avoid problems with inheritance from STL classes
235+
template <typename ImagePtrType>
236+
struct clip_image_batch_base {
237+
std::vector<ImagePtrType> images;
238+
clip_image_batch_base() = default;
239+
void push_back(ImagePtrType&& value) { images.push_back(std::move(value)); }
240+
void clear() noexcept { images.clear(); }
241+
void reserve(size_t n) { images.reserve(n); }
242+
243+
// Capacity
244+
size_t size() const noexcept { return images.size(); }
245+
bool empty() const noexcept { return images.empty(); }
246+
247+
// Element access
248+
ImagePtrType& operator[](size_t pos) { return images[pos]; }
249+
const ImagePtrType& operator[](size_t pos) const { return images[pos]; }
250+
ImagePtrType& at(size_t pos) { return images.at(pos); }
251+
const ImagePtrType& at(size_t pos) const { return images.at(pos); }
206252
};
207253

208-
struct clip_image_f32_ptr : std::unique_ptr<clip_image_f32, clip_image_f32_deleter> {
209-
clip_image_f32_ptr() : std::unique_ptr<clip_image_f32, clip_image_f32_deleter>(clip_image_f32_init()) {}
254+
struct clip_image_u8_batch : clip_image_batch_base<clip_image_u8_ptr> {
255+
clip_image_u8_batch() = default;
256+
~clip_image_u8_batch() = default;
210257
};
211258

212-
typedef std::unique_ptr<clip_image_size, clip_image_size_deleter> clip_image_size_ptr;
213-
214-
// these need to be struct to maintain compatibility with C interface
215-
struct clip_image_u8_batch : std::vector<clip_image_u8_ptr> {
216-
clip_image_u8_batch() : std::vector<clip_image_u8_ptr>() {}
217-
};
218-
struct clip_image_f32_batch : std::vector<clip_image_f32_ptr> {
219-
clip_image_f32_batch() : std::vector<clip_image_f32_ptr>() {}
220-
~clip_image_f32_batch() {}
259+
struct clip_image_f32_batch : clip_image_batch_base<clip_image_f32_ptr> {
260+
clip_image_f32_batch() = default;
261+
~clip_image_f32_batch() = default;
221262
};
222263

223264

0 commit comments

Comments
 (0)