MMDevice
Loading...
Searching...
No Matches
CameraImageMetadata.h
Go to the documentation of this file.
1
2// PROJECT: Micro-Manager
3// SUBSYSTEM: MMDevice
4//-----------------------------------------------------------------------------
5// COPYRIGHT: 2026, Board of Regents of the University of Wisconsin System
6//
7// LICENSE: This file is distributed under the BSD license.
8// License text is included with the source distribution.
9//
10// This file is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty
12// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13//
14// IN NO EVENT SHALL THE COPYRIGHT OWNER OR
15// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
16// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES.
17
18#pragma once
19
20#include <algorithm>
21#include <cassert>
22#include <cstddef>
23#include <limits>
24#include <sstream>
25#include <string>
26
27namespace MM {
28
30public:
32
56 template <typename V>
57 void AddTag(const char* key, V value) {
58 assert(key != nullptr);
59 std::ostringstream strm;
60 strm << value;
61 AppendTag(key, strm.str().c_str());
62 }
63
65 void AddTag(const char* key, const char* value) {
66 assert(key != nullptr);
67 assert(value != nullptr);
68 AppendTag(key, value);
69 }
70
72 template <typename V>
73 void AddTag(const std::string& key, V value) {
74 // Once we drop C++14 support, we could replace the char* and std::string&
75 // overloads with std::string_view.
76 AddTag(key.c_str(), value);
77 }
78
82 void Clear() {
83 buffer_.assign(kCountWidth, ' ');
84 buffer_.push_back('\n');
85 count_ = 0;
86 }
87
102 const char* Serialize() const {
103 // The serialization format is part of the versioned Device Interface:
104 //
105 // Header: <tag_count>\n
106 // (Spaces are tolerated before and after the tag count; this
107 // implementation right-pads the count to a fixed width.)
108 // For each tag: s\n<name>\n_\n1\n<value>\n
109 // (The 's' indicates "single (not array) tag"; arrays are never used.)
110 // (The '_' device-label field is always "_".)
111 // (The '1' indicates "read-only"; no actual meaning.)
112 //
113 // Tags must have unique keys up to DIV 74. In DIV 75+, duplicate keys are
114 // permitted on the wire; the last occurrence wins on deserialization.
115
116 WriteCountHeader();
117 return buffer_.c_str();
118 }
119
120private:
121 // Enough to hold any std::size_t in decimal (20 on 64-bit platforms).
122 static constexpr std::size_t kCountWidth =
123 std::numeric_limits<std::size_t>::digits10 + 1;
124
125 void AppendTag(const char* key, const char* value) {
126 buffer_ += "s\n";
127 buffer_ += key;
128 buffer_ += "\n_\n1\n";
129 buffer_ += value;
130 buffer_ += '\n';
131 ++count_;
132 }
133
134 void WriteCountHeader() const {
135 const std::string s = std::to_string(count_);
136 assert(s.size() <= kCountWidth);
137 auto out = std::copy(s.begin(), s.end(), buffer_.begin());
138 std::fill(out, buffer_.begin() + kCountWidth, ' ');
139 }
140
141 mutable std::string buffer_;
142 std::size_t count_ = 0;
143};
144
145} // namespace MM
Definition CameraImageMetadata.h:29
void AddTag(const char *key, const char *value)
Optimized overload for string values.
Definition CameraImageMetadata.h:65
void Clear()
Remove all tags.
Definition CameraImageMetadata.h:82
void AddTag(const char *key, V value)
Add a tag.
Definition CameraImageMetadata.h:57
const char * Serialize() const
Return this metadata map serialized to string form.
Definition CameraImageMetadata.h:102
void AddTag(const std::string &key, V value)
Overload for std::string key.
Definition CameraImageMetadata.h:73
CameraImageMetadata()
Definition CameraImageMetadata.h:31
Definition CameraImageMetadata.h:27