Home | History | Annotate | Download | only in io
      1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include "tensorflow/core/lib/io/iterator.h"
     17 
     18 namespace tensorflow {
     19 namespace table {
     20 
     21 Iterator::Iterator() {
     22   cleanup_.function = nullptr;
     23   cleanup_.next = nullptr;
     24 }
     25 
     26 Iterator::~Iterator() {
     27   if (cleanup_.function != nullptr) {
     28     (*cleanup_.function)(cleanup_.arg1, cleanup_.arg2);
     29     for (Cleanup* c = cleanup_.next; c != nullptr;) {
     30       (*c->function)(c->arg1, c->arg2);
     31       Cleanup* next = c->next;
     32       delete c;
     33       c = next;
     34     }
     35   }
     36 }
     37 
     38 void Iterator::RegisterCleanup(CleanupFunction func, void* arg1, void* arg2) {
     39   assert(func != nullptr);
     40   Cleanup* c;
     41   if (cleanup_.function == nullptr) {
     42     c = &cleanup_;
     43   } else {
     44     c = new Cleanup;
     45     c->next = cleanup_.next;
     46     cleanup_.next = c;
     47   }
     48   c->function = func;
     49   c->arg1 = arg1;
     50   c->arg2 = arg2;
     51 }
     52 
     53 namespace {
     54 class EmptyIterator : public Iterator {
     55  public:
     56   explicit EmptyIterator(const Status& s) : status_(s) {}
     57   bool Valid() const override { return false; }
     58   void Seek(const StringPiece& target) override {}
     59   void SeekToFirst() override {}
     60   void Next() override { assert(false); }
     61   StringPiece key() const override {
     62     assert(false);
     63     return StringPiece();
     64   }
     65   StringPiece value() const override {
     66     assert(false);
     67     return StringPiece();
     68   }
     69   Status status() const override { return status_; }
     70 
     71  private:
     72   Status status_;
     73 };
     74 }  // namespace
     75 
     76 Iterator* NewEmptyIterator() { return new EmptyIterator(Status::OK()); }
     77 
     78 Iterator* NewErrorIterator(const Status& status) {
     79   return new EmptyIterator(status);
     80 }
     81 
     82 }  // namespace table
     83 }  // namespace tensorflow
     84