Menu

[94f52a]: / cstring.hpp  Maximize  Restore  History

Download this file

59 lines (42 with data), 1.5 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#ifndef CHILON_CSTRING_HPP
#define CHILON_CSTRING_HPP
#include <memory>
#include <boost/utility.hpp>
/// \file Utilities for using cstring's allocated with malloc
namespace chilon {
/**
* Represents a cstring
*/
struct cstring_base : boost::noncopyable {
char const *str() const { return str_; }
char *str() { return str_; }
bool empty() const { return str_; }
cstring_base() : str_(0) {}
cstring_base(char *str) : str_(str) {}
protected:
char *str_;
};
// a cstring which must be dynamically allocated with cmalloc
struct cstring_allocated : cstring_base {
cstring_allocated() : cstring_base() {}
cstring_allocated(char *str) : cstring_base(str) {}
cstring_allocated(cstring_allocated&& rhs) : cstring_base(rhs.str_) { rhs.str_ = 0; }
~cstring_allocated() { if (str_) free(str_); }
};
// a cstring which may be dynamic or stack based
struct cstring : cstring_base {
bool allocated() const { return allocated_; }
cstring() : cstring_base(), allocated_(false) {}
cstring(char * const str, bool const allocated = false)
: cstring_base(str), allocated_(allocated) {}
cstring(char const * const str, bool const allocated = false)
: cstring_base(const_cast<char *>(str)), allocated_(allocated) {}
~cstring() { if (allocated()) free(str_); }
cstring(cstring&& rhs)
: cstring_base(rhs.str_), allocated_(rhs.allocated_)
{ rhs.allocated_ = false; }
private:
bool allocated_;
};
}
#endif
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.