Automatically generating setters/getters for a C++ class in Eclipse/CDT

For example, we have a class that has private member fields as follow in a header file.

class SMCPPacket {
private:
	//Command Message Header section
	std::bitset acknowledgeRequest;//2 bit
	std::bitset smcpVersion; //2 bit
	std::bitset commandTypeID; //4 bit
	unsigned char lowerFOID; //1 octet
	//Command Message Data section
	std::vector operationID; //2 octets
	std::vector parameters; //0-1006 octets
};

Navigate to the “Source” menu -> “Generate Getters and Setters…”. CDT parses the header file, and list getters and setters which applicable to the member fields. Click Finish after selecting items you want, and they will be automatically added to the header or a source file corresponding to the header file.

The example below shows a result of the automatic generation to the above class; the implementations are coded in the header (according to my selection).

class SMCPPacket {
private:
	//Command Message Header section
	std::bitset acknowledgeRequest;//2 bit
	std::bitset smcpVersion; //2 bit
	std::bitset commandTypeID; //4 bit
	unsigned char lowerFOID; //1 octet
	//Command Message Data section
	std::vector operationID; //2 octets
	std::vector parameters; //0-1006 octets
public:
	std::bitset getAcknowledgeRequest() const {
		return acknowledgeRequest;
	}

	std::bitset getCommandTypeID() const {
		return commandTypeID;
	}

	unsigned char getLowerFOID() const {
		return lowerFOID;
	}

	std::vector getOperationID() const {
		return operationID;
	}

	std::vector getParameters() const {
		return parameters;
	}

	std::bitset getSMCPVersion() const {
		return smcpVersion;
	}

	void setCommandTypeID(std::bitset commandTypeID) {
		this->commandTypeID = commandTypeID;
	}

	void setLowerFOID(unsigned char lowerFOID) {
		this->lowerFOID = lowerFOID;
	}

	void setOperationID(std::vector operationID) {
		this->operationID = operationID;
	}

	void setParameters(std::vector parameters) {
		this->parameters = parameters;
	}

	void setSMCPVersion(std::bitset smcpVersion) {
		this->smcpVersion = smcpVersion;
	}

};

Japanese:

Eclipse/CDTは、C++クラスのフィールドメンバのaccessor(getter/setter)を自動生成する機能を持っています。例えば以下のようなクラスがあるとします。

class SMCPPacket {
private:
	//Command Message Header section
	std::bitset acknowledgeRequest;//2 bit
	std::bitset smcpVersion; //2 bit
	std::bitset commandTypeID; //4 bit
	unsigned char lowerFOID; //1 octet
	//Command Message Data section
	std::vector operationID; //2 octets
	std::vector parameters; //0-1006 octets
};

Eclipse/CDTのメニューから、”Source” -> “Generate Getters and Setters…“を選択してください。自動的にファイルがパースされ、生成可能なsetter/getterの一覧が表示されます。作成したいものを選んで、Finishを押すと、コードが生成されます。実装コードはヘッダーに追加するか、ccファイルに入れるか、選択できます。

以下は、上記の例題クラスに対して、この機能を用いてgetter/setterを生成して、ヘッダファイルに実装まで含めた例になります。Javaスタイルのコーディングになっていて、個人的にはひじょうになじみやすい記法です(this->aaa=aaa;とか)。

class SMCPPacket {
private:
	//Command Message Header section
	std::bitset acknowledgeRequest;//2 bit
	std::bitset smcpVersion; //2 bit
	std::bitset commandTypeID; //4 bit
	unsigned char lowerFOID; //1 octet
	//Command Message Data section
	std::vector operationID; //2 octets
	std::vector parameters; //0-1006 octets
public:
	std::bitset getAcknowledgeRequest() const {
		return acknowledgeRequest;
	}

	std::bitset getCommandTypeID() const {
		return commandTypeID;
	}

	unsigned char getLowerFOID() const {
		return lowerFOID;
	}

	std::vector getOperationID() const {
		return operationID;
	}

	std::vector getParameters() const {
		return parameters;
	}

	std::bitset getSMCPVersion() const {
		return smcpVersion;
	}

	void setCommandTypeID(std::bitset commandTypeID) {
		this->commandTypeID = commandTypeID;
	}

	void setLowerFOID(unsigned char lowerFOID) {
		this->lowerFOID = lowerFOID;
	}

	void setOperationID(std::vector operationID) {
		this->operationID = operationID;
	}

	void setParameters(std::vector parameters) {
		this->parameters = parameters;
	}

	void setSMCPVersion(std::bitset smcpVersion) {
		this->smcpVersion = smcpVersion;
	}

};