/* * Copyright 2017, The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ENTITY_H #define ENTITY_H #include #include #include "word_stream.h" namespace android { namespace spirit { class Builder; class IVisitor; class Entity { public: Entity() {} Entity(Builder *b) : mBuilder(b) {} virtual ~Entity() {} virtual void accept(IVisitor *visitor) = 0; virtual bool DeserializeInternal(InputWordStream &IS) = 0; virtual void Serialize(OutputWordStream &OS) const; virtual void dump() const {} void setBuilder(Builder *builder) { mBuilder = builder; } protected: Builder *mBuilder; }; template T *Deserialize(InputWordStream &IS) { std::unique_ptr entity(new T()); if (!entity->DeserializeInternal(IS)) { return nullptr; } return entity.release(); } template T *Deserialize(const std::vector &words) { std::unique_ptr IS(InputWordStream::Create(words)); return Deserialize(*IS); } template void DeserializeZeroOrMore(InputWordStream &IS, std::vector &all) { while (auto entity = Deserialize(IS)) { all.push_back(entity); } } template std::vector Serialize(T* e) { std::unique_ptr OS(OutputWordStream::Create()); e->Serialize(*OS); return OS->getWords(); } } // namespace spirit } // namespace android #endif // ENTITY_H