diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-15 18:31:36 +0100 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-15 18:31:36 +0100 |
commit | 38b866988761f4da01eab769dc660b06b07e97be (patch) | |
tree | 82f49c6a7ca237059229ff60fd05ca07e9f34656 /include | |
parent | f3778ca5c28ccefcc5b60afbae4f131de1292cdf (diff) |
added empty() function, and added HardCoded class
Diffstat (limited to 'include')
-rw-r--r-- | include/hardcoded.h | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/include/hardcoded.h b/include/hardcoded.h new file mode 100644 index 0000000..f48fc61 --- /dev/null +++ b/include/hardcoded.h @@ -0,0 +1,89 @@ +/** + * HardCoded.h + * + * Small class that can be wrapped around a "hardcoded string". Normally, the + * Value object always makes a full copy of a string, because the value object + * may exist for a longer period than the pointer-to-a-string that is wrapped + * in it. + * + * However, in some situations it is already certain that the buffer in + * which the original string is stored will outlive the Value object. This is + * for example true for hardcoded strings. Such const-char* can be wrapped into + * a Php::HardCoded instance before they are assigned to a Php::Value object + * + * This class is called HardCoded because it is often used for hardcoded + * strings, but you can use it for other values as well. + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2014 Copernica BV + */ + +/** + * Php namespace + */ +namespace Php { + +/** + * Class definition + */ +class HardCoded +{ +public: + /** + * Constructors + * + * The class has a regular constructor with a size or not, a copy constructor + * and move constructor. + * + * @param buffer + * @param size + * @param that + */ + HardCoded(const char *buffer, size_t size) : _buffer(buffer), _size(size) {} + HardCoded(const char *buffer) : _buffer(buffer), _size(strlen(buffer)) {} + HardCoded(const char buffer) : _buffer(&buffer), _size(1) {} + HardCoded(const HardCoded &that) : _buffer(that._buffer), _size(that._size) {} + HardCoded(HardCoded &&that) : _buffer(that._buffer), _size(that._size) {} + + /** + * Destructor + */ + virtual ~HardCoded() {} + + /** + * Method to get access to the buffer + * @return const char * + */ + const char *buffer() const + { + return _buffer; + } + + /** + * Size of the buffer + * @return size_t + */ + size_t size() const + { + return _size; + } + +private: + /** + * The actual buffer + * @var const char * + */ + const char *_buffer; + + /** + * Size of the buffer + * @var size_t + */ + size_t _size; +}; + +/** + * End namespace + */ +} + |