Phosphor
inline_zstring.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright 2016 Couchbase, Inc
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
22 #pragma once
23 
24 #include <cstring>
25 #include <iostream>
26 #include <string>
27 
28 #include "utils/string_utils.h"
29 
30 namespace phosphor {
31 
38  template<size_t max_length>
40  public:
41  constexpr inline_zstring() = default;
42 
46  explicit inline_zstring(const std::string& s) {
47  strncpy(_s, s.c_str(),
48  (s.size() < max_length) ? s.size() : max_length);
49  }
50 
54  explicit inline_zstring(const char* s) {
55  strncpy(_s, s, max_length);
56  }
57 
61  explicit inline_zstring(const char* s, size_t len) {
62  strncpy(_s, s, std::min(len, max_length));
63  }
64 
68  operator std::string() const {
69  return std::string{_s, utils::strnlen_s(_s, max_length)
70  };
71  }
72 
76  friend std::ostream& operator<<(std::ostream& stream,
77  const inline_zstring& izs) {
78  stream.write(izs._s, utils::strnlen_s(izs._s, max_length));
79  return stream;
80  }
81 
82  private:
83  char _s[max_length];
84  };
85 
86 }
friend std::ostream & operator<<(std::ostream &stream, const inline_zstring &izs)
Definition: inline_zstring.h:76
Definition: inline_zstring.h:39
inline_zstring(const std::string &s)
Definition: inline_zstring.h:46
inline_zstring(const char *s, size_t len)
Definition: inline_zstring.h:61
inline_zstring(const char *s)
Definition: inline_zstring.h:54
Definition: category_registry.h:32