Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Const qualify some read-only accessor methods. #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/meiattribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ bool mei::MeiAttribute::operator==(const MeiAttribute &other) const {
this->value == other.value);
}

string mei::MeiAttribute::getName() {
string mei::MeiAttribute::getName() const {
return this->name;
}

string mei::MeiAttribute::getValue() {
string mei::MeiAttribute::getValue() const {
return this->value;
}

void mei::MeiAttribute::setValue(string attrvalue) {
this->value = attrvalue;
}

MeiElement* mei::MeiAttribute::getElement() {
MeiElement* mei::MeiAttribute::getElement() const {
return this->element;
}

void mei::MeiAttribute::setElement(MeiElement *el) {
this->element = el;
}
}
6 changes: 3 additions & 3 deletions src/meiattribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ namespace mei {
* Each attribute is created with a name and value, the attribute cannot exist without a name
* \return string containing the attribute name.
*/
std::string getName();
std::string getName() const;

/** \brief Get the value of the attribute.
* \return A string indicating the attribute value
*/
std::string getValue();
std::string getValue() const;

/** \brief Set/change the value of an attribute*/
void setValue(std::string attrvalue);

MeiElement* getElement();
MeiElement* getElement() const;
void setElement(MeiElement* el);

private:
Expand Down
4 changes: 2 additions & 2 deletions src/meidocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ vector<MeiElement*> mei::MeiDocument::getElementsByName(string name) {
return ret;
}

int mei::MeiDocument::getPositionInDocument(MeiElement* element) {
int mei::MeiDocument::getPositionInDocument(MeiElement* element) const {
vector<MeiElement*> els = this->getFlattenedTree();
vector<MeiElement*>::iterator pos = find(els.begin(), els.end(), element);
if (pos != els.end()) {
Expand All @@ -120,7 +120,7 @@ int mei::MeiDocument::getPositionInDocument(MeiElement* element) {
return -1;
}

const std::vector<MeiElement*> &mei::MeiDocument::getFlattenedTree() {
const std::vector<MeiElement*> &mei::MeiDocument::getFlattenedTree() const {
return flattenedDoc;
}

Expand Down
4 changes: 2 additions & 2 deletions src/meidocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class MEI_EXPORT MeiDocument {
/** \brief Gets an element's position in the flattened
* tree representation.
*/
int getPositionInDocument(MeiElement* element);
int getPositionInDocument(MeiElement* element) const;

/**
* \brief Adds an ID and an element to the ID Map.
Expand All @@ -104,7 +104,7 @@ class MEI_EXPORT MeiDocument {
void rmIdMap(std::string id);

/** \brief Returns the flattened document tree */
const std::vector<MeiElement*> &getFlattenedTree();
const std::vector<MeiElement*> &getFlattenedTree() const;

/** \brief Returns the most immediate previous element elName, given a starting point
*/
Expand Down
42 changes: 21 additions & 21 deletions src/meielement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void mei::MeiElement::generateAndSetId() {
this->setId(out);
}

const string mei::MeiElement::getId() {
const string mei::MeiElement::getId() const {
return this->id;
}

Expand All @@ -110,27 +110,27 @@ bool mei::MeiElement::hasId() {
return this->id != "";
}

const string mei::MeiElement::getName() {
const string mei::MeiElement::getName() const {
return this->name;
}

const string mei::MeiElement::getValue() {
const string mei::MeiElement::getValue() const {
return this->value;
}

void mei::MeiElement::setValue(string value) {
this->value = value;
}

const string mei::MeiElement::getTail() {
const string mei::MeiElement::getTail() const {
return this->tail;
}

void mei::MeiElement::setTail(string tail) {
this->tail = tail;
}

const vector<MeiAttribute*>& mei::MeiElement::getAttributes() {
const vector<MeiAttribute*>& mei::MeiElement::getAttributes() const {
return this->attributes;
}

Expand All @@ -142,19 +142,19 @@ void mei::MeiElement::setAttributes(const vector<MeiAttribute*> attrs) {
}
}

MeiAttribute* mei::MeiElement::getAttribute(string name) {
for (vector<MeiAttribute*>::iterator iter = attributes.begin(); iter != attributes.end(); ++iter) {
MeiAttribute* mei::MeiElement::getAttribute(string name) const {
for (vector<MeiAttribute*>::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter) {
if ((*iter)->getName() == name) {
return *iter;
}
}
return NULL;
}

bool mei::MeiElement::hasAttribute(string name) {
bool mei::MeiElement::hasAttribute(string name) const {
if (attributes.empty()) return false;

for (vector<MeiAttribute*>::iterator iter = attributes.begin(); iter != attributes.end(); ++iter) {
for (vector<MeiAttribute*>::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter) {
if ((*iter)->getName() == name) return true;
}
return false;
Expand Down Expand Up @@ -191,15 +191,15 @@ void mei::MeiElement::removeAttribute(string name) {
}
}

bool mei::MeiElement::hasParent() {
bool mei::MeiElement::hasParent() const {
return parent != NULL;
}

void mei::MeiElement::setParent(MeiElement *parent) {
this->parent = parent;
}

mei::MeiElement* mei::MeiElement::getParent() {
mei::MeiElement* mei::MeiElement::getParent() const {
return this->parent;
}

Expand All @@ -218,7 +218,7 @@ void mei::MeiElement::setDocument(MeiDocument *document) throw(DocumentRootNotSe
}
}

MeiDocument* mei::MeiElement::getDocument() {
MeiDocument* mei::MeiElement::getDocument() const {
return this->document;
}

Expand Down Expand Up @@ -272,13 +272,13 @@ void mei::MeiElement::setChildren(vector<MeiElement*> children) {
updateDocument();
}

const vector<mei::MeiElement*>& mei::MeiElement::getChildren() {
const vector<mei::MeiElement*>& mei::MeiElement::getChildren() const {
return this->children;
}

const vector<mei::MeiElement*> mei::MeiElement::getChildrenByName(string name) {
const vector<mei::MeiElement*> mei::MeiElement::getChildrenByName(string name) const {
vector<mei::MeiElement*> res;
for (vector<MeiElement*>::iterator iter = this->children.begin(); iter != this->children.end(); ++iter) {
for (vector<MeiElement*>::const_iterator iter = this->children.begin(); iter != this->children.end(); ++iter) {
if ((*iter)->getName() == name) {
res.push_back(*iter);
}
Expand Down Expand Up @@ -324,19 +324,19 @@ void mei::MeiElement::removeChildrenByName(string name) {
updateDocument();
}

bool mei::MeiElement::hasChildren() {
bool mei::MeiElement::hasChildren() const {
// topsy-turvy world! returns true if not empty.
return !this->children.empty();
}

bool mei::MeiElement::hasChildren(string cname) {
for (vector<MeiElement*>::iterator iter = this->children.begin(); iter != this->children.end(); ++iter) {
bool mei::MeiElement::hasChildren(string cname) const {
for (vector<MeiElement*>::const_iterator iter = this->children.begin(); iter != this->children.end(); ++iter) {
if ((*iter)->getName() == cname) return true;
}
return false;
}

mei::MeiElement* mei::MeiElement::getAncestor(string name) {
mei::MeiElement* mei::MeiElement::getAncestor(string name) const {
if (parent == NULL) {
return NULL;
}
Expand All @@ -346,7 +346,7 @@ mei::MeiElement* mei::MeiElement::getAncestor(string name) {
return parent->getAncestor(name);
}

bool mei::MeiElement::hasAncestor(string name) {
bool mei::MeiElement::hasAncestor(string name) const {
MeiElement* m = getAncestor(name);
if (m != NULL) {
return true;
Expand All @@ -372,7 +372,7 @@ vector<mei::MeiElement*> mei::MeiElement::getDescendantsByName(string name) {
return res;
}

vector<mei::MeiElement*> mei::MeiElement::getPeers() {
vector<mei::MeiElement*> mei::MeiElement::getPeers() const {
if (this->parent) {
return this->parent->getChildren();
}
Expand Down
34 changes: 17 additions & 17 deletions src/meielement.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class MEI_EXPORT MeiElement

/** \brief Get the id of this element.
*/
const std::string getId();
const std::string getId() const;

/** \brief Set an element's ID.
* This is not always necessary since an element
Expand All @@ -97,14 +97,14 @@ class MEI_EXPORT MeiElement

/** \brief Get the name of this element
*/
const std::string getName();
const std::string getName() const;

/** \brief get the xml tail of an Mei Element
*
* \return The xml tail associated with the Mei Element or
* an empty string if the Mei Element has no tail
*/
const std::string getTail();
const std::string getTail() const;

/** \brief Set the xml tail associated with the Mei Element */
void setTail(std::string tail);
Expand All @@ -114,7 +114,7 @@ class MEI_EXPORT MeiElement
* \return A string indicating the value of the Mei Element or
* an empty string if the element has no value
*/
const std::string getValue();
const std::string getValue() const;

/** \brief Set the value associated with the Mei Element */
void setValue(std::string value);
Expand All @@ -123,7 +123,7 @@ class MEI_EXPORT MeiElement
*
* \return A const vector of the attributes on this element.
*/
const std::vector<MeiAttribute*>& getAttributes();
const std::vector<MeiAttribute*>& getAttributes() const;

/**
* \brief add all of the given attributes to this element.
Expand Down Expand Up @@ -152,7 +152,7 @@ class MEI_EXPORT MeiElement
*
* \return the attribute with this name, or NULL if the atribute doesn't exist.
*/
MeiAttribute* getAttribute(std::string name);
MeiAttribute* getAttribute(std::string name) const;

/**
* \brief Remove the attribute with the given name.
Expand All @@ -162,17 +162,17 @@ class MEI_EXPORT MeiElement
/**
* \brief See if this element has an attribute with the given name.
*/
bool hasAttribute(std::string name);
bool hasAttribute(std::string name) const;

/** \brief Check if this element has a parent element
*
* \return True if it does, False if it does not
*/
bool hasParent();
bool hasParent() const;

/** \brief Get this element's parent, if it exists.
*/
MeiElement *getParent();
MeiElement *getParent() const;

/** \brief Links this element and it's children to the given document
*
Expand All @@ -182,7 +182,7 @@ class MEI_EXPORT MeiElement

/** \brief Gets a pointer to the document this element is attached to.
*/
MeiDocument* getDocument();
MeiDocument* getDocument() const;

/** \brief Removes the pointer from this element and it's children to its currently assigned document
*
Expand Down Expand Up @@ -215,12 +215,12 @@ class MEI_EXPORT MeiElement
/**
* \brief Get all of the children of this element.
*/
const std::vector<MeiElement*>& getChildren();
const std::vector<MeiElement*>& getChildren() const;

/**
* \brief Get all of the children of this element that have a given name.
*/
const std::vector<MeiElement*> getChildrenByName(std::string name);
const std::vector<MeiElement*> getChildrenByName(std::string name) const;

/**
* \brief Remove all of the children of this element.
Expand All @@ -245,25 +245,25 @@ class MEI_EXPORT MeiElement
/**
* \brief Check if this element has any children.
*/
bool hasChildren();
bool hasChildren() const;

/**
* \brief Check if this element has any children with the given name.
*/
bool hasChildren(std::string cname);
bool hasChildren(std::string cname) const;

/**
* \brief Get the ancestor with a given element name
*
* \return MeiElement, or NULL if no ancestor is found.
*/
MeiElement* getAncestor(std::string name);
MeiElement* getAncestor(std::string name) const;

/**
* \brief Returns TRUE if this element has an ancestor element
* with a given name; FALSE otherwise
*/
bool hasAncestor(std::string name);
bool hasAncestor(std::string name) const;

/**
* \brief Get all descendants of the current element.
Expand All @@ -287,7 +287,7 @@ class MEI_EXPORT MeiElement
*
* \return A vector of MeiElements (possibly empty).
*/
std::vector<MeiElement*> getPeers();
std::vector<MeiElement*> getPeers() const;

/** \brief Gets this item's position in the flattened document structure.
* Position is numbered by the order the elements occur, so the first
Expand Down
8 changes: 4 additions & 4 deletions tools/langs/cplusplus.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
AUTHORS = "Andrew Hankinson, Alastair Porter, and Others"

METHODS_HEADER_TEMPLATE = """{documentation}
MeiAttribute* get{attNameUpper}();
MeiAttribute* get{attNameUpper}() const;
void set{attNameUpper}(std::string _{attNameLowerJoined});
bool has{attNameUpper}();
bool has{attNameUpper}() const;
void remove{attNameUpper}();
"""

METHODS_IMPL_TEMPLATE = """MeiAttribute* mei::{className}::get{attNameUpper}() {{
METHODS_IMPL_TEMPLATE = """MeiAttribute* mei::{className}::get{attNameUpper}() const {{
if (!{accessor}hasAttribute("{attNameLower}")) {{
return NULL;
}}
Expand All @@ -35,7 +35,7 @@
{accessor}addAttribute(a);
}};

bool mei::{className}::has{attNameUpper}() {{
bool mei::{className}::has{attNameUpper}() const {{
return {accessor}hasAttribute("{attNameLower}");
}};

Expand Down